|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Path to your action.yml file and README.md |
| 4 | +ACTION_YML="action.yml" |
| 5 | +README_MD="README.md" |
| 6 | + |
| 7 | +# Check if yq is installed |
| 8 | +if ! command -v yq &> /dev/null; then |
| 9 | + echo "Error: yq is not installed. Please install it first." |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +# Generate table for inputs |
| 14 | +generate_inputs_table() { |
| 15 | + local table="| Name | Description | Default |\n" |
| 16 | + table+="|------|-------------|--------|\n" |
| 17 | + |
| 18 | + # Get all input names |
| 19 | + local input_names= |
| 20 | + input_names=$(yq e '.inputs | keys | .[]' "${ACTION_YML}") |
| 21 | + |
| 22 | + for name in ${input_names}; do |
| 23 | + # Get description and properly format it |
| 24 | + local description= |
| 25 | + description=$(yq e ".inputs.${name}.description" "${ACTION_YML}") |
| 26 | + # Compress multi-line descriptions and escape pipes |
| 27 | + description=$(echo "${description}" | tr '\n' ' ' | sed 's/|/\\|/g') |
| 28 | + |
| 29 | + # Get default value |
| 30 | + local default= |
| 31 | + default=$(yq e ".inputs.${name}.default" "${ACTION_YML}") |
| 32 | + |
| 33 | + # For multi-line default values, show only first line |
| 34 | + if [[ $(echo "${default}" | wc -l) -gt 1 ]]; then |
| 35 | + local first_line= |
| 36 | + first_line=$(echo "${default}" | head -n 1 | sed 's/|/\\|/g') |
| 37 | + default="${first_line} ..." |
| 38 | + else |
| 39 | + default=$(echo "${default}" | sed 's/|/\\|/g') |
| 40 | + fi |
| 41 | + |
| 42 | + # Truncate long default values |
| 43 | + if [[ ${#default} -gt 50 ]]; then |
| 44 | + default="${default:0:47}..." |
| 45 | + fi |
| 46 | + |
| 47 | + table+="| \`${name}\` | ${description} | \`${default}\` |\n" |
| 48 | + done |
| 49 | + |
| 50 | + echo -e "${table}" |
| 51 | +} |
| 52 | + |
| 53 | +# Generate table for outputs |
| 54 | +generate_outputs_table() { |
| 55 | + local table="| Name | Description |\n" |
| 56 | + table+="|------|-------------|\n" |
| 57 | + |
| 58 | + # Get all output names |
| 59 | + local output_names= |
| 60 | + output_names=$(yq e '.outputs | keys | .[]' "${ACTION_YML}") |
| 61 | + |
| 62 | + for name in ${output_names}; do |
| 63 | + local description= |
| 64 | + description=$(yq e ".outputs.${name}.description" "${ACTION_YML}") |
| 65 | + |
| 66 | + # For complex descriptions like JSON examples, use a simplified approach |
| 67 | + if [[ "${name}" == "json" ]]; then |
| 68 | + description="The changes made by this action, in JSON format. Contains information about updated files, images, and digests." |
| 69 | + else |
| 70 | + # For regular descriptions, flatten them |
| 71 | + description=$(echo "${description}" | tr '\n' ' ' | sed 's/|/\\|/g') |
| 72 | + fi |
| 73 | + |
| 74 | + table+="| \`${name}\` | ${description} |\n" |
| 75 | + done |
| 76 | + |
| 77 | + echo -e "${table}" |
| 78 | +} |
| 79 | + |
| 80 | +# Generate tables for inputs and outputs |
| 81 | +inputs_table=$(generate_inputs_table) |
| 82 | +outputs_table=$(generate_outputs_table) |
| 83 | + |
| 84 | +# Combine the tables with headers |
| 85 | +markdown_table="### Inputs\n\n${inputs_table}\n\n### Outputs\n\n${outputs_table}\n\n> **Note:** For complete details on inputs and outputs, please refer to the [action.yml](./action.yml) file." |
| 86 | + |
| 87 | +# Check if the end placeholder exists, if not add it |
| 88 | +if ! grep -q "<!-- end automated updates do not change -->" "${README_MD}"; then |
| 89 | + echo "<!-- end automated updates do not change -->" >> "${README_MD}" |
| 90 | + echo "Added missing end placeholder to README.md" |
| 91 | +fi |
| 92 | + |
| 93 | +# Create a temporary file |
| 94 | +temp_file=$(mktemp) |
| 95 | + |
| 96 | +# Extract the content before the begin placeholder |
| 97 | +sed -n '1,/<!-- begin automated updates do not change -->/p' "${README_MD}" > "${temp_file}" |
| 98 | + |
| 99 | +# Add our markdown table |
| 100 | +echo -e "${markdown_table}" >> "${temp_file}" |
| 101 | + |
| 102 | +# Extract the content after the end placeholder |
| 103 | +sed -n '/<!-- end automated updates do not change -->/,$p' "${README_MD}" >> "${temp_file}" |
| 104 | + |
| 105 | +# Replace the original file with the temporary file |
| 106 | +mv "${temp_file}" "${README_MD}" |
| 107 | + |
| 108 | +echo "README.md has been updated with the inputs and outputs tables." |
0 commit comments