-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontinuous-integration.bash
More file actions
261 lines (206 loc) · 6.25 KB
/
continuous-integration.bash
File metadata and controls
261 lines (206 loc) · 6.25 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/bash
source definitions.bash > /dev/null
function do_restore()
{
info "Restore"
[[ $dry_run == true ]] && return
dotnet restore -check --verbosity minimal
}
function do_build()
{
declare -r configuration=$1
info "Build '$1' configuration"
[[ $dry_run == true ]] && return
dotnet build -check --verbosity minimal --configuration "$configuration" --no-restore
}
function do_test()
{
declare -r configuration=$1
info "Test '$1' configuration"
[[ $dry_run == true ]] && return
dotnet test -check --verbosity normal --configuration "$configuration" --no-build
}
function do_publish()
{
declare -r configuration=$1
declare -r project_name=$2
declare -r destination=$3
declare -r package_pathname=$project_name/bin/$configuration/*.nupkg
info "Publish '$configuration' configuration to '$destination' (project '$project_name')"
[[ $dry_run == true ]] && return
# PEARL: dotnet nuget push gives the developer the convenience of specifying the package to push using a wildcard,
# but if the wildcard matches more than one file, then it will sabotage the developer by failing with a
# misleading error message that says "File does not exist" instead of "More than one file exists". For this
# reason, we have to remove all files matching the wildcard before creating the package.
remove_if_exists "$package_pathname"
dotnet pack -check --verbosity normal --configuration "$configuration" --no-build
case "$destination" in
"github-packages")
dotnet nuget push "$package_pathname" --source https://nuget.pkg.github.com/MikeNakis/index.json --api-key "$github_packages_nuget_api_key"
;;
"nuget-org")
dotnet nuget push "$package_pathname" --source https://api.nuget.org/v3/index.json --api-key "$nuget_org_nuget_api_key"
;;
*)
error "Invalid publish destination: '$destination'"
exit 1
esac
}
function run()
{
run_this_script_in_its_directory
declare project_name=""
declare github_packages_nuget_api_key=""
declare nuget_org_nuget_api_key=""
declare dry_run=false
while [ $# -gt 0 ]; do
case "$1" in
--project-name=*)
project_name="${1#*=}"
;;
--github-packages-nuget-api-key=*)
github_packages_nuget_api_key="${1#*=}"
;;
--nuget-org-nuget-api-key=*)
nuget_org_nuget_api_key="${1#*=}"
;;
--dry-run)
dry_run=true
;;
*)
error "Invalid argument: '$1'"
exit 1
esac
shift
done
if [ -z "$project_name" ]; then
project_name=$(basename $PWD)
fi
if [ -z "$github_packages_nuget_api_key" ]; then
error "Missing argument: '--github-packages-nuget-api-key'"
exit 1
fi
if [ -z "$nuget_org_nuget_api_key" ]; then
error "Missing argument: '--nuget-org-nuget-api-key'"
exit 1
fi
if [ ! -f *.sln? ]; then
error "Current directory is not a solution directory."
exit 1
fi
declare -r project_file=$project_name/$project_name.csproj
if [ ! -f "$project_file" ]; then
error "Project file not found: '$project_file'"
exit 1
fi
declare -r -l output_type=$(get_output_type "$project_file")
info "Output Type: $output_type"
declare -r -l pack_as_tool=$(get_pack_as_tool "$project_file")
info "Pack as tool: $pack_as_tool"
declare -r configurations=$(get_configurations "$project_file")
info "Configurations: $configurations"
declare -r publishing_destination=$(get_publishing_destination)
info "Publishing destination: $publishing_destination"
if [[ "$output_type" == "exe" && $configurations == *Develop* ]]; then
warn "This project builds an executable but has a 'Develop' configuration!?"
fi
# The logic:
# Do a dotnet restore.
# Build and test the 'Debug' configuration, or the 'Optimized' configuration if one has been defined.
# If we are publishing, then:
# If this is a library, or an exe packaged as tool, then:
# Build and publish the 'Develop' configuration, if defined.
# Build and publish the 'Release' configuration, if defined.
do_restore
if [[ "$configurations" == *Optimized* ]]; then
do_build Optimized
do_test Optimized
elif [[ "$configurations" == *Debug* ]]; then
do_build Debug
do_test Debug
fi
if [[ "$publishing_destination" != "none" ]]; then
if [[ "$output_type" == "library" || "$output_type" == "exe" && "$pack_as_tool" == "true" ]]; then
if [[ "$configurations" == *Develop* ]]; then
do_build Develop
do_publish Develop "$project_name" "$publishing_destination"
fi
if [[ "$configurations" == *Release* ]]; then
do_build Release
do_publish Release "$project_name" "$publishing_destination"
fi
else
error "This project cannot be published"
exit 1
fi
fi
}
function get_output_type
{
declare -r project_file=$1
# PEARL: In an MSBuild project file, a missing OutputType property defaults to 'Library'; however, a present but
# empty OutputType property seems to default to 'Exe'! (WTF?) Note: we are not accounting for a present but empty
# OutputType property here.
declare -l output_type=$(get_xml_value "$project_file" "OutputType" "library")
case "$output_type" in
"library")
;;
"exe")
;;
*)
error "Invalid output type: '$output_type'"
exit 1
esac
printf "$output_type"
}
function get_configurations
{
declare -r project_file=$1
declare -r configurations=$(get_xml_value "$project_file" "Configurations")
declare t="$configurations"
t=${t/Debug}
t=${t/Optimized}
t=${t/Develop}
t=${t/Release}
t=${t//;}
if [[ "$t" != "" ]]; then
error "Invalid configuration: '$t'."
exit 1
fi
printf "$configurations"
}
function get_pack_as_tool
{
declare -r project_file=$1
declare -l pack_as_tool=$(get_xml_value "$project_file" "PackAsTool" "false")
case "$pack_as_tool" in
"true")
;;
"false")
;;
*)
error "Invalid 'PackAsTool' value: '$pack_as_tool'"
exit 1
esac
printf "$pack_as_tool"
}
function get_publishing_destination
{
declare -r last_commit_message=$(git_get_last_commit_message)
declare -r release_commit_message_prefix="Release of version "
if [[ $last_commit_message == $release_commit_message_prefix* ]]; then
declare -r version=${last_commit_message#"$release_commit_message_prefix"}
declare -i major
declare -i minor
declare -i patch
IFS=. read -r major minor patch <<< "$version"
if [[ $patch == 0 ]]; then
printf "nuget-org"
else
printf "github-packages"
fi
else
printf "none"
fi
}
run $@