Skip to content

Commit 20f575c

Browse files
committed
Run stylua when converting
1 parent 3141002 commit 20f575c

File tree

6 files changed

+39
-10
lines changed

6 files changed

+39
-10
lines changed

Lib/stylua/Linux/stylua

9.22 MB
Binary file not shown.

Lib/stylua/Windows/stylua.exe

5.11 MB
Binary file not shown.

Python/convert.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from Python import warnings
1212
from Python.case_check import case_check
1313
from Python import utils
14+
from Python import stylua
1415

1516
from Python.ini_converting import ini_cst_builder
1617
from Python.ini_converting import ini_rules
@@ -23,13 +24,12 @@
2324

2425

2526
def convert():
26-
print("") # Just prints a newline.
27-
28-
output_folder_path = sg.user_settings_get_entry("cccp_folder")
27+
print("") # Newline.
2928

3029
time_start = time.time()
3130

3231
input_folder_path = cfg.sg.user_settings_get_entry("input_folder")
32+
output_folder_path = sg.user_settings_get_entry("cccp_folder")
3333
cccp_folder_path = cfg.sg.user_settings_get_entry("cccp_folder")
3434

3535
zips_py.unzip(input_folder_path)
@@ -40,6 +40,8 @@ def convert():
4040

4141
converter_walk(input_folder_path, output_folder_path)
4242

43+
stylua.run(input_folder_path, output_folder_path)
44+
4345
ini_cst = ini_cst_builder.get_ini_cst(input_folder_path, output_folder_path, input_folder_path)
4446
ini_rules.apply_rules_on_ini_cst(ini_cst)
4547
ini_writer.write_converted_ini_cst(ini_cst, Path(output_folder_path))
@@ -50,7 +52,6 @@ def convert():
5052
if cfg.sg.user_settings_get_entry("play_finish_sound"):
5153
playsound(utils.resource_path("Media/finish.wav"), block=(platform.system() == "Linux"))
5254

53-
5455
elapsed = math.floor(time.time() - time_start)
5556
print(f"Finished in {elapsed} {pluralize('second', elapsed)}.")
5657

@@ -151,4 +152,4 @@ def apply_conversion_rules(all_lines):
151152

152153

153154
def pluralize(word, count):
154-
return word + "s" if count != 1 else word
155+
return word + "s" if count != 1 else word

Python/ini_converting/ini_cst_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def get_ini_cst(input_folder_path, output_folder_path, subfolder_path):
1919
if not utils.is_mod_folder_or_subfolder(relative_subfolder): # TODO: Remove this once CCCP has a Mods folder that can be iterated over.
2020
continue
2121
elif p.is_file() and p.suffix == ".ini" and p.stem != "desktop": # Skip the desktop.ini Windows metadata file.
22-
output_file_path = utils.get_output_file_path_from_input_file_path(input_folder_path, output_folder_path, p)
22+
output_file_path = utils.get_output_path_from_input_path(input_folder_path, output_folder_path, p)
2323
tokens = ini_tokenizer.get_tokens(output_file_path)
2424
parsed_portion[name] = ini_parser.get_parsed_tokens(tokens)
2525
elif p.is_dir():

Python/stylua.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os, subprocess
2+
3+
from pathlib import Path
4+
5+
6+
from Python import utils
7+
8+
9+
class WronglyFormattedLuaFile(Exception):
10+
pass
11+
12+
13+
def run(input_folder_path, output_folder_path):
14+
for name in os.listdir(input_folder_path):
15+
input_path = input_folder_path / Path(name)
16+
if not utils.is_mod_folder(input_path):
17+
continue
18+
19+
output_path = utils.get_output_path_from_input_path(input_folder_path, output_folder_path, input_path)
20+
21+
if os.name == "nt": # If the OS is Windows
22+
stylua_path = utils.resource_path("Lib/stylua/Windows/stylua.exe")
23+
elif os.name == "posix": # If the OS is Linux
24+
stylua_path = utils.resource_path("Lib/stylua/Linux/stylua")
25+
26+
result = subprocess.run([stylua_path, output_path], capture_output=True, text=True)
27+
28+
if result.stderr:
29+
raise WronglyFormattedLuaFile(result.stderr)
30+

Python/utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def get_relative_subfolder(input_folder_path, input_subfolder_path):
1818

1919

2020
def is_mod_folder(path):
21-
path_parts = Path(path).parts
22-
# If it is a folder inside of the input folder and if it is the mod folder.
23-
return len(path_parts) == 1 and path_parts[0].endswith(".rte")
21+
return path.suffix == ".rte"
2422

2523

2624
def is_mod_folder_or_subfolder(path):
@@ -29,6 +27,6 @@ def is_mod_folder_or_subfolder(path):
2927
return len(path_parts) >= 1 and path_parts[0].endswith(".rte")
3028

3129

32-
def get_output_file_path_from_input_file_path(input_folder_path, output_folder_path, p):
30+
def get_output_path_from_input_path(input_folder_path, output_folder_path, p):
3331
relative = p.relative_to(input_folder_path)
3432
return str(output_folder_path / relative)

0 commit comments

Comments
 (0)