| 1 |
/* |
| 2 |
* Copyright (C)2005-2016 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
package haxelib.client; |
| 23 |
|
| 24 |
import sys.FileSystem; |
| 25 |
#if haxe4 |
| 26 |
import sys.thread.Thread; |
| 27 |
import sys.thread.Lock; |
| 28 |
#else |
| 29 |
import neko.vm.Thread; |
| 30 |
import neko.vm.Lock; |
| 31 |
#end |
| 32 |
using haxelib.client.Vcs; |
| 33 |
|
| 34 |
interface IVcs { |
| 35 |
var name(default, null):String; |
| 36 |
var directory(default, null):String; |
| 37 |
var executable(default, null):String; |
| 38 |
var available(get, null):Bool; |
| 39 |
var settings(default, null):Settings; |
| 40 |
|
| 41 |
/** |
| 42 |
Clone repo into `libPath`. |
| 43 |
**/ |
| 44 |
function clone(libPath:String, vcsPath:String, ?branch:String, ?version:String):Void; |
| 45 |
|
| 46 |
/** |
| 47 |
Update to HEAD repo contains in CWD or CWD/`Vcs.directory`. |
| 48 |
CWD must be like "...haxelib-repo/lib/git" for Git. |
| 49 |
Returns `true` if update successful. |
| 50 |
**/ |
| 51 |
function update(libName:String):Bool; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
@:enum abstract VcsID(String) to String { |
| 56 |
var Hg = "hg"; |
| 57 |
var Git = "git"; |
| 58 |
} |
| 59 |
|
| 60 |
enum VcsError { |
| 61 |
VcsUnavailable(vcs:Vcs); |
| 62 |
CantCloneRepo(vcs:Vcs, repo:String, ?stderr:String); |
| 63 |
CantCheckoutBranch(vcs:Vcs, branch:String, stderr:String); |
| 64 |
CantCheckoutVersion(vcs:Vcs, version:String, stderr:String); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
typedef Settings = { |
| 69 |
@:optional var flat:Bool; |
| 70 |
@:optional var debug:Bool; |
| 71 |
@:optional var quiet:Bool; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
class Vcs implements IVcs { |
| 76 |
static var reg:Map<VcsID, Vcs>; |
| 77 |
|
| 78 |
public var name(default, null):String; |
| 79 |
public var directory(default, null):String; |
| 80 |
public var executable(default, null):String; |
| 81 |
public var settings(default, null):Settings; |
| 82 |
|
| 83 |
public var available(get, null):Bool; |
| 84 |
|
| 85 |
var availabilityChecked = false; |
| 86 |
var executableSearched = false; |
| 87 |
|
| 88 |
public static function initialize(settings:Settings) { |
| 89 |
if (reg == null) { |
| 90 |
reg = [ |
| 91 |
VcsID.Git => new Git(settings), |
| 92 |
VcsID.Hg => new Mercurial(settings) |
| 93 |
]; |
| 94 |
} else { |
| 95 |
if (reg.get(VcsID.Git) == null) |
| 96 |
reg.set(VcsID.Git, new Git(settings)); |
| 97 |
if (reg.get(VcsID.Hg) == null) |
| 98 |
reg.set(VcsID.Hg, new Mercurial(settings)); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
function new(executable:String, directory:String, name:String, settings:Settings) { |
| 104 |
this.name = name; |
| 105 |
this.directory = directory; |
| 106 |
this.executable = executable; |
| 107 |
this.settings = { |
| 108 |
flat: settings.flat != null ? settings.flat : false, |
| 109 |
debug: settings.debug != null ? settings.debug : false, |
| 110 |
quiet: settings.quiet != null ? settings.quiet : false |
| 111 |
} |
| 112 |
|
| 113 |
if (settings.debug) { |
| 114 |
this.settings.quiet = false; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
public static function get(id:VcsID, settings:Settings):Null<Vcs> { |
| 120 |
initialize(settings); |
| 121 |
return reg.get(id); |
| 122 |
} |
| 123 |
|
| 124 |
static function set(id:VcsID, vcs:Vcs, settings:Settings, ?rewrite:Bool):Void { |
| 125 |
initialize(settings); |
| 126 |
var existing = reg.get(id) != null; |
| 127 |
if (!existing || rewrite) |
| 128 |
reg.set(id, vcs); |
| 129 |
} |
| 130 |
|
| 131 |
public static function getVcsForDevLib(libPath:String, settings:Settings):Null<Vcs> { |
| 132 |
initialize(settings); |
| 133 |
for (k in reg.keys()) { |
| 134 |
if (FileSystem.exists(libPath + "/" + k) && FileSystem.isDirectory(libPath + "/" + k)) |
| 135 |
return reg.get(k); |
| 136 |
} |
| 137 |
return null; |
| 138 |
} |
| 139 |
|
| 140 |
function sure(commandResult:{code:Int, out:String}):Void { |
| 141 |
switch (commandResult) { |
| 142 |
case {code: 0}: //pass |
| 143 |
case {code: code, out:out}: |
| 144 |
if (!settings.debug) |
| 145 |
Sys.stderr().writeString(out); |
| 146 |
Sys.exit(code); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
function command(cmd:String, args:Array<String>):{ |
| 151 |
code: Int, |
| 152 |
out: String |
| 153 |
} { |
| 154 |
var p = try { |
| 155 |
new sys.io.Process(cmd, args); |
| 156 |
} catch(e:Dynamic) { |
| 157 |
return { |
| 158 |
code: -1, |
| 159 |
out: Std.string(e) |
| 160 |
} |
| 161 |
} |
| 162 |
var streamsLock = new sys.thread.Lock(); |
| 163 |
function readFrom(stream:haxe.io.Input, to: {value: String}) { |
| 164 |
to.value = stream.readAll().toString(); |
| 165 |
streamsLock.release(); |
| 166 |
} |
| 167 |
|
| 168 |
var out = {value: ""}; |
| 169 |
var err = {value: ""}; |
| 170 |
Thread.create(readFrom.bind(p.stdout, out)); |
| 171 |
Thread.create(readFrom.bind(p.stderr, err)); |
| 172 |
|
| 173 |
var code = p.exitCode(); |
| 174 |
for (_ in 0...2) { |
| 175 |
// wait until we finish reading from both streams |
| 176 |
streamsLock.wait(); |
| 177 |
} |
| 178 |
|
| 179 |
if (settings.debug && out.value != "") |
| 180 |
Sys.println(out.value); |
| 181 |
if (settings.debug && err.value != "") |
| 182 |
Sys.stderr().writeString(err.value); |
| 183 |
|
| 184 |
var ret = { |
| 185 |
code: code, |
| 186 |
out: code == 0 ? out.value : err.value |
| 187 |
}; |
| 188 |
p.close(); |
| 189 |
return ret; |
| 190 |
} |
| 191 |
|
| 192 |
function searchExecutable():Void { |
| 193 |
executableSearched = true; |
| 194 |
} |
| 195 |
|
| 196 |
function checkExecutable():Bool { |
| 197 |
available = (executable != null) && try command(executable, []).code == 0 catch(_:Dynamic) false; |
| 198 |
availabilityChecked = true; |
| 199 |
|
| 200 |
if (!available && !executableSearched) |
| 201 |
searchExecutable(); |
| 202 |
|
| 203 |
return available; |
| 204 |
} |
| 205 |
|
| 206 |
@:final function get_available():Bool { |
| 207 |
if (!availabilityChecked) |
| 208 |
checkExecutable(); |
| 209 |
return available; |
| 210 |
} |
| 211 |
|
| 212 |
public function clone(libPath:String, vcsPath:String, ?branch:String, ?version:String):Void { |
| 213 |
throw "This method must be overridden."; |
| 214 |
} |
| 215 |
|
| 216 |
public function update(libName:String):Bool { |
| 217 |
throw "This method must be overridden."; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
class Git extends Vcs { |
| 223 |
|
| 224 |
public function new(settings:Settings) |
| 225 |
super("git", "git", "Git", settings); |
| 226 |
|
| 227 |
override function checkExecutable():Bool { |
| 228 |
// with `help` cmd because without any cmd `git` can return exit-code = 1. |
| 229 |
available = (executable != null) && try command(executable, ["help"]).code == 0 catch(_:Dynamic) false; |
| 230 |
availabilityChecked = true; |
| 231 |
|
| 232 |
if (!available && !executableSearched) |
| 233 |
searchExecutable(); |
| 234 |
|
| 235 |
return available; |
| 236 |
} |
| 237 |
|
| 238 |
override function searchExecutable():Void { |
| 239 |
super.searchExecutable(); |
| 240 |
|
| 241 |
if (available) |
| 242 |
return; |
| 243 |
|
| 244 |
// if we have already msys git/cmd in our PATH |
| 245 |
var match = ~/(.*)git([\\|\/])cmd$/; |
| 246 |
for (path in Sys.getEnv("PATH").split(";")) { |
| 247 |
if (match.match(path.toLowerCase())) { |
| 248 |
var newPath = match.matched(1) + executable + match.matched(2) + "bin"; |
| 249 |
Sys.putEnv("PATH", Sys.getEnv("PATH") + ";" + newPath); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
if (checkExecutable()) |
| 254 |
return; |
| 255 |
|
| 256 |
// look at a few default paths |
| 257 |
for (path in ["C:\\Program Files (x86)\\Git\\bin", "C:\\Progra~1\\Git\\bin"]) { |
| 258 |
if (FileSystem.exists(path)) { |
| 259 |
Sys.putEnv("PATH", Sys.getEnv("PATH") + ";" + path); |
| 260 |
if (checkExecutable()) |
| 261 |
return; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
override public function update(libName:String):Bool { |
| 267 |
if ( |
| 268 |
command(executable, ["diff", "--exit-code", "--no-ext-diff"]).code != 0 |
| 269 |
|| |
| 270 |
command(executable, ["diff", "--cached", "--exit-code", "--no-ext-diff"]).code != 0 |
| 271 |
) { |
| 272 |
if (Cli.ask("Reset changes to " + libName + " " + name + " repo so we can pull latest version")) { |
| 273 |
sure(command(executable, ["reset", "--hard"])); |
| 274 |
} else { |
| 275 |
if (!settings.quiet) |
| 276 |
Sys.println(name + " repo left untouched"); |
| 277 |
return false; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
var code = command(executable, ["pull"]).code; |
| 282 |
// But if before we pulled specified branch/tag/rev => then possibly currently we haxe "HEAD detached at ..". |
| 283 |
if (code != 0) { |
| 284 |
// get parent-branch: |
| 285 |
var branch = command(executable, ["show-branch"]).out; |
| 286 |
var regx = ~/\[([^]]*)\]/; |
| 287 |
if (regx.match(branch)) |
| 288 |
branch = regx.matched(1); |
| 289 |
|
| 290 |
sure(command(executable, ["checkout", branch, "--force"])); |
| 291 |
sure(command(executable, ["pull"])); |
| 292 |
} |
| 293 |
return true; |
| 294 |
} |
| 295 |
|
| 296 |
override public function clone(libPath:String, url:String, ?branch:String, ?version:String):Void { |
| 297 |
var oldCwd = Sys.getCwd(); |
| 298 |
|
| 299 |
var vcsArgs = ["clone", url, libPath]; |
| 300 |
|
| 301 |
if (settings == null || !settings.flat) |
| 302 |
vcsArgs.push('--recursive'); |
| 303 |
|
| 304 |
//TODO: move to Vcs.run(vcsArgs) |
| 305 |
//TODO: use settings.quiet |
| 306 |
if (command(executable, vcsArgs).code != 0) |
| 307 |
throw VcsError.CantCloneRepo(this, url/*, ret.out*/); |
| 308 |
|
| 309 |
|
| 310 |
Sys.setCwd(libPath); |
| 311 |
|
| 312 |
if (version != null && version != "") { |
| 313 |
var ret = command(executable, ["checkout", "tags/" + version]); |
| 314 |
if (ret.code != 0) |
| 315 |
throw VcsError.CantCheckoutVersion(this, version, ret.out); |
| 316 |
} else if (branch != null) { |
| 317 |
var ret = command(executable, ["checkout", branch]); |
| 318 |
if (ret.code != 0) |
| 319 |
throw VcsError.CantCheckoutBranch(this, branch, ret.out); |
| 320 |
} |
| 321 |
|
| 322 |
// return prev. cwd: |
| 323 |
Sys.setCwd(oldCwd); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
class Mercurial extends Vcs { |
| 329 |
|
| 330 |
public function new(settings:Settings) |
| 331 |
super("hg", "hg", "Mercurial", settings); |
| 332 |
|
| 333 |
override function searchExecutable():Void { |
| 334 |
super.searchExecutable(); |
| 335 |
|
| 336 |
if (available) |
| 337 |
return; |
| 338 |
|
| 339 |
// if we have already msys git/cmd in our PATH |
| 340 |
var match = ~/(.*)hg([\\|\/])cmd$/; |
| 341 |
for(path in Sys.getEnv("PATH").split(";")) { |
| 342 |
if(match.match(path.toLowerCase())) { |
| 343 |
var newPath = match.matched(1) + executable + match.matched(2) + "bin"; |
| 344 |
Sys.putEnv("PATH", Sys.getEnv("PATH") + ";" + newPath); |
| 345 |
} |
| 346 |
} |
| 347 |
checkExecutable(); |
| 348 |
} |
| 349 |
|
| 350 |
override public function update(libName:String):Bool { |
| 351 |
command(executable, ["pull"]); |
| 352 |
var summary = command(executable, ["summary"]).out; |
| 353 |
var diff = command(executable, ["diff", "-U", "2", "--git", "--subrepos", "--no-ext-diff"]); |
| 354 |
var status = command(executable, ["status"]); |
| 355 |
|
| 356 |
// get new pulled changesets: |
| 357 |
// (and search num of sets) |
| 358 |
summary = summary.substr(0, summary.length - 1); |
| 359 |
summary = summary.substr(summary.lastIndexOf("\n") + 1); |
| 360 |
// we don't know any about locale then taking only Digit-exising:s |
| 361 |
var changed = ~/(\d)/.match(summary); |
| 362 |
if (changed && !settings.quiet) |
| 363 |
// print new pulled changesets: |
| 364 |
Sys.println(summary); |
| 365 |
|
| 366 |
|
| 367 |
if (diff.code + status.code + diff.out.length + status.out.length != 0) { |
| 368 |
if (!settings.quiet) |
| 369 |
Sys.println(diff.out); |
| 370 |
if (Cli.ask("Reset changes to " + libName + " " + name + " repo so we can update to latest version")) { |
| 371 |
sure(command(executable, ["update", "--clean"])); |
| 372 |
} else { |
| 373 |
changed = false; |
| 374 |
if (!settings.quiet) |
| 375 |
Sys.println(name + " repo left untouched"); |
| 376 |
} |
| 377 |
} else if (changed) { |
| 378 |
sure(command(executable, ["update"])); |
| 379 |
} |
| 380 |
|
| 381 |
return changed; |
| 382 |
} |
| 383 |
|
| 384 |
override public function clone(libPath:String, url:String, ?branch:String, ?version:String):Void { |
| 385 |
var vcsArgs = ["clone", url, libPath]; |
| 386 |
|
| 387 |
if (branch != null) { |
| 388 |
vcsArgs.push("--branch"); |
| 389 |
vcsArgs.push(branch); |
| 390 |
} |
| 391 |
|
| 392 |
if (version != null) { |
| 393 |
vcsArgs.push("--rev"); |
| 394 |
vcsArgs.push(version); |
| 395 |
} |
| 396 |
|
| 397 |
if (command(executable, vcsArgs).code != 0) |
| 398 |
throw VcsError.CantCloneRepo(this, url/*, ret.out*/); |
| 399 |
} |
| 400 |
} |