Created
July 22, 2012 23:40
-
-
Save panbanda/3161398 to your computer and use it in GitHub Desktop.
GIT: Precommit Minify CSS + Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
// See if we even need to run this file | |
$modified_files = shell_exec("git st"); | |
if (strpos($modified_files, '.js') === FALSE AND strpos($modified_files, '.css') === FALSE) die("/ Skipping minification.\n"); | |
// Find the config files to minify | |
$search = shell_exec("find . -name min.config.xml"); | |
$config_files = explode("\n", $search); | |
// Lets minify each file | |
foreach ($config_files as $config_file) | |
{ | |
$directory = dirname($config_file)."/"; | |
if ($config_file != "") | |
{ | |
$xml = simplexml_load_file($config_file); | |
// For each output file | |
foreach ($xml->children() as $node) | |
{ | |
$content = ''; | |
$attributes = $node->attributes(); | |
$min_filename = (string) $attributes['minFileName']; | |
// Combine the child contents | |
foreach ($node->children() as $file) | |
{ | |
if (is_file($directory.$file)) | |
{ | |
$ext = pathinfo($file, PATHINFO_EXTENSION); | |
$content .= file_get_contents($directory.$file); | |
} | |
else echo "! $directory.$file does not exist [$config_file : $min_filename]\n"; | |
} | |
// Setup the filenames | |
$uncompressed_file = "$directory$min_filename.uncompressed.$ext"; | |
$minified_file = "$directory$min_filename.min.$ext"; | |
file_put_contents($uncompressed_file, $content); | |
// Compress the correct type | |
if ($ext == "css") shell_exec("java -jar minify/yuicompressor-2.4.jar $uncompressed_file -o $minified_file"); | |
elseif ($ext == "js") shell_exec("java -jar minify/closure.jar --js $uncompressed_file --js_output_file $minified_file"); | |
print_r("+ Minified: $minified_file\n"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<root> | |
<node minFileName="main"> | |
<file>jquery-ui.css</file> | |
<file>jquery-transform.css</file> | |
<file>account.css</file> | |
</node> | |
</root> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment