Skip to content

Commit

Permalink
Merge pull request #4908 from pdelboca/4639-migrate-cli-commands
Browse files Browse the repository at this point in the history
4639 - Migrate CLI commands
  • Loading branch information
smotornyuk committed Jul 26, 2019
2 parents af33aeb + 2bfbbc1 commit 9af3365
Show file tree
Hide file tree
Showing 5 changed files with 431 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ckan/cli/cli.py
Expand Up @@ -12,6 +12,10 @@
translation,
dataset,
plugin_info,
notify,
tracking,
minify,
less
)

from ckan.config.middleware import make_app
Expand Down Expand Up @@ -46,3 +50,7 @@ def ckan(ctx, config, *args, **kwargs):
ckan.add_command(translation.translation)
ckan.add_command(dataset.dataset)
ckan.add_command(plugin_info.plugin_info)
ckan.add_command(notify.notify)
ckan.add_command(tracking.tracking)
ckan.add_command(minify.minify)
ckan.add_command(less.less)
95 changes: 95 additions & 0 deletions ckan/cli/less.py
@@ -0,0 +1,95 @@
# encoding: utf-8

import click
import subprocess
import os

from ckan.common import config
from ckan.cli import error_shout


_custom_css = {
u'fuchsia': u'''
@layoutLinkColor: #E73892;
@footerTextColor: mix(#FFF, @layoutLinkColor, 60%);
@footerLinkColor: @footerTextColor;
@mastheadBackgroundColor: @layoutLinkColor;
@btnPrimaryBackground: lighten(@layoutLinkColor, 10%);
@btnPrimaryBackgroundHighlight: @layoutLinkColor;
''',

u'green': u'''
@layoutLinkColor: #2F9B45;
@footerTextColor: mix(#FFF, @layoutLinkColor, 60%);
@footerLinkColor: @footerTextColor;
@mastheadBackgroundColor: @layoutLinkColor;
@btnPrimaryBackground: lighten(@layoutLinkColor, 10%);
@btnPrimaryBackgroundHighlight: @layoutLinkColor;
''',

u'red': u'''
@layoutLinkColor: #C14531;
@footerTextColor: mix(#FFF, @layoutLinkColor, 60%);
@footerLinkColor: @footerTextColor;
@mastheadBackgroundColor: @layoutLinkColor;
@btnPrimaryBackground: lighten(@layoutLinkColor, 10%);
@btnPrimaryBackgroundHighlight: @layoutLinkColor;
''',

u'maroon': u'''
@layoutLinkColor: #810606;
@footerTextColor: mix(#FFF, @layoutLinkColor, 60%);
@footerLinkColor: @footerTextColor;
@mastheadBackgroundColor: @layoutLinkColor;
@btnPrimaryBackground: lighten(@layoutLinkColor, 10%);
@btnPrimaryBackgroundHighlight: @layoutLinkColor;
''',
}


@click.command(
name=u'less',
short_help=u'Compile all root less documents into their CSS counterparts')
def less():
command = (u'npm', u'bin')
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
output = process.communicate()
directory = output[0].strip()
if not directory:
error_shout(u'Command "{}" returned nothing. Check that npm is '
u'installed.'.format(' '.join(command)))
less_bin = os.path.join(directory, u'lessc')

public = config.get(u'ckan.base_public_folder')

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


def _compile_less(root, less_bin, color):
click.echo(u'compile {}.css'.format(color))
main_less = os.path.join(root, u'less', u'main.less')
main_css = os.path.join(root, u'css', u'{}.css'.format(color))
command = (less_bin, main_less, main_css)
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
output = process.communicate()
click.echo(output)
80 changes: 80 additions & 0 deletions ckan/cli/minify.py
@@ -0,0 +1,80 @@
# encoding: utf-8

import click
import os
import ckan.include.rjsmin as rjsmin
import ckan.include.rcssmin as rcssmin

_exclude_dirs = [u'vendor']


@click.command(name=u'minify')
@click.option(
u'--clean', is_flag=True, help=u'remove any minified files in the path.')
@click.argument(u'path', nargs=-1, type=click.Path())
def minify(clean, path):
u'''Create minified versions of the given Javascript and CSS files.'''
for base_path in path:
if os.path.isfile(base_path):
if clean:
_clear_minifyed(base_path)
else:
_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 d not in _exclude_dirs]
for filename in files:
path = os.path.join(root, filename)
if clean:
_clear_minifyed(path)
else:
_minify_file(path)
else:
# Path is neither a file or a dir?
continue


def _clear_minifyed(path):
u'''Remove the minified version of the file'''
path_only, extension = os.path.splitext(path)

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

if path_only.endswith(u'.min'):
click.echo(u'removing {}'.format(path))
os.remove(path)


def _minify_file(path):
u'''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.
:param path: The path to the .js or .css file to minify
'''
import ckan.lib.fanstatic_resources as fanstatic_resources
path_only, extension = os.path.splitext(path)

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

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

path_min = fanstatic_resources.min_path(path)

source = open(path, u'r').read()
f = open(path_min, u'w')
if path.endswith(u'.css'):
f.write(rcssmin.cssmin(source))
elif path.endswith(u'.js'):
f.write(rjsmin.jsmin(source))
f.close()
click.echo(u"Minified file '{}'".format(path))
23 changes: 23 additions & 0 deletions ckan/cli/notify.py
@@ -0,0 +1,23 @@
# encoding: utf-8

import click
from ckan.model import Session, Package, DomainObjectOperation
from ckan.model.modification import DomainObjectModificationExtension


@click.group(
name=u'notify',
short_help=u'Send out modification notifications.'
)
def notify():
pass


@notify.command(
name=u'replay',
short_help=u'Send out modification signals.'
)
def replay():
dome = DomainObjectModificationExtension()
for package in Session.query(Package):
dome.notify(package, DomainObjectOperation.changed)

0 comments on commit 9af3365

Please sign in to comment.