From 00537aa34e64f1760cc08cd38baddf95638194d2 Mon Sep 17 00:00:00 2001 From: keyboard-slayer Date: Sat, 27 May 2023 23:42:35 +0200 Subject: [PATCH] fix: better init command --- osdk/cmds.py | 31 ++++++++++++++++++------------- osdk/const.py | 2 +- osdk/shell.py | 6 ++++++ requirements.txt | 3 +-- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/osdk/cmds.py b/osdk/cmds.py index a2c7a9c..d6b2c25 100644 --- a/osdk/cmds.py +++ b/osdk/cmds.py @@ -1,9 +1,9 @@ import os import json import logging +import tempfile import requests -from pyfzf.pyfzf import FzfPrompt from typing import Callable, cast from osdk import context, shell, const, vt100, builder, graph @@ -211,20 +211,25 @@ cmds += [Cmd("i", "install", "Install all the external packages", installCmd)] def initCmd(args: Args): - logger.info("Fetching registry...") - r = requests.get('https://raw.githubusercontent.com/cute-engineering/osdk-template/main/registry.json') + template = args.consumeArg() + repo = const.DEFAULT_REPO_TEMPLATES if not "repo" in args.opts else args.opts["repo"] + list = "list" in args.opts - if r.status_code != 200: - logger.error('Failed to fetch registry') - exit(1) - - registry = json.loads(r.text) - _fzf = FzfPrompt() - - result = _fzf.prompt([f"{r['id']} - {r['description']}" for r in registry], "--cycle --header='Select a template:'") - result = result[0].split(' - ')[0].strip() - os.system(f'svn --quiet checkout https://github.com/cute-engineering/osdk-template/trunk/{result}') + if list: + logger.info("Fetching registry...") + r = requests.get(f'https://raw.githubusercontent.com/{repo}/main/registry.json') + if r.status_code != 200: + logger.error('Failed to fetch registry') + exit(1) + + print('\n'.join(f"* {entry['id']} - {entry['description']}" for entry in json.loads(r.text))) + else: + with tempfile.TemporaryDirectory() as tmp: + shell.exec(*["git", "clone", "-n", "--depth=1", "--filter=tree:0", f"https://github.com/{repo}", os.path.join(tmp, "osdk-repo"), "-q"]) + shell.exec(*["git", "-C", os.path.join(tmp, "osdk-repo"), "sparse-checkout", "set", "--no-cone", template, "-q"]) + shell.exec(*["git", "-C", os.path.join(tmp, "osdk-repo"), "checkout", "-q"]) + shell.mv(os.path.join(tmp, "osdk-repo", template), os.path.join(".", template)) cmds += [Cmd("I", "init", "Start a new project", initCmd)] diff --git a/osdk/const.py b/osdk/const.py index 1fc9f0c..1211fec 100644 --- a/osdk/const.py +++ b/osdk/const.py @@ -1,6 +1,5 @@ import os import sys -import subprocess VERSION = "0.4.1" MODULE_DIR = os.path.dirname(os.path.realpath(__file__)) @@ -12,3 +11,4 @@ EXTERN_DIR = os.path.join(OSDK_DIR, "extern") SRC_DIR = "src" META_DIR = f"meta" TARGETS_DIR = os.path.join(META_DIR, "targets") +DEFAULT_REPO_TEMPLATES = "cute-engineering/osdk-template" \ No newline at end of file diff --git a/osdk/shell.py b/osdk/shell.py index ae8f782..5efb615 100644 --- a/osdk/shell.py +++ b/osdk/shell.py @@ -179,6 +179,12 @@ def cp(src: str, dst: str): shutil.copy(src, dst) +def mv(src: str, dst: str): + logger.info(f"Moving {src} to {dst}") + + shutil.move(src, dst) + + def cpTree(src: str, dst: str): logger.info(f"Copying {src} to {dst}") diff --git a/requirements.txt b/requirements.txt index 27f36a7..7fb454a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ requests ~= 2.28.0 -graphviz ~= 0.20.1 -pyfzf ~= 0.3.1 \ No newline at end of file +graphviz ~= 0.20.1 \ No newline at end of file