comparison libervia/server/tasks/implicit/task_sass.py @ 1257:1ec41ac1e7cf

server: seperation between production build dir and dev build dir: LiberviaRootResource instances's `build_path` is the path where generated files are put and served by the HTTP server, while `dev_buid_path` is used for temporary files/libraries needed for the generation/compilation of files.
author Goffi <goffi@goffi.org>
date Sun, 03 May 2020 18:25:11 +0200
parents
children 10748aa888a9
comparison
equal deleted inserted replaced
1256:08cd652dea14 1257:1ec41ac1e7cf
1 #!/ur/bin/env python3
2
3 import json
4 from pathlib import Path
5 from sat.core.i18n import _
6 from sat.core.log import getLogger
7 from sat.core import exceptions
8 from libervia.server.constants import Const as C
9 from libervia.server.tasks import task
10
11
12 log = getLogger(__name__)
13
14 SASS_SUFFIXES = ('.sass', '.scss')
15
16
17 class Task(task.Task):
18 """Compile .sass and .scss files found in themes browser paths"""
19 AFTER = ['js_modules']
20
21 async def prepare(self):
22 # we look for any Sass file, and cancel this task if none is found
23 sass_dirs = set()
24 for browser_path in self.resource.browser_modules.get('themes_browser_paths', []):
25 for p in browser_path.iterdir():
26 if p.suffix in SASS_SUFFIXES:
27 sass_dirs.add(browser_path)
28 break
29
30 if not sass_dirs:
31 raise exceptions.CancelError("No Sass file found")
32
33 # we have some Sass files, we need to install the compiler
34 d_path = self.resource.dev_build_path
35 package_path = d_path / "package.json"
36 try:
37 with package_path.open() as f:
38 package = json.load(f)
39 except FileNotFoundError:
40 package = {}
41 except Exception as e:
42 log.error(f"Unexepected exception while parsing package.json: {e}")
43
44 if 'node-sass' not in package.setdefault('dependencies', {}):
45 package['dependencies']['node-sass'] = 'latest'
46 with package_path.open('w') as f:
47 json.dump(package, f, indent=4)
48
49 try:
50 cmd = self.findCommand('yarn')
51 except exceptions.NotFound:
52 cmd = self.findCommand('npm')
53 await self.runCommand(cmd, 'install', path=str(d_path))
54
55 self.WATCH_DIRS = list(sass_dirs)
56
57 async def onDirEvent(self, host, filepath, flags):
58 if filepath.suffix in SASS_SUFFIXES:
59 await self.manager.runTaskInstance(self)
60
61 async def start(self):
62 d_path = self.resource.dev_build_path
63 node_sass = d_path / 'node_modules' / 'node-sass' / 'bin' / 'node-sass'
64 for browser_path in self.resource.browser_modules['themes_browser_paths']:
65 for p in browser_path.iterdir():
66 if p.suffix not in SASS_SUFFIXES:
67 continue
68 await self.runCommand(
69 str(node_sass),
70 "--omit-source-map-url",
71 "--output-style", "compressed",
72 "--output", str(self.build_path),
73 str(p),
74 path=str(self.build_path)
75 )