From 058ddf2e7c552927d5432e1e369a46146be22999 Mon Sep 17 00:00:00 2001 From: Alejandro-Morales Date: Tue, 27 Sep 2022 09:51:43 +0100 Subject: [PATCH 1/2] feat: added add workspace command --- src/jenesis/cmd/add/__init__.py | 3 +++ src/jenesis/config/__init__.py | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/jenesis/cmd/add/__init__.py b/src/jenesis/cmd/add/__init__.py index f1ca351..bfc6672 100644 --- a/src/jenesis/cmd/add/__init__.py +++ b/src/jenesis/cmd/add/__init__.py @@ -2,6 +2,7 @@ from jenesis.cmd.add.contract import run_add_contract from jenesis.cmd.add.contract import add_contract_command +from jenesis.cmd.add.workspace import add_workspace_command from jenesis.cmd.add.profile import add_profile_command @@ -11,4 +12,6 @@ def add_add_command(parser): subparsers = add_cmd.add_subparsers() add_contract_command(subparsers) + add_workspace_command(subparsers) add_profile_command(subparsers) + \ No newline at end of file diff --git a/src/jenesis/config/__init__.py b/src/jenesis/config/__init__.py index 53f95a1..7969c1e 100644 --- a/src/jenesis/config/__init__.py +++ b/src/jenesis/config/__init__.py @@ -448,3 +448,50 @@ def add_contract(project_root: str, template: str, name: str, branch: str) -> Co shutil.rmtree(temp_clone_path) return parse_contract(project_root, name) + + @staticmethod + def add_workspace(path: str, template: str, branch: str) -> Contract: + + # create the temporary clone folder + temp_clone_path = mkdtemp(prefix="jenesis-", suffix="-tmpl") + + # clone the templates folder out in the temporary file + print("Downloading template...") + cmd = ["git", "clone", "--single-branch"] + if branch is not None: + cmd += ["--branch", branch] + cmd += [TEMPLATE_GIT_URL, "."] + with open(os.devnull, "w", encoding="utf8") as null_file: + subprocess.check_call( + cmd, stdout=null_file, stderr=subprocess.STDOUT, cwd=temp_clone_path + ) + + # find the target workspace + contract_template_path = os.path.join(temp_clone_path, "workspaces", template) + if not os.path.isdir(contract_template_path): + print(f"Unknown template {template}") + return + print("Downloading template...complete") + + # process all the files as part of the template + print("Rendering template...") + for root, _, files in os.walk(contract_template_path): + for filename in files: + file_path = os.path.join(root, filename) + rel_path = os.path.relpath(file_path, contract_template_path) + + output_filepath = os.path.join(path, rel_path) + os.makedirs(os.path.dirname(output_filepath), exist_ok=True) + with open(file_path, "r", encoding="utf8") as input_file: + with open(output_filepath, "w", encoding="utf8") as output_file: + try: + contents = input_file.read() + + output_file.write(contents) + except: + print("Couldnt read ", filename) + + print("Rendering template...complete") + + # clean up the temporary folder + shutil.rmtree(temp_clone_path) From 7c8240046884a09babb8d72b23079e57c646c297 Mon Sep 17 00:00:00 2001 From: Alejandro-Morales Date: Tue, 27 Sep 2022 09:54:17 +0100 Subject: [PATCH 2/2] feat: added workspace.py --- src/jenesis/cmd/add/workspace.py | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/jenesis/cmd/add/workspace.py diff --git a/src/jenesis/cmd/add/workspace.py b/src/jenesis/cmd/add/workspace.py new file mode 100644 index 0000000..bd2f261 --- /dev/null +++ b/src/jenesis/cmd/add/workspace.py @@ -0,0 +1,46 @@ +import argparse +import os + +from jenesis.config import Config + + +def add_workspace_command(parser): + + add_workspace_cmd = parser.add_parser("workspace") + add_workspace_cmd.add_argument("template", help="The name of the template to use") + add_workspace_cmd.add_argument( + "-b", "--branch", help="The name of the branch that should be used" + ) + add_workspace_cmd.add_argument( + "-p", "--profile", default="testing", help="The profile to initialize" + ) + add_workspace_cmd.add_argument( + "-n", "--network", default="fetchai-testnet", help="Network to use" + ) + add_workspace_cmd.set_defaults(handler=run_add_workspace) + + +def run_add_workspace(args: argparse.Namespace): + template = args.template + branch = args.branch + + root = os.path.abspath(os.getcwd()) + + workspace_root = os.path.join(root, template) + + # check to see if the contract already exists + if os.path.exists(workspace_root): + print(f'Workspace "{template}" already exists') + return False + + Config.add_workspace(workspace_root, template, branch) + + # check if workspace was downloaded correctly + if not os.path.exists(workspace_root): + print(f'Couldnt find "{template}" workspace') + return False + + # create jenesis project + Config.create_project(workspace_root, args.profile, args.network) + + return True