Skip to content

Instantly share code, notes, and snippets.

@amdgigabyte
Created April 20, 2012 13:49
Show Gist options
  • Save amdgigabyte/2428820 to your computer and use it in GitHub Desktop.
Save amdgigabyte/2428820 to your computer and use it in GitHub Desktop.
YUIcompressor Shell Script
#! /bin/bash
# YUI Compressor Bash Script
# @author [email protected]
# @version 0.1 2012-04-20
# 安装 #
# 1. 设置好你的JAVA_HOME环境变量
# 2. 在自己的家目录下面创建一个bin文件夹 ~/bin
# 2. 复制这个 compress.sh 以及 yuicompressor.jar 到 ~/bin,并执行
# chmod +x compress.sh(以保证compress.sh可执行)
# 3. 打开你的~/.bash_profile
# 复制 alias yc="sh ~/bin/compress.sh" 到.bash_profile文件中,可以加到文件最
# 后, 这样就完成了安装
# 用法 #
# yc path_to_your_js_or_css_file
# 例子 #
# 假设在worksapce目前下面有test.js
# cd workspace
# yc ./test.js
filename=$1
function switch_dir() {
cd `pwd`
main
}
function main () {
echo "YUI Compressor"
echo "=================="
input_filter
}
function input_filter() {
#Check File Extension
if [[ $filename = '' ]]; then
no_file
fi
if [[ ! -f $filename ]]; then
no_file
fi
if [[ ${filename##*.} != 'js' ]]; then
if [ ${filename##*.} != 'css' ]
then
no_file
fi
fi
if [[ ! -f $HOME/bin/yuicompressor.jar ]];then
echo "please set copy yuicompressor.jar to ~/bin/"
exit
fi
if [[ ${JAVA_HOME} = '' ]]; then
no_java "home"
fi
if [[ ! -f $JAVA_HOME/bin/java ]]; then
no_java "java"
fi
if [[ ! -f $JAVA_HOME/bin/native2ascii ]]; then
no_java "native2ascii"
fi
output_filter $filename
}
function output_filter () {
#fliter file name
# 1. filename.source.js -> filename.js
# 2. filename.js -> filename-min.js
local input_filename=$1
local out_filename
local out_fileext
local out_filemain
out_fileext=${input_filename##*.}
out_filemain=${input_filename%.*}
#if there is a ".source" inside the filename,
#the output name would be *.css| *.js
#otherwise the output filename would be *-min.css | *-min.js
out_filename=$out_filemain"-min."$out_fileext
if [[ $input_filename == *.source* ]]; then
out_filename=${input_filename%%.source.*}.$out_fileext
fi
compress $out_filename
}
function compress() {
$JAVA_HOME/bin/java -jar ~/bin/yuicompressor.jar --charset GB18030 $filename -o $1
fix_encoding $1
}
function fix_encoding() {
#start fixing encoding
#translate chinese to unicode text
local fixedfile=$1
cp $fixedfile $fixedfile.tmp
$JAVA_HOME/bin/native2ascii -encoding GB18030 $fixedfile.tmp $fixedfile.tmp1
rm $fixedfile.tmp
if [[ ${fixedfile##*.} == 'css' ]]; then
sed 's/\\\u/\\/g' $fixedfile.tmp1 > $fixedfile
fi
rm $fixedfile.tmp1
print_result $fixedfile
}
function print_result() {
local file=$1
echo "Compress $filename to $file"
echo "The file is compressed from "`stat -f "%Z" $filename` bytes to `stat -f "%Z" $file` bytes
end
}
#function bonus() {
# grep "taobao.net" > /dev/null
#}
function no_file() {
echo "no file found"
exit
}
function no_java() {
echo "Please set your JAVA_HOME enviroment variable -- $1!"
exit
}
function end () {
echo "...Finished!"
exit
}
switch_dir
@sandywu
Copy link

sandywu commented Apr 23, 2012

昆哥威武

@amdgigabyte
Copy link
Author

最新修复文件名中有多个点时,扩展名截取的bug

@Arnoldnuo
Copy link

可以一次性批量压缩所有的js吗?我调用yc *.source.js 只能压缩第一个,不能把所有符合的都压缩,可否一次性压缩所有的js?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment