From 7ed1732b49b080dd6bd5a8bad166a4bc221163a3 Mon Sep 17 00:00:00 2001 From: Jorge Marques Date: Thu, 30 May 2024 20:09:41 -0300 Subject: [PATCH] Add fallback to npm-less installs Most of the times, we don't care about the CSS and JavaScript. When launching author-mode, if the minified scripts are not found and the user does not have the npm modules installed, propose to fetch from the latest release and patch the repo with the minified scripts. Signed-off-by: Jorge Marques --- README.md | 12 ++++-- adi_doctools/cli/author_mode.py | 73 +++++++++++++++++++++++++++------ adi_doctools/cli/hdl_gen.py | 3 -- docs/install.rst | 11 ++++- 4 files changed, 78 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ee4da87..8f5fd69 100644 --- a/README.md +++ b/README.md @@ -58,14 +58,16 @@ reinstalling. Also extends Author Mode to watch changes on the webpage source code (use `--dev`/`-r` option to enable this). -Before getting started, install `npm`. -It is required due to the web scripts (`js modules`) and style sheets (`sass`). +### Install the web compiler + +If you care about the web scripts (`js modules`) and style sheets (`sass`), +install `npm` first, if not, just skip this section. > **_NOTE:_** If the ``npm`` provided by your package manager is too old and > updating with `npm install npm -g` fails, consider installing with > [NodeSource](https://github.com/nodesource/distributions). -At this path, install the `npm` dependencies locally: +At the repository root, install the `npm` dependencies locally: ``` npm install rollup \ @rollup/plugin-terser \ @@ -74,11 +76,15 @@ npm install rollup \ --save-dev ``` +### Fetch third-party resources + Fetch third-party fonts: ``` ./ci/fetch-fonts.sh ``` +### Install the repository + Finally, do a symbolic install of this repo: ``` pip install -e . --upgrade diff --git a/adi_doctools/cli/author_mode.py b/adi_doctools/cli/author_mode.py index 5d4c917..38b7422 100644 --- a/adi_doctools/cli/author_mode.py +++ b/adi_doctools/cli/author_mode.py @@ -1,4 +1,4 @@ -from os import path, listdir, remove +from os import path, listdir, remove, mkdir from os import pardir, killpg, getpgid from shutil import copy import click @@ -11,8 +11,12 @@ 'inv_bdir': "Could not find BUILDDIR {}.", 'inv_srcdir': "Could not find SOURCEDIR {}.", 'no_selenium': "Package 'selenium' is not installed, pooling enabled.", - 'rollup': "Couldn't find {}, ensure this a symbolic install", - 'node': "Couldn't find {}, please you install the npm tools locally." + 'rollup': "Couldn't find {}, ensure this a symbolic install.", + 'node': "Couldn't find {}, please install the npm tools locally.", + 'comp': "Couldn't find the minified web files ", + 'no_npm': "and the npm tools are not installed.", + 'with_npm': "run with the --just-regen flag (npm detected).", + 'fetch': "Do you want to fetch from the release?" } # Hall of shame of poorly managed artifacts @@ -41,7 +45,7 @@ '-r', is_flag=True, default=False, - help="Watch source code (requires symbolic install)." + help="Watch web source code (requires symbolic install)." ) @click.option( '--no-selenium', @@ -54,7 +58,7 @@ '-g', is_flag=True, default=False, - help="Just regenerate minified files and exit." + help="Just regenerate the web minified files and exit." ) def author_mode(directory, port, dev, no_selenium, just_regen): """ @@ -91,17 +95,61 @@ def dir_assert(file, msg): else: return False + source_files = {'app.umd.js', 'app.umd.js.map', 'style.min.css', + 'style.min.css.map'} + + def fetch_compiled(path_): + req = path.join(path_, 'docs', 'requirements.txt') + dist = path.join(path_, '.dist') + file = "adi_doctools.tar.gz" + + f = open(req, 'r') + for line in f: + if 'adi-doctools' in line: + break + f.close() + + from urllib.request import urlretrieve + from shutil import rmtree + if not path.isdir(dist): + mkdir(dist) + urlretrieve(line, path.join(dist, file)) + subprocess.call(f"tar -xf {file}", + shell=True, cwd=dist) + remove(path.join(dist, file)) + for d in listdir(dist): + break + base = path.join('adi_doctools', 'theme', 'cosmic', 'static') + for f in source_files: + src = path.join(dist, d, base, f) + dest = path.join(path_, base, f) + copy(src, dest) + rmtree(dist) + click.echo("Success fetching the pre-compiled files!") + + src_dir = path.abspath(path.join(path.dirname(__file__), pardir)) + par_dir = path.abspath(path.join(src_dir, pardir)) + rollup_bin = path.join(par_dir, 'node_modules', '.bin', 'rollup') + rollup_conf = path.join(par_dir, 'ci', 'rollup.config.app.mjs') if just_regen or dev: - src_dir = path.join(path.dirname(path.abspath(__file__)), pardir) - par_dir = path.abspath(path.join(src_dir, pardir)) - - rollup_conf = path.join(par_dir, 'ci', 'rollup.config.app.mjs') - rollup_bin = path.join(par_dir, 'node_modules', '.bin', 'rollup') - if symbolic_assert(rollup_conf, log['rollup'].format(rollup_conf)): return if symbolic_assert(rollup_bin, log['node'].format(rollup_bin)): return + else: + compiled = path.join(src_dir, 'theme', 'cosmic', 'static') + compiled = path.abspath(path.join(compiled, 'app.umd.js')) + if not path.isfile(compiled): + if symbolic_assert(rollup_bin, log['comp'] + log['no_npm']): + pass + else: + click.echo(log['comp'] + log['with_npm']) + return + if click.confirm(log['fetch']): + if fetch_compiled(par_dir): + return + else: + return if just_regen: subprocess.call(f"{rollup_bin} -c {rollup_conf}", @@ -144,8 +192,7 @@ def dir_assert(file, msg): watch_file_src = {} watch_file_rst = {} if dev: - source_files = ['app.umd.js', 'app.umd.js.map', 'style.min.css', - 'style.min.css.map', 'icons.svg'] + source_files.add('icons.svg') w_files = [] # Check if minified files exists, if not, run rollup once rollup_cache = True diff --git a/adi_doctools/cli/hdl_gen.py b/adi_doctools/cli/hdl_gen.py index 4131ab4..b2fd307 100644 --- a/adi_doctools/cli/hdl_gen.py +++ b/adi_doctools/cli/hdl_gen.py @@ -1,5 +1,3 @@ -from typing import TypedDict, Dict, List - import click import subprocess import re @@ -10,7 +8,6 @@ from ..parser.hdl import resolve_hdl_regmap from ..parser.hdl import expand_hdl_regmap from ..parser.hdl import parse_hdl_vendor -from ..parser.hdl import parse_hdl_library from ..writer.hdl import write_hdl_regmap diff --git a/docs/install.rst b/docs/install.rst index bf0207e..1db92e5 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -65,8 +65,11 @@ reinstalling. Also extends Author Mode to watch changes on the webpage source code (use `--dev`/`-r` option to enable this). -Before getting started, install `npm`. -It is required due to the web scripts (`js modules`) and style sheets (`sass`). +Install the web compiler +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If you care about the web scripts (`js modules`) and style sheets (`sass`), +install ``npm`` first, if not, just skip this section. .. note:: @@ -84,6 +87,8 @@ At the repository root, install the `npm` dependencies locally: sass \ --save-dev +Fetch third-party resources +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fetch third-party fonts: @@ -91,6 +96,8 @@ Fetch third-party fonts: ./ci/fetch-fonts.sh +Install the repository +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Finally, do a symbolic install of this repo: