|
6 | 6 | import yaml
|
7 | 7 |
|
8 | 8 | folder_path = "./themes"
|
9 |
| -dest_path = './installs' |
| 9 | +dest_path = "./installs" |
10 | 10 | themes = []
|
11 | 11 |
|
12 |
| -# List files and directories in the folder |
13 |
| -folder_contents = os.listdir(dest_path) |
| 12 | +# Ensure the destination directory exists |
| 13 | +os.makedirs(dest_path, exist_ok=True) |
14 | 14 |
|
15 |
| -# Delete each file in the folder |
16 |
| -for item in folder_contents: |
| 15 | +# Remove all files in the destination directory |
| 16 | +for item in os.listdir(dest_path): |
17 | 17 | item_path = os.path.join(dest_path, item)
|
18 | 18 | if os.path.isfile(item_path):
|
19 | 19 | os.remove(item_path)
|
20 | 20 |
|
| 21 | +# Process YAML files |
21 | 22 | for filename in os.listdir(folder_path):
|
22 | 23 | if filename.endswith(".yml"):
|
23 |
| - with open(os.path.join(folder_path, filename), "r") as f: |
24 |
| - data = yaml.safe_load(f) |
25 |
| - theme = {f"{key}": data[key] for key in data if key.startswith("color")} |
26 |
| - theme.update({ |
27 |
| - "name": data["name"], |
28 |
| - "foreground": data["foreground"], |
29 |
| - "background": data["background"], |
30 |
| - "cursorColor": data["cursor"] |
31 |
| - }) |
32 |
| - themes.append(theme) |
| 24 | + file_path = os.path.join(folder_path, filename) |
33 | 25 |
|
34 |
| -themes = sorted(themes, key=lambda x: x["name"]) |
| 26 | + # Read and clean YAML content |
| 27 | + with open(file_path, "r") as f: |
| 28 | + content = f.read() |
| 29 | + |
| 30 | + # Replace tabs with 4 spaces to avoid YAML parsing errors |
| 31 | + content = content.replace("\t", " ") |
| 32 | + |
| 33 | + # Load the cleaned YAML |
| 34 | + data = yaml.safe_load(content) |
35 | 35 |
|
36 |
| -colors_data = {"themes": themes} |
| 36 | + # Build the theme dictionary |
| 37 | + theme = {f"{key}": data[key] for key in data if key.startswith("color")} |
| 38 | + theme.update({ |
| 39 | + "name": data["name"], |
| 40 | + "foreground": data["foreground"], |
| 41 | + "background": data["background"], |
| 42 | + "cursorColor": data["cursor"] |
| 43 | + }) |
| 44 | + themes.append(theme) |
37 | 45 |
|
| 46 | +# Sort themes by name |
| 47 | +themes = sorted(themes, key=lambda x: x["name"]) |
38 | 48 |
|
39 |
| -# create a template string for the shell script |
| 49 | +# Shell script template |
40 | 50 | template = """\
|
41 | 51 | #!/usr/bin/env bash
|
42 | 52 |
|
|
91 | 101 | fi
|
92 | 102 | """
|
93 | 103 |
|
94 |
| -# loop over the color schemes and create a new shell script for each one |
95 |
| -for scheme in colors_data['themes']: |
96 |
| - # convert the scheme name to lowercase, replace spaces with dashes, remove accents, and replace non-alphanumeric characters with underscores |
| 104 | +# Generate .sh files for each color scheme |
| 105 | +for scheme in themes: |
| 106 | + # Convert theme name to a valid filename |
97 | 107 | filename = re.sub(r'[^a-zA-Z0-9]+', '-', unidecode(scheme['name']).lower().replace(' ', '-'))
|
98 | 108 | filename = re.sub(r'[-]+', '-', filename).strip('-')
|
99 | 109 | filename = f"{dest_path}/{filename}.sh"
|
| 110 | + |
100 | 111 | with open(filename, 'w') as f:
|
101 | 112 | f.write(template.format(**scheme))
|
102 | 113 |
|
103 |
| -# Find all files with ".sh" extension in the folder |
104 |
| -files = [f for f in os.listdir(dest_path) if os.path.isfile(os.path.join(dest_path, f)) and f.endswith('.sh')] |
105 |
| - |
106 |
| -# Change permissions on each file |
107 |
| -for f in files: |
108 |
| - subprocess.run(['chmod', '775', os.path.join(dest_path, f)]) |
| 114 | +# Change permissions for generated scripts |
| 115 | +for f in os.listdir(dest_path): |
| 116 | + file_path = os.path.join(dest_path, f) |
| 117 | + if os.path.isfile(file_path) and f.endswith('.sh'): |
| 118 | + subprocess.run(['chmod', '775', file_path]) |
0 commit comments