Skip to content

Commit

Permalink
[#2950] Add paster minify command
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hammond committed Oct 2, 2012
1 parent 2923395 commit 4a47c14
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
85 changes: 85 additions & 0 deletions ckan/lib/cli.py
Expand Up @@ -4,6 +4,8 @@
import logging
from pprint import pprint
import re
import ckan.include.rjsmin as rjsmin
import ckan.include.rcssmin as rcssmin

import paste.script
from paste.registry import Registry
Expand Down Expand Up @@ -1665,3 +1667,86 @@ def mangle_po(self):
po.save(out_po)
po.save_as_mofile(out_mo)
print 'zh_TW has been mangled'


class MinifyCommand(CkanCommand):
'''Create minified versions of the given Javascript and CSS files.
Usage:
paster minify [FILE|DIRECTORY] ...
for example:
paster minify ckan/public/base
paster minify ckan/public/base/css/*.css
paster minify ckan/public/base/css/red.css
'''
summary = __doc__.split('\n')[0]
usage = __doc__
min_args = 1

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

def min_path(self, path):
'''Return the .min.* filename for the given .js or .css file.
For example moo.js -> moo.min.js
'''
path, ext = os.path.splitext(path)
return path + '.min' + ext

def minify_file(self, path):
'''Create the minified version of the given file.
If the file is not a .js or .css file (e.g. it's a .min.js or .min.css
file, or it's some other type of file entirely) it will not be
minifed.
If the minified version of the file already exists and is newer than
the source file, the file will not be minified.
:param path: The path to the .js or .css file to minify
'''
path_only, extension = os.path.splitext(path)

if path_only.endswith('.min'):
# This is already a minified file.
return

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

path_min = self.min_path(path)

if os.path.exists(path_min) and (
os.path.getmtime(path) < os.path.getmtime(path_min)):
# Minified file exists and is newer than source file.
return

source = open(path, 'r').read()
if path.endswith('.css'):
f = open(path_min, 'w')
f.write(rcssmin.cssmin(source))
f.close()
print "Minified file '{0}'".format(path)
elif path.endswith('.js'):
f = open(path_min, 'w')
f.write(rjsmin.jsmin(source))
f.close()
print "Minified file '{0}'".format(path)
3 changes: 0 additions & 3 deletions ckan/public/base/.gitignore

This file was deleted.

1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -88,6 +88,7 @@
color = ckan.lib.cli:CreateColorSchemeCommand
check-po-files = ckan.i18n.check_po_files:CheckPoFiles
trans = ckan.lib.cli:TranslationsCommand
minify = ckan.lib.cli:MinifyCommand
datastore = ckanext.datastore.commands:SetupDatastoreCommand
[console_scripts]
Expand Down

0 comments on commit 4a47c14

Please sign in to comment.