From 6082b5e706e9f7fe6c702e3d5c90baa98473e88a Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 20 Aug 2024 23:00:40 -0400 Subject: [PATCH 1/5] fix: Remove */ to avoid confusing quicktype --- src/schema/meta/context.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/schema/meta/context.yaml b/src/schema/meta/context.yaml index 2481e15d99..8783b46371 100644 --- a/src/schema/meta/context.yaml +++ b/src/schema/meta/context.yaml @@ -71,7 +71,7 @@ properties: additionalProperties: false properties: sub_dirs: - description: 'Subjects as determined by sub-*/ directories' + description: 'Subjects as determined by sub-* directories' type: array items: type: string @@ -100,7 +100,7 @@ properties: additionalProperties: false properties: ses_dirs: - description: 'Sessions as determined by ses-*/ directories' + description: 'Sessions as determined by ses-* directories' type: array items: type: string From 335db0201251e1899884ab177e67b600d906bc55 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 20 Aug 2024 23:01:29 -0400 Subject: [PATCH 2/5] feat(cli): Add command to export metaschema --- tools/schemacode/bidsschematools/__main__.py | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tools/schemacode/bidsschematools/__main__.py b/tools/schemacode/bidsschematools/__main__.py index c8d554cf0a..a250e37cab 100644 --- a/tools/schemacode/bidsschematools/__main__.py +++ b/tools/schemacode/bidsschematools/__main__.py @@ -1,8 +1,15 @@ import logging import os +import sys import click +if sys.version_info < (3, 9): + from importlib_resources import files +else: + from importlib.resources import files + + from .schema import export_schema, load_schema @@ -32,5 +39,19 @@ def export(ctx, schema, output): fobj.write(text) +@cli.command() +@click.option("--output", default="-") +@click.pass_context +def export_metaschema(ctx, output): + """Export BIDS schema to JSON document""" + metaschema = files("bidsschematools.data").joinpath("metaschema.json").read_text() + if output == "-": + print(metaschema, end="") + else: + output = os.path.abspath(output) + with open(output, "w") as fobj: + fobj.write(metaschema) + + if __name__ == "__main__": cli() From 9eeab0139a852192b5b7fc89af262a8bbfbe3b15 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Wed, 21 Aug 2024 09:54:32 -0400 Subject: [PATCH 3/5] chore(ci): Prepare JSR distribution --- .github/workflows/publish_schema.yml | 83 ++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 .github/workflows/publish_schema.yml diff --git a/.github/workflows/publish_schema.yml b/.github/workflows/publish_schema.yml new file mode 100644 index 0000000000..d50ba7f307 --- /dev/null +++ b/.github/workflows/publish_schema.yml @@ -0,0 +1,83 @@ +name: "Publish schema" + +on: + push: + branches: + - "master" + - "jsr/*" + tags: + - "schema-*" + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +defaults: + run: + shell: bash + +env: + GIT_AUTHOR_NAME: BIDS CI + GIT_AUTHOR_EMAIL: bids.maintenance@gmail.com + GIT_COMMITTER_NAME: BIDS CI + GIT_COMMITTER_EMAIL: bids.maintenance@gmail.com + +jobs: + publish: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: 3 + - name: Install bidsschematools + run: | + pip install --upgrade tools/schemacode + git clean -fxd tools/schemacode + - name: Checkout jsr-dist + run: | + git fetch --depth=1 origin jsr-dist + git checkout -t origin/jsr-dist + - name: Regenerate schema + run: bst export > schema.json + - name: Regenerate context types + run: | + jq .meta.context schema.json \ + | npx quicktype --src-lang schema --lang ts -t Context --just-types \ + > context.ts + - name: Regenerate metaschema types + run: | + # Name the file schema so the type will be named Schema + bst export-metaschema > /tmp/schema.json + npx --package=json-schema-to-typescript json2ts --unknownAny /tmp/schema.json > metaschema.ts + - name: Determine next version + run: | + BASE=$( jq -r .schema_version schema.json ) + if [[ "$BASE" =~ ^[0-9]*.[0-9]*.[0-9]*$ ]]; then + # Release, so unconditionally update version + VERSION=$BASE + jq ".version = \"$VERSION\"" jsr.json > tmp.json && mv tmp.json jsr.json + else + DENOVER=$( jq -r .version jsr.json ) + # Should switch to using the commit hash of the source repo? + HASH=$( sha256sum schema.json | head -c 7 ) + if [[ $DENOVER =~ ^"$BASE".[0-9] ]]; then + PREFIX=${DENOVER%+*} + let SERIAL=1+${PREFIX#$BASE.} + else + SERIAL=1 + fi + VERSION="$BASE.$SERIAL+$HASH" + fi + echo VERSION=$VERSION | tee -a $GITHUB_ENV + - name: Check for changes, set version and commit + run: | + if ! git diff -s --exit-code; then + jq ".version = \"$VERSION\"" jsr.json > tmp.json && mv tmp.json jsr.json + git add jsr.json schema.json context.ts metaschema.ts + git commit -m "Update schema JSR distribution" + fi + - name: Publish to JSR + if: success() + run: | + npx jsr publish --dry-run From 6ba6974a9143846560ff2c1aa068b3cad05976dc Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Wed, 21 Aug 2024 11:02:58 -0400 Subject: [PATCH 4/5] feat: Use git hash of latest change to src/schema --- .github/workflows/publish_schema.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish_schema.yml b/.github/workflows/publish_schema.yml index d50ba7f307..cc59da788e 100644 --- a/.github/workflows/publish_schema.yml +++ b/.github/workflows/publish_schema.yml @@ -27,6 +27,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 + filter: "blob:none" - uses: actions/setup-python@v5 with: python-version: 3 @@ -36,7 +39,6 @@ jobs: git clean -fxd tools/schemacode - name: Checkout jsr-dist run: | - git fetch --depth=1 origin jsr-dist git checkout -t origin/jsr-dist - name: Regenerate schema run: bst export > schema.json @@ -59,8 +61,8 @@ jobs: jq ".version = \"$VERSION\"" jsr.json > tmp.json && mv tmp.json jsr.json else DENOVER=$( jq -r .version jsr.json ) - # Should switch to using the commit hash of the source repo? - HASH=$( sha256sum schema.json | head -c 7 ) + # Get the reference of the latest commit to touch the schema directory + HASH=$( git log -n 1 --pretty=%h $REF -- src/schema ) if [[ $DENOVER =~ ^"$BASE".[0-9] ]]; then PREFIX=${DENOVER%+*} let SERIAL=1+${PREFIX#$BASE.} @@ -70,6 +72,8 @@ jobs: VERSION="$BASE.$SERIAL+$HASH" fi echo VERSION=$VERSION | tee -a $GITHUB_ENV + env: + REF: ${{ github.ref }} - name: Check for changes, set version and commit run: | if ! git diff -s --exit-code; then From 42265d6648cdb9df50854ab5bf018d68ca622187 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Mon, 26 Aug 2024 16:49:47 -0400 Subject: [PATCH 5/5] chore: Remove the safety --- .github/workflows/publish_schema.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/publish_schema.yml b/.github/workflows/publish_schema.yml index cc59da788e..842c513eab 100644 --- a/.github/workflows/publish_schema.yml +++ b/.github/workflows/publish_schema.yml @@ -4,7 +4,6 @@ on: push: branches: - "master" - - "jsr/*" tags: - "schema-*" @@ -22,6 +21,10 @@ env: GIT_COMMITTER_NAME: BIDS CI GIT_COMMITTER_EMAIL: bids.maintenance@gmail.com +permissions: + contents: write + id-token: write + jobs: publish: runs-on: ubuntu-latest @@ -80,8 +83,9 @@ jobs: jq ".version = \"$VERSION\"" jsr.json > tmp.json && mv tmp.json jsr.json git add jsr.json schema.json context.ts metaschema.ts git commit -m "Update schema JSR distribution" + git push fi - name: Publish to JSR if: success() run: | - npx jsr publish --dry-run + npx jsr publish