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