|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# THIS SCRIPT IS INTENDED FOR LOCAL GIT CLONE TEST ENVIRONMENTS ONLY. REVIEW THIS SCRIPT THOROUGHLY TO DETERMINE SUITABILITY FOR YOUR TEST ENVIRONMENT. |
| 4 | + |
| 5 | +function intro () |
| 6 | +{ |
| 7 | +echo; echo "########################################################### |
| 8 | +# # |
| 9 | +# THIS SCRIPT IS INTENDED FOR LOCAL GIT CLONE TEST # |
| 10 | +# ENVIRONMENTS ONLY. REVIEW THIS SCRIPT THOROUGHLY TO # |
| 11 | +# DETERMINE SUITABILITY FOR YOUR TEST ENVIRONMENT. # |
| 12 | +# # |
| 13 | +# This script lists which AsciiDoc assemblies in a Git # |
| 14 | +# repository include a particular module. You can use # |
| 15 | +# the output to check that the assembly references at # |
| 16 | +# the top of modules are up to date. The script can be # |
| 17 | +# run for one module or for all modules updated in a Git # |
| 18 | +# commit. # |
| 19 | +# # |
| 20 | +# For the latter, the script assumes that all modules # |
| 21 | +# are stored in a './module' directory and that all # |
| 22 | +# assemblies are outside of that. # |
| 23 | +# # |
| 24 | +###########################################################"; echo |
| 25 | +} |
| 26 | + |
| 27 | +function get_repo () |
| 28 | +{ |
| 29 | +# Request the absolute path of the Git repository: |
| 30 | +read -p "Input the absolute path of your local Git repository (e.g. /home/myusername/github-repos/myrepo). You can use tab to auto-complete: " -e REPODIR |
| 31 | +# Check that the directory exists: |
| 32 | +if [[ -d "${REPODIR}" ]]; then |
| 33 | + GITDIRCHECK="${REPODIR}/.git" |
| 34 | + if [[ -d "${GITDIRCHECK}" ]]; then |
| 35 | + echo; echo "${REPODIR} is a Git repository." |
| 36 | + # Change to the Git repo directory: |
| 37 | + cd ${REPODIR} |
| 38 | + # List the current branch: |
| 39 | + BRANCH=$(git branch --show-current) |
| 40 | + echo; echo "The current branch is ${BRANCH}. You can change branches by exiting this script with ctrl+c, running 'git checkout <branch>' from ${REPODIR} in another terminal window and then running this script again." |
| 41 | + else |
| 42 | + echo; echo "${REPODIR} is not a Git repository." |
| 43 | + exit 0 |
| 44 | + fi |
| 45 | +else |
| 46 | + echo "Directory does not exist." |
| 47 | + exit 0 |
| 48 | +fi |
| 49 | +} |
| 50 | + |
| 51 | +# Clarify if the user wants to run the script for one module or for all modules updated in a Git commit: |
| 52 | +function module_check () |
| 53 | +{ |
| 54 | + SWITCH="off" |
| 55 | + while [ "${SWITCH}" = "off" ]; do |
| 56 | + echo |
| 57 | + echo "Please select an option:" |
| 58 | + echo "" |
| 59 | + echo "1. Run the script for one module" |
| 60 | + echo "2. Run the script for all modules updated in a Git commit" |
| 61 | + echo "" |
| 62 | + echo -e "Please select a number from the list above (or 'q' to quit): \c" |
| 63 | + read MODULESELECT |
| 64 | + echo |
| 65 | + |
| 66 | + case "${MODULESELECT}" in |
| 67 | + 1) one_module; break ;; |
| 68 | + 2) commit_modules; break ;; |
| 69 | + q|Q) exit 0 ;; |
| 70 | + *) echo -e "Answer must be either '1', '2', 'q' or 'Q' (or 'ctrl+c')..."; sleep 3;; |
| 71 | + esac |
| 72 | + done |
| 73 | +} |
| 74 | + |
| 75 | +# Obtain the Git commit ID for your pull request: |
| 76 | +function commit_modules () |
| 77 | +{ |
| 78 | +read -p "Input your local Git commit ID (e.g. b3e939f1365c95a2969ec09eee59664c597275e5). The commit ID can be obtained by running 'git log' from ${REPODIR}: " COMMITID |
| 79 | +} |
| 80 | + |
| 81 | +# Set the commit ID to "None" when the script is to be run against one module only: |
| 82 | +function one_module () |
| 83 | +{ |
| 84 | +COMMITID="None" |
| 85 | +} |
| 86 | + |
| 87 | +function get_adocs () |
| 88 | +{ |
| 89 | +# Get a list of all assembly files in the repo: |
| 90 | +ASSEMBLIES=$(find . -iname "*.adoc" -not -path "./modules/*" -not -path "./_preview/*") |
| 91 | +# Get a list of modules: |
| 92 | +if [[ "${COMMITID}" = "None" ]]; then |
| 93 | + read -p "Input the relative path to your module (e.g. modules/mymodule.adoc). You can use tab to auto-complete: " -e MODULEPATH |
| 94 | + # Remove preceding `./` characters if they exist: |
| 95 | + MODULES=$(echo ${MODULEPATH} | sed s/"^.\/"/" "/g) |
| 96 | + echo ; echo "Listing the assemblies that include ${MODULES}..."; echo |
| 97 | +else |
| 98 | + MODULES=$(git show ${COMMITID} --name-only | grep -is '^modules/') |
| 99 | + echo ; echo "Listing the assemblies that include each module updated in Git commit ${COMMITID}..."; echo |
| 100 | +fi |
| 101 | +} |
| 102 | + |
| 103 | +# List the assemblies in the repo that include each module that is updated in the commit: |
| 104 | +function list_assemblies () |
| 105 | +{ |
| 106 | +for i in ${MODULES} |
| 107 | +do |
| 108 | + echo "###${i}" |
| 109 | + echo "// Module included in the following assemblies:" |
| 110 | + echo "//" |
| 111 | + for j in ${ASSEMBLIES} |
| 112 | + do |
| 113 | + RESULT=$(grep -is ${i} ${j} | grep -is '^include::') |
| 114 | + if [[ -z ${RESULT} ]]; then |
| 115 | + : |
| 116 | + else |
| 117 | + TRUNCJ=$(echo ${j} | sed s/"^.\/"/" "/g) |
| 118 | + echo "// *${TRUNCJ}" |
| 119 | + fi |
| 120 | + done |
| 121 | + echo |
| 122 | +done |
| 123 | + |
| 124 | +if [[ "${COMMITID}" = "None" ]]; then |
| 125 | + echo "You can use this output to check that the assembly references at the top of ${MODULES} are up to date in your local Git repository."; echo |
| 126 | +else |
| 127 | + echo "You can use this output to check that the assembly references at the top of each module file are up to date in your local Git repository."; echo |
| 128 | +fi |
| 129 | +} |
| 130 | + |
| 131 | +# Main: |
| 132 | +intro |
| 133 | +get_repo |
| 134 | +module_check |
| 135 | +get_adocs |
| 136 | +list_assemblies |
0 commit comments