-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
411 lines (398 loc) · 14.4 KB
/
action.yml
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
inputs:
id:
description: The NuPkg ID (e.g. StirlingLabs.Utilities.Magic)
type: string
required: true
version:
description: Package version number
type: string
required: true
title:
description: Display title of the NuPkg, visible on nuget.org
type: string
description:
description: Description of the NuPkg, indexed and visible on nuget.org
type: string
authors:
description: Metadata authors of the NuPkg
type: string
default: Stirling Labs Team
homepage:
description: Link to information about the project
type: string
readme:
description: Readme file location
type: string
default: README.md
icon:
description: Icon file location (64x64 PNG)
type: string
default: docs/SL.png
tags:
description: Tags that help describe the project or make it more findable
type: string
license:
description: SPDX License code (see https://spdx.org/licenses/)
type: string
gitUrl:
description: The URL of the git repo for the source used to build the binaries in this package
type: string
gitBranch:
description: The git branch of the source used to build the binaries in this package
type: string
gitCommit:
description: The git commit of the source used to build the binaries in this package
type: string
fileSources:
description: Path of files that should be included in the NuPkg including filename, in space-separated format (wildcards ok) e.g. 'file1 "file 2"'.
type: string
required: true
fileTargets:
description: If the path of the above files is not as it should be placed in the NuPkg, provide them here, in space-separated format.
type: string
targetFrameworks:
description: Target frameworks for the NuPkg, in semi-colon-separated format (e.g. netstandard2.0;net6.0)
type: string
outputs:
filename:
description: "Filename of the created NuSpec"
value: ${{ steps.create.outputs.filename }}
runs:
using: 'composite'
steps:
- name: Debugging
if: false
shell: bash
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: | #TODO
# - Add "Copyright" to NuSpec
# - License should be proprietary on private repos
echo "$GITHUB_CONTEXT"
- name: Repo Selection
id: repo
shell: bash
env:
inputGitUrl: ${{ inputs.gitUrl }}
repository: ${{ github.repository }}
run: |
if [[ "$inputGitUrl" != "" ]] ; then
if [[ "$inputGitUrl" != *"github"* ]] ; then
echo "This Action only supports GitHub repos." && exit 1
fi
re="^((https?|ssh|git|ftps?):\/\/)?(([^\/@]+)@)?([^\/:]+)[\/:]([^\/:]+)\/(.+).git\/?$"
if [[ $inputGitUrl =~ $re ]]; then
# protocol=${BASH_REMATCH[1]} && separator=${BASH_REMATCH[2]} && hostname=${BASH_REMATCH[3]}
owner=${BASH_REMATCH[4]}
project=${BASH_REMATCH[5]}
repository="${owner}/${project}"
echo "gitUrl=$inputGitUrl" >> $GITHUB_OUTPUT
else
echo "This git URL is invalid: $inputGitUrl" && exit 1
fi
else
owner="${repository%/*}"
project="${repository#*/}"
fi
echo "owner=$owner" >> $GITHUB_OUTPUT
echo "project=$project" >> $GITHUB_OUTPUT
echo "repository=$repository" >> $GITHUB_OUTPUT
- name: Collect Data
id: data
shell: bash
env:
repository: ${{ steps.repo.outputs.repository }}
description: ${{ github.event.repository.description }}
homepage: ${{ github.event.repository.homepage }}
htmlUrl: ${{ github.event.repository.html_url }}
topics: ${{ toJSON(github.event.repository.topics) }}
license: ${{ github.event.repository.license.spdx_id }}
gitUrl: ${{ github.event.repository.git_url }}
defaultBranch: ${{ github.event.repository.default_branch }}
run: | #TODO
# - Better documentation and/or clearer code
# - e.g. homepage comes from‼️file proj then repo settings then repo itself
# init
json=""
api="https://api.github.com/repos/${repository}"
echo "api=${api}" >> $GITHUB_OUTPUT
# Define function
updateValue() {
[[ "$1" != "" ]] && _var="$1" || exit 1
[[ "$2" != "" ]] && _term="$2" || _term="$1"
# if the value is an empty string or empty array
if [[ "${!_var}" =~ "^$|\[[[:space:]]*\]" ]] ; then
[[ "$json" == "" ]] && json=$(curl -s $api);
read "${_var}" <<< $(jq -r ".${_term}" <<< "$json")
fi
echo "Setting ${_var} = ${!_var}"
echo "${_var}=${!_var}" >> $GITHUB_OUTPUT
}
# Update values with
updateValue "description"
updateValue "homepage"
updateValue "htmlUrl" "html_url"
updateValue "license" "license.spdx_id"
updateValue "gitUrl" "git_url"
updateValue "defaultBranch" "default_branch"
# Update Topics
jsonTopics=$(jq -r ".topics" <<< "$json")
bothTopicArrays="$topics $jsonTopics"
topics=$(jq -s -c 'add | unique' <<< $bothTopicArrays)
echo "Topics are $topics"
echo "topics=$topics" >> $GITHUB_OUTPUT
- name: Title
id: title
shell: bash
env:
title: ${{ inputs.title }}
githubRepository: ${{ steps.data.outputs.repository }}
version: ${{ inputs.version }}
run: |
if [ "$title" == "" ] ; then
title="${githubRepository#*/} $version"
fi
echo "title=$title" >> $GITHUB_OUTPUT
- name: Description
id: description
shell: bash
env:
description: ${{ inputs.description }}
repoDescription: ${{ steps.data.outputs.description }}
run: |
if [ "$description" == "" ] ; then
description="$repoDescription"
fi
echo "description=$description" >> $GITHUB_OUTPUT
- name: Homepage
id: url
shell: bash
env:
repoHomepage: ${{ steps.data.outputs.homepage }}
repoHtmlUrl: ${{ steps.data.outputs.html_url }}
homepage: ${{ inputs.homepage }}
run: |
echo "homepage=${homepage}."
[[ "$homepage" == "" ]] && homepage="$repoHomepage"
[[ "$homepage" == "" ]] && homepage="$repoHtmlUrl"
[[ "$homepage" == "" ]] && homepage="https://stirlinglabs.com"
echo "homepage=${homepage}."
echo "homepage=$homepage" >> $GITHUB_OUTPUT
- name: ReadMe file
id: readme
shell: bash
env:
readme: ${{ inputs.readme }}
run: |
if [[ -s "$readme" ]] ; then
echo "README exists at $readme"
else
echo "README does not exist at $readme"
exit 1
fi
- name: Icon file
id: icon
shell: bash
env:
icon: ${{ inputs.icon }}
run: |
if [[ -s "$icon" ]] ; then
echo "Icon exists at $icon"
else
echo "Icon does not exist at $icon, getting one"
wget --output-document "$icon" "https://raw.githubusercontent.com/StirlingLabs/CreateNuSpecAction/main/SL.png"
if [[ -s "$icon" ]] ; then
echo "Icon now exists at $icon"
else
echo "Icon still doesn't exist at $icon"
exit 1
fi
fi
- name: Tags
id: tags
shell: bash
env:
tags: ${{ inputs.tags }}
topics: ${{ steps.data.outputs.topics }}
owner: ${{ steps.repo.outputs.owner }}
project: ${{ steps.repo.outputs.project }}
run: |
topicsArray=$(jq -r '.[]' <<< $topics)
if [ "$tags" == "" ] ; then
space=""
else
t="$tags"
space=" "
fi
for topic in ${topicsArray[@]} ; do
t="$t$space$topic"
space=" "
done
t="$t$space$owner $project"
echo "tags=$t" >> $GITHUB_OUTPUT
- name: License
id: license
shell: bash
env:
license: ${{ inputs.license }}
repoLicense: ${{ steps.data.outputs.license }}
run: |
if [ "$license" == "" ] ; then
if [ "$repoLicense" != "" ] ; then
license="$repoLicense"
else
echo "Defaulting to no published license (i.e. proprietary). This will probably fail."
fi
fi
echo "license=$license" >> $GITHUB_OUTPUT
- name: Git Details
id: git
shell: bash
env:
gitUrl: ${{ steps.repo.outputs.gitUrl }}
gitBranch: ${{ inputs.gitBranch }}
gitCommit: ${{ inputs.gitCommit }}
currentRepoUrl: ${{ steps.data.outputs.git_url }}
currentRepoDefaultBranch: ${{ steps.data.outputs.default_branch }}
currentRepoCommit: ${{ github.sha }}
run: |
if [[ "$gitUrl" == "" ]] ; then
gitUrl=$currentRepoUrl
fi
if [[ "$gitBranch" == "" ]] ; then
gitBranch="${GITHUB_REF#refs/heads/}"
echo Branches current $gitBranch default $defaultBranch
if [[ $gitBranch == refs/tags/* ]] ; then
gitBranch=$currentRepoDefaultBranch
fi
fi
if [[ "$gitCommit" == "" ]] ; then
gitCommit=$currentRepoCommit
fi
echo "url=${gitUrl}" >> $GITHUB_OUTPUT
echo "branch=${gitBranch}" >> $GITHUB_OUTPUT
echo "commit=${gitCommit}" >> $GITHUB_OUTPUT
- name: Files
id: files
shell: bash
env:
sources: ${{ inputs.fileSources }}
targets: ${{ inputs.fileTargets }}
run: |
echo "Sources... ${sources}"
echo "Targets... ${targets}"
# Create an array of source files
sourceCount=0
for source in ${sources[@]} ; do
if [[ -d $source ]] ; then
# source is a directory, get the list of individual files from disk
sourceFiles[${sourceCount}]=$(ls -1p ${source})
echo "Adding files in $source... $sourceFiles{[${sourceCount}]}"
elif [[ -f $source ]] ; then
sourceFiles[${sourceCount}]=$source
echo "Adding $source"
else
echo "File in $source does not exist"
exit 1
fi
sourceCount=$((sourceCount+1))
done
# Create a JSON document aligning sources with targets
json="[ "
targetCount=0
for target in ${targets[@]} ; do
fileCount=0
for file in ${sourceFiles[${targetCount}]} ; do
json="${json}${jsonComma}{\"source\": \"${file}\", \"target\": \"${target}\"}"
jsonComma=", "
echo "${targetCount}:${fileCount} ${file} -> ${target}"
fileCount=$((fileCount+1))
done
targetCount=$((targetCount+1))
done
json="${json} ]"
echo "---"
echo $json
echo "---"
echo "json=$json" >> $GITHUB_OUTPUT
- name: Create NuSpec
id: create
shell: bash
env:
id: ${{ inputs.id }}
version: ${{ inputs.version }}
title: ${{ steps.title.outputs.title }}
description: ${{ steps.description.outputs.description }}
authors: ${{ inputs.authors }}
homepage: ${{ steps.url.outputs.homepage }}
readme: ${{ inputs.readme }}
icon: ${{ inputs.icon }}
tags: ${{ steps.tags.outputs.tags }}
license: ${{ steps.license.outputs.license }}
gitUrl: ${{ steps.git.outputs.url }}
branch: ${{ steps.git.outputs.branch }}
commit: ${{ steps.git.outputs.commit }}
files: ${{ steps.files.outputs.json }}
targetFrameworks: ${{ inputs.targetFrameworks }}
run: |
echo "files... ${files}"
sources=$(jq -r ".[].source" <<< $files)
targets=$(jq -r ".[].target" <<< $files)
echo "Sources... ${sources}"
echo "Targets... ${targets}"
t=0
for target in ${targets[@]} ; do
fileTarget[$t]="${target}"
t=$((t+1))
done
text="<?xml version=\"1.0\" encoding=\"utf-8\"?>"
text="${text}\n<package xmlns=\"http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd\">"
text="${text}\n <metadata>"
text="${text}\n <id>${id}</id>"
text="${text}\n <version>${version}</version>"
text="${text}\n <title>${title}</title>"
text="${text}\n <description>${description}</description>"
text="${text}\n <authors>${authors}</authors>"
text="${text}\n <projectUrl>${homepage}</projectUrl>"
text="${text}\n <readme>docs/README.md</readme>"
text="${text}\n <icon>images/icon.png</icon>"
text="${text}\n <tags>$tags</tags>"
text="${text}\n <license type=\"expression\">${license}</license>"
text="${text}\n <requireLicenseAcceptance>true</requireLicenseAcceptance>"
if [ "$gitUrl" != "" ] || [ "$branch" != "" ] || [ "$commit" != "" ] ; then
repoDetail=""
[ "$gitUrl" != "" ] && repoDetail="${repoDetail}url=\"${gitUrl}\" "
[ "$branch" != "" ] && repoDetail="${repoDetail}branch=\"${branch}\" "
[ "$commit" != "" ] && repoDetail="${repoDetail}commit=\"${commit}\" "
text="${text}\n <repository type=\"git\" ${repoDetail}/>"
fi
if [[ "$targetFrameworks" != "" ]] ; then
echo "targetFrameworks... $targetFrameworks"
readarray -td';' frameworks <<<"$targetFrameworks;"
unset 'frameworks[-1]' # remove final entry that was created by the ; we added
# [[ ${#frameworks[@]} -gt 1 ]] && targetFrameworkTag="targetFrameworks" || targetFrameworkTag="targetFramework"
if [[ ${#frameworks[@]} -gt 1 ]] ; then
text="${text}\n <dependencies>"
for framework in ${frameworks[@]} ; do
text="${text}\n <group targetFramework=\"${framework}\" />"
done
text="${text}\n </dependencies>"
fi
fi
text="${text}\n </metadata>"
text="${text}\n <files>"
text="${text}\n <file src=\"${readme}\" target=\"docs/README.md\" />"
text="${text}\n <file src=\"${icon}\" target=\"images/icon.png\" />"
s=0
for source in ${sources[@]} ; do
target="${fileTarget[$s]}"
echo "Adding ${source} -> ${target}"
text="${text}\n <file src=\"${source}\" target=\"${target}\" />"
s=$((s+1))
done
text="${text}\n </files>"
text="${text}\n</package>"
filename=${id}.nuspec
echo -e $text > $filename
cat $filename
echo "filename=${filename}" >> $GITHUB_OUTPUT