Skip to content

Commit

Permalink
[ckan#469] Add --clean option to paster minify and add extensions to …
Browse files Browse the repository at this point in the history
…minified dirs
  • Loading branch information
tobes committed Mar 1, 2013
1 parent 15c23b2 commit d4811c6
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions ckan/lib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1744,35 +1744,62 @@ class MinifyCommand(CkanCommand):
Usage:
paster minify [FILE|DIRECTORY] ...
paster minify [--clean] [FILE|DIRECTORY] ...
for example:
paster minify ckan/public/base
paster minify ckan/public/base/css/*.css
paster minify ckan/public/base/css/red.css
if the --clean option is provided any minified files will be removed.
'''
summary = __doc__.split('\n')[0]
usage = __doc__
min_args = 1

exclude_dirs = ['vendor']

def __init__(self, name):

super(MinifyCommand, self).__init__(name)

self.parser.add_option('--clean', dest='clean',
action='store_true', default=False, help='remove any minified files in the path')

def command(self):
self._load_config()
for base_path in self.args:
if os.path.isfile(base_path):
self.minify_file(base_path)
if self.options.clean:
self.clear_minifyed(base_path)
else:
self.minify_file(base_path)
elif os.path.isdir(base_path):
for root, dirs, files in os.walk(base_path):
dirs[:] = [d for d in dirs if not d in self.exclude_dirs]
for filename in files:
path = os.path.join(root, filename)
self.minify_file(path)
if self.options.clean:
self.clear_minifyed(path)
else:
self.minify_file(path)
else:
# Path is neither a file or a dir?
continue

def clear_minifyed(self, path):
path_only, extension = os.path.splitext(path)

if extension not in ('.css', '.js'):
# This is not a js or css file.
return

if path_only.endswith('.min'):
print 'removing %s' % path
os.remove(path)

def minify_file(self, path):
'''Create the minified version of the given file.
Expand Down

0 comments on commit d4811c6

Please sign in to comment.