diff --git a/.github/ISSUE_TEMPLATE/create-theme.yml b/.github/ISSUE_TEMPLATE/create-theme.yml index dc3e943a..8aca6209 100644 --- a/.github/ISSUE_TEMPLATE/create-theme.yml +++ b/.github/ISSUE_TEMPLATE/create-theme.yml @@ -51,4 +51,15 @@ body: label: Readme description: The README file for the theme. validations: - required: true \ No newline at end of file + required: true + - type: textarea + id: preferences + attributes: + render: json + label: Preferences + description: The preferences for the theme in JSON format. Leave empty if there are no preferences. + placeholder: | + { + "uc.my-preference.enable-this": "Enable this feature for the theme", + "uc.my-preference.show-that": "Show that feature for the theme" + } \ No newline at end of file diff --git a/.github/workflows/create-theme.yml b/.github/workflows/create-theme.yml index 8f17313e..0e24e093 100644 --- a/.github/workflows/create-theme.yml +++ b/.github/workflows/create-theme.yml @@ -46,6 +46,12 @@ jobs: contents: ${{ fromJson(steps.issue-parser.outputs.jsonString)['readme'] }} path: theme-readme.md + - name: Write preferences to file + uses: "DamianReeves/write-file-action@master" + with: + contents: ${{ fromJson(steps.issue-parser.outputs.jsonString)['preferences'] }} + path: theme-preferences.json + - name: Setup Git run: | git config --global user.name "github-actions[bot]" diff --git a/scripts/submit-theme.py b/scripts/submit-theme.py index d727fe29..c1757df1 100644 --- a/scripts/submit-theme.py +++ b/scripts/submit-theme.py @@ -10,9 +10,11 @@ import imghdr STYLES_FILE = "chrome.css" README_FILE = "readme.md" IMAGE_FILE = "image.png" +PREFERENCES_FILE = "preferences.json" TEMPLATE_STYLES_FILE = "./theme-styles.css" TEMPLATE_README_FILE = "./theme-readme.md" +TEMPLATE_PREFERENCES_FILE = "./theme-preferences.json" def create_theme_id(): return str(uuid.uuid4()) @@ -33,6 +35,40 @@ def get_readme(): content = content[len("```markdown"):] content = content[:-len("```")] return content + +def validate_preferences(preferences): + for key, value in preferences.items(): + if not isinstance(key, str): + print("Preference key must be a string.", file=sys.stderr) + exit(1) + if not isinstance(value, str): + print("Preference description must be a string.", file=sys.stderr) + exit(1) + if len(key) == 0: + print("Preference key is required.", file=sys.stderr) + exit(1) + for char in key: + if not char.isalnum() and char != '.' and char != '-' and char != '_': + print("Preference key must only contain letters, numbers, periods, dashes, and underscores.", file=sys.stderr) + exit(1) + if len(value) == 0: + print("Preference description is required.", file=sys.stderr) + exit(1) + return preferences + +def get_preferences(): + with open(TEMPLATE_PREFERENCES_FILE, 'r') as f: + try: + content = f.read() + if content.strip() == "": + return {} + content = content[len("```json"):] + content = content[:-len("```")] + return validate_preferences(json.loads(content)) + except json.JSONDecodeError as e: + print("Preferences file is invalid.", file=sys.stderr) + print(e, file=sys.stderr) + exit(1) def validate_name(name): if len(name) == 0: @@ -107,8 +143,6 @@ Just joking, you can do whatever you want. You're the boss. } os.makedirs(f"themes/{theme_id}") - with open(f"themes/{theme_id}/theme.json", 'w') as f: - json.dump(theme, f) with open(f"themes/{theme_id}/{STYLES_FILE}", 'w') as f: f.write(get_styles()) @@ -116,8 +150,19 @@ Just joking, you can do whatever you want. You're the boss. with open(f"themes/{theme_id}/{README_FILE}", 'w') as f: f.write(get_readme()) + with open(f"themes/{theme_id}/{PREFERENCES_FILE}", 'w') as f: + prefs = get_preferences() + if len(prefs) > 0: + print("Detected preferences file. Please review the preferences below.") + print(prefs) + theme['preferences'] = get_static_asset(theme_id, PREFERENCES_FILE) + json.dump(prefs, f) + download_image(image, f"themes/{theme_id}/{IMAGE_FILE}") + with open(f"themes/{theme_id}/theme.json", 'w') as f: + json.dump(theme, f) + print(f"Theme submitted with ID: {theme_id}") for key, value in theme.items(): print(f"\t{key}: {value}")