All checks were successful
Install define-things / Install define-things with uv (push) Successful in 22s
213 lines
No EOL
7.3 KiB
Python
213 lines
No EOL
7.3 KiB
Python
from importlib.resources import files
|
|
from pathlib import Path
|
|
import click
|
|
import linkml
|
|
import subprocess
|
|
import warnings
|
|
import shutil
|
|
import time
|
|
|
|
|
|
SUPPORTED_FORMATS = [
|
|
'doc',
|
|
'owl',
|
|
'jsonld-ctx',
|
|
'shacl',
|
|
]
|
|
|
|
|
|
def get_outfile(schema, cmd):
|
|
# {schema} will be filled with relative path containing schema name and version
|
|
# e.g. 'my-schema/unreleased'
|
|
OUTFILES = {
|
|
'owl': f'build/linkml-docs/s/{schema}.owl.ttl',
|
|
'jsonld-ctx': f'build/linkml-docs/s/{schema}.context.jsonld',
|
|
'shacl': f'build/linkml-docs/s/{schema}.shacl.ttl',
|
|
}
|
|
return OUTFILES[cmd]
|
|
|
|
def get_command(schema, cmd, data={}):
|
|
# {schema} will be filled with relative path containing schema name and version
|
|
# e.g. 'my-schema/unreleased'
|
|
COMMANDS = {
|
|
'doc': [
|
|
'gen-doc',
|
|
'--hierarchical-class-view',
|
|
'--metadata',
|
|
'--format',
|
|
'markdown',
|
|
'--example-directory',
|
|
f'src/{schema}/examples/',
|
|
'-d',
|
|
f'build/linkml-docs/s/{schema}',
|
|
f'src/{schema}.yaml'
|
|
],
|
|
'owl': [
|
|
'gen-owl',
|
|
'-f',
|
|
'owl',
|
|
'--mergeimports',
|
|
'--consolidate-cardinality-axioms',
|
|
'--skip-vacuous-min-zero-cardinality-axioms',
|
|
'--skip-vacuous-local-range-axioms',
|
|
*(['--importmap', data['importmap']] if 'importmap' in data else []),
|
|
f'src/{schema}.yaml'
|
|
],
|
|
'jsonld-ctx': [
|
|
'gen-jsonld-context',
|
|
'--mergeimports',
|
|
*(['--importmap', data['importmap']] if 'importmap' in data else []),
|
|
f'src/{schema}.yaml'
|
|
],
|
|
'shacl': [
|
|
'gen-shacl',
|
|
'--include-annotations',
|
|
'--include-stripped-prefixes',
|
|
'--exclude-order',
|
|
*(['--importmap', data['importmap']] if 'importmap' in data else []),
|
|
f'src/{schema}.yaml'
|
|
],
|
|
'properdocs': [
|
|
'properdocs',
|
|
'build'
|
|
],
|
|
'properdocs-serve': [
|
|
'properdocs',
|
|
'serve',
|
|
'-f',
|
|
'mkdocs.yml'
|
|
]
|
|
}
|
|
return COMMANDS[cmd]
|
|
|
|
|
|
def run_command(
|
|
cmd: list[str],
|
|
directory: Path | None = None,
|
|
check: bool = True,
|
|
to_stdout: bool = False,
|
|
out_path: str = None,
|
|
):
|
|
if not directory:
|
|
directory = Path.cwd()
|
|
stdout_var = None
|
|
if to_stdout:
|
|
if out_path is None:
|
|
raise ValueError("out_path is required when to_stdout=True")
|
|
|
|
out_path = Path(out_path)
|
|
out_path.parent.mkdir(parents=True, exist_ok=True)
|
|
with out_path.open("w") as output_file:
|
|
subprocess.run(
|
|
cmd,
|
|
cwd=directory,
|
|
check=check,
|
|
stdout=output_file,
|
|
)
|
|
else:
|
|
subprocess.run(
|
|
cmd,
|
|
cwd=directory,
|
|
check=check,
|
|
)
|
|
|
|
|
|
def generate_docs(base_dir, schema_str, data={}):
|
|
|
|
# Copy existing yaml schema to docs output
|
|
schema_path = base_dir / f'src/{schema_str}.yaml'
|
|
outpath = base_dir / f'build/linkml-docs/s/{schema_str}.yaml'
|
|
outpath.parent.mkdir(parents=True, exist_ok=True)
|
|
shutil.copy2(schema_path, outpath)
|
|
|
|
# Then copy any schema-specific extra docs to associated schema build dir
|
|
schema_build_dir = base_dir / f'build/linkml-docs/s/{schema_str}'
|
|
schema_build_dir.mkdir(parents=True, exist_ok=True)
|
|
extra_docs_dir = base_dir / f'src/{schema_str}/extra-docs'
|
|
for file_path in extra_docs_dir.glob("*.md"):
|
|
if file_path.is_file():
|
|
shutil.copy2(file_path, schema_build_dir)
|
|
|
|
run_command(
|
|
cmd=get_command(schema_str, 'doc', data=data),
|
|
directory=base_dir,
|
|
)
|
|
|
|
# Then generate extra formats, their outputs will go into the intended locations
|
|
for f in SUPPORTED_FORMATS:
|
|
if f == 'doc':
|
|
continue
|
|
run_command(
|
|
cmd=get_command(schema_str, f, data=data),
|
|
directory=base_dir,
|
|
to_stdout=True,
|
|
out_path=str(base_dir / get_outfile(schema_str, f)),
|
|
)
|
|
|
|
|
|
@click.command('generate')
|
|
@click.option('--project', '-p', type=str, default=None, help='Path to the concepts project for which exports should be generated in various formats; if not provided, current working directory is assumed.',)
|
|
@click.option('--schema', '-s', type=str, help='Relative path to the schema for which exports should be generated in various formats',)
|
|
@click.option('--formats', '-f', type=str, multiple=True, help='Schema output format to generate; options are: (doc, owl, jsonld-ctx, shacl). if no format is provided, it will generate all formats; if doc is provided, it will generate all formats along with documentation structure',)
|
|
@click.option('--importmap', '-i', type=str, default=None, help='Path to an importmap. A YAML or JSON file that provides custom redirects between an import name and the path to the corresponding schema.',)
|
|
@click.option('--serve', is_flag=True, help='Serve the docs locally at http://127.0.0.1:8000/ after building; only applicable when --format doc is provided',)
|
|
def cli(
|
|
project: str = None,
|
|
schema: str = None,
|
|
formats: tuple | None = None,
|
|
importmap: str = None,
|
|
serve: bool | None = False,
|
|
):
|
|
|
|
if len(list(formats)) < 1:
|
|
formats = SUPPORTED_FORMATS
|
|
|
|
# if no formats are supplied via flags, we generate all
|
|
# generating docs implies generating all
|
|
|
|
# get base project dir
|
|
base_dir = Path(project) if project is not None else Path.cwd()
|
|
# get schema name and version
|
|
schema_path = base_dir / schema
|
|
if not bool(schema_path.suffix):
|
|
schema_path = Path(str(schema_path) + '.yaml')
|
|
schema_version = schema_path.stem
|
|
schema_name = schema_path.parent.stem
|
|
|
|
# before generating any formats, ensure output directory exists for the given schema
|
|
output_dir = base_dir / 'build' / 'linkml-docs' / 's' / schema_name / schema_version
|
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
# relative schema path, without extension
|
|
schema_str = f'{schema_name}/{schema_version}'
|
|
|
|
# importmap if provided
|
|
data = {}
|
|
if importmap:
|
|
data['importmap'] = importmap
|
|
|
|
if 'doc' in formats:
|
|
# First copy top-level extra docs to build dir, this is not schema-specifif
|
|
extra_docs_top = base_dir / 'extra-docs'
|
|
for file_path in extra_docs_top.glob("*.md"):
|
|
shutil.copy2(file_path, base_dir / 'build' / 'linkml-docs')
|
|
# Now run schema-specific generation of all doc outputs
|
|
generate_docs(base_dir, schema_str, data=data)
|
|
# Generate full docs site
|
|
run_command(cmd=get_command(None, 'properdocs', data=data), directory=base_dir,)
|
|
# Serve if instructed
|
|
if serve:
|
|
run_command(cmd=get_command(None, 'properdocs-serve', data=data), directory=base_dir,)
|
|
return
|
|
|
|
for f in formats:
|
|
if f not in SUPPORTED_FORMATS:
|
|
warnings.warn(f'Skipping unsupported format: {f}\nSupported formats are: {SUPPORTED_FORMATS}')
|
|
continue
|
|
run_command(
|
|
cmd=get_command(schema_str, f, data=data),
|
|
directory=base_dir,
|
|
to_stdout=True,
|
|
out_path=str(base_dir / get_outfile(schema_str, f)),
|
|
)
|
|
|