Skip to content

Commit 1c92f13

Browse files
authored
unnecessary double checking file type
1 parent 308e1d1 commit 1c92f13

File tree

1 file changed

+88
-91
lines changed

1 file changed

+88
-91
lines changed

robloxpyc/basecompilers.py

Lines changed: 88 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
def cppcompile(r, file, pluscount=False):
19-
if '.cpp' in file and file.endswith(".cpp"):
19+
if '.cpp' in file:
2020
# compile the file to a file with the same name and path but .lua
2121
try:
2222
newctranslator = parser.CodeConverter(file, getconfig("c", "dynamiclibpath", "None"))
@@ -59,7 +59,7 @@ def cppcompile(r, file, pluscount=False):
5959

6060
return 0
6161
def ccompile(r, file, pluscount=False):
62-
if '.c' in file and file.endswith(".c"):
62+
if '.c' in file:
6363
# compile the file to a file with the same name and path but .lua
6464
try:
6565
newctranslator = parser.CodeConverter(file, getconfig("c", "dynamiclibpath", "None"))
@@ -103,122 +103,119 @@ def ccompile(r, file, pluscount=False):
103103
#count += 1
104104
return 0
105105
def pycompile(r, file, pluscount=False):
106-
if file.endswith(".py"):
107-
# compile the file to a file with the same name and path but .lua
108-
contents = ""
106+
# compile the file to a file with the same name and path but .lua
107+
contents = ""
108+
109+
try:
110+
with open(os.path.join(r, file)) as rf:
111+
contents = rf.read()
112+
except Exception as e:
113+
print(error(f"Failed to read {os.path.join(r, file)}!\n\n "+str(e)))
114+
# do not compile the file if it cannot be read
115+
return
116+
117+
try:
118+
translator = pytranslator.Translator()
119+
lua_code = translator.translate(contents)
120+
#print(colortext.green("Compiled "+os.path.join(r, file)))
121+
# get the relative path of the file and replace .py with .lua
122+
path = os.path.join(r, file)
123+
124+
relative_path = backwordreplace(path,".py", ".lua", 1)
125+
126+
if not os.path.exists(os.path.dirname(relative_path)):
127+
os.makedirs(os.path.dirname(relative_path))
128+
129+
with open(relative_path, "w") as f:
130+
f.write(lua_code)
131+
132+
if pluscount:
133+
#pluscount.error()
134+
pluscount.update(1)
135+
pluscount.current += 1
136+
#global count
137+
#count += 1
138+
except Exception as e:
139+
print(error(f"Compile Error!\n\n "+str(e), f"{os.path.join(r, file)}"))
140+
debug("Compile error at "+str(e))
141+
if pluscount:
142+
#pluscount.error()
143+
pluscount.update(1)
144+
pluscount.current += 1
145+
#global count
146+
#count += 1
147+
return 0
148+
def lunarcompile(r, file, pluscount=False):
149+
# compile the file to a file with the same name and path but .lua
150+
# Run command and check if anything is outputted to stderr, stdout, or stdin
109151

110-
try:
111-
with open(os.path.join(r, file)) as rf:
112-
contents = rf.read()
113-
except Exception as e:
114-
print(error(f"Failed to read {os.path.join(r, file)}!\n\n "+str(e)))
115-
# do not compile the file if it cannot be read
116-
return
152+
process = subprocess.Popen(["moonc", os.path.join(r, file)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
153+
stdout, stderr = process.communicate()
117154

118-
try:
119-
translator = pytranslator.Translator()
120-
lua_code = translator.translate(contents)
121-
#print(colortext.green("Compiled "+os.path.join(r, file)))
122-
# get the relative path of the file and replace .py with .lua
123-
path = os.path.join(r, file)
124-
125-
relative_path = backwordreplace(path,".py", ".lua", 1)
126-
127-
if not os.path.exists(os.path.dirname(relative_path)):
128-
os.makedirs(os.path.dirname(relative_path))
129-
130-
with open(relative_path, "w") as f:
131-
f.write(lua_code)
132-
155+
if stdout or stderr:
156+
if stdout:
157+
print(error(f"Compile Error!\n\n "+str(stdout), f"{os.path.join(r, file)}"))
133158
if pluscount:
134159
#pluscount.error()
135160
pluscount.update(1)
136161
pluscount.current += 1
137162
#global count
138163
#count += 1
139-
except Exception as e:
140-
print(error(f"Compile Error!\n\n "+str(e), f"{os.path.join(r, file)}"))
141-
debug("Compile error at "+str(e))
164+
return 0
165+
else:
166+
print(error(f"Compile Error!\n\n "+str(stderr), f"{os.path.join(r, file)}"))
142167
if pluscount:
143168
#pluscount.error()
144169
pluscount.update(1)
145170
pluscount.current += 1
146171
#global count
147172
#count += 1
148173
return 0
149-
def lunarcompile(r, file, pluscount=False):
150-
if file.endswith(".moon"):
151-
# compile the file to a file with the same name and path but .lua
152-
# Run command and check if anything is outputted to stderr, stdout, or stdin
153-
154-
process = subprocess.Popen(["moonc", os.path.join(r, file)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
155-
stdout, stderr = process.communicate()
156-
157-
if stdout or stderr:
158-
if stdout:
159-
print(error(f"Compile Error!\n\n "+str(stdout), f"{os.path.join(r, file)}"))
160-
if pluscount:
161-
#pluscount.error()
162-
pluscount.update(1)
163-
pluscount.current += 1
164-
#global count
165-
#count += 1
166-
return 0
167-
else:
168-
print(error(f"Compile Error!\n\n "+str(stderr), f"{os.path.join(r, file)}"))
169-
if pluscount:
170-
#pluscount.error()
171-
pluscount.update(1)
172-
pluscount.current += 1
173-
#global count
174-
#count += 1
175-
return 0
176-
else:
177-
try:
178-
newheader = header.lunarheader([])
179-
180-
# check if the new file has been created
181-
if os.path.exists(os.path.join(r, file.replace(".moon", ".lua"))):
182-
#print(colortext.green("Compiled "+os.path.join(r, file)))
183-
with open(os.path.join(r, file.replace(".moon", ".lua")), "r") as f:
184-
contents = f.read()
185-
with open(os.path.join(r, file.replace(".moon", ".lua")), "w") as f:
186-
f.write(newheader+contents+header.pyfooter)
187-
188-
else:
189-
print(error("File error for "+os.path.join(r, file)+"!"))
190-
if pluscount:
191-
pluscount.update(1)
192-
pluscount.current += 1
193-
#global count
194-
#count += 1
195-
except Exception as e:
196-
print(error(f"Compile Error!\n\n "+str(e), f"{os.path.join(r, file)}"))
197-
198-
if pluscount:
199-
#pluscount.error()
200-
pluscount.update(1)
201-
pluscount.current += 1
202-
#global count
203-
#count += 1
204-
return 0
205-
def robloxtscompile(r, file, pluscount=False):
206-
if file.endswith(".ts") or file.endswith(".tsx"):
207-
# Just add to pluscount, add later
174+
else:
208175
try:
209-
print(warn("At the moment roblox-ts is not supported, please wait for a future update."))
176+
newheader = header.lunarheader([])
177+
178+
# check if the new file has been created
179+
if os.path.exists(os.path.join(r, file.replace(".moon", ".lua"))):
180+
#print(colortext.green("Compiled "+os.path.join(r, file)))
181+
with open(os.path.join(r, file.replace(".moon", ".lua")), "r") as f:
182+
contents = f.read()
183+
with open(os.path.join(r, file.replace(".moon", ".lua")), "w") as f:
184+
f.write(newheader+contents+header.pyfooter)
185+
186+
else:
187+
print(error("File error for "+os.path.join(r, file)+"!"))
210188
if pluscount:
211-
#pluscount.error()
212189
pluscount.update(1)
213190
pluscount.current += 1
214191
#global count
215192
#count += 1
216193
except Exception as e:
217194
print(error(f"Compile Error!\n\n "+str(e), f"{os.path.join(r, file)}"))
195+
218196
if pluscount:
219197
#pluscount.error()
220198
pluscount.update(1)
221199
pluscount.current += 1
222200
#global count
223201
#count += 1
224202
return 0
203+
def robloxtscompile(r, file, pluscount=False):
204+
# Just add to pluscount, add later
205+
try:
206+
print(warn("At the moment roblox-ts is not supported, please wait for a future update."))
207+
if pluscount:
208+
#pluscount.error()
209+
pluscount.update(1)
210+
pluscount.current += 1
211+
#global count
212+
#count += 1
213+
except Exception as e:
214+
print(error(f"Compile Error!\n\n "+str(e), f"{os.path.join(r, file)}"))
215+
if pluscount:
216+
#pluscount.error()
217+
pluscount.update(1)
218+
pluscount.current += 1
219+
#global count
220+
#count += 1
221+
return 0

0 commit comments

Comments
 (0)