11#! /bin/bash
22
3- #
3+ # This script compares the files in the Asciidoc and HTML directories and reports any discrepancies.
4+
45# Licensed under the Apache License v2.0 with LLVM Exceptions.
56# See https://llvm.org/LICENSE.txt for license information.
67# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
1112#
1213
1314# Define variables for the directories
14- ASCIIDOC_DIR=" share/mrdocs/addons/generator/asciidoc"
15- HTML_DIR=" share/mrdocs/addons/generator/html"
15+ ASCIIDOC_DIR=" share/mrdocs/addons/generator/asciidoc" # Asciidoc directory path
16+ HTML_DIR=" share/mrdocs/addons/generator/html" # HTML directory path
1617
1718# Function to check for missing files
19+ # Arguments:
20+ # src_dir: Source directory
21+ # dest_dir: Destination directory
22+ # src_ext: Source file extension
23+ # dest_ext: Destination file extension
1824check_missing_files () {
1925 local src_dir=$1
2026 local dest_dir=$2
2127 local src_ext=$3
2228 local dest_ext=$4
2329 local missing_files=()
2430
31+ # Loop over each file in the source directory
2532 while IFS= read -r src_file; do
2633 relative_path=" ${src_file# $src_dir / } "
2734 dest_file=" $dest_dir /${relative_path% $src_ext } $dest_ext "
35+ # If the file does not exist in the destination directory, add it to the missing_files array
2836 if [[ ! -f " $dest_file " ]]; then
2937 missing_files+=(" ${relative_path% $src_ext } $dest_ext " )
3038 fi
3139 done < <( find " $src_dir " -type f -name " *$src_ext " )
3240
41+ # Print the missing files
3342 printf " %s\n" " ${missing_files[@]} "
3443}
3544
36- missing_in_html=$( check_missing_files " $ASCIIDOC_DIR " " $HTML_DIR " " .adoc.hbs" " .html.hbs" )
37- missing_in_asciidoc=$( check_missing_files " $HTML_DIR " " $ASCIIDOC_DIR " " .html.hbs" " .adoc.hbs" )
45+ # Check for missing files in both directions
46+ readarray -t missing_in_html < <( check_missing_files " $ASCIIDOC_DIR " " $HTML_DIR " " .adoc.hbs" " .html.hbs" )
47+ readarray -t missing_in_asciidoc < <( check_missing_files " $HTML_DIR " " $ASCIIDOC_DIR " " .html.hbs" " .adoc.hbs" )
3848
49+ # Normalize the arrays to remove any empty elements
50+ mapfile -t missing_in_html < <( printf " %s\n" " ${missing_in_html[@]} " | sed ' /^$/d' )
51+ mapfile -t missing_in_asciidoc < <( printf " %s\n" " ${missing_in_asciidoc[@]} " | sed ' /^$/d' )
52+
53+ # If there are no missing files, print a success message
3954if [ ${# missing_in_html[@]} -eq 0 ] && [ ${# missing_in_asciidoc[@]} -eq 0 ]; then
4055 echo " All files match between the Asciidoc and HTML directories."
4156else
42- if [ -n " $missing_in_html " ]; then
43- html_message=" The following files are missing from the HTML directory:\n$( printf " %s\n" " $missing_in_html " | sed ' s/^/ - /' ) "
57+ # If there are missing files, prepare error messages
58+ html_message=" "
59+ asciidoc_message=" "
60+
61+ if [ ${# missing_in_html[@]} -ne 0 ]; then
62+ html_message=" The following files are missing from the HTML directory:\n$( printf " %s\n" " ${missing_in_html[@]} " | sed ' s/^/ - /' ) "
4463 fi
4564
46- if [ -n " $ missing_in_asciidoc" ]; then
47- asciidoc_message=" The following files are missing from the Asciidoc directory:\n$( printf " %s\n" " $missing_in_asciidoc " | sed ' s/^/ - /' ) "
65+ if [ ${ # missing_in_asciidoc[@]} -ne 0 ]; then
66+ asciidoc_message=" The following files are missing from the Asciidoc directory:\n$( printf " %s\n" " ${ missing_in_asciidoc[@]} " | sed ' s/^/ - /' ) "
4867 fi
4968
69+ # If running in a CI environment, print a formatted warning message
5070 if [ -z " $CI " ] || [ -z " $GITHUB_ACTION " ]; then
5171 echo -e " HTML and Asciidoc templates do not match.\n$html_message \n$asciidoc_message "
5272 else
5373 final_message=$( echo -e " $html_message . $asciidoc_message " | tr -d ' \n' )
5474 echo -e " ::warning title=HTML and Asciidoc templates do not match::$final_message "
5575 fi
56- fi
76+ fi
0 commit comments