Mercurial > libervia-web
annotate libervia/server/tasks/implicit/task_sass.py @ 1412:6fdef01f6671
browser (lists/view): Q&D fix for status change
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 22 Apr 2021 21:37:50 +0200 |
parents | 10748aa888a9 |
children | 2f6dac783c8e |
rev | line source |
---|---|
1257
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/ur/bin/env python3 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 import json |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 from pathlib import Path |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 from sat.core.i18n import _ |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 from sat.core.log import getLogger |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 from sat.core import exceptions |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 from libervia.server.constants import Const as C |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 from libervia.server.tasks import task |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 log = getLogger(__name__) |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 SASS_SUFFIXES = ('.sass', '.scss') |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 class Task(task.Task): |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 """Compile .sass and .scss files found in themes browser paths""" |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 AFTER = ['js_modules'] |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 async def prepare(self): |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 # we look for any Sass file, and cancel this task if none is found |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 sass_dirs = set() |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 for browser_path in self.resource.browser_modules.get('themes_browser_paths', []): |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 for p in browser_path.iterdir(): |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 if p.suffix in SASS_SUFFIXES: |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 sass_dirs.add(browser_path) |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 break |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 if not sass_dirs: |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 raise exceptions.CancelError("No Sass file found") |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 # we have some Sass files, we need to install the compiler |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 d_path = self.resource.dev_build_path |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 package_path = d_path / "package.json" |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 try: |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 with package_path.open() as f: |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 package = json.load(f) |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 except FileNotFoundError: |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 package = {} |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 except Exception as e: |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 log.error(f"Unexepected exception while parsing package.json: {e}") |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 if 'node-sass' not in package.setdefault('dependencies', {}): |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 package['dependencies']['node-sass'] = 'latest' |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 with package_path.open('w') as f: |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 json.dump(package, f, indent=4) |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
1273
10748aa888a9
tasks (js_modules, sass): check for `yarnpkg` before `yarn`, as it is the name used in Debian
Goffi <goffi@goffi.org>
parents:
1257
diff
changeset
|
49 cmd = self.findCommand('yarnpkg', 'yarn', 'npm') |
1257
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 await self.runCommand(cmd, 'install', path=str(d_path)) |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 self.WATCH_DIRS = list(sass_dirs) |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 async def onDirEvent(self, host, filepath, flags): |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 if filepath.suffix in SASS_SUFFIXES: |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 await self.manager.runTaskInstance(self) |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 async def start(self): |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 d_path = self.resource.dev_build_path |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 node_sass = d_path / 'node_modules' / 'node-sass' / 'bin' / 'node-sass' |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 for browser_path in self.resource.browser_modules['themes_browser_paths']: |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 for p in browser_path.iterdir(): |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 if p.suffix not in SASS_SUFFIXES: |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 continue |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 await self.runCommand( |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 str(node_sass), |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 "--omit-source-map-url", |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 "--output-style", "compressed", |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 "--output", str(self.build_path), |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 str(p), |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 path=str(self.build_path) |
1ec41ac1e7cf
server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 ) |