annotate libervia/web/server/tasks/implicit/task_sass.py @ 1518:eb00d593801d

refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 16:49:28 +0200
parents libervia/server/tasks/implicit/task_sass.py@106bae41f5c8
children 54ba0f74a488
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
4 from libervia.backend.core.log import getLogger
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
5 from libervia.backend.core import exceptions
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
6 from libervia.web.server.tasks import task
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
7
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
8
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 log = getLogger(__name__)
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 SASS_SUFFIXES = ('.sass', '.scss')
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
12
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 class Task(task.Task):
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 """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
16 AFTER = ['js_modules']
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 async def prepare(self):
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 # 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
20 sass_dirs = set()
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 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
22 for p in browser_path.iterdir():
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 if p.suffix in SASS_SUFFIXES:
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 sass_dirs.add(browser_path)
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 break
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 if not sass_dirs:
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 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
29
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 # 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
31 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
32 package_path = d_path / "package.json"
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 try:
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 with package_path.open() as f:
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 package = json.load(f)
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 except FileNotFoundError:
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 package = {}
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 except Exception as e:
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 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
40
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 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
42 package['dependencies']['node-sass'] = 'latest'
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 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
44 json.dump(package, f, indent=4)
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1434
diff changeset
46 cmd = self.find_command('yarnpkg', 'yarn')
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 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
48
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.WATCH_DIRS = list(sass_dirs)
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1434
diff changeset
51 async def on_dir_event(self, host, filepath, flags):
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 if filepath.suffix in SASS_SUFFIXES:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1434
diff changeset
53 await self.manager.run_task_instance(self)
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 async def start(self):
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 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
57 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
58 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
59 for p in browser_path.iterdir():
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 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
61 continue
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 await self.runCommand(
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 str(node_sass),
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 "--omit-source-map-url",
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 "--output-style", "compressed",
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 "--output", str(self.build_path),
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 str(p),
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 path=str(self.build_path)
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 )