Debian Codegraph @

extra/haxelib_src/src/legacyhaxelib/Main.hx

raw
1 /*
2 * Copyright (C)2005-2012 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 legacyhaxelib;
23 import haxe.zip.Reader;
24
25 enum Answer {
26 Yes;
27 No;
28 Always;
29 }
30
31 class SiteProxy extends haxe.remoting.Proxy<legacyhaxelib.SiteApi> {
32 }
33
34 class Progress extends haxe.io.Output {
35
36 var o : haxe.io.Output;
37 var cur : Int;
38 var max : Int;
39 var start : Float;
40
41 public function new(o) {
42 this.o = o;
43 cur = 0;
44 start = haxe.Timer.stamp();
45 }
46
47 function bytes(n) {
48 cur += n;
49 if( max == null )
50 Sys.print(cur+" bytes\r");
51 else
52 Sys.print(cur+"/"+max+" ("+Std.int((cur*100.0)/max)+"%)\r");
53 }
54
55 public override function writeByte(c) {
56 o.writeByte(c);
57 bytes(1);
58 }
59
60 public override function writeBytes(s,p,l) {
61 var r = o.writeBytes(s,p,l);
62 bytes(r);
63 return r;
64 }
65
66 public override function close() {
67 super.close();
68 o.close();
69 var time = haxe.Timer.stamp() - start;
70 var speed = (cur / time) / 1024;
71 time = Std.int(time * 10) / 10;
72 speed = Std.int(speed * 10) / 10;
73 Sys.print("Download complete : "+cur+" bytes in "+time+"s ("+speed+"KB/s)\n");
74 }
75
76 public override function prepare(m) {
77 max = m;
78 }
79
80 }
81
82 class ProgressIn extends haxe.io.Input {
83
84 var i : haxe.io.Input;
85 var pos : Int;
86 var tot : Int;
87
88 public function new( i, tot ) {
89 this.i = i;
90 this.pos = 0;
91 this.tot = tot;
92 }
93
94 public override function readByte() {
95 var c = i.readByte();
96 doRead(1);
97 return c;
98 }
99
100 public override function readBytes(buf,pos,len) {
101 var k = i.readBytes(buf,pos,len);
102 doRead(k);
103 return k;
104 }
105
106 function doRead( nbytes : Int ) {
107 pos += nbytes;
108 Sys.print( Std.int((pos * 100.0) / tot) + "%\r" );
109 }
110
111 }
112
113 class Main {
114
115 static var VERSION = 104;
116 static var REPNAME = "lib";
117 static var SERVER = {
118 host : "lib.haxe.org",
119 port : 80,
120 dir : "",
121 url : "index.n"
122 };
123
124 var argcur : Int;
125 var args : Array<String>;
126 var commands : List<{ name : String, doc : String, f : Void -> Void, net : Bool }>;
127 var siteUrl : String;
128 var site : SiteProxy;
129
130 function new() {
131 args = Sys.args();
132 commands = new List();
133 addCommand("install",install,"install a given library");
134 addCommand("list",list,"list all installed libraries",false);
135 addCommand("upgrade",upgrade,"upgrade all installed libraries");
136 addCommand("update",update,"update a single library");
137 addCommand("remove",remove,"remove a given library/version",false);
138 addCommand("set",set,"set the current version for a library",false);
139 addCommand("search",search,"list libraries matching a word");
140 addCommand("info",info,"list information on a given library");
141 addCommand("user",user,"list information on a given user");
142 addCommand("register",register,"register a new user");
143 addCommand("submit",submit,"submit or update a library package");
144 addCommand("setup",setup,"set the haxelib repository path",false);
145 addCommand("config",config,"print the repository path",false);
146 addCommand("path",path,"give paths to libraries",false);
147 addCommand("run",run,"run the specified library with parameters",false);
148 addCommand("local",local,"install the specified package locally",false);
149 addCommand("dev",dev,"set the development directory for a given library",false);
150 addCommand("git", git, "uses git repository as library");
151 addCommand("proxy", proxy, "setup the Http proxy");
152 initSite();
153 }
154
155 function initSite() {
156 siteUrl = "http://"+SERVER.host+":"+SERVER.port+"/"+SERVER.dir+'/';
157 site = new SiteProxy(haxe.remoting.HttpConnection.urlConnect(siteUrl+SERVER.url).resolve("api"));
158 }
159
160 function param( name, ?passwd ) {
161 if( args.length > argcur )
162 return args[argcur++];
163 Sys.print(name+" : ");
164 if( passwd ) {
165 var s = new StringBuf();
166 var c;
167 while( (c = Sys.getChar(false)) != 13 )
168 s.addChar(c);
169 print("");
170 return s.toString();
171 }
172 return Sys.stdin().readLine();
173 }
174
175 function ask( question ) {
176 while( true ) {
177 Sys.print(question+" [y/n/a] ? ");
178 switch( Sys.stdin().readLine() ) {
179 case "n": return No;
180 case "y": return Yes;
181 case "a": return Always;
182 }
183 }
184 return null;
185 }
186
187 function paramOpt() {
188 if( args.length > argcur )
189 return args[argcur++];
190 return null;
191 }
192
193 function addCommand( name, f, doc, ?net = true ) {
194 commands.add({ name : name, doc : doc, f : f, net : net });
195 }
196
197 function usage() {
198 var vmin = Std.string(VERSION % 100);
199 var ver = Std.int(VERSION/100) + "." + if( vmin.length == 1 ) "0"+vmin else vmin;
200 print("Haxe Library Manager "+ver+" - (c)2006-2012 Haxe Foundation");
201 print(" Usage : haxelib [command] [options]");
202 print(" Commands :");
203 for( c in commands )
204 print(" "+c.name+" : "+c.doc);
205 Sys.exit(1);
206 }
207
208 function process() {
209 var debug = false;
210 argcur = 0;
211 while( true ) {
212 var a = args[argcur++];
213 if( a == null )
214 break;
215 switch( a ) {
216 case "-debug":
217 debug = true;
218 case "-notimeout":
219 haxe.remoting.HttpConnection.TIMEOUT = 0;
220 case "-R":
221 var path = args[argcur++];
222 var r = ~/^(http:\/\/)?([^:\/]+)(:[0-9]+)?\/?(.*)$/;
223 if( !r.match(path) )
224 throw "Invalid repository format '"+path+"'";
225 SERVER.host = r.matched(2);
226 if( r.matched(3) != null )
227 SERVER.port = Std.parseInt(r.matched(3).substr(1));
228 SERVER.dir = r.matched(4);
229 initSite();
230 default:
231 argcur--;
232 break;
233 }
234 }
235 var cmd = args[argcur++];
236 if( cmd == null )
237 usage();
238 for( c in commands )
239 if( c.name == cmd ) {
240 try {
241 if( c.net ) loadProxy();
242 c.f();
243 } catch( e : Dynamic ) {
244 if( e == "std@host_resolve" ) {
245 print("Host "+SERVER.host+" was not found");
246 print("Please ensure that your internet connection is on");
247 print("If you don't have an internet connection or if you are behing a proxy");
248 print("please download manually the file from http://lib.haxe.org/files");
249 print("and run 'haxelib local <file>' to install the Library.");
250 print("You can also setup the proxy with 'haxelib proxy'.");
251 Sys.exit(1);
252 }
253 if( e == "Blocked" ) {
254 print("Http connection timeout. Try running haxelib -notimeout <command> to disable timeout");
255 Sys.exit(1);
256 }
257 if( debug )
258 neko.Lib.rethrow(e);
259 print(Std.string(e));
260 Sys.exit(1);
261 }
262 return;
263 }
264 print("Unknown command "+cmd);
265 usage();
266 }
267
268 // ---- COMMANDS --------------------
269
270 function search() {
271 var word = param("Search word");
272 var l = site.search(word);
273 for( s in l )
274 print(s.name);
275 print(l.length+" libraries found");
276 }
277
278 function info() {
279 var prj = param("Library name");
280 var inf = site.infos(prj);
281 print("Name: "+inf.name);
282 print("Tags: "+inf.tags.join(", "));
283 print("Desc: "+inf.desc);
284 print("Website: "+inf.website);
285 print("License: "+inf.license);
286 print("Owner: "+inf.owner);
287 print("Version: "+inf.curversion);
288 print("Releases: ");
289 if( inf.versions.length == 0 )
290 print(" (no version released yet)");
291 for( v in inf.versions )
292 print(" "+v.date+" "+v.name+" : "+v.comments);
293 }
294
295 function user() {
296 var uname = param("User name");
297 var inf = site.user(uname);
298 print("Id: "+inf.name);
299 print("Name: "+inf.fullname);
300 print("Mail: "+inf.email);
301 print("Libraries: ");
302 if( inf.projects.length == 0 )
303 print(" (no libraries)");
304 for( p in inf.projects )
305 print(" "+p);
306 }
307
308 function register() {
309 doRegister(param("User"));
310 print("Registration successful");
311 }
312
313 function doRegister(name) {
314 var email = param("Email");
315 var fullname = param("Fullname");
316 var pass = param("Password",true);
317 var pass2 = param("Confirm",true);
318 if( pass != pass2 )
319 throw "Password does not match";
320 pass = haxe.crypto.Md5.encode(pass);
321 site.register(name,pass,email,fullname);
322 return pass;
323 }
324
325 function submit() {
326 var file = param("Package");
327 var data = sys.io.File.getBytes(file);
328 var zip = Reader.readZip(new haxe.io.BytesInput(data));
329 var infos = Data.readInfos(zip,true);
330 var user = infos.developers.first();
331 var password;
332 if( site.isNewUser(user) ) {
333 print("This is your first submission as '"+user+"'");
334 print("Please enter the following information for registration");
335 password = doRegister(user);
336 } else {
337 if( infos.developers.length > 1 )
338 user = param("User");
339 password = haxe.crypto.Md5.encode(param("Password",true));
340 if( !site.checkPassword(user,password) )
341 throw "Invalid password for "+user;
342 }
343 site.checkDeveloper(infos.project,user);
344
345 // check dependencies validity
346 for( d in infos.dependencies ) {
347 var infos = site.infos(d.project);
348 if( d.version == "" )
349 continue;
350 var found = false;
351 for( v in infos.versions )
352 if( v.name == d.version ) {
353 found = true;
354 break;
355 }
356 if( !found )
357 throw "Library "+d.project+" does not have version "+d.version;
358 }
359
360 // check if this version already exists
361 var sinfos = try site.infos(infos.project) catch( _ : Dynamic ) null;
362 if( sinfos != null )
363 for( v in sinfos.versions )
364 if( v.name == infos.version && ask("You're about to overwrite existing version '"+v.name+"', please confirm") == No )
365 throw "Aborted";
366
367 // query a submit id that will identify the file
368 var id = site.getSubmitId();
369
370 // directly send the file data over Http
371 var h = new haxe.Http("http://"+SERVER.host+":"+SERVER.port+"/"+SERVER.url);
372 h.onError = function(e) { throw e; };
373 h.onData = print;
374 h.fileTransfert("file",id,new ProgressIn(new haxe.io.BytesInput(data),data.length),data.length);
375 print("Sending data.... ");
376 h.request(true);
377
378 // processing might take some time, make sure we wait
379 print("Processing file.... ");
380 haxe.remoting.HttpConnection.TIMEOUT = 1000;
381 // ask the server to register the sent file
382 var msg = site.processSubmit(id,user,password);
383 print(msg);
384 }
385
386 function install() {
387 var prj = param("Library name");
388 if( sys.FileSystem.exists(prj) && !sys.FileSystem.isDirectory(prj) ) {
389 if( !StringTools.endsWith(prj,".zip") )
390 throw "Local file to install must be a zip";
391 doInstallFile(prj,true,true);
392 return;
393 }
394 var inf = site.infos(prj);
395 if( inf.curversion == null )
396 throw "This library has not yet released a version";
397 var reqversion = paramOpt();
398 var version = if( reqversion != null ) reqversion else inf.curversion;
399 var found = false;
400 for( v in inf.versions )
401 if( v.name == version ) {
402 found = true;
403 break;
404 }
405 if( !found )
406 throw "No such version "+version;
407 doInstall(inf.name,version,version == inf.curversion);
408 }
409
410 function doInstall( project, version, setcurrent ) {
411 var rep = getRepository();
412
413 // check if exists already
414 if( sys.FileSystem.exists(rep+Data.safe(project)+"/"+Data.safe(version)) ) {
415 print("You already have "+project+" version "+version+" installed");
416 setCurrent(project,version,true);
417 return;
418 }
419
420 // download to temporary file
421 var filename = Data.fileName(project,version);
422 var filepath = rep+filename;
423 var out = sys.io.File.write(filepath,true);
424 var progress = new Progress(out);
425 var h = new haxe.Http(siteUrl + Data.REPOSITORY + "/" + filename);
426 h.onError = function(e) {
427 progress.close();
428 sys.FileSystem.deleteFile(filepath);
429 throw e;
430 };
431 print("Downloading "+filename+"...");
432 h.customRequest(false,progress);
433
434 doInstallFile(filepath,setcurrent);
435 site.postInstall(project,version);
436 }
437
438 function doInstallFile(filepath,setcurrent,?nodelete) {
439
440 // read zip content
441 var f = sys.io.File.read(filepath,true);
442 var zip = Reader.readZip(f);
443 f.close();
444 var infos = Data.readInfos(zip,false);
445
446 // create directories
447 var pdir = getRepository() + Data.safe(infos.project);
448 safeDir(pdir);
449 pdir += "/";
450 var target = pdir + Data.safe(infos.version);
451 safeDir(target);
452 target += "/";
453
454 // locate haxelib.xml base path
455 var basepath = null;
456 for( f in zip ) {
457 if( StringTools.endsWith(f.fileName,Data.XML) ) {
458 basepath = f.fileName.substr(0,f.fileName.length - Data.XML.length);
459 break;
460 }
461 }
462 if( basepath == null )
463 throw "No "+Data.XML+" found";
464
465 // unzip content
466 for( zipfile in zip ) {
467 var n = zipfile.fileName;
468 if( StringTools.startsWith(n,basepath) ) {
469 // remove basepath
470 n = n.substr(basepath.length,n.length-basepath.length);
471 if( n.charAt(0) == "/" || n.charAt(0) == "\\" || n.split("..").length > 1 )
472 throw "Invalid filename : "+n;
473 var dirs = ~/[\/\\]/g.split(n);
474 var path = "";
475 var file = dirs.pop();
476 for( d in dirs ) {
477 path += d;
478 safeDir(target+path);
479 path += "/";
480 }
481 if( file == "" ) {
482 if( path != "" ) print(" Created "+path);
483 continue; // was just a directory
484 }
485 path += file;
486 print(" Install "+path);
487 var data = Reader.unzip(zipfile);
488 sys.io.File.saveBytes(target+path,data);
489 }
490 }
491
492 // set current version
493 if( setcurrent || !sys.FileSystem.exists(pdir+".current") ) {
494 sys.io.File.saveContent(pdir+".current",infos.version);
495 print(" Current version is now "+infos.version);
496 }
497
498 // end
499 if( !nodelete )
500 sys.FileSystem.deleteFile(filepath);
501 print("Done");
502
503 // process dependencies
504 for( d in infos.dependencies ) {
505 print("Installing dependency "+d.project+" "+d.version);
506 if( d.version == "" )
507 d.version = site.infos(d.project).curversion;
508 doInstall(d.project,d.version,false);
509 }
510 }
511
512 function safeDir( dir ) {
513 if( sys.FileSystem.exists(dir) ) {
514 if( !sys.FileSystem.isDirectory(dir) )
515 throw ("A file is preventing "+dir+" to be created");
516 return false;
517 }
518 try {
519 sys.FileSystem.createDirectory(dir);
520 } catch( e : Dynamic ) {
521 throw "You don't have enough user rights to create the directory "+dir;
522 }
523 return true;
524 }
525
526 function safeDelete( file ) {
527 try {
528 sys.FileSystem.deleteFile(file);
529 return true;
530 } catch (e:Dynamic) {
531 if( Sys.systemName() == "Windows") {
532 try {
533 Sys.command("attrib -R \"" +file+ "\"");
534 sys.FileSystem.deleteFile(file);
535 return true;
536 } catch (e:Dynamic) {
537 }
538 }
539 return false;
540 }
541 }
542
543 function getRepository( ?setup : Bool ) {
544 var win = Sys.systemName() == "Windows";
545 var haxepath = Sys.getEnv("HAXEPATH");
546 if( haxepath != null ) {
547 var last = haxepath.charAt(haxepath.length - 1);
548 if( last != "/" && last != "\\" )
549 haxepath += "/";
550 }
551 var config_file;
552 if( win )
553 config_file = Sys.getEnv("HOMEDRIVE") + Sys.getEnv("HOMEPATH");
554 else
555 config_file = Sys.getEnv("HOME");
556 config_file += "/.haxelib";
557 var rep = try
558 sys.io.File.getContent(config_file)
559 catch( e : Dynamic ) try
560 sys.io.File.getContent("/etc/.haxelib")
561 catch( e : Dynamic ) {
562 if( setup ) {
563 (win ? haxepath : "/usr/lib/haxe/")+REPNAME;
564 } else if( win ) {
565 // Windows have a default directory (no need for setup)
566 if( haxepath == null )
567 throw "HAXEPATH environment variable not defined, please run haxesetup.exe first";
568 var rep = haxepath+REPNAME;
569 try {
570 safeDir(rep);
571 } catch( e : Dynamic ) {
572 throw "The directory defined by HAXEPATH does not exist, please run haxesetup.exe again";
573 }
574 return rep+"\\";
575 } else
576 throw "This is the first time you are running haxelib. Please run haxelib setup first";
577 }
578 rep = StringTools.trim(rep);
579 if( setup ) {
580 if( args.length <= argcur ) {
581 print("Please enter haxelib repository path with write access");
582 print("Hit enter for default (" + rep + ")");
583 }
584 var line = param("Path");
585 if( line != "" )
586 rep = line;
587 if( !sys.FileSystem.exists(rep) ) {
588 try {
589 sys.FileSystem.createDirectory(rep);
590 } catch( e : Dynamic ) {
591 print("Failed to create directory '"+rep+"' ("+Std.string(e)+"), maybe you need appropriate user rights");
592 print("Check also that the parent directory exists");
593 Sys.exit(1);
594 }
595 }
596 rep = try sys.FileSystem.fullPath(rep) catch( e : Dynamic ) rep;
597 sys.io.File.saveContent(config_file, rep);
598 } else if( !sys.FileSystem.exists(rep) )
599 throw "haxelib Repository "+rep+" does not exists. Please run haxelib setup again";
600 return rep+"/";
601 }
602
603 function setup() {
604 var path = getRepository(true);
605 print("haxelib repository is now "+path);
606 }
607
608 function config() {
609 print(getRepository());
610 }
611
612 function getCurrent( dir ) {
613 return StringTools.trim(sys.io.File.getContent(dir + "/.current"));
614 }
615
616 function getDev( dir ) {
617 return StringTools.trim(sys.io.File.getContent(dir + "/.dev"));
618 }
619
620 function list() {
621 var rep = getRepository();
622 for( p in sys.FileSystem.readDirectory(rep) ) {
623 if( p.charAt(0) == "." )
624 continue;
625 var versions = new Array();
626 var current = getCurrent(rep + p);
627 var dev = try StringTools.trim(sys.io.File.getContent(rep+p+"/.dev")) catch( e : Dynamic ) null;
628 for( v in sys.FileSystem.readDirectory(rep+p) ) {
629 if( v.charAt(0) == "." )
630 continue;
631 v = Data.unsafe(v);
632 if( dev == null && v == current )
633 v = "["+v+"]";
634 versions.push(v);
635 }
636 if( dev != null )
637 versions.push("[dev:"+dev+"]");
638 print(Data.unsafe(p) + ": "+versions.join(" "));
639 }
640 }
641
642 function upgrade() {
643 var state = { rep : getRepository(), prompt : true, updated : false };
644 for( p in sys.FileSystem.readDirectory(state.rep) ) {
645 if( p.charAt(0) == "." || !sys.FileSystem.isDirectory(state.rep+"/"+p) )
646 continue;
647 var p = Data.unsafe(p);
648 print("Checking " + p);
649 doUpdate(p,state);
650 }
651 if( state.updated )
652 print("Done");
653 else
654 print("All libraries are up-to-date");
655 }
656
657 function doUpdate( p : String, state ) {
658 var rep = state.rep;
659 if( sys.FileSystem.exists(rep + "/" + p + "/git") && sys.FileSystem.isDirectory(rep + "/" + p + "/git") ) {
660 checkGit();
661 var oldCwd = Sys.getCwd();
662 Sys.setCwd(rep + "/" + p + "/git");
663 Sys.command("git pull");
664 // TODO: update haxelib.xml version?
665 Sys.setCwd(oldCwd);
666 state.updated = true;
667 } else {
668 var inf = try site.infos(p) catch( e : Dynamic ) { Sys.println(e); return; };
669 if( !sys.FileSystem.exists(rep+Data.safe(p)+"/"+Data.safe(inf.curversion)) ) {
670 if( state.prompt )
671 switch ask("Upgrade "+p+" to "+inf.curversion) {
672 case Yes:
673 case Always: state.prompt = false;
674 case No:
675 return;
676 }
677 doInstall(p,inf.curversion,true);
678 state.updated = true;
679 } else
680 setCurrent(p, inf.curversion, true);
681 }
682 }
683
684 function update() {
685 var prj = param("Library");
686 var state = { rep : getRepository(), prompt : false, updated : false };
687 doUpdate(prj,state);
688 if( !state.updated )
689 print(prj + " is up to date");
690 }
691
692 function deleteRec(dir) {
693 for( p in sys.FileSystem.readDirectory(dir) ) {
694 var path = dir+"/"+p;
695 if( sys.FileSystem.isDirectory(path) )
696 deleteRec(path);
697 else
698 safeDelete(path);
699 }
700 sys.FileSystem.deleteDirectory(dir);
701 }
702
703 function remove() {
704 var prj = param("Library");
705 var version = paramOpt();
706 var rep = getRepository();
707 var pdir = rep + Data.safe(prj);
708 if( version == null ) {
709 if( !sys.FileSystem.exists(pdir) )
710 throw "Library "+prj+" is not installed";
711 deleteRec(pdir);
712 print("Library "+prj+" removed");
713 return;
714 }
715
716 var vdir = pdir + "/" + Data.safe(version);
717 if( !sys.FileSystem.exists(vdir) )
718 throw "Library "+prj+" does not have version "+version+" installed";
719
720 var cur = getCurrent(pdir);
721 if( cur == version )
722 throw "Can't remove current version of library "+prj;
723 deleteRec(vdir);
724 print("Library "+prj+" version "+version+" removed");
725 }
726
727 function set() {
728 var prj = param("Library");
729 var version = param("Version");
730 setCurrent(prj,version,false);
731 }
732
733 function setCurrent( prj : String, version : String, doAsk : Bool ) {
734 var pdir = getRepository() + Data.safe(prj);
735 var vdir = pdir + "/" + Data.safe(version);
736 if( !sys.FileSystem.exists(vdir) )
737 throw "Library "+prj+" version "+version+" is not installed";
738 if( getCurrent(pdir) == version )
739 return;
740 if( doAsk && ask("Set "+prj+" to version "+version) == No )
741 return;
742 sys.io.File.saveContent(pdir+"/.current",version);
743 print("Library "+prj+" current version is now "+version);
744 }
745
746 function checkRec( prj : String, version : String, l : List<{ project : String, version : String }> ) {
747 var pdir = getRepository() + Data.safe(prj);
748 if( !sys.FileSystem.exists(pdir) )
749 throw "Library "+prj+" is not installed : run 'haxelib install "+prj+"'";
750 var version = if( version != null ) version else getCurrent(pdir);
751 var vdir = pdir + "/" + Data.safe(version);
752 if( StringTools.endsWith(vdir, "dev") )
753 vdir = getDev(pdir);
754 if( !sys.FileSystem.exists(vdir) )
755 throw "Library "+prj+" version "+version+" is not installed";
756 for( p in l )
757 if( p.project == prj ) {
758 if( p.version == version )
759 return;
760 throw "Library "+prj+" has two version included "+version+" and "+p.version;
761 }
762 l.add({ project : prj, version : version });
763 var xml = sys.io.File.getContent(vdir+"/haxelib.xml");
764 var inf = Data.readData(xml,false);
765 for( d in inf.dependencies )
766 checkRec(d.project,if( d.version == "" ) null else d.version,l);
767 }
768
769 function path() {
770 var list = new List();
771 while( argcur < args.length ) {
772 var a = args[argcur++].split(":");
773 checkRec(a[0],a[1],list);
774 }
775 var rep = getRepository();
776 for( d in list ) {
777 var pdir = Data.safe(d.project)+"/"+Data.safe(d.version)+"/";
778 var dir = rep + pdir;
779 try {
780 dir = getDev(rep+Data.safe(d.project));
781 if( dir.length == 0 || (dir.charAt(dir.length-1) != '/' && dir.charAt(dir.length-1) != '\\') )
782 dir += "/";
783 pdir = dir;
784 } catch( e : Dynamic ) {
785 }
786 var ndir = dir + "ndll";
787 if( sys.FileSystem.exists(ndir) ) {
788 var sysdir = ndir+"/"+Sys.systemName();
789 var is64 = neko.Lib.load("std", "sys_is64", 0)();
790 if( is64 ) sysdir += "64";
791 if( !sys.FileSystem.exists(sysdir) )
792 throw "Library "+d.project+" version "+d.version+" does not have a neko dll for your system";
793 Sys.println("-L "+pdir+"ndll/");
794 }
795 try {
796 var f = sys.io.File.getContent(dir + "extraParams.hxml");
797 Sys.println(StringTools.trim(f));
798 } catch( e : Dynamic ) {
799 }
800 Sys.println(dir);
801 Sys.println("-D "+d.project);
802 }
803 }
804
805 function dev() {
806 var rep = getRepository();
807 var project = param("Library");
808 var dir = paramOpt();
809 var proj = rep + Data.safe(project);
810 if( !sys.FileSystem.exists(proj) ) {
811 sys.FileSystem.createDirectory(proj);
812 sys.io.File.saveContent(proj + "/.current", "dev");
813 }
814 var devfile = proj+"/.dev";
815 if( dir == null ) {
816 if( sys.FileSystem.exists(devfile) )
817 sys.FileSystem.deleteFile(devfile);
818 print("Development directory disabled");
819 } else {
820 try {
821 sys.io.File.saveContent(devfile, dir);
822 print("Development directory set to "+dir);
823 }
824 catch (e:Dynamic) {
825 print("Could not write to " +proj + "/.dev");
826 }
827 }
828 }
829
830 function checkGit() {
831 var gitExists = function()
832 try { command("git", []); return true; } catch (e:Dynamic) return false;
833 if( gitExists() )
834 return;
835 // if we have already msys git/cmd in our PATH
836 var match = ~/(.*)git([\\|\/])cmd$/ ;
837 for (path in Sys.getEnv("PATH").split(";")) {
838 if (match.match(path.toLowerCase()))
839 {
840 var newPath = match.matched(1) + "git" +match.matched(2) + "bin";
841 Sys.putEnv("PATH", Sys.getEnv("PATH") + ";" +newPath);
842 }
843 }
844 if( gitExists() )
845 return;
846 // look at a few default paths
847 for( path in ["C:\\Program Files (x86)\\Git\\bin","C:\\Progra~1\\Git\\bin"] )
848 if( sys.FileSystem.exists(path) ) {
849 Sys.putEnv("PATH", Sys.getEnv("PATH") + ";" +path);
850 if( gitExists() )
851 return;
852 }
853 print("Could not execute git, please make sure it is installed and available in your PATH.");
854 }
855
856 function git() {
857 var libName = param("Library name");
858 var rep = getRepository();
859 var libPath = rep + Data.safe(libName) + "/git";
860
861 if( sys.FileSystem.exists(libPath) ) {
862 var state = { rep : rep, prompt : false, updated : false };
863 doUpdate(libName,state);
864 if( !state.updated )
865 print("You already have a git version of "+libName+" installed");
866 return;
867 }
868
869 var gitPath = param("Git path");
870 var subDir = paramOpt();
871
872 var match = ~/@([0-9]+)/;
873 var rev = if (match.match(gitPath) && match.matchedRight() == "")
874 {
875 gitPath = match.matchedLeft();
876 match.matched(1);
877 }
878 else
879 null;
880
881 print("Installing " +libName + " from " +gitPath);
882 checkGit();
883
884 if( Sys.command("git clone \"" +gitPath + "\" \"" +libPath + "\"") != 0 ) {
885 print("Could not clone git repository");
886 return;
887 }
888 Sys.setCwd(libPath);
889 if (rev != null) {
890 var ret = command("git", ["checkout", rev]);
891 if (ret.code != 0)
892 {
893 print("Could not checkout revision: " +ret.out);
894 // TODO: We might have to get rid of the cloned repository here
895 return;
896 }
897 }
898 var revision = command("git", ["rev-parse", "HEAD"]).out;
899
900 var devPath = libPath + (subDir == null ? "" : "/" + subDir);
901 if (!sys.FileSystem.exists(devPath +"/haxelib.xml"))
902 {
903 var haxelib = "<project name='" +libName + "' url='" +gitPath + "' license='BSD'>"
904 +"<description></description>"
905 +"<version name='" +revision + "'>Updated from git.</version>"
906 +"</project>";
907 sys.io.File.saveContent(devPath +"/haxelib.xml", haxelib);
908 }
909
910 Sys.setCwd(libPath + "/../");
911 sys.io.File.saveContent(".current", "dev");
912 sys.io.File.saveContent(".dev", devPath);
913 print("Done");
914 }
915
916 function run() {
917 var rep = getRepository();
918 var project = param("Library");
919 var temp = project.split(":");
920 project = temp[0];
921 var pdir = rep + Data.safe(project);
922 if( !sys.FileSystem.exists(pdir) )
923 throw "Library "+project+" is not installed";
924 pdir += "/";
925 var version = temp[1] != null ? temp[1] : getCurrent(pdir);
926 var dev = try getDev(pdir) catch ( e : Dynamic ) null;
927 var vdir = dev!=null ? dev : pdir + Data.safe(version);
928 var rdir = vdir + "/run.n";
929 if( !sys.FileSystem.exists(rdir) )
930 throw "Library "+project+" version "+version+" does not have a run script";
931 args.push(Sys.getCwd());
932 Sys.setCwd(vdir);
933 var cmd = "neko run.n";
934 for( i in argcur...args.length )
935 cmd += " "+escapeArg(args[i]);
936 Sys.exit(Sys.command(cmd));
937 }
938
939 function escapeArg( a : String ) {
940 if( a.indexOf(" ") == -1 )
941 return a;
942 return '"'+a+'"';
943 }
944
945 function local() {
946 var file = param("Package");
947 doInstallFile(file,true,true);
948 }
949
950 function command( cmd:String, args:Array<String> ) {
951 var p = new sys.io.Process(cmd, args);
952 var code = p.exitCode();
953 return { code:code, out: code == 0 ? p.stdout.readAll().toString() : p.stderr.readAll().toString() };
954 }
955
956 function proxy() {
957 var rep = getRepository();
958 var host = param("Proxy host");
959 if( host == "" ) {
960 if( sys.FileSystem.exists(rep + "/.proxy") ) {
961 sys.FileSystem.deleteFile(rep + "/.proxy");
962 print("Proxy disabled");
963 } else
964 print("No proxy specified");
965 return;
966 }
967 var port = Std.parseInt(param("Proxy port"));
968 var authName = param("Proxy user login");
969 var authPass = authName == "" ? "" : param("Proxy user pass");
970 var proxy = {
971 host : host,
972 port : port,
973 auth : authName == "" ? null : { user : authName, pass : authPass },
974 };
975 haxe.Http.PROXY = proxy;
976 print("Testing proxy...");
977 try haxe.Http.requestUrl("http://www.google.com") catch( e : Dynamic ) {
978 print("Proxy connection failed");
979 return;
980 }
981 sys.io.File.saveContent(rep + "/.proxy", haxe.Serializer.run(proxy));
982 print("Proxy setup done");
983 }
984
985 function loadProxy() {
986 var rep = getRepository();
987 try haxe.Http.PROXY = haxe.Unserializer.run(sys.io.File.getContent(rep + "/.proxy")) catch( e : Dynamic ) { };
988 }
989
990 // ----------------------------------
991
992 static function print(str) {
993 Sys.print(str+"\n");
994 }
995
996 static function main() {
997 new Main().process();
998 }
999
1000 }