Skip to content

Commit

Permalink
[Feat] add script for replace template
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed Feb 9, 2024
1 parent fd393d2 commit 951bef1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Setup python
uses: actions/setup-python@v5

- name: Replace template
run: python3 scripts/replace-template.py

- name: Commit changes
uses: EndBug/add-and-commit@v9
with:
message: '[Update] apply template'

- name: Read version from project config
id: read_toml
uses: SebRollen/[email protected]
Expand Down
6 changes: 3 additions & 3 deletions doc/plugin-name.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
**plugin-name.txt** make share code location easier
**plugin-name** description

Author: mistricy (Mist) <[email protected]>
version: 0.0.1
Author: Mist <[email protected]>
version: 0.0.1
2 changes: 2 additions & 0 deletions project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
name = "plugin-name"
version = "0.0.1"
description = "description"
author = "Mist"
email = "[email protected]"
19 changes: 19 additions & 0 deletions scripts/replace-template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from glob import glob
from typing import Callable, Final
from re import sub
from functools import reduce
import tomllib

PROJECT_CONFIG_PATH: Final = "project.toml"
TEMPLATE_PATH: Final = "template/**/*.template"

read_config: Callable[[str], dict] = lambda path: tomllib.load(open(path, 'rb'))

apply_config: Callable[[dict, list[str]], str] = lambda config, keys: reduce(lambda config, key: config[key], keys, config)

replace: Callable[[str, dict], str] = lambda content, config: sub(r"{{([^}]+)}}", lambda match: apply_config(config, match.group(1).split('.')), content)

update: Callable[[str, str], None] = lambda path, content: open(sub(r'^template/(.+)\.template$', r"\1", path), 'w').write(content)

for template in map(lambda file: {'path': file, 'content': replace(open(file, 'r').read(), read_config(PROJECT_CONFIG_PATH))}, glob(TEMPLATE_PATH)):
update(template['path'], template['content'])
4 changes: 4 additions & 0 deletions template/doc/plugin-name.txt.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**{{project.name}}** {{project.description}}

Author: {{project.author}} <{{project.email}}>
version: {{project.version}}

0 comments on commit 951bef1

Please sign in to comment.