Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added add workspace command #126

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/jenesis/cmd/add/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)

46 changes: 46 additions & 0 deletions src/jenesis/cmd/add/workspace.py
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions src/jenesis/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)