-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombiner.py
117 lines (94 loc) · 3.39 KB
/
combiner.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
# // ---------------------------------------------------------------------
# // ------- [cuhHub] Util Scripts - Combiner
# // ---------------------------------------------------------------------
# -----------------------------------------
# // ---- Imports
# -----------------------------------------
import os
import time
# -----------------------------------------
# // ---- Variables
# -----------------------------------------
mainPath = "src"
outputFile = "script.lua"
__allowedFileExtensions = [".lua"]
__fileExceptions = ["src/p1_framework/intellisense.lua"]
__folderExceptions = ["src/intellisense"]
# -----------------------------------------
# // ---- Functions
# -----------------------------------------
# // Read a file
def quickRead(path: str):
with open(path, "r") as f:
return f.read()
# // Write to a file
def quickWrite(path: str, content: str):
with open(path, "w") as f:
return f.write(content)
# // Check if path is in list
def pathInList(path: str, paths: list):
for currentPath in paths:
if os.path.samefile(path, currentPath):
return True
return False
# // Get contents of all files in a path
def recursiveRead(targetDir: str, allowedFileExtensions: list[str], fileExceptions: list[str], folderExceptions: list[str]) -> dict[str, str]:
# list files
files = os.listdir(targetDir)
contents = {}
# iterate through them
for file in files:
# get file-related variables
_, extension = os.path.splitext(file)
path = os.path.join(targetDir, file).replace("\\", "/") # replacing for formatting reasons
# file extension check
if extension == "":
# file is folder, but is an exception
if pathInList(path, folderExceptions):
continue
# file is folder, so read it too
contents = {**contents, **recursiveRead(path, allowedFileExtensions, fileExceptions, folderExceptions)}
# correct extension check
if extension not in allowedFileExtensions:
continue
# exceptions check
if pathInList(path, fileExceptions):
continue
# get file content
content = quickRead(path)
# append file content to contents
contents[path] = content
return contents
# -----------------------------------------
# // ---- Main
# -----------------------------------------
# // Setup
# prevent combining output file and this file
__fileExceptions.extend([outputFile, os.path.relpath(__file__)])
# // Main combine loop
while True:
# wait
time.sleep(0.1)
# get content of all files
result = recursiveRead(
mainPath,
__allowedFileExtensions,
__fileExceptions,
__folderExceptions
)
# print message
print("Combined the following files:\n- " + "\n- ".join(result.keys()))
# format result
for path, content in result.items():
newContent = [
"----------------------------------------------",
f"-- // [File] {path}",
"----------------------------------------------",
content
]
result[path] = "\n".join(newContent)
# dump it into output file
try:
quickWrite(outputFile, "\n\n".join(result.values()))
except:
print("Failed to output.")