Editor fixes
[redakcja.git] / apps / build / management / commands / build.py
1 import os
2 from subprocess import call
3 from optparse import make_option
4
5 from django.core.management.base import BaseCommand, CommandError
6 from django.core.management import call_command
7
8
9 class Command(BaseCommand):
10     
11     option_list = BaseCommand.option_list + (
12         make_option('--node-bin-path',
13             action='store',
14             dest='node_bin_path',
15             type='string',
16             default=None,
17             help='Path to node binary'),
18         make_option('--npm-bin',
19             action='store',
20             dest='npm_bin',
21             type='string',
22             default='npm',
23             help='Path to npm binary'),
24         make_option('--editor-npm-env',
25             action='store',
26             dest='editor_npm_env',
27             type='string',
28             default=None,
29             help='Destination path of npm environment, defaults to ./node_modules'),
30         make_option('--editor-optimize',
31             action='store',
32             dest='editor_optimize',
33             type='string',
34             default=None,
35             help='Optimization strategy for editor build'),
36         )
37
38     def handle(self, **options):
39         wiki_base_dir = os.path.join(os.getcwd(), 'apps', 'wiki', 'static', 'wiki')
40         rng_base_dir = os.path.join(wiki_base_dir, 'editor')
41         build_dir = os.path.join(wiki_base_dir, 'build')
42
43         self.stdout.write('Installing editor dependencies')
44         if options['editor_npm_env']:
45             npm_env = os.path.join(rng_base_dir, options['editor_npm_env'])
46             if not os.path.exists(npm_env):
47                 os.makedirs(npm_env)
48             assert os.path.isdir(npm_env)
49             os.symlink(npm_env, os.path.join(rng_base_dir, 'node_modules'))
50         try:
51             call([options['npm_bin'], 'install'], cwd = rng_base_dir)
52         except OSError:
53             raise CommandError('Something went wrong, propably npm binary not found. Tried: %s' % options['npm_bin'])
54
55         self.stdout.write('Building editor')
56         if options['node_bin_path']:
57             # grunt needs npm binary to be foundable in PATH
58             os.environ['PATH'] = '%s:%s' % (options['node_bin_path'], os.environ['PATH'])
59         args =  ['./node_modules/.bin/grunt', 'build', '--output-dir=%s' % build_dir]
60         if options['editor_optimize']:
61             args.append('--optimize=%s' % options['editor_optimize'])
62         call(args, cwd = rng_base_dir)
63
64         call_command('collectstatic', interactive = False, ignore_patterns = ['editor'])