add mobi download
[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     option_list = BaseCommand.option_list + (
17         make_option(
18             '--node-bin-path',
19             action='store',
20             dest='node_bin_path',
21             type='string',
22             default=None,
23             help='Path to node binary'),
24         make_option(
25             '--npm-bin',
26             action='store',
27             dest='npm_bin',
28             type='string',
29             default='npm',
30             help='Path to npm binary'),
31         make_option(
32             '--editor-npm-env',
33             action='store',
34             dest='editor_npm_env',
35             type='string',
36             default=None,
37             help='Destination path of npm environment, defaults to ./node_modules'),
38         make_option(
39             '--editor-optimize',
40             action='store',
41             dest='editor_optimize',
42             type='string',
43             default=None,
44             help='Optimization strategy for editor build'),
45         )
46
47     def handle(self, **options):
48         wiki_base_dir = os.path.join(os.getcwd(), 'apps', 'wiki', 'static', 'wiki')
49         rng_base_dir = os.path.join(wiki_base_dir, 'editor')
50         build_dir = os.path.join(wiki_base_dir, 'build')
51
52         self.stdout.write('Installing editor dependencies')
53         if options['editor_npm_env']:
54             npm_env = os.path.join(rng_base_dir, options['editor_npm_env'])
55             if not os.path.exists(npm_env):
56                 os.makedirs(npm_env)
57             assert os.path.isdir(npm_env)
58             os.symlink(npm_env, os.path.join(rng_base_dir, 'node_modules'))
59         try:
60             call([options['npm_bin'], 'install'], cwd=rng_base_dir)
61         except OSError:
62             raise CommandError('Something went wrong, propably npm binary not found. Tried: %s' % options['npm_bin'])
63
64         self.stdout.write('Building editor')
65         if options['node_bin_path']:
66             # grunt needs npm binary to be foundable in PATH
67             os.environ['PATH'] = '%s:%s' % (options['node_bin_path'], os.environ['PATH'])
68         args = ['./node_modules/.bin/grunt', 'build', '--output-dir=%s' % build_dir]
69         if options['editor_optimize']:
70             args.append('--optimize=%s' % options['editor_optimize'])
71         self.stdout.write('Calling %s at %s' % (' '.join(args), rng_base_dir))
72         call(args, cwd=rng_base_dir)
73
74         call_command('collectstatic', interactive=False, ignore_patterns=['editor'])