fix: better init command

This commit is contained in:
Jordan ⌨️ 2023-05-27 23:42:35 +02:00 committed by Sleepy Monax
parent 6e3c8bfd80
commit 00537aa34e
4 changed files with 26 additions and 16 deletions

View file

@ -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):
template = args.consumeArg()
repo = const.DEFAULT_REPO_TEMPLATES if not "repo" in args.opts else args.opts["repo"]
list = "list" in args.opts
if list:
logger.info("Fetching registry...")
r = requests.get('https://raw.githubusercontent.com/cute-engineering/osdk-template/main/registry.json')
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)
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}')
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)]

View file

@ -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"

View file

@ -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}")

View file

@ -1,3 +1,2 @@
requests ~= 2.28.0
graphviz ~= 0.20.1
pyfzf ~= 0.3.1