この minfier スクリプトを Python で作成しました。Bills ソリューションと同様に、YUI コンプレッサーを使用しますが、メイクレス環境で動作します。生の (縮小されていない) ファイルは /some/path/src/ にあり、縮小されたバージョンは /some/path/ にあると想定しています。また、yuicompressor jar ファイルが現在のフォルダーにあると想定しています。
import os, glob
from subprocess import Popen,PIPE
def RunCommand( cmd, show_output ):
p = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE)
for line in p.stdout:
if show_output:
print line
outerr = "\n".join(p.stderr.readlines() )
if len(outerr) > 0:
print "ERROR: " + outerr
sys.exit()
code = p.wait()
if ( code > 0 ):
print ("ERROR CODE: %i" % code )
sys.exit()
compresser = "yuicompressor-2.4.2.jar"
dirs = [ "../wwwroot/css/", "../wwwroot/JavaScript/"]
extensions = ["*.js", "*.css" ]
for dir in dirs:
src = dir + "/src/"
for ext in extensions:
for path in glob.glob( os.path.join( src, ext)):
file = os.path.basename(path)
src_file = os.path.normpath( src + "/" + file )
dest_file = os.path.normpath( dir + "/" + file )
if not os.path.isfile(dest_file) or os.path.getmtime(src_file) > os.path.getmtime(dest_file):
print "minifying %s..." % (dest_file)
command = "java -jar %s %s -o %s" % ( compresser, src_file, dest_file )
RunCommand(command, True)