|
16 | 16 |
|
17 | 17 |
|
18 | 18 | def cppcompile(r, file, pluscount=False):
|
19 |
| - if '.cpp' in file and file.endswith(".cpp"): |
| 19 | + if '.cpp' in file: |
20 | 20 | # compile the file to a file with the same name and path but .lua
|
21 | 21 | try:
|
22 | 22 | newctranslator = parser.CodeConverter(file, getconfig("c", "dynamiclibpath", "None"))
|
@@ -59,7 +59,7 @@ def cppcompile(r, file, pluscount=False):
|
59 | 59 |
|
60 | 60 | return 0
|
61 | 61 | def ccompile(r, file, pluscount=False):
|
62 |
| - if '.c' in file and file.endswith(".c"): |
| 62 | + if '.c' in file: |
63 | 63 | # compile the file to a file with the same name and path but .lua
|
64 | 64 | try:
|
65 | 65 | newctranslator = parser.CodeConverter(file, getconfig("c", "dynamiclibpath", "None"))
|
@@ -103,122 +103,119 @@ def ccompile(r, file, pluscount=False):
|
103 | 103 | #count += 1
|
104 | 104 | return 0
|
105 | 105 | 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 |
109 | 151 |
|
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() |
117 | 154 |
|
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)}")) |
133 | 158 | if pluscount:
|
134 | 159 | #pluscount.error()
|
135 | 160 | pluscount.update(1)
|
136 | 161 | pluscount.current += 1
|
137 | 162 | #global count
|
138 | 163 | #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)}")) |
142 | 167 | if pluscount:
|
143 | 168 | #pluscount.error()
|
144 | 169 | pluscount.update(1)
|
145 | 170 | pluscount.current += 1
|
146 | 171 | #global count
|
147 | 172 | #count += 1
|
148 | 173 | 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: |
208 | 175 | 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)+"!")) |
210 | 188 | if pluscount:
|
211 |
| - #pluscount.error() |
212 | 189 | pluscount.update(1)
|
213 | 190 | pluscount.current += 1
|
214 | 191 | #global count
|
215 | 192 | #count += 1
|
216 | 193 | except Exception as e:
|
217 | 194 | print(error(f"Compile Error!\n\n "+str(e), f"{os.path.join(r, file)}"))
|
| 195 | + |
218 | 196 | if pluscount:
|
219 | 197 | #pluscount.error()
|
220 | 198 | pluscount.update(1)
|
221 | 199 | pluscount.current += 1
|
222 | 200 | #global count
|
223 | 201 | #count += 1
|
224 | 202 | 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