-
Notifications
You must be signed in to change notification settings - Fork 0
/
partisan.php
executable file
·942 lines (805 loc) · 16.6 KB
/
partisan.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
#!/usr/bin/env php
<?php
// Try and make PHP not insane.
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_error_handler("e_handler");
function e_handler( $errno, $errstr)
{
#echo "$errno, $errstr";
debug_print_backtrace();
exit(1);
}
// Maybe this script can work with windows someday.
define('SEP', DIRECTORY_SEPARATOR);
define('TESTING', false);
// options that are specificaly bound to partisan
$OUR_SHORT = "";
$OUR_LONG = array(
"add-project:",
"forget-project:",
"show-projects",
"create-project:",
"run-server::",
"help"
);
// settings array
$SETTINGS = array();
// the path to the current project detected from the
// current working directory
$PROJECT = null;
// options provided by shell
$OPTIONS = array();
// Tree structure containg all project directories
$PROJ_TREE = array();
// Flat array structure containg all project directories
$PROJ_TABLE = array();
/*
* interpret arguments given from shell and act accordingly.
*
* @return void
*/
function run()
{
global $OUR_SHORT;
global $OUR_LONG;
global $argv;
// shell arguments
$ARGV = $argv;
// current working directory provided by shell
$PWD = getenv('PWD');
// initialize options using the php builtin
$options = getopt($OUR_SHORT, $OUR_LONG);
set_options($options);
// reformat option keys for easier use
$opt_keys = strip_merge($OUR_SHORT, $OUR_LONG);
// check current working directory is a project.
$project = detect_project($PWD);
if ($project !== null)
{
set_project($project);
}
$status = null;
if (for_partisan($opt_keys, $options))
{
// command is internal to partisan
$status = internal_exec($options);
}
elseif($project != null)
{
// command is forwarded to artisan
$status = artisan_exec($ARGV);
}
else
{
// no arguments just print out the help
$status = partisan_help();
}
$msg = get_msg($status);
if ($msg)
{
fwrite(STDERR, "$msg"."\n");
}
exit($status);
}
/*
* strip ':' symboles from shell option definitions and place
* them in a array
*
* @param string $options
* @param array $long_opts
*
* @return array
*/
function strip_merge($options = "", $long_opts = array())
{
// remove all ':' characters from the option definitions
$strip_colon = function($string){
return str_replace(':', '', $string);
};
// perform the actual stripping
$short_keys = array();
if ($options)
{
$short_keys = str_split($strip_colon($options));
}
$long_keys = array_map($strip_colon, $long_opts);
return array_merge($short_keys, $long_keys);
}
/*
* detect if the nodes of a path provided satisfy a
* stored project path
*
* @param string $path
*/
function detect_project($path)
{
$tree = get_tree();
return search_tree($path, $tree);
}
/*
* determineif any options passed from the shell
* are not partisan specific
*
* @param array $opt_keys
* @param array $option
*/
function for_partisan($opt_keys, $options)
{
foreach (array_keys($options) as $option)
{
if ( in_array($option, $opt_keys))
{
return true;
}
}
return false;
}
/*
* filter and return any options passed from the shell
* that are not specific to partisan
*
* @TODO this is currently cruft, could be usefull
* so leaving it in for now.
*
* @param array $opt_keys
* @param array $recieved
*
* @return array
*/
function non_partisan($opt_keys, $options)
{
// optoins that are not specific to partisan
$not_ours = array();
// look for non partisan specific options
foreach ($recieved as $key => $val)
{
if ( ! in_array($key, $opt_keys))
{
//There is an option not recognized by partisan
$not_ours[$key] = $val;
}
}
return $not_ours;
}
/*
* run commmands that are internal to partisan
*
* @param array $options
* @param string $project
*/
function internal_exec($options)
{
$status = null;
$keys = array_keys($options);
if (in_array("run-server", $keys))
{
$status = run_server();
}
elseif(in_array("add-project", $keys))
{
$status = add_project();
}
elseif(in_array("forget-project", $keys))
{
$status = forget_project();
}
elseif(in_array("create-project", $keys))
{
$status = create_project();
}
elseif(in_array("show-projects", $keys))
{
$status = show_projects();
}
elseif(in_array("help", $keys))
{
$status = partisan_help();
}
return $status;
}
/*
* Print Partisan specific help and then call
* call artisan's help if pwd in a project
*/
function partisan_help()
{
$msg = <<<EOT
You are currently using Partisan, the Artisan command wrapper for
the Laravel PHP framework.
options:
--add-project path Track a new project path
--forget-project path Stop tracking the project path
--create-project name Create a new project in current directory
--show-projects Show all projects registered with Partisan
--run-server[=hostname:port] Run the builtin php webserver for the
current project (whitespace is not
an allowed argument separator)
EOT;
print $msg . "\n";
$project = get_project();
if ($project)
{
// Append real Artisan's help message to Partisan's
print "Real Artisan help:\n";
artisan_exec("--help");
}
return 0;
}
/*
* Store a project path so it can be detected from the working
* directory
*
* @param string $path
*
* @return integer
*/
function add_project($path = null)
{
if ($path == null)
{
$path = get_options("add-project");
}
$project = realpath($path);
if ( ! is_dir($project))
{
return 22;
}
elseif( ! file_exists($project . '/artisan'))
{
return 23;
}
$table = get_table();
if (in_array($project, $table))
{
//avoid duplication
return 21;
}
$table[] = $project;
set_table($table);
$tree = get_tree();
$tree = tree_add_path($tree, $project);
set_tree($tree);
// need to store table first or they will be considered out
// of sync
if (store_table($table) and store_tree($tree))
{
echo "project location $project has been added to partisan\n";
}
return 0;
}
/*
* remove the given project from the project table
* and persist the change
*
* @param string $project
*
* @return integer
*/
function forget_project()
{
$raw_input = get_options("forget-project");
$path = realpath($raw_input);
$table = get_table();
if ($table === null)
{
return 24;
}
$index = array_search($path, $table);
if ( $index === false)
{
// given path is not tracked
return 25;
}
// remove the project from the project table
unset($table[$index]);
set_table($table);
$success = store_table($table);
if($success === false)
{
return 5;
}
// currently no way to delete projects from tree,
// rebuild the whole thing.
$tree = build_tree($table);
set_tree($tree);
$success = store_tree($tree);
if($success === false)
{
return 6;
}
return 0;
}
/*
* Create a new Laravel 4 "Illuminate" project and
* add register it with Partisan
*/
function create_project()
{
$status = null;
$name = get_options("create-project");
$git = shell_exec("which git");
$git = trim($git);
// no git binary found
if ( ! $git) return 71;
$repository = get_settings("laravel_repo");
passthru("git clone $repository $name", $status);
if ($status !== 0)
{
return 70;
}
$PWD = getenv('PWD');
$project = $PWD . "/$name";
$version = get_settings("laravel_version");
if ($version == "3")
{
// laravel 3 doesn't use composer
add_project($project);
return 0;
}
$composer = shell_exec("which composer");
if ($composer == null)
{
$composer = shell_exec("which composer.phar");
}
else
{
$installed = composer_install($project);
if($installed == 0) $composer = "$project/composer.phar";
}
$composer = trim($composer);
if ($composer)
{
#echo "$composer install -d $project"; exit;
passthru("$composer install -d $project", $status);
}
add_project($project);
return $status;
}
function composer_install($path = null)
{
$status = null;
if($path == null)
{
$project = get_project();
if ($project == null)
{
// not in a project dir
return 60;
}
$path = $project;
}
$installer = file_get_contents('https://getcomposer.org/installer');
// monkey patch composer's stupid install script to follow our
// fake arguments
$coerced = array("--install-dir=$path");
$installer = str_replace('$argv', '$coerced', $installer);
$installer = str_replace('<?php', '', $installer);
$status = eval($installer);
return $status;
}
/*
* print out all of the currently detectable projects
*
* @return void
*/
function show_projects()
{
$table = get_table();
foreach ($table as $project)
{
print $project . "\n";
}
return 0;
}
/*
* Run the php webserver for the current project
*
* @param string $project
*/
function run_server()
{
$project = get_project();
if ($project == null)
{
return 60; // status code
}
$host_port = get_options('run-server');
if ( ! $host_port) $host_port = "localhost:8000";
$webroot = $project . '/public';
$status = null;
passthru("php -S $host_port -t $webroot 1>&2", $status);
return $status;
}
/* forward arguments to artisan binary and execute it.
*
* @param string $bin_path
* @param array $arguments
*
* @return integer
*/
function artisan_exec($arguments)
{
$project = get_project();
$artisan = $project .'/artisan';
if ( ! file_exists($artisan))
{
return 65;
}
if ( ! is_string($arguments))
{
array_shift($arguments);
$arguments = join(" ", $arguments);
}
passthru("php $artisan $arguments", $status);
return $status;
}
/*
* loads and contains settings array
*
* @param string $setting
*
* @return mixed
*/
function get_settings($setting)
{
global $SETTINGS;
if (empty($SETTINGS))
{
$file_name = "settings.php";
$path = user_dir() . "/" . $file_name;
if ( ! file_exists($path))
{
$path = __DIR__ . "/" . $file_name;
}
$SETTINGS = require $path;
}
if ( ! array_key_exists($setting, $SETTINGS))
{
print "couldn't find settings for key '$settings'";
exit(1);
}
return $SETTINGS[$setting];
}
/*
* contains all status messages
*
* @param integer $status
*/
function get_msg($status)
{
$messages = array(
0 => null,
1 => null, // let the called external programs handle it.
5 => "Couldn't write to tracked projects table",
6 => "Couldn't write to tracked projects tree",
21 => "Project path is already registered",
22 => "Given project path is not a valid directory",
23 => "Could not find artisan binary at given project path",
24 => "There are currently no tracked projects",
25 => "Given project path is not currently tracked",
60 => "Working directory not in a regsitered project",
65 => "No Artisan binary found in the current project",
70 => "Git clone failed",
71 => "No git binary was found in your \$path",
);
if ( ! array_key_exists($status, $messages))
{
return "unkown error code: $status";
}
return $messages[$status];
}
function user_dir()
{
$HOME = getenv('HOME');
$path = "$HOME" . SEP . ".partisan";
if ( ! is_dir($path))
{
mkdir($path);
}
return $path;
}
/*
* returns all the paths used for cache and configuration
*
* @param string $key
*
* @return string
*/
function get_path($key)
{
$user_dir = user_dir();
switch ($key)
{
case "tree_cache":
return $user_dir . '/.tree_cache';
break;
case "table":
return $user_dir . '/projects.inc';
break;
}
}
/*
* return the value of an option provided by the shell
*
* @param string $option
*/
function get_options($option = null)
{
global $OPTIONS;
$options = $OPTIONS;
if($option)
{
return $options[$option];
}
return $options;
}
/*
* Set the global options provided by the calling shell
*
* @param array $options
*/
function set_options($options)
{
global $OPTIONS;
$OPTIONS = $options;
}
/* Get the projected detected from the working directory
*
* @param string $project
*/
function get_project()
{
global $PROJECT;
$project = $PROJECT;
return $project;
}
/* set the projected detected from the working directory
*
* @param string $project
*/
function set_project($project)
{
global $PROJECT;
$PROJECT = $project;
}
/*
* get the current global project tree or grab the cached
* version and set the global
*
* @return array $PROJ_TREE
*/
function get_tree()
{
global $PROJ_TREE;
if (empty($PROJ_TREE))
{
if (tree_is_synced())
{
// tree is in sync with table, load it from cache
$path = get_path('tree_cache');
$tree = restore_tree($path);
}
else
{
// tree is behind changes made to the table, rebuild
// the entire tree from the new table.
$table = get_table();
$tree = build_tree($table);
}
store_tree($tree);
set_tree($tree);
}
$proj_tree = $PROJ_TREE;
return $proj_tree;
}
/*
* determine if the tree is synced with the
* project table
*/
function tree_is_synced()
{
$tree_path = get_path('tree_cache');
if( ! file_exists($tree_path))
{
return false;
}
$tree_time = filemtime($tree_path);
$table_path = get_path('table');
if ( ! file_exists($table_path))
{
return false;
}
$table_time = filemtime($table_path);
if ($table_time > $tree_time)
{
return false;
}
return true;
}
/*
* set the state of the global project tree
*
* @param array tree
*/
function set_tree($tree)
{
global $PROJ_TREE;
$PROJ_TREE = $tree;
}
/*
* Load a project tree from given path.
*
* @param string $path
*
* @return array
*/
function restore_tree($path)
{
if ( ! file_exists($path))
{
return null;
}
return unserialize(file_get_contents($path));
}
/*
* Persist the given project tree to the proper path in
* the user directory
*
* @param array $tree
*
* @return mixed
*/
function store_tree($tree)
{
$path = get_path('tree_cache');
return file_put_contents($path, serialize($tree));
}
/*
* build a new tree from array of path strings
*
* @param array $paths
*
* @return array
*/
function build_tree($paths)
{
$tree = array();
foreach ($paths as $path)
{
$tree = tree_add_path($tree, $path);
}
return $tree;
}
/*
* Add a new path to a tree
*
* @param string $path
* @param array $tree
*
* @return array
*/
function tree_add_path($tree, $path)
{
$path_nodes = path2array($path);
$branch = &$tree;
foreach ($path_nodes as $node)
{
if ( ! array_key_exists($node, $branch))
{
#$branch[$node] = array("#parent" => &$branch);
$branch[$node] = array();
}
$branch = &$branch[$node];
}
$leef = &$branch;
$leef = $path;
return $tree;
}
/*
* split a path path string into a flat array
* of directories
*
* @param string $dir
*
* @return array
*/
function path2array($dir)
{
// stripp root, trailing slash; surrounding space chars
$stripped = trim($dir, SEP.' \t');
return explode(SEP, $stripped);
}
/*
* Search a tree for the given pth
*
* @param string $path
* @param array $tree
*
* @return string
*/
function search_tree($path, $tree)
{
$nodes = path2array($path);
$branch = $tree;
foreach ($nodes as $node)
{
if (array_key_exists($node, $branch))
{
$branch = $branch[$node];
if (is_string($branch))
{
// The branch is a string and therefore a leaf
// containing the matching project's root dir.
return $branch;
}
}
}
return null;
}
/*
* Pull in the global project table, try and restore
* from file if in null state.
*
* @return array
*/
function get_table()
{
global $PROJ_TABLE;
if ($PROJ_TABLE == null)
{
$table = array();
$path = get_path('table');
if(file_exists($path))
{
$table = restore_table($path);
}
else
{
// create an empty table file
store_table($table);
}
}
set_table($table);
return $table;
}
/*
* Set the table global project table from input
*
* @return void
*/
function set_table($table)
{
global $PROJ_TABLE;
$PROJ_TABLE = $table;
}
/*
* Load the project table array from file
*
* @param string $path
*
* @return array
*/
function restore_table($path)
{
if ( ! file_exists($path)) return null;
return require $path;
}
/*
* store the state of the given project table to file
* in user directtory
*
* @param array $table
*/
function store_table($table)
{
$path = get_path('table');
$comment = "For information purposes, DO NOT EDIT!";
$code = "<?php\n\n#$comment \n\n return " .
var_export($table, true) . ';';
return file_put_contents($path, $code);
}
if (( ! TESTING) and php_sapi_name() === 'cli')
{
run();
}