Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
123 lines (105 loc) · 4.41 KB
/
Copy pathcli.py
File metadata and controls
123 lines (105 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import argparse
import os
import re
import subprocess
import webbrowser
import platform
from git import Repo, GitCommandError
# Define available templates with branch names and descriptions
TEMPLATES = [
{"branch": "0", "description": "Play sound"},
{"branch": "1", "description": "Open blank window"},
]
def build():
print("Building the project...")
def clean():
print("Cleaning up...")
def test():
print("Running tests...")
def run():
print("Attempting to open ScriptBlocks...")
# Define paths for ScriptBlocks based on the platform
scriptblocks_paths = {
"Windows": os.path.join(os.environ["LOCALAPPDATA"], "Programs", "scriptblocks-app", "ScriptBlocks.exe"),
"Darwin": "/Applications/ScriptBlocks.app/Contents/MacOS/ScriptBlocks",
"Linux": "/usr/local/bin/scriptblocks-app"
}
current_platform = platform.system()
scriptblocks_path = scriptblocks_paths.get(current_platform)
try:
if scriptblocks_path and os.path.exists(scriptblocks_path):
# Open the executable directly if the path exists
subprocess.run([scriptblocks_path], check=True)
else:
# Prompt the user to enter the path if not found
scriptblocks_path = input("ScriptBlocks path not found. Please enter the full path to the executable: ")
subprocess.run([scriptblocks_path], check=True)
except (subprocess.CalledProcessError, FileNotFoundError):
# Redirect to the GitHub releases page if opening fails
print(f"Failed to open ScriptBlocks on {current_platform}. Redirecting to the latest release page...")
webbrowser.open("https://github.com/ScriptBlocks/ScriptBlocks/releases/latest")
def list_templates():
print("Available templates:")
for i, template in enumerate(TEMPLATES, start=1):
print(f"[{template['branch']}] - {template['description']}")
def select_template():
list_templates()
while True:
choice = input("Enter the branch name of the template you want to use: ").strip()
for template in TEMPLATES:
if template["branch"] == choice:
return template["branch"]
print("Invalid choice. Please enter a valid branch name.")
def create():
current_dir = os.getcwd()
print(f"Current directory: {current_dir}")
project_name = input("Enter the name of your project: ")
sanitized_name = re.sub(r'[^a-zA-Z0-9\s-]', '', project_name).lower().replace(' ', '-')
project_dir = os.path.join(current_dir, sanitized_name)
try:
os.makedirs(project_dir, exist_ok=True)
print(f"Created project directory: {project_dir}")
except OSError as e:
print(f"Error creating directory: {e}")
return
use_template = input("Do you want to use a template? (y/n): ").strip().lower()
if use_template == 'y':
selected_branch = select_template()
repo_url = "https://github.com/ScriptBlocks/templates"
try:
print(f"Cloning the '{selected_branch}' template from GitHub...")
Repo.clone_from(repo_url, project_dir, branch=selected_branch)
print(f"Project cloned into {project_dir}")
except GitCommandError as e:
print(f"Error cloning the repository: {e}")
else:
example_repo_url = "https://github.com/ScriptBlocks/example-project"
try:
print("No template selected. Cloning the example project template...")
Repo.clone_from(example_repo_url, project_dir)
print(f"Example project cloned into {project_dir}")
except GitCommandError as e:
print(f"Error cloning the example project repository: {e}")
def main():
parser = argparse.ArgumentParser(prog="scriptblocks", description="ScriptBlocks CLI")
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("build", help="Build the project")
subparsers.add_parser("clean", help="Clean up")
subparsers.add_parser("test", help="Run tests")
subparsers.add_parser("run", help="Run the project")
subparsers.add_parser("create", help="Create a new component")
args = parser.parse_args()
if args.command == "build":
build()
elif args.command == "clean":
clean()
elif args.command == "test":
test()
elif args.command == "run":
run()
elif args.command == "create":
create()
else:
parser.print_help()
if __name__ == "__main__":
main()