/
pkr.php
executable file
·60 lines (54 loc) · 1.46 KB
/
pkr.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
#!/usr/bin/php
<?php
/*
* Packer para los archivos JS y CSS
*
* Requiere Composer
*
* System Alias configuration(~/.bashrc recommended):
alias pkr='/absolute_path/.../pkr/pkr.php'
*
*
* Git Alias configuration(.git/config):
* [alias]
* pkr=!php /absolute_path/.../pkr/pkr.php
*/
$path = realpath(dirname(__FILE__));
echo "\033[01;31m Packer ".date('Y-m-d H:i:s')."\033[0m\n\n\n";
include($path . '/libs/linkorb/jsmin-php/src/jsmin-1.1.1.php');
include($path . '/libs/natxet/CssMin/src/CssMin.php');
class Pkr{
public function __construct () {
$cmd = "git status ";
$lines = explode(PHP_EOL, shell_exec($cmd));
$pattern = '/^.*:\s+(.*\.(js|css))$/';
foreach($lines as $line) {
$matches = array();
preg_match($pattern, $line, $matches);
if(count($matches) == 3) {
if(!$this->isMin($matches[1])) {
$this->packer($matches[1], $matches[2]);
}
}
}
}
public function isMin($file){
$pattern = '/^.*\.min\.(js|css)$/';
return preg_match($pattern, $file);
}
public function packer($file, $type) {
if(!file_exists($file)) {
return;
}
$fileDst = preg_replace('/\.(js|css)$/', '.min.\1', $file);
if($type == "js") {
$minContent = JSMin::minify(file_get_contents($file));
} else {
$minContent = CssMin::minify(file_get_contents($file));
}
echo "\033[01;41m " . $file . " \033[0m -> \033[32;40m".$fileDst."\033[0m\n";
file_put_contents($fileDst, $minContent);
echo shell_exec("git add " . $fileDst);
}
}
new Pkr();