add cli checks to Php code
[roojs1] / buildSDK / pre-commit
1 #!/usr/bin/php
2 <?php
3 if (php_sapi_name() != 'cli') {
4     die("CLI ONLY");
5 }
6 // install : ln -s buildSDK/pre-commit .git/hooks/
7
8 // the recomendation is to check if the current commit is same as the one on any of the files/directories that need monitoring.
9
10 register_shutdown_function ( 'unlock');
11 lock();
12
13 trigger_action('build_docs', array('Roo.js', 'Roo/', 'Array.js', 'Function.js', 'Date.js', 'String.js', 'Number.js'));
14
15 $core = trigger_action_file('compile_core', 'dependancy_core.txt');
16 $ui = trigger_action_file('compile_ui',  'dependancy_ui.txt');
17 trigger_action_file('compile_bootstrap',  'dependancy_bootstrap.txt');
18 trigger_action_file('compile_mailer',  'dependancy_mailer.txt');
19 trigger_action_file('compile_calendar',  'dependancy_calendar.txt');
20
21 trigger_action('compile_less', array('less/'));
22 trigger_action('compile_scss', array('scss/'));
23
24 if ($core || $ui) {
25     merge_files();
26 }
27
28 function lock()
29 {
30     static $tries = 0;
31     clearstatcache();
32     if (file_exists(".git/pre-commit-running")) {
33         if (file_exists(".git/pre-commit-pending")) {
34             exit;
35         }
36         file_put_contents(".git/pre-commit-pending","\n");
37         sleep(30); // wait 30 seconds
38         unlink(".git/pre-commit-pending");
39         $tries++;
40         if ($tries > 3) {
41             exit;
42         }
43     }
44     file_put_contents(".git/pre-commit-running","\n");
45 }
46 function unlock()
47 {
48     unlink(".git/pre-commit-running");
49 }
50  
51 function changed_files()
52 {
53     static $output = array();
54     if (!empty($output)) {
55         return $output;
56     }
57     $return = 0;
58     exec('git rev-parse --verify HEAD 2> /dev/null', $output, $return);
59     if ($return != 0) {
60         exit;
61     }
62     
63     exec("git diff-index --cached --name-only 'HEAD'", $output);
64     return $output;
65 }
66
67 function trigger_action_file($fn, $filename)
68 {
69     $ret = explode("\n",file_get_contents(__DIR__ . "/" . $filename));
70     $add = array();
71     foreach($ret as $r) {
72         $r=trim($r);
73         if (strlen($r)) {
74             $add[] = str_replace('.','/', $r) . ".js";
75         }
76     }
77     $ret[] = "buildSDK/".$filename;
78     return trigger_action($fn, $add);
79     
80 }
81
82 function trigger_action($fn, $files)
83 {
84     
85     foreach(changed_files() as $cg) {
86         foreach($files as $f) {
87             $f = trim($f);
88             if ($f == $cg) {
89                 echo "RUNNING : $fn\n";
90                 $fn();
91                 return true;
92             }
93             if (substr($f, -1,1) == '/' && substr($cg,0, strlen($f)) == $f) {
94                 echo "RUNNING : $fn\n";
95                 $fn();
96                 return true;    
97             }
98         }
99     }
100     return false;
101 }
102
103 function build_docs()
104 {
105     exec("roojspacker -i buildSDK/dependancy_core.txt  -i buildSDK/dependancy_ui.txt  " . 
106     "-i buildSDK/dependancy_bootstrap.txt -i buildSDK/dependancy_calendar.txt " .
107     "-i buildSDK/dependancy_svg.txt  " .
108    "--doc-target=docs -D");
109 }
110 function compile_core()
111 {
112     exec("roojspacker -t roojs-core.js -T roojs-core-debug.js -i buildSDK/dependancy_core.txt ");
113 }
114
115 function compile_ui()
116 {
117     exec("roojspacker -t roojs-ui.js -T roojs-ui-debug.js -i buildSDK/dependancy_ui.txt");
118 }
119
120 function merge_files()
121 {
122     exec("cat      roojs-core.js  roojs-ui.js >  roojs-all.js");
123     exec("cat roojs-core-debug.js  roojs-ui-debug.js > roojs-debug.js");
124 }
125
126  
127 function compile_bootstrap()
128 {
129     exec("roojspacker -t roojs-bootstrap.js -T roojs-bootstrap-debug.js -i buildSDK/dependancy_bootstrap.txt");
130 }
131  
132 function compile_calendar()
133 {
134     exec("roojspacker -t roojs-calendar.js -T roojs-calendar-debug.js -i buildSDK/dependancy_calendar.txt");
135 }  
136   
137   
138 function compile_mailer()
139 {
140
141     exec("roojspacker -t roojs-mailer.js -T roojs-mailer-debug.js -i buildSDK/dependancy_mailer.txt");
142 }  
143  
144 function compile_docbook()
145 {
146     exec("roojspacker -t roojs-doc.js -T roojs-doc-debug.js -i buildSDK/dependancy_doc.txt");
147 }
148
149   
150
151 function compile_less()
152 {
153     exec("php buildSDK/less-bootstrap-complier.php");
154 }
155
156 function compile_scss()
157 {
158     exec("php buildSDK/scss-bootstrap.php");
159
160  
161
162
163
164  
165        
166