-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
135 lines (110 loc) · 4.04 KB
/
main.py
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
124
125
126
127
128
129
130
131
132
133
134
135
import os
import subprocess
import json
import shutil
def create_folder(path):
try:
os.makedirs(path, exist_ok=True)
print(f"Pasta criada em: {path}")
except OSError as error:
print(f"Falha ao criar pasta: {error}")
def download_repository(url, path):
try:
subprocess.run(["git", "clone", url, path])
print(f"Repositório baixado em: {path}")
except Exception as e:
print(f"Falha ao baixar repositório: {e}")
def copy_env_files(project):
config_folder = os.path.join(os.getcwd(), 'config')
for repo in project['repositories']:
env_file_name = f"{project['name']}-{repo['path']}.env"
env_file_path = os.path.join(config_folder, env_file_name)
if os.path.exists(env_file_path):
destination_path = os.path.join(project['path'], repo['path'])
destination_env_path = os.path.join(destination_path, '.env')
shutil.copy(env_file_path, destination_env_path)
print(f"{env_file_name} file copied to {destination_env_path}")
def create_tasks(project):
tasks_json = {
"version": "2.0.0",
"configurations": {
"type": "node",
"runtimeVersion": "14.21.3",
"request": "launch",
"name": "Launch",
"preLaunchTask": "",
},
"presentation": {
"echo": False,
"reveal": "always",
"focus": False,
"panel": "dedicated",
"showReuseMessage": True
},
"tasks": []
}
dependencies = set()
depends_on_set = set()
for task in project.get('tasks', []):
task_entry = {
"label": task['label'],
"type": "shell",
"command": task['command'],
"isBackground": True,
"problemMatcher": [],
"presentation": {},
"dependsOn": []
}
# Add 'group' if it exists
if 'group' in task:
task_entry["presentation"]["group"] = task['group']
# Adiciona 'dependsOn' se existir
if 'dependsOn' in task:
task_entry["dependsOn"] = task['dependsOn']
depends_on_set.update(task['dependsOn'])
tasks_json['tasks'].append(task_entry)
dependencies.add(task['label'])
# Removes the tasks that are 'Dependon' from others
dependencies.difference_update(depends_on_set)
# Adds the "Create Terminals" input only if there are dependencies
create_terminals_entry = {
"label": "Create terminals",
"dependsOn": list(dependencies),
"group": {
"kind": "build",
"isDefault": True
},
"runOptions": {
"runOn": "folderOpen"
}
}
tasks_json['tasks'].append(create_terminals_entry)
return tasks_json
def process(project):
config_path = project.get('config', {}).get('path', './')
root_folder = os.path.join(config_path, project['path'])
create_folder(root_folder)
# Processes the repository in the project
for repo in project['repositories']:
folder_destination = os.path.join(root_folder, repo['path'])
url_repository = repo['repository']
create_folder(folder_destination)
download_repository(url_repository, folder_destination)
# Creates the .vscode directory and the Tasks.json file inside it
vscode_path = os.path.join(root_folder, '.vscode')
create_folder(vscode_path)
tasks_json_path = os.path.join(vscode_path, 'tasks.json')
tasks_json_content = create_tasks(project)
# Copy .env files
copy_env_files(project)
with open(tasks_json_path, 'w') as tasks_file:
json.dump(tasks_json_content, tasks_file, indent=2)
def read_config_json(config_json):
with open(config_json) as f:
data = json.load(f)
for project in data.get('projects', []):
config_path = project.get('config', {}).get('path', './')
project['config'] = {'path': config_path}
process(project)
config_json = "./config.json"
read_config_json(config_json)