Skip to content

Commit

Permalink
Merge pull request #1899 from bids-standard/jsr/publish-schema
Browse files Browse the repository at this point in the history
[INFRA] Publish schema to Javascript Registry (https://jsr.io/@bids/schema) on changes and releases
  • Loading branch information
effigies committed Aug 26, 2024
2 parents 93b6d1f + 42265d6 commit c23fa9f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
91 changes: 91 additions & 0 deletions .github/workflows/publish_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: "Publish schema"

on:
push:
branches:
- "master"
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: [email protected]
GIT_COMMITTER_NAME: BIDS CI
GIT_COMMITTER_EMAIL: [email protected]

permissions:
contents: write
id-token: write

jobs:
publish:
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
- name: Install bidsschematools
run: |
pip install --upgrade tools/schemacode
git clean -fxd tools/schemacode
- name: Checkout jsr-dist
run: |
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 )
# 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.}
else
SERIAL=1
fi
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
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
4 changes: 2 additions & 2 deletions src/schema/meta/context.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tools/schemacode/bidsschematools/__main__.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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()

0 comments on commit c23fa9f

Please sign in to comment.