|
| 1 | +#!/bin/bash |
| 2 | +# create docs for a configurable project |
| 3 | +# WF 2024-07-30 - updated |
| 4 | + |
| 5 | +# Extract project name from pyproject.toml |
| 6 | +PROJECT_NAME=$(grep "\[project\]" pyproject.toml -A1 | grep name | cut -d '=' -f2 | tr -d ' "') |
| 7 | +PACKAGE_NAME=$(grep "\[tool.hatch.build.targets.wheel.sources\]" pyproject.toml -A1 | tail -1 | cut -d '=' -f2 | tr -d ' "') |
| 8 | + |
| 9 | + |
| 10 | +# Function to print usage information |
| 11 | +print_usage() { |
| 12 | + echo "Usage: $0 [OPTIONS]" |
| 13 | + echo "Options:" |
| 14 | + echo " -pr, --project NAME Set the project name (default: $PROJECT_NAME)" |
| 15 | + echo " -pa, --package NAME Set the package name (default: $PACKAGE_NAME)" |
| 16 | + echo " -d, --deploy Deploy the documentation after building" |
| 17 | + echo " -h, --help Display this help message" |
| 18 | +} |
| 19 | + |
| 20 | +# Parse command line arguments |
| 21 | +DEPLOY=false |
| 22 | +while [[ "$#" -gt 0 ]]; do |
| 23 | + case $1 in |
| 24 | + -pr|--project) PROJECT_NAME="$2"; shift ;; |
| 25 | + -pa|--package) PACKAGE_NAME="$2"; shift ;; |
| 26 | + -d|--deploy) DEPLOY=true ;; |
| 27 | + -h|--help) print_usage; exit 0 ;; |
| 28 | + *) echo "Unknown parameter: $1"; print_usage; exit 1 ;; |
| 29 | + esac |
| 30 | + shift |
| 31 | +done |
| 32 | + |
| 33 | +# Ensure we're in the correct directory |
| 34 | +if [[ ! -d "$PACKAGE_NAME" ]]; then |
| 35 | + echo "Error: $PACKAGE_NAME package directory not found. Are you in the correct directory?" |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +# Check if mkdocs is installed |
| 40 | +if ! command -v mkdocs &> /dev/null; then |
| 41 | + pip install mkdocs mkdocs-material mkdocstrings[python] |
| 42 | +fi |
| 43 | + |
| 44 | +# Create or update mkdocs.yml |
| 45 | +cat << EOF > mkdocs.yml |
| 46 | +site_name: $PROJECT_NAME API Documentation |
| 47 | +theme: |
| 48 | + name: material |
| 49 | +plugins: |
| 50 | + - search |
| 51 | + - mkdocstrings: |
| 52 | + handlers: |
| 53 | + python: |
| 54 | + setup_commands: |
| 55 | + - import sys |
| 56 | + - import os |
| 57 | + - sys.path.insert(0, os.path.abspath(".")) |
| 58 | + selection: |
| 59 | + docstring_style: google |
| 60 | + rendering: |
| 61 | + show_source: true |
| 62 | +nav: |
| 63 | + - API: index.md |
| 64 | +EOF |
| 65 | + |
| 66 | +# Create or update index.md |
| 67 | +index_md=docs/index.md |
| 68 | +mkdir -p docs |
| 69 | +cat << EOF > $index_md |
| 70 | +# $PROJECT_NAME API Documentation |
| 71 | +
|
| 72 | +::: $PACKAGE_NAME |
| 73 | + options: |
| 74 | + show_submodules: true |
| 75 | +EOF |
| 76 | + |
| 77 | +# Ignore DeprecationWarnings during build |
| 78 | +export PYTHONWARNINGS="ignore::DeprecationWarning" |
| 79 | + |
| 80 | +# Build the documentation |
| 81 | +mkdocs build --config-file ./mkdocs.yml |
| 82 | + |
| 83 | +# Deploy if requested |
| 84 | +if [ "$DEPLOY" = true ]; then |
| 85 | + mkdocs gh-deploy --force --config-file ./mkdocs.yml |
| 86 | +fi |
| 87 | + |
| 88 | +echo "Documentation process completed for $PROJECT_NAME." |
0 commit comments