Skip to content

Commit c23fa9f

Browse files
authored
Merge pull request #1899 from bids-standard/jsr/publish-schema
[INFRA] Publish schema to Javascript Registry (https://jsr.io/@bids/schema) on changes and releases
2 parents 93b6d1f + 42265d6 commit c23fa9f

File tree

3 files changed

+114
-2
lines changed

3 files changed

+114
-2
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
name: "Publish schema"
2+
3+
on:
4+
push:
5+
branches:
6+
- "master"
7+
tags:
8+
- "schema-*"
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.ref }}
12+
cancel-in-progress: true
13+
14+
defaults:
15+
run:
16+
shell: bash
17+
18+
env:
19+
GIT_AUTHOR_NAME: BIDS CI
20+
GIT_AUTHOR_EMAIL: [email protected]
21+
GIT_COMMITTER_NAME: BIDS CI
22+
GIT_COMMITTER_EMAIL: [email protected]
23+
24+
permissions:
25+
contents: write
26+
id-token: write
27+
28+
jobs:
29+
publish:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v4
33+
with:
34+
fetch-depth: 0
35+
filter: "blob:none"
36+
- uses: actions/setup-python@v5
37+
with:
38+
python-version: 3
39+
- name: Install bidsschematools
40+
run: |
41+
pip install --upgrade tools/schemacode
42+
git clean -fxd tools/schemacode
43+
- name: Checkout jsr-dist
44+
run: |
45+
git checkout -t origin/jsr-dist
46+
- name: Regenerate schema
47+
run: bst export > schema.json
48+
- name: Regenerate context types
49+
run: |
50+
jq .meta.context schema.json \
51+
| npx quicktype --src-lang schema --lang ts -t Context --just-types \
52+
> context.ts
53+
- name: Regenerate metaschema types
54+
run: |
55+
# Name the file schema so the type will be named Schema
56+
bst export-metaschema > /tmp/schema.json
57+
npx --package=json-schema-to-typescript json2ts --unknownAny /tmp/schema.json > metaschema.ts
58+
- name: Determine next version
59+
run: |
60+
BASE=$( jq -r .schema_version schema.json )
61+
if [[ "$BASE" =~ ^[0-9]*.[0-9]*.[0-9]*$ ]]; then
62+
# Release, so unconditionally update version
63+
VERSION=$BASE
64+
jq ".version = \"$VERSION\"" jsr.json > tmp.json && mv tmp.json jsr.json
65+
else
66+
DENOVER=$( jq -r .version jsr.json )
67+
# Get the reference of the latest commit to touch the schema directory
68+
HASH=$( git log -n 1 --pretty=%h $REF -- src/schema )
69+
if [[ $DENOVER =~ ^"$BASE".[0-9] ]]; then
70+
PREFIX=${DENOVER%+*}
71+
let SERIAL=1+${PREFIX#$BASE.}
72+
else
73+
SERIAL=1
74+
fi
75+
VERSION="$BASE.$SERIAL+$HASH"
76+
fi
77+
echo VERSION=$VERSION | tee -a $GITHUB_ENV
78+
env:
79+
REF: ${{ github.ref }}
80+
- name: Check for changes, set version and commit
81+
run: |
82+
if ! git diff -s --exit-code; then
83+
jq ".version = \"$VERSION\"" jsr.json > tmp.json && mv tmp.json jsr.json
84+
git add jsr.json schema.json context.ts metaschema.ts
85+
git commit -m "Update schema JSR distribution"
86+
git push
87+
fi
88+
- name: Publish to JSR
89+
if: success()
90+
run: |
91+
npx jsr publish

src/schema/meta/context.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ properties:
7171
additionalProperties: false
7272
properties:
7373
sub_dirs:
74-
description: 'Subjects as determined by sub-*/ directories'
74+
description: 'Subjects as determined by sub-* directories'
7575
type: array
7676
items:
7777
type: string
@@ -100,7 +100,7 @@ properties:
100100
additionalProperties: false
101101
properties:
102102
ses_dirs:
103-
description: 'Sessions as determined by ses-*/ directories'
103+
description: 'Sessions as determined by ses-* directories'
104104
type: array
105105
items:
106106
type: string

tools/schemacode/bidsschematools/__main__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import logging
22
import os
3+
import sys
34

45
import click
56

7+
if sys.version_info < (3, 9):
8+
from importlib_resources import files
9+
else:
10+
from importlib.resources import files
11+
12+
613
from .schema import export_schema, load_schema
714

815

@@ -32,5 +39,19 @@ def export(ctx, schema, output):
3239
fobj.write(text)
3340

3441

42+
@cli.command()
43+
@click.option("--output", default="-")
44+
@click.pass_context
45+
def export_metaschema(ctx, output):
46+
"""Export BIDS schema to JSON document"""
47+
metaschema = files("bidsschematools.data").joinpath("metaschema.json").read_text()
48+
if output == "-":
49+
print(metaschema, end="")
50+
else:
51+
output = os.path.abspath(output)
52+
with open(output, "w") as fobj:
53+
fobj.write(metaschema)
54+
55+
3556
if __name__ == "__main__":
3657
cli()

0 commit comments

Comments
 (0)