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