From dc39a2a979075b22363eaf8ab96754ec4a6fba2f Mon Sep 17 00:00:00 2001 From: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Date: Wed, 28 Aug 2024 15:26:04 +0200 Subject: [PATCH] Feat: the JSON Schema HTML viewer generator script now supports generating only for one particular CycloneDX version, including the possibility of generating the HTML only for draft version of CycloneDX during dev time. Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Fix the deletion of the docs folder Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> Move deletion of the docs folder to CLI Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com> --- docgen/json/gen.sh | 67 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/docgen/json/gen.sh b/docgen/json/gen.sh index 19149d5b..8f01b187 100755 --- a/docgen/json/gen.sh +++ b/docgen/json/gen.sh @@ -5,9 +5,8 @@ THIS_PATH="$(realpath "$(dirname "$0")")" SCHEMA_PATH="$(realpath "$THIS_PATH/../../schema")" DOCS_PATH="$THIS_PATH/docs" TEMPLATES_PATH="$THIS_PATH/templates" - -rm -f -R "$DOCS_PATH" -mkdir -p "$DOCS_PATH/"{1.2,1.3,1.4,1.5,1.6} +VALID_CYCLONEDX_VERSIONS=(1.2 1.3 1.4 1.5 1.6) +DRAFT_CYCLONEDX_VERSIONS=() # Check to see if generate-schema-doc is executable and is in the path. If not, install JSON Schema for Humans. if ! [ -x "$(command -v generate-schema-doc)" ] @@ -16,8 +15,31 @@ then pip3 install -r "$THIS_PATH/requirements.txt" fi +# Function to check if a version is in an array +version_in_list() { + local version="$1" + shift + local list=("$@") + for item in "${list[@]}"; do + if [[ "$item" == "$version" ]]; then + return 0 + fi + done + return 1 +} + generate () { version="$1" + + # Check if the version match a valid CycloneDX version or a draft under development + if ! version_in_list "$version" "${VALID_CYCLONEDX_VERSIONS[@]}" && ! version_in_list "$version" "${DRAFT_CYCLONEDX_VERSIONS[@]}"; then + echo "Failed: wrong CycloneDX version: $version" + exit 1 + fi + + echo "Create folder $DOCS_PATH/$version" + mkdir -p "$DOCS_PATH/$version" + title="CycloneDX v${version} JSON Reference" echo "Generating: $title" @@ -45,8 +67,37 @@ generate () { sed -i -e "s/\${version}/$version/g" "$DOCS_PATH/$version/index.html" } -generate 1.2 -generate 1.3 -generate 1.4 -generate 1.5 -generate 1.6 +USAGE_HELP="Generate HTML JSON Schema navigator for CyccloneDX +Usage: $0 : runs only for + $0 : loops over all valid and draft CycloneDX versions" + +# Main logic to handle the argument using a switch case +case "$#" in + 0) + # No arguments provided: Loop over all VALID_CYCLONEDX_VERSIONS and DRAFT_CYCLONEDX_VERSIONS + echo "Deleting folder $DOCS_PATH" + rm -f -R "$DOCS_PATH" + for version in "${VALID_CYCLONEDX_VERSIONS[@]}" "${DRAFT_CYCLONEDX_VERSIONS[@]}"; do + generate "$version" + done + ;; + 1) + case "$1" in + "-h"|"--help") + echo "Usage: $USAGE_HELP" + exit 1 + ;; + *) + # One argument provided: Call generate with the specific version + echo "Deleting folder $DOCS_PATH" + rm -f -R "$DOCS_PATH" + generate "$1" + ;; + esac + ;; + *) + # More than one argument provided: Show usage help + echo "Usage: $USAGE_HELP" + exit 1 + ;; +esac