Created
April 20, 2021 23:03
-
-
Save bollwyvl/2278cddfb0af2caf77611588b58895a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import doit | |
import json | |
import tempfile | |
import tarfile | |
import shutil | |
from pathlib import Path | |
os.environ.update( | |
NODE_OPTS="--max-old-space-size=4096", | |
PYTHONIOENCODING="utf-8", | |
PIP_DISABLE_PIP_VERSION_CHECK="1", | |
MAMBA_NO_BANNER="1", | |
) | |
DOIT_CONFIG = { | |
"backend": "sqlite3", | |
"verbosity": 2, | |
"par_type": "thread", | |
"default_tasks": ["lint", "build"], | |
} | |
def task_setup(): | |
yield dict( | |
name="js", | |
file_dep=[P.YARN_LOCK, *P.PACKAGE_JSONS, P.ROOT_PACKAGE_JSON], | |
actions=[U.do("jlpm", "--prefer-offline", "--ignore-optional")], | |
targets=[P.YARN_INTEGRITY], | |
) | |
yield U.ok( | |
P.OK_PIP_E, | |
name="py", | |
file_dep=[P.SETUP_CFG, P.SETUP_PY], | |
actions=[U.do("python", "-m", "pip", "install", "-e", ".", "--no-deps")], | |
) | |
def task_lint(): | |
yield U.ok( | |
P.OK_PRETTIER, | |
name="prettier", | |
file_dep=P.ALL_PRETTIER, | |
actions=[U.do("jlpm", "prettier")], | |
) | |
yield U.ok( | |
P.OK_ESLINT, | |
name="eslint", | |
file_dep=[P.OK_PRETTIER], | |
actions=[U.do("jlpm", "eslint")], | |
) | |
yield U.ok( | |
P.OK_BLACK, | |
name="black", | |
file_dep=P.ALL_BLACK, | |
actions=[U.do("black", *P.ALL_BLACK)], | |
) | |
def task_build(): | |
yield dict( | |
name="js:lib", | |
file_dep=[*P.ALL_TS, P.ROOT_PACKAGE_JSON, *P.PACKAGE_JSONS, P.YARN_INTEGRITY], | |
actions=[ | |
U.do("jlpm", "build:lib"), | |
], | |
targets=[P.META_BUILDINFO], | |
) | |
for app_json in P.APP_JSONS: | |
app = app_json.parent | |
app_data = json.loads(app_json.read_text(encoding="utf-8")) | |
yield dict( | |
name=f"js:app:{app.name}", | |
file_dep=[P.META_BUILDINFO, app_json, P.WEBPACK_CONFIG, app / "index.js"], | |
actions=[ | |
U.do("yarn", "lerna", "run", "build:prod", "--scope", app_data["name"]) | |
], | |
targets=[app / "build/bundle.js"], | |
) | |
yield dict( | |
name="js:pack", | |
file_dep=[P.META_BUILDINFO, *P.APP.glob("*/build/bundle.js")], | |
actions=[ | |
(doit.tools.create_folder, [P.DIST]), | |
U.do("npm", "pack", "../app", cwd=P.DIST), | |
], | |
targets=[P.APP_PACK], | |
) | |
def _deploy(): | |
with tempfile.TemporaryDirectory() as td: | |
with tarfile.open(P.APP_PACK, "r:gz") as tar: | |
tar.extractall(td) | |
if P.STATIC.exists(): | |
shutil.rmtree(P.STATIC) | |
shutil.copytree(Path(td) / "package", P.STATIC) | |
yield dict( | |
name="js:deploy", | |
file_dep=[P.APP_PACK], | |
actions=[_deploy], | |
targets=[P.STATIC_INDEX], | |
) | |
yield dict( | |
name="pip", | |
file_dep=[ | |
*P.ALL_PY_SRC, | |
P.SETUP_CFG, | |
P.SETUP_PY, | |
P.STATIC_INDEX, | |
P.MANIFEST_IN, | |
], | |
actions=[U.do("python", "setup.py", "sdist", "bdist_wheel")], | |
targets=[B.WHEEL, B.SDIST], | |
) | |
def task_serve(): | |
yield dict( | |
name="py", file_dep=[P.SERVE], actions=[U.do("python", P.SERVE, "--path=app")] | |
) | |
class C: | |
NAME = "jupyterlite" | |
APPS = ["classic", "lab"] | |
class P: | |
DODO = Path(__file__) | |
META = DODO.parent | |
SERVE = META / "serve.py" | |
ROOT = META / C.NAME | |
PACKAGES = ROOT / "packages" | |
PACKAGE_JSONS = [*PACKAGES.glob("*/package.json")] | |
ROOT_PACKAGE_JSON = ROOT / "package.json" | |
YARN_LOCK = ROOT / "yarn.lock" | |
PY_SRC = ROOT / "src/jupyterlite" | |
SETUP_PY = ROOT / "setup.py" | |
SETUP_CFG = ROOT / "setup.cfg" | |
MANIFEST_IN = ROOT / "MANIFEST.in" | |
ALL_PY_SRC = [*PY_SRC.rglob("*.py")] | |
APP = ROOT / "app" | |
WEBPACK_CONFIG = APP / "webpack.config.js" | |
APP_JSONS = [*APP.glob("*/package.json")] | |
# deploy | |
STATIC = PY_SRC / "static" | |
STATIC_INDEX = STATIC / "index.html" | |
# built | |
NODE_MODULES = ROOT / "node_modules" | |
YARN_INTEGRITY = NODE_MODULES / ".yarn-integrity" | |
META_BUILDINFO = PACKAGES / "_metapackage/tsconfig.tsbuildinfo" | |
DIST = ROOT / "dist" | |
APP_PACK = DIST / "jupyterlite-app-0.1.0.tgz" | |
# linting | |
ALL_TS = [*PACKAGES.rglob("*/src/**/*.js"), *PACKAGES.rglob("*/src/**/*.ts")] | |
ALL_JSON = [*PACKAGE_JSONS, *APP_JSONS, ROOT_PACKAGE_JSON, *ALL_TS] | |
ALL_PRETTIER = [*ALL_JSON] | |
ALL_BLACK = [DODO, SERVE, SETUP_PY, *ALL_PY_SRC] | |
OK = META / "ok" | |
OK_PRETTIER = OK / "prettier" | |
OK_ESLINT = OK / "eslint" | |
OK_BLACK = OK / "black" | |
OK_PIP_E = OK / "pip-e" | |
class B: | |
DIST = P.ROOT / "dist" | |
BUNDLES_JS = [P.STATIC / app / "build/bundle.js" for app in C.APPS] | |
SDIST = DIST / "jupyterlite-0.1.0.tar.gz" | |
WHEEL = DIST / "jupyterlite-0.1.0-py3-none-any.whl" | |
class U: | |
@staticmethod | |
def do(*args, cwd=P.ROOT, **kwargs): | |
"""wrap a CmdAction for consistency""" | |
return doit.tools.CmdAction(list(args), shell=False, cwd=str(Path(cwd))) | |
@staticmethod | |
def ok(ok, **task): | |
task.setdefault("targets", []).append(ok) | |
task["actions"] = [ | |
lambda: [ok.unlink() if ok.exists() else None, None][-1], | |
*task["actions"], | |
(doit.tools.create_folder, [P.OK]), | |
lambda: [ok.touch(), None][-1], | |
] | |
return task |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment