-
Notifications
You must be signed in to change notification settings - Fork 39
/
init.sh
executable file
·171 lines (147 loc) · 7.77 KB
/
init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
set -euo pipefail
# Check if the script is running in the expected root directory
if [ ! -e ".git" ]; then
echo "ERROR: This script must be run from the root directory of the repository."
exit 1
fi
# Check if an installation directory argument is supplied; if not, prompt for it
install_directory="${1:-}"
if [ -z "$install_directory" ]; then
read -rp "Enter the installation directory (e.g., '.' or 'docs'): " install_directory
fi
# Check if md or rst is preferred for the documentation being built
promptForFileChoice() {
while true; do
read -p "Will you be using the 'rst' or 'md' file format for the documentation (default: 'rst'): " filetype_choice
filetype_choice=${filetype_choice:-rst}
if [ "$filetype_choice" = "md" ] || [ "$filetype_choice" = "rst" ]; then
file_type="$filetype_choice"
echo "Setting filetype to: $file_type"
break
else
echo "Invalid input. Please enter either 'md' or 'rst':"
fi
done
}
# If default variable for filetype choice is defined in CI then don't prompt user for choice
if [ -z "${default_filetype_choice:-}" ]; then
promptForFileChoice
else
file_type="$default_filetype_choice"
echo "Using predefined filetype choice: $file_type"
fi
# Normalise the install_directory path
install_directory=$(realpath -m --relative-to="$(pwd)" "$install_directory")
echo "Installing at $install_directory..."
# Come up with a unique temporary directory name based on the current timestamp
temp_directory="temp-starter-pack-$(date +%Y%m%d%H%M%S)"
# Clone the starter pack repository into the temporary directory and de-git it
echo "Cloning the starter pack repository..."
git clone --depth 1 -b main --single-branch https://github.com/canonical/starter-pack "$temp_directory"
rm -rf "$temp_directory/.git"
# Update workflow and documentation files based on the installation directory
echo "Updating working directory in workflow files..."
sed -i "s|working-directory:\s*'\.'|working-directory: '$install_directory'|g" "$temp_directory/sp-files/.github/workflows"/*
echo "Updating .readthedocs.yaml configuration..."
sed -i "s|configuration:\s*sp-docs/conf\.py|configuration: $install_directory/conf.py|g" "$temp_directory/sp-files/.readthedocs.yaml"
sed -i "s|requirements:\s*sp-docs/\.sphinx/requirements\.txt|requirements: $install_directory/.sphinx/requirements.txt|g" "$temp_directory/sp-files/.readthedocs.yaml"
echo "Updating contribution guide..."
sed -i "s|DOCDIR|$install_directory|g" "$temp_directory/sp-files/contributing.rst"
[ "$install_directory" != "." ] && sed -i "s/Makefile.sp/$install_directory\/Makefile.sp/" "$temp_directory/sp-files/.github/workflows/sphinx-python-dependency-build-checks.yml"
[ "$install_directory" != "." ] && sed -i "s/.sphinx\/.markdownlint.json/$install_directory\/.sphinx\.markdownlint.json/" "$temp_directory/sp-files/.github/workflows/markdown-style-checks.yml"
# Update the GitHub folder path in the configuration file
echo "Updating conf.py configuration..."
github_folder="/$install_directory/"
[ "$install_directory" == "." ] && github_folder="/"
sed -i "s|'github_folder':\s*'/sp-docs/'|'github_folder': '$github_folder'|g" "$temp_directory/sp-files/conf.py"
# Tell that the directory's about to be created if it doesn't exist
if [ ! -d "$install_directory" ]; then
echo "Creating the installation directory: $install_directory"
mkdir -p "$install_directory"
fi
# Check if .gitignore exists in the destination directory
# If it does, append the contents of the source .gitignore to the destination
if [ -f "$install_directory/.gitignore" ]; then
echo "ACTION REQUIRED: .gitignore already exists in the destination directory."
read -p "Do you want to append the list of ignored files for Sphinx docs to the existing .gitignore? Enter 'n' to skip. (y/n): " confirm
if [ "$confirm" = "y" ]; then
echo "Appending contents to the existing .gitignore..."
cat "$temp_directory/sp-files/.gitignore" >>"$install_directory/.gitignore"
else
echo "Operation skipped by the user. Add the .gitignore rules for the Sphinx docs to your .gitignore file manually."
fi
rm "$temp_directory/sp-files/.gitignore"
fi
# Check if Makefile exists in the destination directory
if [ -f "$install_directory/Makefile" ]; then
echo "ACTION REQUIRED: Makefile already exists in the destination directory. Check the contents before running the targets!"
read -p "Do you want to add the Sphinx docs targets into the Makefile? The existing file will be saved a backup file. Enter 'n' to skip. (y/n): " confirm
if [ "$confirm" = "y" ]; then
# Create a copy of the existing Makefile as backup
existing_makefile="$install_directory/Makefile"
backup_makefile="$install_directory/Makefile.backup.$(date +%Y%m%d%H%M%S)"
echo "Creating a backup: $backup_makefile"
cp "$existing_makefile" "$backup_makefile"
echo "Appending Sphinx docs targets to the existing Makefile..."
echo "" >>"$existing_makefile" # Add a new line before appending the contents
cat "$temp_directory/sp-files/Makefile" >>"$existing_makefile"
else
echo "Operation skipped by the user. Add the Makefile targets for Sphinx docs manually."
fi
rm "$temp_directory/sp-files/Makefile"
fi
# Copy the rest of the starter pack repository to the installation directory
echo "Copying contents to the installation directory..."
cp -R "$temp_directory"/sp-files/* "$temp_directory"/sp-files/.??* "$install_directory"
# Delete files not of preferred filetype in the installation directory, rst preferred default
# No wildcard delete to avoid data loss if user Git-inits in dir with pre-existing files
if [ "$file_type" = 'md' ]; then
echo "Deleting .rst files..."
rm "$install_directory"/contributing.rst
rm "$install_directory"/doc-cheat-sheet.rst
rm "$install_directory"/index.rst
for file in "$install_directory"/*-myst.md; do
mv "$file" "${file/-myst.md/.md}"
done
else
echo "Deleting .md files..."
rm "$install_directory"/contributing-myst.md
rm "$install_directory"/doc-cheat-sheet-myst.md
rm "$install_directory"/index-myst.md
fi
# Ensure GitHub workflows and woke config are placed in the repo root
# if installing in a non-root directory
if [ "$install_directory" != "." ]; then
echo "Handling GitHub workflow files and .wokeignore configuration..."
mkdir -p .github/workflows
for file in "$install_directory/.github/workflows/"*; do
[ -f "$file" ] || continue
basefile=$(basename "$file")
if [ ! -f .github/workflows/"$basefile" ]; then
mv "$file" .github/workflows/
else
echo "ACTION REQUIRED: GitHub workflow '$basefile' already exists and was not overwritten."
fi
done
rmdir -p --ignore-fail-on-non-empty "$install_directory/.github/workflows"
if [ -f "$install_directory/.wokeignore" ]; then
if [ -f .wokeignore ]; then
echo "ACTION REQUIRED: A .wokeignore file already exists in the root directory."
read -p "Do you want to append the contents of $install_directory/.wokeignore to the existing .wokeignore? Enter 'n' to skip. (y/n): " confirm
if [ "$confirm" = "y" ]; then
echo "Appending contents to the existing .wokeignore..."
cat "$install_directory/.wokeignore" >>.wokeignore
rm "$install_directory/.wokeignore"
else
echo "Operation skipped by the user. Add the rules from $install_directory/.wokeignore to your .wokeignore file manually."
fi
else
ln -s "$install_directory/.wokeignore" .wokeignore
fi
fi
fi
# Clean up the temporary directory
echo "Cleaning up temporary files..."
rm -rf "$temp_directory"
echo "Setup successfully completed!"