|
| 1 | +package source; // Yeah, I know... |
| 2 | + |
| 3 | +import sys.FileSystem; |
| 4 | +import sys.io.File; |
| 5 | + |
| 6 | +using StringTools; |
| 7 | + |
| 8 | +/** |
| 9 | + * A script which executes after the game is built. |
| 10 | + */ |
| 11 | +class Postbuild |
| 12 | +{ |
| 13 | + static inline final BUILD_TIME_FILE:String = '.build_time'; |
| 14 | + |
| 15 | + static function main():Void |
| 16 | + { |
| 17 | + printBuildTime(); |
| 18 | + } |
| 19 | + |
| 20 | + static function printBuildTime():Void |
| 21 | + { |
| 22 | + // get buildEnd before fs operations since they are blocking |
| 23 | + var end:Float = Sys.time(); |
| 24 | + if (FileSystem.exists(BUILD_TIME_FILE)) |
| 25 | + { |
| 26 | + var fi:sys.io.FileInput = File.read(BUILD_TIME_FILE); |
| 27 | + var start:Float = fi.readDouble(); |
| 28 | + fi.close(); |
| 29 | + |
| 30 | + sys.FileSystem.deleteFile(BUILD_TIME_FILE); |
| 31 | + |
| 32 | + Sys.println('[INFO] Build took: ${format(end - start)}'); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + static function format(time:Float, decimals:Int = 1):String |
| 37 | + { |
| 38 | + var units = [ |
| 39 | + {name: "day", secs: 86400}, |
| 40 | + {name: "hour", secs: 3600}, |
| 41 | + {name: "minute", secs: 60}, |
| 42 | + {name: "second", secs: 1} |
| 43 | + ]; |
| 44 | + |
| 45 | + var parts:Array<String> = []; |
| 46 | + var remaining:Float = time; |
| 47 | + |
| 48 | + for (unit in units) |
| 49 | + { |
| 50 | + var value:Float; |
| 51 | + if (unit.name == "second") |
| 52 | + { |
| 53 | + // handle seconds with decimals |
| 54 | + value = Math.round(remaining * Math.pow(10, decimals)) / Math.pow(10, decimals); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + value = Math.floor(remaining / unit.secs); |
| 59 | + remaining %= unit.secs; |
| 60 | + } |
| 61 | + |
| 62 | + // |
| 63 | + if (value > 0 || (unit.name == "second" && parts.length == 0)) |
| 64 | + { |
| 65 | + parts.push('${value} ${unit.name}${value == 1 ? "" : "s"}'); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return parts.join(" "); |
| 70 | + } |
| 71 | +} |
0 commit comments