Build Scripts Guidelines
This document provides conventions and best practices for shell scripts in the ProvLabs documentation build system.
Script Structure
File Organization
- All build scripts are located in the
scripts/directory - Use executable permissions (
chmod +x) for all shell scripts - Use
.shextension for all shell scripts
Script Headers
#!/bin/bash
# Brief description of what this script does
# Include usage examples if the script takes parameters
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
Error Handling
Exit Codes
- Use
|| exitfor commands that must succeed - Use
set -eat the top of scripts to exit on any error - Provide meaningful error messages before exiting
Directory Operations
# Good: Safe directory operations
pushd "$SCRIPT_DIR/.." || exit
# ... operations ...
popd || exit
# Good: Check if directories exist before operations
if [[ ! -d "$TARGET_DIR" ]]; then
mkdir -p "$TARGET_DIR"
fi
OpenAPI Generation Patterns
Protocol Buffer Processing
- Use
buf generatefor generating OpenAPI specs from proto files - Apply consistent filtering for query and service proto files only
- Clean up temporary directories after generation
File Processing Patterns
# Standard pattern for processing proto directories
proto_dirs=$(find ./path -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)
for dir in $proto_dirs; do
query_file=$(find "${dir}" -maxdepth 1 \( -name 'query.proto' -o -name 'service.proto' \))
if [[ ! -z "$query_file" ]]; then
buf generate --template ../../template.yaml -o ../ "$query_file"
fi
done
OpenAPI Manipulation
- Use
jqfor JSON manipulation and schema removal - Use
redoclyfor linting, joining, and splitting OpenAPI specifications - Apply consistent schema cleanup with
removeBaseQuerySchemasfunction
File Management
Cleanup Operations
# Safe removal patterns
rm -rf "$OPENAPI_DIR"
mkdir -p "$OPENAPI_DIR"
# Conditional cleanup
if [[ -d "$TEMP_DIR" ]]; then
rm -r "$TEMP_DIR"
fi
File Copying
# Recursive copying with proper error handling
cp -R source_dir/* destination_dir/
mv source_file destination_file
Submodule Operations
Git Submodule Patterns
- Work within submodule directories using
pushd/popd - Generate files outside of submodule directories to avoid git issues
- Clean up generated files within submodules after moving them
Dependencies
Required Tools
Scripts assume the following tools are available:
buf- Protocol Buffer toolingjq- JSON processingredocly- OpenAPI tooling- Standard Unix tools (
find,xargs,sed,mv,cp,rm)
Tool Verification
# Check for required tools at script start
command -v buf >/dev/null 2>&1 || { echo "buf is required but not installed"; exit 1; }
command -v jq >/dev/null 2>&1 || { echo "jq is required but not installed"; exit 1; }
Schema Management
Base Schema References
- Remove duplicate schemas to avoid conflicts when joining specs
- Update references to point to centralized base schema file
- Use consistent sed patterns for reference replacement
Schema Cleanup Function
function removeBaseQuerySchemas() {
SVC_FILE=$1
jq "del( \
.components.schemas.\"schema.name.1\", \
.components.schemas.\"schema.name.2\" \
)" "$SVC_FILE" > "$SVC_FILE.tmp"
mv "$SVC_FILE.tmp" "$SVC_FILE"
# Update references
sed -i '' -e "s|old-reference|new-reference|g" "$SVC_FILE"
}
Integration Points
npm Script Integration
- Scripts are called from
package.jsonscripts (e.g.,preparescript) - Ensure scripts can be run from repository root
- Scripts should be idempotent - safe to run multiple times
Build Pipeline
protoc-openapi.shgenerates all OpenAPI filessplit-openapi.shis called automatically by protoc scripttag-operations.shandpost-provenance.shhandle post-processing- Final output should be ready for Docusaurus build
Performance Considerations
Parallel Processing
- Use
findwithxargsfor efficient file processing - Process multiple proto files in batches when possible
- Clean up intermediate files promptly to save disk space
Caching Strategy
- Generated files in
openapi/directory are not committed - Intermediate files should be cleaned up after use
- Consider timestamp-based regeneration for development efficiency