From f7e29875c62d2c44dcaa8861b3a4268a0c97a682 Mon Sep 17 00:00:00 2001 From: monoid Date: Sat, 30 Apr 2022 19:30:12 +0900 Subject: [PATCH] refactor --- cli.py | 36 ++++++++++++++++++++++-------------- tools/printDocument.ts | 33 ++++++++------------------------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/cli.py b/cli.py index 9553c83..0192a56 100644 --- a/cli.py +++ b/cli.py @@ -4,17 +4,24 @@ import argparse import subprocess import sys import os +from typing import List, Union + +def cmdExecute(command: List[str], verbose = False, head_msg: Union[str,None] = None ,msg: Union[str,None] = None): + msg = msg or " ".join(command) + head_msg = head_msg or "cmd execute: " + if verbose: + print(head_msg, msg) + p = subprocess.run(command) + p.check_returncode() def updateIssue(issuePath: str, verbose = False): - if verbose: - print("get issues") - p = subprocess.run(["deno", "run", "-A","tools/getIssue.ts", "--path",issuePath]) - p.check_returncode() + cmd = ["deno", "run","--no-check", "-A","tools/getIssue.ts", "--path",issuePath] + cmdExecute(cmd, verbose, "update issue:") def printDocument(issuePath:str, outDir:str, watch = False, verbose = False): if verbose: - print("build document") - cmd = ["deno", "run", "-A","tools/printDocument.ts", "--issue_path", issuePath, "--outDir", outDir] + print("build document : issuePath(", issuePath, ") to ", outDir) + cmd = ["deno", "run","--no-check" ,"-A","tools/printDocument.ts", "--issue_path", issuePath, "--outDir", outDir] if watch: cmd.append("--watch") p = subprocess.run(cmd) @@ -35,6 +42,8 @@ def build(args): if args.update_issues: updateIssue(issuePath, args.verbose) printDocument(issuePath, args.outDir, args.watch, args.verbose) + cmd = ["mdbook", "build"] + cmdExecute(cmd, args.verbose) def serve(args): """serve the documentation and reload on changes""" @@ -74,6 +83,11 @@ def buildPdf(args): parser.add_argument('--outDir', default="build/doc.pdf", help='output directory') parser.add_argument('--browser-path', help='path to the browser') args = parser.parse_args(args) + if os.path.exists(args.outDir): + if args.verbose: + print("remove old file") + os.remove(args.outDir) + absPath = os.path.normpath(os.path.join( os.getcwd(),'book' , 'print.html' )).replace('\\', '/') url = f"file://{absPath}" if args.verbose: @@ -83,15 +97,9 @@ def buildPdf(args): if args.browser_path: cmd.append("--chromeDir") cmd.append(args.browser_path) - if args.verbose: - print("cmd: ", " ".join(cmd)) - p = subprocess.run(cmd) - p.check_returncode() + cmdExecute(cmd, args.verbose, "print pdf:") cmd = ["java", "-jar", "tools/pdfbox-app-2.0.26.jar", "PDFMerger", "cover.pdf", args.outDir, args.outDir] - if args.verbose: - print("merge cmd: ", " ".join(cmd)) - p = subprocess.run(cmd) - p.check_returncode() + cmdExecute(cmd, args.verbose, "merge pdf:") commandList = { 'build': build, diff --git a/tools/printDocument.ts b/tools/printDocument.ts index a9f8124..c9bb84b 100644 --- a/tools/printDocument.ts +++ b/tools/printDocument.ts @@ -49,27 +49,6 @@ async function printDoc(param: printDocParam, option?: { } } -async function readAndPrint(args: { - issue_path?: string, - outDir?: string, - targets: string[], -}) { - const c = await readContent(args.issue_path); - const issues = JSON.parse(c) as Issue[]; - issues.sort((a, b) => a.number - b.number); - for (const target of args.targets) { - await printDoc({ - target: target, - data: { - issues - } - } - , { - outDir: args.outDir - }); - } -} - async function main() { const parsedArg = argParse(Deno.args); const { issue_path, outDirArg, w, watch } = parsedArg; @@ -109,8 +88,12 @@ async function main() { const targets = ["SUMMARY.md", "overall.md", "specific.md", "intro.md", "support.md"]; const targetsR = await Promise.all(targets.map(async (t) => { - return await createReactive(() => { - return readAndPrint({ issue_path, outDir, targets:[t] }); + return await createReactive(async () => { + await printDoc({ + target: t, data: { + issues: issuesR.value + } + }, { outDir: outDir }); }); } @@ -133,10 +116,10 @@ async function main() { for (const path of event.paths) { const p = parsePath(path); if (p.dir === viewPath) { - if(p.ext === ".md") { + if (p.ext === ".md") { targetsR[targets.indexOf(p.base)].update(); } - else{ + else { copyOp.update(); } }