|
| 1 | +import eval.integers.Int64; |
| 2 | +import eval.luv.Buffer; |
| 3 | +import eval.luv.File.FileSync; |
| 4 | +import haxelib.SemVer; |
| 5 | +import sys.io.File; |
| 6 | +import haxe.Json; |
| 7 | +import sys.FileSystem; |
| 8 | +import haxe.io.Path; |
| 9 | + |
| 10 | +import tools.Utils; |
| 11 | + |
| 12 | +enum HaxeRcError { |
| 13 | + NotFound; |
| 14 | + ParseError(err:String); |
| 15 | +} |
| 16 | + |
| 17 | +class LixTools { |
| 18 | + public static function safeGetRc():Null<String> { |
| 19 | + return try getRc() catch(_) null; |
| 20 | + } |
| 21 | + |
| 22 | + public static function getRc():Null<String> { |
| 23 | + final cwd = Utils.getCallSite(); |
| 24 | + |
| 25 | + // TODO (?): check parent directories too |
| 26 | + final rc = Path.join([cwd, ".haxerc"]); |
| 27 | + |
| 28 | + if (FileSystem.exists(rc)) { |
| 29 | + try { |
| 30 | + final version = Json.parse(File.getContent(rc)).version; |
| 31 | + return version; |
| 32 | + } catch (e) { |
| 33 | + throw ParseError(Std.string(e)); |
| 34 | + } |
| 35 | + } else { |
| 36 | + throw NotFound; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public static function resolveHaxe() { |
| 41 | + try { |
| 42 | + final version = getRc(); |
| 43 | + |
| 44 | + if (SemVer.isValid(version)) { |
| 45 | + HaxeDownload.downloadRelease(version, r -> HaxeSelect.select(r)); |
| 46 | + } else if (HaxeNightlies.isValid(version)) { |
| 47 | + switch (Utils.resolveRelease(version)) { |
| 48 | + case null: |
| 49 | + HaxeDownload.downloadNightly(version, r -> HaxeSelect.select(r)); |
| 50 | + case r: |
| 51 | + HaxeSelect.select(r); |
| 52 | + } |
| 53 | + } |
| 54 | + } catch (e:HaxeRcError) { |
| 55 | + switch e { |
| 56 | + case NotFound: |
| 57 | + Utils.displayError('Did not find any .haxerc to apply'); |
| 58 | + case ParseError(e): |
| 59 | + Utils.displayError('Could not get Haxe version from .haxerc: $e'); |
| 60 | + } |
| 61 | + |
| 62 | + Sys.exit(1); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static var installReg = ~/^#\s@install:\slix\s--silent\sdownload\s"([^"]+)"\s/; |
| 67 | + |
| 68 | + static function getInstallHxml():String { |
| 69 | + final cwd = Utils.getCallSite(); |
| 70 | + final haxe_libraries = Path.join([cwd, "haxe_libraries"]); |
| 71 | + |
| 72 | + var installHxml = ""; |
| 73 | + |
| 74 | + function addHaxelib(lib:String, version:String) { |
| 75 | + installHxml += '-lib $lib:$version\n'; |
| 76 | + } |
| 77 | + |
| 78 | + function addGitLib(lib:String, url:String) { |
| 79 | + installHxml += '-lib $lib:git:$url\n'; |
| 80 | + } |
| 81 | + |
| 82 | + function processLib(hxml) { |
| 83 | + final lib = Path.withoutExtension(hxml); |
| 84 | + final contents = File.getContent(Path.join([haxe_libraries, hxml])).split("\n"); |
| 85 | + |
| 86 | + // TODO: handle post-install |
| 87 | + for (line in contents) { |
| 88 | + if (installReg.match(line)) { |
| 89 | + final source = installReg.matched(1); |
| 90 | + final protocol = source.split(":").shift(); |
| 91 | + |
| 92 | + switch protocol { |
| 93 | + case null: |
| 94 | + throw ParseError('Cannot parse install instructions for lib $lib: $source'); |
| 95 | + |
| 96 | + case "haxelib": |
| 97 | + final parts = source.split("#"); |
| 98 | + final libName = parts[0].substr(protocol.length + 2); |
| 99 | + addHaxelib(libName, parts[1]); |
| 100 | + |
| 101 | + case "gh" | "github": |
| 102 | + addGitLib(lib, "https" + source.substr(protocol.length)); |
| 103 | + |
| 104 | + case "http" | "https": |
| 105 | + addGitLib(lib, source); |
| 106 | + |
| 107 | + case v: |
| 108 | + trace(v); |
| 109 | + addGitLib(lib, source); // This could work? xD |
| 110 | + } |
| 111 | + |
| 112 | + return; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + trace('Warning: installation instructions not found for $lib'); |
| 117 | + } |
| 118 | + |
| 119 | + if (FileSystem.exists(haxe_libraries)) { |
| 120 | + for (lib in FileSystem.readDirectory(haxe_libraries)) { |
| 121 | + if (Path.extension(lib) == "hxml") processLib(lib); |
| 122 | + } |
| 123 | + |
| 124 | + return installHxml; |
| 125 | + } else { |
| 126 | + throw NotFound; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public static function generateInstallHxml(file:String) { |
| 131 | + final installHxml = getInstallHxml(); |
| 132 | + |
| 133 | + if (installHxml != "") { |
| 134 | + final cwd = Utils.getCallSite(); |
| 135 | + final path = Path.isAbsolute(file) ? file : Path.join([cwd, file]); |
| 136 | + File.saveContent(path, installHxml); |
| 137 | + Sys.println('Saved lib installation instructions to $file'); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public static function applyLibs() { |
| 142 | + final installHxml = getInstallHxml(); |
| 143 | + if (installHxml != "") { |
| 144 | + final cwd = Utils.getCallSite(); |
| 145 | + final old_cwd = Sys.getCwd(); |
| 146 | + Sys.setCwd(cwd); |
| 147 | + |
| 148 | + switch FileSync.mkstemp("install_XXXXXX") { |
| 149 | + case Error(e): |
| 150 | + Sys.setCwd(old_cwd); |
| 151 | + throw e; |
| 152 | + |
| 153 | + case Ok({name: name, file: file}): |
| 154 | + final hxml = name.toString() + ".hxml"; |
| 155 | + |
| 156 | + FileSync.write(file, Int64.ZERO, [Buffer.fromString(installHxml)]); |
| 157 | + FileSync.close(file); |
| 158 | + FileSync.rename(name, hxml); |
| 159 | + Sys.command("hxlib", ["state", "load", hxml]); |
| 160 | + FileSync.unlink(hxml); |
| 161 | + Sys.setCwd(old_cwd); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments