Skip to content

Commit

Permalink
Merge pull request #176 from okfn/compile-less-via-paster
Browse files Browse the repository at this point in the history
Patch to compile less when minifying files - updated logic
  • Loading branch information
johnmartin committed Feb 19, 2013
2 parents acc65c5 + 4a067ff commit 26c75e5
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
119 changes: 119 additions & 0 deletions ckan/lib/cli.py
Expand Up @@ -1804,3 +1804,122 @@ def minify_file(self, path):
f.write(rjsmin.jsmin(source))
f.close()
print "Minified file '{0}'".format(path)


class LessCommand(CkanCommand):
'''Compile all root less documents into their CSS counterparts
Usage:
paster less
'''
summary = __doc__.split('\n')[0]
usage = __doc__
min_args = 0

def command(self):
self.less()

custom_css = {
'fuchsia': '''
@layoutLinkColor: #b509b5;
@mastheadBackgroundColorStart: #dc0bdc;
@mastheadBackgroundColorEnd: #f31df3;
@btnPrimaryBackground: #f544f5;
@btnPrimaryBackgroundHighlight: #f76bf7;
''',

'green': '''
@layoutLinkColor: #045b04;
@mastheadBackgroundColorStart: #068106;
@mastheadBackgroundColorEnd: #08a808;
@btnPrimaryBackground: #0acf0a;
@btnPrimaryBackgroundHighlight: #10f210
''',

'red': '''
@layoutLinkColor: #b50909;
@mastheadBackgroundColorStart: #dc0b0b;
@mastheadBackgroundColorEnd: #f31d1d;
@btnPrimaryBackground: #f54444;
@btnPrimaryBackgroundHighlight: #f76b6b;
''',

'maroon': '''
@layoutLinkColor: #5b0404;
@mastheadBackgroundColorStart: #810606;
@mastheadBackgroundColorEnd: #a80808;
@btnPrimaryBackground: #cf0a0a;
@btnPrimaryBackgroundHighlight: #f21010;
''',
}
def less(self):
''' Compile less files '''
import subprocess
command = 'npm bin'
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
output = process.communicate()
directory = output[0].strip()
less_bin = os.path.join(directory, 'lessc')

root = os.path.join(os.path.dirname(__file__), '..', 'public', 'base')
root = os.path.abspath(root)
custom_less = os.path.join(root, 'less', 'custom.less')
for color in self.custom_css:
f = open(custom_less, 'w')
f.write(self.custom_css[color])
f.close()
self.compile_less(root, less_bin, color)
f = open(custom_less, 'w')
f.write('// This file is needed in order for ./bin/less to compile in less 1.3.1+\n')
f.close()
self.compile_less(root, less_bin, 'main')



def compile_less(self, root, less_bin, color):
print 'compile %s.css' % color
import subprocess
main_less = os.path.join(root, 'less', 'main.less')
main_css = os.path.join(root, 'css', '%s.css' % color)

command = '%s %s %s' % (less_bin, main_less, main_css)

process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
output = process.communicate()



class FrontEndBuildCommand(CkanCommand):
''' Creates and minifies css and JavaScript files
Usage:
paster front-end-build
'''

summary = __doc__.split('\n')[0]
usage = __doc__
min_args = 0

def command(self):
self._load_config()

# Less css
cmd = LessCommand('less')
cmd.command()

# js translation strings
cmd = TranslationsCommand('trans')
cmd.options = self.options
cmd.args = ('js',)
cmd.command()

# minification
cmd = MinifyCommand('minify')
cmd.options = self.options
root = os.path.join(os.path.dirname(__file__), '..', 'public', 'base')
root = os.path.abspath(root)
cmd.args = (root,)
cmd.command()
3 changes: 3 additions & 0 deletions setup.py
Expand Up @@ -89,7 +89,10 @@
check-po-files = ckan.i18n.check_po_files:CheckPoFiles
trans = ckan.lib.cli:TranslationsCommand
minify = ckan.lib.cli:MinifyCommand
less = ckan.lib.cli:LessCommand
datastore = ckanext.datastore.commands:SetupDatastoreCommand
front-end-build = ckan.lib.cli:FrontEndBuildCommand
[console_scripts]
ckan-admin = bin.ckan_admin:Command
Expand Down

0 comments on commit 26c75e5

Please sign in to comment.