11.stylix/modules/vscode/update.py
Daniel Thwaites c5558f7de9
vscode: convert template to Nix and refactor module
Benefits:

- No need to run Mustache, so better performance
- Allows the code to be linted and formatted
- Generated file is minified
- Can use arbitrary variables to name colors and group settings
  (although this is not implemented)

Drawbacks:

- Update script is more complex

I opted to change the sorting to alphabetical order because it's
easier to implement in the update script. The script prints a list
of added and removed options, so ordering to match the upstream
documentation is not necessary.
2025-03-12 17:36:02 +00:00

59 lines
1.7 KiB
Python
Executable file

#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python3Packages.markdown-it-py python3Packages.requests
# This script parses the reference page and updates our template with any
# added or removed settings, then sorts the template alphabetically.
#
# New settings are null by default. VSCode can handle this gracefully, but
# normally a value should be chosen before committing the update.
#
# This only updates 'colors' and not 'tokenColors' for now.
import os
from markdown_it import MarkdownIt
import requests
documentation = requests.get('https://raw.githubusercontent.com/microsoft/vscode-docs/main/api/references/theme-color.md').text
color_names = [
token.children[0].content
for token in MarkdownIt().parse(documentation)
if token.type == 'inline' and token.children[0].type == 'code_inline'
]
template_path = os.path.join(os.path.dirname(__file__), 'templates/theme.nix')
before_lines = []
color_lines = []
after_lines = []
with open(template_path, 'r') as template_file:
while line := template_file.readline():
before_lines.append(line)
if line == ' colors = {\n':
break
while line := template_file.readline():
if line == ' };\n':
after_lines.append(line)
break
name = line.split('"')[1]
if name in color_names:
color_lines.append(line)
color_names.remove(name)
else:
print('-', name)
for name in color_names:
print('+', name)
color_lines.append(f' "{name}" = null;\n')
color_lines.sort()
after_lines.extend(template_file.readlines())
with open(template_path, 'w') as template_file:
template_file.writelines(before_lines + color_lines + after_lines)