add AJATUS_ROOT to include path since autoloading is not used everywhere, some places...
[ajatus.git] / build / packJavascripts.php
blobbc31a2f3a8665c66193935a78f8e1b46c9f90154
1 <?php
4 /**
5 * @copyright The Midgard Project, http://www.midgard-project.org
6 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7 *
8 */
9 require_once "phing/Task.php";
11 require 'class.JavaScriptPacker.php';
13 /**
14 * Javascript packer task for Phing
16 class packJavascripts extends Task
18 function __construct()
23 protected $returnProperty; // name of property to set to return value
25 /**
26 * The root path to where the files are stored.
28 private $path = null;
30 /**
31 * List of files to be packed
33 private $js_files = array();
34 /**
35 * List of packed files
37 private $js_packed_files = array();
39 private $statistics = null;
41 private $js_src_files = array();
43 private $js_file_names = array();
45 private $no_source = true;
47 private $action = 'pack';
49 /**
50 * The target directory where the packed files should be saved.
52 protected $target_dir = null;
54 /**
55 * The setter for the attribute "target_dir"
57 public function setTarget_dir($str)
59 $this->target_dir = $str;
62 public function setPath($str)
64 $this->path = $str;
67 public function setAction($str)
69 $this->action = $str;
72 /**
73 * Sets property name to set with return value of function or expression.
75 public function setReturnProperty($r)
77 $this->returnProperty = $r;
80 protected $copyfiles = array ();
82 /**
83 * The init method: Do init steps.
85 public function init()
87 // nothing to do here
90 /**
91 * The main entry point method.
93 public function main()
95 $this->directory_list_js_files($this->path);
97 $this->prepare_files();
99 if ($this->action == 'pack')
101 $this->pack_files();
103 else
105 $this->unpack_files();
108 $this->project->setProperty($this->returnProperty, $this->statistics);
112 * Generate the filelist
113 * @param array $config File listing configuration
114 * @return string File XML list
116 function directory_list_js_files($path)
118 $directory = dir($path);
120 // List contents
121 while (false !== ($entry = $directory->read()))
123 if (substr($entry, 0, 1) == '.')
125 // Ignore dotfiles
126 continue;
128 if ($entry == '.svn')
130 // Ignore SVN directories
131 continue;
134 // Check for js files
135 $path_parts = pathinfo($entry);
136 switch ($path_parts['extension'])
138 case 'js':
139 $clean_filename = str_replace('.src', '', $path_parts['filename']);
140 $clean_filename = str_replace('.pack', '', $clean_filename);
142 if (! in_array($path_parts['filename'], $this->js_file_names))
144 if ( strpos($path_parts['filename'], '.src') === false
145 && strpos($path_parts['filename'], '.pack') === false)
147 $this->js_file_names[$clean_filename] = $path_parts['filename'];
148 $this->js_files[$clean_filename] = $path . "/" . $entry;
151 if (strpos($path_parts['filename'], '.pack') !== false)
153 $this->js_packed_files[$clean_filename] = $path . "/" . $entry;
155 if (strpos($path_parts['filename'], '.src') !== false)
157 $this->js_src_files[$clean_filename] = $path . "/" . $entry;
159 break;
160 default:
161 break;
164 if (is_dir("{$path}/{$entry}"))
166 // List the subdirectory
167 $subpath = "{$path}/{$entry}";
168 $this->directory_list_js_files($subpath);
173 function prepare_files()
175 foreach ($this->js_file_names as $clean_name => $file)
177 $old_packed = $this->js_packed_files[$clean_name];
178 $src_file = $this->js_src_files[$clean_name];
179 $sel_source = '';
180 //echo "\nClean name: {$clean_name}, file: {$file}\n";
181 //echo "\nPacked: {$old_packed}\n";
182 //echo "\nSource: {$src_file}\n";
184 $orig_file_parts = pathinfo($this->js_files[$clean_name]);
185 $new_packed_file = $orig_file_parts['dirname'] . "/" . $orig_file_parts['filename'] . ".pack.js";
186 $new_source_file = $orig_file_parts['dirname'] . "/" . $orig_file_parts['filename'] . ".src.js";
188 if (empty($src_file))
190 //echo "\nWe have no separate source file, assume the original is the source.\n";
191 $this->no_source = true;
192 $sel_source = $this->js_files[$clean_name];
194 else
196 //echo "\nWe have source file. Then we also must have either packed file, and/or the original is packed.\n";
197 $this->no_source = false;
198 $sel_source = $src_file;
200 if (! empty($old_packed))
202 //echo "\nWe have old packed file. Delete this.\n";
203 unlink($old_packed);
207 //echo "\nSelected source file {$sel_source}\n";
209 if (! $this->no_source)
211 //Separate source file
212 //echo "\nRename current packed file {$this->js_files[$clean_name]} to .pack\n";
213 rename($this->js_files[$clean_name], $new_packed_file);
214 //echo "\nRename current source file {$sel_source} to {$this->js_files[$clean_name]}\n";
215 rename($sel_source, $this->js_files[$clean_name]);
220 function pack_files()
222 $t1 = microtime(true);
224 $file_count = count($this->js_files);
225 foreach ($this->js_files as $key => $src_file)
227 $src_file_contents = file_get_contents($src_file);
229 $src_file_parts = pathinfo($src_file);
230 $src_new_file = $src_file_parts['dirname'] . "/" . $src_file_parts['filename'] . ".src.js";
231 $trgt_file = $src_file;
233 if (file_exists($src_file))
235 rename($src_file, $src_new_file);
236 //echo "\nRename source file {$src_file} to {$src_new_file}\n";
237 $this->statistics .= "\nrename {$src_file} to {$src_new_file}\n";
239 else
241 echo "\nFATAL ERROR: Could not find source file {$src_file}\n";
242 continue;
245 $this->js_src_files[$key] = $src_new_file;
246 $this->js_packed_files[$key] = $trgt_file;
248 $this->statistics .= "Packing file: {$src_new_file} to {$trgt_file}... ";
250 $packer = new JavaScriptPacker($src_file_contents, 'Normal', true, false);
251 $packed = $packer->pack();
253 $this->statistics .= "writing packed file... ";
254 file_put_contents($trgt_file, $packed);
255 $this->statistics .= "DONE\n";
258 $t2 = microtime(true);
259 $time = sprintf('%.4f', ($t2 - $t1) );
260 $this->statistics .= "{$file_count} files packed in {$time} seconds. \n";
263 function unpack_files()
265 $t1 = microtime(true);
267 $file_count = count($this->js_files);
268 foreach ($this->js_files as $key => $file)
270 $file_parts = pathinfo($file);
271 $src_file = $this->js_src_files[$key];
272 $packed_file = $this->js_packed_files[$key];
273 $new_src_file = $file;
274 $new_packed_file = $file_parts['dirname'] . "/" . $file_parts['filename'] . ".pack.js";
276 if (file_exists($packed_file))
278 if ( file_exists($new_packed_file)
279 && $packed_file != $new_packed_file)
281 //echo "\nWe have old packed file. Delete this.\n";
282 unlink($new_packed_file);
283 $this->statistics .= "\nunlink {$new_packed_file}\n";
286 //echo "\nrename {$packed_file} to {$new_packed_file}\n";
287 rename($packed_file, $new_packed_file);
288 $this->statistics .= "\nrename {$packed_file} to {$new_packed_file}\n";
291 if (file_exists($src_file))
293 //echo "\nrename {$src_file} to {$new_src_file}\n";
294 rename($src_file, $new_src_file);
295 $this->statistics .= "\nrename {$src_file} to {$new_src_file}\n";
299 $t2 = microtime(true);
300 $time = sprintf('%.4f', ($t2 - $t1) );
301 $this->statistics .= "\nunpacked {$file_count} files in {$time} seconds. \n";