Skip to content

Instantly share code, notes, and snippets.

@tiomoreno
Last active December 16, 2015 12:58
Show Gist options
  • Save tiomoreno/5437962 to your computer and use it in GitHub Desktop.
Save tiomoreno/5437962 to your computer and use it in GitHub Desktop.
Ferramenta para minificar arquivos javascript e css usando o YUI Compressor
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
Tool for minifing static files with yui compressor
"""
import os
from subprocess import call
from optparse import OptionParser
def main():
"""
Main execution of tool
"""
parser = OptionParser(
usage="Usage: %prog [options] --dir=SOURCE_DIR",
version="%prog 0.1"
)
parser.set_defaults(
compressor="/home/hugo/tools/yuicompressor-2.4.7.jar"
)
parser.add_option(
"--dir",
"-d",
dest="dir",
help="directory to search for javascript or css files"
)
parser.add_option(
"--compressor",
"-c",
dest="compressor",
help="path to the yui compressor jar file"
)
options = parser.parse_args()[0]
if not options.dir:
parser.error("You have to specify a directory.")
if not os.path.isfile(options.compressor):
parser.error("YUI compressor not found.")
options.dir = os.path.abspath(options.dir)
minify_files(options)
def minify_files(options):
"""
Minifing files based on the parsed options
"""
for filename in [filename
for filename
in os.listdir(options.dir)
if file_not_minified(filename)]:
print "Minifing file: %s" % os.path.join(options.dir, filename)
file_input = os.path.join(options.dir, filename)
file_output = os.path.join(options.dir,
os.path.splitext(filename)[0]
+ ".min%s" % os.path.splitext(filename)[1])
call(["java",
"-jar",
options.compressor,
file_input,
"-o",
file_output])
os.remove(file_input)
def file_not_minified(filename):
"""
Check if the file hasn't been minified
"""
filename_pieces = filename.split(".")
return len(filename_pieces) == 2 and filename_pieces[1] in ["js", "css"]
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment