|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Converts the folder name that the component documentation file |
| 4 | +# is stored in into the integration slug of the component. |
| 5 | +componentTypeFromFolderName() { |
| 6 | + if [[ "$1" = "builders" ]]; then |
| 7 | + echo "builder" |
| 8 | + elif [[ "$1" = "provisioners" ]]; then |
| 9 | + echo "provisioner" |
| 10 | + elif [[ "$1" = "post-processors" ]]; then |
| 11 | + echo "post-processor" |
| 12 | + elif [[ "$1" = "datasources" ]]; then |
| 13 | + echo "data-source" |
| 14 | + else |
| 15 | + echo "" |
| 16 | + fi |
| 17 | +} |
| 18 | + |
| 19 | +# $1: The content to adjust links |
| 20 | +# $2: The organization of the integration |
| 21 | +rewriteLinks() { |
| 22 | + local result="$1" |
| 23 | + local organization="$2" |
| 24 | + |
| 25 | + urlSegment="([^/]+)" |
| 26 | + urlAnchor="(#[^/]+)" |
| 27 | + |
| 28 | + # Rewrite Component Index Page links to the Integration root page. |
| 29 | + # |
| 30 | + # (\1) (\2) (\3) |
| 31 | + # /packer/plugins/datasources/amazon#anchor-tag--> |
| 32 | + # /packer/integrations/hashicorp/amazon#anchor-tag |
| 33 | + local find="\(\/packer\/plugins\/$urlSegment\/$urlSegment$urlAnchor?\)" |
| 34 | + local replace="\(\/packer\/integrations\/$organization\/\2\3\)" |
| 35 | + result="$(echo "$result" | sed -E "s/$find/$replace/g")" |
| 36 | + |
| 37 | + |
| 38 | + # Rewrite Component links to the Integration component page |
| 39 | + # |
| 40 | + # (\1) (\2) (\3) (\4) |
| 41 | + # /packer/plugins/datasources/amazon/parameterstore#anchor-tag --> |
| 42 | + # /packer/integrations/{organization}/amazon/latest/components/datasources/parameterstore |
| 43 | + local find="\(\/packer\/plugins\/$urlSegment\/$urlSegment\/$urlSegment$urlAnchor?\)" |
| 44 | + local replace="\(\/packer\/integrations\/$organization\/\2\/latest\/components\/\1\/\3\4\)" |
| 45 | + result="$(echo "$result" | sed -E "s/$find/$replace/g")" |
| 46 | + |
| 47 | + # Rewrite the Component URL segment from the Packer Plugin format |
| 48 | + # to the Integrations format |
| 49 | + result="$(echo "$result" \ |
| 50 | + | sed "s/\/datasources\//\/data-source\//g" \ |
| 51 | + | sed "s/\/builders\//\/builder\//g" \ |
| 52 | + | sed "s/\/post-processors\//\/post-processor\//g" \ |
| 53 | + | sed "s/\/provisioners\//\/provisioner\//g" \ |
| 54 | + )" |
| 55 | + |
| 56 | + echo "$result" |
| 57 | +} |
| 58 | + |
| 59 | +# $1: Docs Dir |
| 60 | +# $2: Web Docs Dir |
| 61 | +# $3: Component File |
| 62 | +# $4: The org of the integration |
| 63 | +processComponentFile() { |
| 64 | + local docsDir="$1" |
| 65 | + local webDocsDir="$2" |
| 66 | + local componentFile="$3" |
| 67 | + |
| 68 | + local escapedDocsDir="$(echo "$docsDir" | sed 's/\//\\\//g' | sed 's/\./\\\./g')" |
| 69 | + local componentTypeAndSlug="$(echo "$componentFile" | sed "s/$escapedDocsDir\///g" | sed 's/\.mdx//g')" |
| 70 | + |
| 71 | + # Parse out the Component Slug & Component Type |
| 72 | + local componentSlug="$(echo "$componentTypeAndSlug" | cut -d'/' -f 2)" |
| 73 | + local componentType="$(componentTypeFromFolderName "$(echo "$componentTypeAndSlug" | cut -d'/' -f 1)")" |
| 74 | + if [[ "$componentType" = "" ]]; then |
| 75 | + echo "Failed to process '$componentFile', unexpected folder name." |
| 76 | + echo "Documentation for components must be stored in one of:" |
| 77 | + echo "builders, provisioners, post-processors, datasources" |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | + |
| 81 | + |
| 82 | + # Calculate the location of where this file will ultimately go |
| 83 | + local webDocsFolder="$webDocsDir/components/$componentType/$componentSlug" |
| 84 | + mkdir -p "$webDocsFolder" |
| 85 | + local webDocsFile="$webDocsFolder/README.md" |
| 86 | + local webDocsFileTmp="$webDocsFolder/README.md.tmp" |
| 87 | + |
| 88 | + # Copy over the file to its webDocsFile location |
| 89 | + cp "$componentFile" "$webDocsFile" |
| 90 | + |
| 91 | + # Remove the Header |
| 92 | + local lastMetadataLine="$(grep -n -m 2 '^\-\-\-' "$componentFile" | tail -n1 | cut -d':' -f1)" |
| 93 | + cat "$webDocsFile" | tail -n +"$(($lastMetadataLine+2))" > "$webDocsFileTmp" |
| 94 | + mv "$webDocsFileTmp" "$webDocsFile" |
| 95 | + |
| 96 | + # Remove the top H1, as this will be added automatically on the web |
| 97 | + cat "$webDocsFile" | tail -n +3 > "$webDocsFileTmp" |
| 98 | + mv "$webDocsFileTmp" "$webDocsFile" |
| 99 | + |
| 100 | + # Rewrite Links |
| 101 | + rewriteLinks "$(cat "$webDocsFile")" "$4" > "$webDocsFileTmp" |
| 102 | + mv "$webDocsFileTmp" "$webDocsFile" |
| 103 | +} |
| 104 | + |
| 105 | +# Compiles the Packer SDC compiled docs folder down |
| 106 | +# to a integrations-compliant folder (web docs) |
| 107 | +# |
| 108 | +# $1: The directory of the plugin |
| 109 | +# $2: The directory of the SDC compiled docs files |
| 110 | +# $3: The output directory to place the web-docs files |
| 111 | +# $4: The org of the integration |
| 112 | +compileWebDocs() { |
| 113 | + local docsDir="$1/$2" |
| 114 | + local webDocsDir="$1/$3" |
| 115 | + |
| 116 | + echo "Compiling MDX docs in '$2' to Markdown in '$3'..." |
| 117 | + # Create the web-docs directory if it hasn't already been created |
| 118 | + mkdir -p "$webDocsDir" |
| 119 | + |
| 120 | + # Copy the README over |
| 121 | + cp "$docsDir/README.md" "$webDocsDir/README.md" |
| 122 | + |
| 123 | + # Process all MDX component files (exclude index files, which are unsupported) |
| 124 | + for file in $(find "$docsDir" | grep "$docsDir/.*/.*\.mdx" | grep --invert-match "index.mdx"); do |
| 125 | + processComponentFile "$docsDir" "$webDocsDir" "$file" "$4" |
| 126 | + done |
| 127 | +} |
| 128 | + |
| 129 | +compileWebDocs "$1" "$2" "$3" "$4" |
0 commit comments