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