-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_sources
executable file
·187 lines (172 loc) · 5.33 KB
/
generate_sources
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2021 Robin Vobruba <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# See the output of "$0 -h" for more details.
#
# Dependencies:
# * sed
# Exit immediately on each error and unset variable;
# see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
#set -Eeuo pipefail
set -Eeu
script_dir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
# shellcheck source=./_shared_git
source "$script_dir/_shared_git"
# initial default values
APP_NAME="PCB Enricher"
python_replacer_script="$script_dir/kicad_replace_project_variables.py"
function print_help() {
script_name="$(basename "$0")"
echo "$APP_NAME - Replaces template values, generating a KiCad PCB file,"
echo "with a KiCad PCB file as input."
echo "The template keys have to match the whole content of a Text field, exactly."
echo
echo "Usage:"
echo " $script_name [OPTIONS] <local-repo-dir>"
echo "Options:"
echo " -h, --help Show this help message"
echo " -r, --repo-url <string> Public project repo URL"
echo " -n, --name <string> Project name (prefferably without spaces)"
echo " -v, --version <string> Project version (prefferably without spaces)"
echo " --version-date <string> Date at which this version of the project was committed/released"
echo " --build-date <string> Date at which the currently being-made build of the project is made"
echo " This shold basically always be left on the default,"
echo " which is the current date."
echo "Examples:"
echo " $0"
echo " $0 -n my-proj -v 1.2.0"
}
# read command-line args
POSITIONAL=()
while [[ $# -gt 0 ]]
do
arg="$1"
shift # $2 -> $1, $3 -> $2, ...
case "$arg" in
-h|--help)
print_help
exit 0
;;
-r|--repo-url)
project_repo="$1"
shift # past argument
;;
-n|--name)
project_name="$1"
shift # past argument
;;
-v|--version)
project_version="$1"
shift # past argument
;;
--version-date)
project_version_date="$1"
shift # past argument
;;
--build-date)
project_build_date="$1"
shift # past argument
;;
*) # non-/unknown option
POSITIONAL+=("$arg") # save it in an array for later
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
root_dir="${1:-$(pwd)}"
root_dir_abs="$(cd "$root_dir"; pwd)"
build_root="${root_dir}/build"
gen_src_root="${build_root}/gen-src"
vcs_branch="$(git -C "$root_dir_abs" rev-parse --abbrev-ref HEAD)"
vcs_remote_tracking_branch="$(_git_remote_tracking_branch "$root_dir" "$vcs_branch")"
vcs_remote="${vcs_remote_tracking_branch%/*}"
project_repo="${project_repo:-$( (git -C "$root_dir_abs" remote -v || echo "") \
| grep \
-e '^'"$vcs_remote"'[[:space:]].* (fetch)$' \
| sed \
-e 's|'"$vcs_remote"'.||' -e 's|git@|https://|' \
-e 's|com:|com/|' \
-e 's| (fetch)$||' \
-e 's|\.git$||')}"
project_name="${project_name:-$(cd "$root_dir"; basename "$(pwd)")}"
project_version="${project_version:-$("${script_dir}/git-version" -e "$root_dir")}"
project_version_date="${project_version_date:-$(git -C "$root_dir_abs" show -s --format=%ci HEAD | sed -e 's/ .*//')}"
project_build_date="${project_build_date:-$(date +'%Y-%m-%d')}"
if [ -z "$project_repo" ]
then
>&2 echo "WARNING: No project repo given!"
print_help
exit 1
fi
if [ -z "$project_name" ]
then
>&2 echo "WARNING: No project name given!"
print_help
exit 1
fi
if [ -z "$project_version" ]
then
>&2 echo "WARNING: No project version given!"
print_help
exit 1
fi
if [[ $project_version = *-dirty ]] || [[ $project_version = *-dirty-* ]]
then
>&2 echo "WARNING: Dirty project version! (you have uncommitted changes in your project)"
print_help
exit 1
fi
if [ -z "$project_version_date" ]
then
>&2 echo "WARNING: No project version date given!"
print_help
exit 1
fi
mkdir -p "${gen_src_root}"
printf "Generating final PCB sources ...\n"
printf "\twith common values:\n"
printf "\t%-24s = '%s'\n" "PROJECT_REPO_URL" "$project_repo"
printf "\t%-24s = '%s'\n" "PROJECT_NAME" "$project_name"
printf "\t%-24s = '%s'\n" "PROJECT_VERSION" "$project_version"
printf "\t%-24s = '%s'\n" "PROJECT_VERSION_DATE" "$project_version_date"
printf "\t%-24s = '%s'\n" "PROJECT_BUILD_DATE" "$project_build_date"
echo
# Recursive KiCad PCB file search
# (with wall-clock time meassurement)
# (all tracked and untracked, un(git)ignored files
#for pcb in {**,.}/*.kicad_pcb
time \
( git -C "$root_dir_abs" status --short \
| grep '^?' \
| cut -d\ -f2- \
&& git -C "$root_dir_abs" ls-files ) \
| sort -u \
| ( xargs -d '\n' -- stat -c%n 2>/dev/null ||: ) \
| grep "\.kicad_pcb\$" \
| while read -r pcb
do
pcb="${pcb#\.\/}"
# Create generated file-name
pcb_gen="${gen_src_root}/$pcb"
mkdir -p "$(dirname "$pcb_gen")"
source_file_path="$pcb"
# Replace place-holder text parts with their respective values
# and store inside a separate file.
echo " '$pcb' -> '${pcb_gen#"${root_dir}/"}' ..."
printf "\twith additional values:\n"
printf "\t%-24s = '%s'\n" "SOURCE_FILE_PATH" "$source_file_path"
python3 \
"$python_replacer_script" \
"$pcb" \
"$pcb_gen" \
"PROJECT_REPO_URL=$project_repo" \
"PROJECT_NAME=$project_name" \
"PROJECT_VERSION=$project_version" \
"PROJECT_VERSION_DATE=$project_version_date" \
"PROJECT_BUILD_DATE=$project_build_date" \
"SOURCE_FILE_PATH=$source_file_path" \
--verbose
done
echo "done."