diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7eb5f05..f7eac80 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -2,155 +2,168 @@ name: Package Release # A manual workflow, triggered from the UI on: - workflow_dispatch: - branches: - - main - inputs: - py_rust_release: - description: "Rust-backed Python Release" - type: boolean - required: false - default: false - py_rust_version: - description: "Rust-backed Python Version (now 0.0.9)" - default: "0.0.9" + workflow_dispatch: + branches: + - main + inputs: + py_rust_release: + description: "Rust-backed Python Release" + type: boolean + required: false + default: false + py_rust_version: + description: "Rust-backed Python Version (now 0.0.9)" + default: "0.0.9" + + jobs: - commit_versions: - name: Bump Pkg Versions - runs-on: ubuntu-latest - permissions: - contents: write - pull-requests: write - outputs: - new-sha: ${{ steps.sha.outputs.SHA }} - steps: - - name: Checks out repo with a token that allows pushing to main without PR - uses: actions/checkout@v4 - with: - token: ${{ secrets.VERSION_BOT_PAT }} - - uses: actions/setup-python@v4 - - uses: ./.github/actions/install-pre-commit - - - uses: dtolnay/rust-toolchain@nightly - if: ${{ inputs.py_rust_release }} || ${{ inputs.rust_release }} - with: - components: rustfmt, clippy - - - name: Update Rust-backed Python version - if: ${{ inputs.py_rust_release }} - run: | - ./dev_scripts/pkg.sh ver_py_rust_update ${{ inputs.py_rust_version }} - - - name: add and format added files with pre-commit - # Running on staged change only as that's all that's needed, || true as don't want it to fail, just modify - run: | - git add . && - ./dev_scripts/test.sh pre_till_success && - git add . - - - name: Commit the new version to the repo - uses: EndBug/add-and-commit@v9 - with: - push: true - message: "chore: bump package versions for release" - author_name: github-actions - author_email: action@github.com - - - name: get sha - id: sha - run: | - sha_new=$(git rev-parse HEAD) - echo $sha_new - echo "::set-output name=SHA::$sha_new" - - - run: echo ${{ steps.sha.outputs.SHA }} - - # Rebuild docs each time this is run, note there is currently no versioning in the docs for each of the individually packages, will need to be added when a public package is being properly released - docs: - name: Docs Release - needs: [commit_versions] - runs-on: ubuntu-latest - permissions: - contents: write - pull-requests: write - steps: - - uses: actions/checkout@v4 - with: - ref: ${{ needs.commit_versions.outputs.new-sha }} - - - name: Set up PDM - uses: pdm-project/setup-pdm@v3 - with: - python-version: "3.12" - cache: true - cache-dependency-path: "docs/pdm.lock" - - - name: Install dependencies - run: | - pdm sync -p ./docs - - - name: Build docs - run: | - ./dev_scripts/docs.sh build - - - name: Deploy to GitHub Pages - uses: crazy-max/ghaction-github-pages@v4 - with: - keep_history: true - target_branch: docs-site - build_dir: site - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - py_rust_build: - needs: [commit_versions] - # Run only if requested: - if: ${{ inputs.py_rust_release }} - uses: ./.github/workflows/py-rust-build.yml + commit_versions: + name: Bump Pkg Versions + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + outputs: + new-sha: ${{ steps.sha.outputs.SHA }} + steps: + - name: Checks out repo with a token that allows pushing to main without PR + uses: actions/checkout@v4 + with: + token: ${{ secrets.VERSION_BOT_PAT }} + - uses: actions/setup-python@v4 + - uses: ./.github/actions/install-pre-commit + + + + - uses: dtolnay/rust-toolchain@nightly + if: ${{ inputs.py_rust_release }} || ${{ inputs.rust_release }} with: - new-sha: ${{ needs.commit_versions.outputs.new-sha }} + components: rustfmt, clippy - py_rust_release: - name: Rust-backed Python Library Release - # Make needs multiline: - needs: [commit_versions, py_rust_build] - # Run only if requested: + - name: Update Rust-backed Python version if: ${{ inputs.py_rust_release }} - runs-on: ubuntu-latest - permissions: - contents: write - pull-requests: write - steps: - - uses: actions/checkout@v4 - with: - ref: ${{ needs.commit_versions.outputs.new-sha }} - - - name: get dist artifacts built in py_rust_build job - uses: actions/download-artifact@v3 - with: - name: py_rust_build_files - path: py_rust/dist - - - uses: actions/setup-python@v4 - with: - python-version: "3.12" - - run: pip install twine - - - name: Make sure release contents seem valid - run: twine check py_rust/dist/* - - - name: upload to pypi - run: twine upload py_rust/dist/* - env: - TWINE_USERNAME: __token__ - TWINE_PASSWORD: "${{ secrets.PYPI_TOKEN }}" - - - name: Publish the release notes - uses: release-drafter/release-drafter@v5 - with: - publish: true - tag: "v${{ inputs.py_rust_version }}" - name: "v${{ inputs.py_rust_version }}" - env: - GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" + run: | + ./dev_scripts/pkg.sh ver_py_rust_update ${{ inputs.py_rust_version }} + + + - name: add and format added files with pre-commit + # Running on staged change only as that's all that's needed, || true as don't want it to fail, just modify + run: | + git add . && + ./dev_scripts/test.sh pre_till_success && + git add . + + - name: Commit the new version to the repo + uses: EndBug/add-and-commit@v9 + with: + push: true + message: "chore: bump package versions for release" + author_name: github-actions + author_email: action@github.com + + - name: get sha + id: sha + run: | + sha_new=$(git rev-parse HEAD) + echo $sha_new + echo "::set-output name=SHA::$sha_new" + + - run: + echo ${{ steps.sha.outputs.SHA }} + + # Rebuild docs each time this is run, note there is currently no versioning in the docs for each of the individually packages, will need to be added when a public package is being properly released + docs: + name: Docs Release + needs: [commit_versions] + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ needs.commit_versions.outputs.new-sha }} + + - name: Set up PDM + uses: pdm-project/setup-pdm@v3 + with: + python-version: "3.12" + cache: true + cache-dependency-path: 'docs/pdm.lock' + + - name: Install dependencies + run: | + pdm sync -p ./docs + + + - name: Build docs + run: | + ./dev_scripts/docs.sh build + + - name: Deploy to GitHub Pages + uses: crazy-max/ghaction-github-pages@v4 + with: + keep_history: true + target_branch: docs-site + build_dir: site + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + + + + py_rust_build: + needs: [commit_versions] + # Run only if requested: + if: ${{ inputs.py_rust_release }} + uses: ./.github/workflows/py-rust-build.yml + with: + new-sha: ${{ needs.commit_versions.outputs.new-sha }} + + py_rust_release: + name: Rust-backed Python Library Release + # Make needs multiline: + needs: [commit_versions, py_rust_build] + # Run only if requested: + if: ${{ inputs.py_rust_release }} + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ needs.commit_versions.outputs.new-sha }} + + + - name: get dist artifacts built in py_rust_build job + uses: actions/download-artifact@v3 + with: + name: py_rust_build_files + path: py_rust/dist + + - uses: actions/setup-python@v4 + with: + python-version: "3.12" + - run: pip install twine + + - name: Make sure release contents seem valid + run: twine check py_rust/dist/* + + - name: upload to pypi + run: twine upload py_rust/dist/* + env: + TWINE_USERNAME: __token__ + TWINE_PASSWORD: "${{ secrets.PYPI_TOKEN }}" + + - name: Publish the release notes + uses: release-drafter/release-drafter@v5 + with: + publish: true + tag: "v${{ inputs.py_rust_version }}" + name: "v${{ inputs.py_rust_version }}" + env: + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" + + diff --git a/.prettierignore b/.prettierignore index fd25287..9cb3374 100644 --- a/.prettierignore +++ b/.prettierignore @@ -10,6 +10,9 @@ target site docs/js_ref/ docs/rust_ref/ +*.yml +*.yaml +*.toml *.zetch.* # Specific to zetch, disable for e.g. some test data that needs its formatting: diff --git a/.zetch.lock b/.zetch.lock index c56fbdc..ba6ad22 100644 --- a/.zetch.lock +++ b/.zetch.lock @@ -1,17 +1,17 @@ { "version": "0.0.2", "files": { - "CODE_OF_CONDUCT.zetch.md": "bf106326ffc75f5167cfde27c997c77c6b97c843a9e392b564355d0e70e50b97", - "docs/CONTRIBUTING.zetch.md": "1dd9ac3bddcc64ec6725fce7eebc9c67ba6f4c17eb78ddac956ef2a73bec364c", - ".github/workflows/release.yml.zetch": "f8cb3de6d12b7628731196dad57844ab6f70c97e6dfd99dddc06e66f06c31146", - "README.zetch.md": "9d85b0018f7ec86a5bf7af90158b7b18d9e7a08e10cd2656fda3e6c3a6012b2b", - "LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", "py_rust/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", - "setup.zetch.cfg": "aa93e5265982316a8dbc4614a4e407387ecf3f3a53803650590be6ffd61799e9", + "LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", "docs/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", - "py_rust/README.zetch.md": "3433c7e3173890ce85efea4e93bd92c70334be98bacb5999c19140178096b3ad", + "CODE_OF_CONDUCT.zetch.md": "bf106326ffc75f5167cfde27c997c77c6b97c843a9e392b564355d0e70e50b97", + "README.zetch.md": "9d85b0018f7ec86a5bf7af90158b7b18d9e7a08e10cd2656fda3e6c3a6012b2b", "docs/index.zetch.md": "3433c7e3173890ce85efea4e93bd92c70334be98bacb5999c19140178096b3ad", + "CONTRIBUTING.zetch.md": "1dd9ac3bddcc64ec6725fce7eebc9c67ba6f4c17eb78ddac956ef2a73bec364c", + "setup.zetch.cfg": "aa93e5265982316a8dbc4614a4e407387ecf3f3a53803650590be6ffd61799e9", + "docs/CONTRIBUTING.zetch.md": "1dd9ac3bddcc64ec6725fce7eebc9c67ba6f4c17eb78ddac956ef2a73bec364c", "docs/CODE_OF_CONDUCT.zetch.md": "bf106326ffc75f5167cfde27c997c77c6b97c843a9e392b564355d0e70e50b97", - "CONTRIBUTING.zetch.md": "1dd9ac3bddcc64ec6725fce7eebc9c67ba6f4c17eb78ddac956ef2a73bec364c" + "py_rust/README.zetch.md": "3433c7e3173890ce85efea4e93bd92c70334be98bacb5999c19140178096b3ad", + ".github/workflows/release.yml.zetch": "f8cb3de6d12b7628731196dad57844ab6f70c97e6dfd99dddc06e66f06c31146" } } \ No newline at end of file diff --git a/py_rust/src/config/engine.rs b/py_rust/src/config/engine.rs index e1feb1b..47a9a16 100644 --- a/py_rust/src/config/engine.rs +++ b/py_rust/src/config/engine.rs @@ -73,10 +73,6 @@ pub struct Engine { comment_start: String, #[serde(default = "default_comment_end")] comment_end: String, - #[serde(default = "default_keep_trailing_newline")] - keep_trailing_newline: bool, - #[serde(default = "default_allow_undefined")] - allow_undefined: bool, #[serde(default = "default_custom_extensions")] pub custom_extensions: Vec, } @@ -91,8 +87,6 @@ impl Engine { variable_end: default_variable_end(), comment_start: default_comment_start(), comment_end: default_comment_end(), - keep_trailing_newline: default_keep_trailing_newline(), - allow_undefined: default_allow_undefined(), custom_extensions: default_custom_extensions(), } } @@ -117,12 +111,12 @@ impl Engine { comment_end: self.comment_end.clone().into(), }) .change_context(Zerr::InternalError)?; - env.set_keep_trailing_newline(self.keep_trailing_newline); - env.set_undefined_behavior(if self.allow_undefined { - minijinja::UndefinedBehavior::Lenient - } else { - minijinja::UndefinedBehavior::Strict - }); + + // Used to be user configurable, but want to modify code as little as possible, so forcibly disable modification of newlines: + env.set_keep_trailing_newline(true); + + // Forcibly prevent undefined vars: + env.set_undefined_behavior(minijinja::UndefinedBehavior::Strict); // Disable all default auto escaping, this caused problems with e.g. adding strings around values in json files: env.set_auto_escape_callback(|_: &str| -> minijinja::AutoEscape { @@ -324,17 +318,6 @@ fn default_comment_end() -> String { "#}".to_string() } -fn default_keep_trailing_newline() -> bool { - // NOTE: when changing make sure to update schema.json default for config hinting - // Don't modify a user's source code if we can help it: - true -} - -fn default_allow_undefined() -> bool { - // NOTE: when changing make sure to update schema.json default for config hinting - false -} - fn default_custom_extensions() -> Vec { // NOTE: when changing make sure to update schema.json default for config hinting vec![] diff --git a/py_rust/src/config/schema.json b/py_rust/src/config/schema.json index f4f3310..643aec6 100644 --- a/py_rust/src/config/schema.json +++ b/py_rust/src/config/schema.json @@ -58,16 +58,6 @@ "description": "The pattern marking the end of a comment in a template.", "default": "#}" }, - "keep_trailing_newline": { - "type": "boolean", - "description": "Whether to keep trailing newlines at the end of rendered templates.", - "default": true - }, - "allow_undefined": { - "type": "boolean", - "description": "Whether to render nothing silently when a template variable is undefined. When this is false an error is always raised.", - "default": false - }, "custom_extensions": { "type": "array", "description": "A list of custom python extensions to load. Each extension must be a Python file or package (dir with an __init__.py) that uses @zetch.register_function to render custom functions which can be used in templates. Relative paths are resolved relative to the config file's directory.", diff --git a/py_rust/src/init.rs b/py_rust/src/init.rs index 6db5345..5c7868a 100644 --- a/py_rust/src/init.rs +++ b/py_rust/src/init.rs @@ -44,7 +44,7 @@ static SCHEMA_DIRECTIVE_PREFIX: &str = "#:schema "; /// Note this should be prepended with `"#:schema"` fn get_schema_directive() -> String { format!( - "https://github.com/zakstucke/zetch/blob/v{}/py_rust/src/config/schema.json", + "https://raw.githubusercontent.com/zakstucke/zetch/v{}/py_rust/src/config/schema.json", env!("CARGO_PKG_VERSION") ) } @@ -89,8 +89,6 @@ exclude = [] matchers = ["zetch"] [engine] -keep_trailing_newline = true -allow_undefined = false custom_extensions = [] [context.static] diff --git a/py_rust/tests/helpers/types.py b/py_rust/tests/helpers/types.py index 3d44a41..05c4f79 100644 --- a/py_rust/tests/helpers/types.py +++ b/py_rust/tests/helpers/types.py @@ -27,8 +27,6 @@ class Engine(tp.TypedDict): block_end: tp.NotRequired[str] comment_start: tp.NotRequired[str] comment_end: tp.NotRequired[str] - keep_trailing_newline: tp.NotRequired[bool] - allow_undefined: tp.NotRequired[bool] custom_extensions: tp.NotRequired["list[str]"] diff --git a/py_rust/tests/render/test_custom_extensions.py b/py_rust/tests/render/test_custom_extensions.py index e440b73..6378f60 100644 --- a/py_rust/tests/render/test_custom_extensions.py +++ b/py_rust/tests/render/test_custom_extensions.py @@ -432,8 +432,8 @@ def test_custom_ext_nice_user_invalid_errs( # DONE pre and post commands, post have access to context via env vars. # DONE move zetch file to separate read, del and write commands # DONE allow input as string instead of file, that should print to stdout for read, write, del -# TODO probably remove most engine config, maybe making top level if minimal enough, we don't want to mess with files and error early (so enforce no_undefined and keep_trailing_newline) -# TODO fix schema - not sure why its not working +# DONE probably remove most engine config, maybe making top level if minimal enough, we don't want to mess with files and error early (so enforce no_undefined and keep_trailing_newline) +# DONE fix schema - not sure why its not working # TODO some sort of heavy/light/modes solution to caching values and not recomputing, maybe also for ban-defaults etc. maybe a modes top level config section, where a mode can override any config, set ban-defaults etc, need to think, but also need a way to only run certain post and pre in certain modes, need to think on best api. # TODO think about interop with jinja,cookiecutter,copier,etc # TODO decide and document optimal formatting, probably using scolvins and making sure it can working with custom extensions. diff --git a/py_rust/tests/render/test_engine.py b/py_rust/tests/render/test_engine.py index 534a5e6..cba43c4 100644 --- a/py_rust/tests/render/test_engine.py +++ b/py_rust/tests/render/test_engine.py @@ -99,17 +99,9 @@ def test_engine_misc_syntax(template_src: str, config: InputConfig, expected: st "block_end": "%}", "comment_start": "{#", "comment_end": "#}", - "keep_trailing_newline": True, - "allow_undefined": False, }, "Hello, World!\nmybool is True\n", ), - # Newline should be stripped when keep_trailing_newline is False: - ( - DEFAULT_TEMPLATE_SRC, - {"keep_trailing_newline": False}, - "Hello, World!\nmybool is True", - ), # Custom syntax matchers should work: ( "Hello, [< var >]![# this is an ignored comment #]\nmybool is [? if mybool ?]True[? else ?]False[? endif ?]\n", @@ -168,40 +160,16 @@ def test_engine_config(template_src: str, engine_config: Engine, expected: str): "Hello, World! My name is Bob!", False, ), - # Should raise when name in undefined as by default banned: + # Should raise when name in undefined as this is banned: ( "Hello, {{ var }}! My name is {{ name }}!", {"context": {"static": {"var": {"value": "World"}}}}, "Failed to render template: 'undefined value", True, ), - # Should also raise when specifically set: - ( - "Hello, {{ var }}! My name is {{ name }}!", - { - "context": {"static": {"var": {"value": "World"}}}, - "engine": { - "allow_undefined": False, - }, - }, - "Failed to render template: 'undefined value", - True, - ), - # Should be fine when allowed: - ( - "Hello, {{ var }}! My name is {{ name }}!", - { - "context": {"static": {"var": {"value": "World"}}}, - "engine": { - "allow_undefined": True, - }, - }, - "Hello, World! My name is !", - False, - ), ], ) -def test_allow_undefined( +def test_undefined_banned( template_src: str, config: InputConfig, expected: str, expected_is_err_match: bool ): """Check errors on unknown context when no allow_defined or allow_defined is False, but when true should work fine.""" diff --git a/py_rust/tests/test_config_valid.py b/py_rust/tests/test_config_valid.py index 2528624..d1c4877 100644 --- a/py_rust/tests/test_config_valid.py +++ b/py_rust/tests/test_config_valid.py @@ -119,20 +119,18 @@ def cfg_str(config: InputConfig) -> str: cfg_str( { "engine": { - "allow_undefined": True, - "keep_trailing_newline": False, + "comment_start": "|||/", + "comment_end": "/|||", } } ), { - "allow_undefined": True, - "keep_trailing_newline": False, "block_start": "{%", "block_end": "%}", "variable_start": "{{", "variable_end": "}}", - "comment_start": "{#", - "comment_end": "#}", + "comment_start": "|||/", + "comment_end": "/|||", "custom_extensions": [], }, ), diff --git a/zetch.config.toml b/zetch.config.toml index 37e2aae..233640c 100644 --- a/zetch.config.toml +++ b/zetch.config.toml @@ -1,11 +1,9 @@ -# :schema https://github.com/zakstucke/zetch/blob/v0.0.16/py_rust/src/config/schema.json +# :schema https://github.com/zakstucke/zetch/blob/v0.0.9/py_rust/src/config/schema.json ignore_files = [".gitignore"] exclude = [] [engine] -keep_trailing_newline = true -allow_undefined = false custom_extensions = [] [context.static]