-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitignore_generator.py
62 lines (48 loc) · 1.97 KB
/
gitignore_generator.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
# Author Steven Yeoh
# Copyright (c) 2019. All rights reserved.
import os
import sys
import json
if os.path.exists(os.path.join("config", "setting.json")):
with open(os.path.join("config", "setting.json")) as json_file:
json_object = json.loads(json_file.read())
drive = json_object["drive"]
workspace_name = json_object["workspace_name"]
project_name = json_object["project_name"]
language = json_object["language"]
elif len(sys.argv) == 5:
drive, workspace_name, project_name, language = sys.argv[1:]
else:
drive = input("Enter drive: ")
workspace_name = input("Enter workspace: ")
project_name = input("Enter project_name: ")
language = input("Enter programming language: ")
os.chdir("{}:/".format(drive.upper()))
languages = {
"python": [".idea/", "venv/", "resources/"],
"spring": [".idea/", "target/"],
"java": [".idea/", "out/"],
}
def search_workspace():
for directories in os.walk("."):
for directory in [directory for directory in directories if directory.__contains__(workspace_name)]:
for folder in [folder for folder in directory if folder == workspace_name]:
return os.path.join(os.getcwd(), folder)
def search_project(parent):
for folder in [folder for folder in os.listdir(parent) if folder == project_name]:
return os.path.join(parent, folder)
def get_gitignore_list():
language_type = languages.get(language)
if language is None:
return None
return language_type
def write_gitignore_file():
if get_gitignore_list() is not None:
workspace_path = search_workspace()
target_abs_path = search_project(workspace_path)
with open(r"{}/.gitignore".format(target_abs_path), "w") as gitignore_file:
gitignore_file.writelines('\n'.join(get_gitignore_list()))
print("Created .gitignore in {}".format(target_abs_path))
else:
print("Unable to generate .gitignore")
write_gitignore_file()