-
-
Notifications
You must be signed in to change notification settings - Fork 118
245 lines (201 loc) · 8.57 KB
/
template-janitor.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
# This workflow should cleanup everything unneeded from the template project
name: Template Janitor
on:
pull_request:
release:
types: [published]
push:
branches:
- main
- develop
permissions:
contents: write
env:
TEMPLATES_PATH: ".github/template"
jobs:
template-cleanup:
name: Cleanup after create
runs-on: ubuntu-latest
strategy:
matrix:
compiler:
- gcc-11
generator:
- "Unix Makefiles"
build_type:
- Debug
developer_mode:
- OFF
steps:
- uses: actions/checkout@v3
- name: Setup Cache
uses: ./.github/actions/setup_cache
with:
compiler: ${{ matrix.compiler }}
build_type: ${{ matrix.build_type }}
developer_mode: ${{ matrix.developer_mode }}
generator: ${{ matrix.generator }}
- name: Get organization and project name
run: |
echo "NEW_ORG=${{ github.repository_owner }}" >> $GITHUB_ENV
echo "NEW_PROJECT=${{ github.event.repository.name }}" >> $GITHUB_ENV
echo "NEW_URL=${{ github.repositoryUrl }}" >> $GITHUB_ENV
- uses: octokit/[email protected]
id: get_repo_meta
with:
route: GET /repos/{owner}/{repo}
owner: ${{ env.NEW_ORG }}
repo: ${{ env.NEW_PROJECT }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Use testing variables if still a template
if: fromJson(steps.get_repo_meta.outputs.data).is_template == true
run: |
# This name is unsafe because it is not a valid C++ identifier
echo "NEW_PROJECT=my-unsafe.project" >> $GITHUB_ENV
- name: Add safe replacement variable versions
run: |
# hyphens and dots in c++ identifiers are forbidden. Use underscores instead.
NEW_SAFE_PROJECT=$(echo ${{ env.NEW_PROJECT }} | sed "s/-/_/g" | sed "s/\./_/g" )
echo "NEW_SAFE_PROJECT=$NEW_SAFE_PROJECT" >> $GITHUB_ENV
# Rename all cpp_starter_project occurrences to current repository and remove this workflow
- name: Insert new org and project
run: |
# rename the CMake project to match the github project
find src include test fuzz_test cmake -type f -exec sed -i "s/myproject/${{ env.NEW_SAFE_PROJECT }}/gi" .github/constants.env CMakeLists.txt Dependencies.cmake ProjectOptions.cmake .github/workflows/ci.yml .github/workflows/codeql-analysis.yml configured_files/config.hpp.in {} +
# Update URL placeholders for project
sed -i "s|%%myurl%%|${{ fromJson(steps.get_repo_meta.outputs.data).html_url }}|gi" CMakeLists.txt
# fill in placeholders of readme and move it into place
sed -i "s/%%myorg%%/${{ env.NEW_ORG }}/g" ${{ env.TEMPLATES_PATH }}/README.md
sed -i "s/%%myproject%%/${{ env.NEW_PROJECT }}/g" ${{ env.TEMPLATES_PATH }}/README.md
sed -i "s|%%description%%|${{ fromJson(steps.get_repo_meta.outputs.data).description }}|g" ${{ env.TEMPLATES_PATH }}/README.md
mv include/myproject include/${{ env.NEW_SAFE_PROJECT }}
cp ${{ env.TEMPLATES_PATH }}/README.md README.md
- name: Print diff after replacement
run: |
# Exclude the README as that is checked separately!
git diff ':!README.md'
# following should not have any diffs
diff ${{ env.TEMPLATES_PATH }}/README.md README.md
- name: Remove unwanted files
run: |
# No tests needed as this will fail if any file from the list is missing/misspelled
xargs rm -r < ${{ env.TEMPLATES_PATH }}/removal-list
- name: Clean up before commit and push
run: |
rm -r ${{ env.TEMPLATES_PATH }}
# Can we get that from a variable?
# Remove this workflow as it has fulfilled its purpose
rm .github/workflows/template-janitor.yml
- name: Setup Cpp
uses: aminya/setup-cpp@v1
with:
compiler: ${{ matrix.compiler }}
cmake: true
ninja: false
vcpkg: false
ccache: false
clangtidy: false
cppcheck: false
gcovr: false
opencppcoverage: false
- name: Project Name
uses: cardinalby/export-env-action@v2
with:
envFile: '.github/constants.env'
- name: Test simple configuration to make sure nothing broke
run: |
cmake -S . -B ./build -G "${{ matrix.generator }}" -DCMAKE_BUILD_TYPE:STRING=${{ matrix.build_type }} -D${{ env.NEW_SAFE_PROJECT }}_PACKAGING_MAINTAINER_MODE:BOOL=ON
# Build it because we may have broken something in the cpp/hpp files
cmake --build build
- uses: EndBug/add-and-commit@v9
# only commit and push if we are not a template project anymore!
if: fromJson(steps.get_repo_meta.outputs.data).is_template != true
with:
add: -A
author_name: Template Janitor
author_email: [email protected]
message: 'Cleanup template and initialize repository'
pathspec_error_handling: exitImmediately
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
template-rename:
name: Renames template when a new name is detected
runs-on: ubuntu-latest
strategy:
matrix:
compiler:
- gcc-11
generator:
- "Unix Makefiles"
build_type:
- Debug
developer_mode:
- OFF
steps:
- uses: actions/checkout@v3
- name: Setup Cache
uses: ./.github/actions/setup_cache
with:
compiler: ${{ matrix.compiler }}
build_type: ${{ matrix.build_type }}
developer_mode: ${{ matrix.developer_mode }}
generator: ${{ matrix.generator }}
- name: Get organization and project name
run: |
echo "TEST_RUN=false" >> $GITHUB_ENV
echo "NEW_ORG=${{ github.repository_owner }}" >> $GITHUB_ENV
echo "NEW_PROJECT=${{ github.event.repository.name }}" >> $GITHUB_ENV
echo "NEW_REPOSITORY=${{ github.repository }}" >> $GITHUB_ENV
echo "TEMPLATE_NAME=`cat ${{ env.TEMPLATES_PATH }}/template_name`" >> $GITHUB_ENV
echo "TEMPLATE_REPOSITORY=`cat ${{ env.TEMPLATES_PATH }}/template_repository`" >> $GITHUB_ENV
- uses: octokit/[email protected]
id: get_repo_meta
with:
route: GET /repos/{owner}/{repo}
owner: ${{ env.NEW_ORG }}
repo: ${{ env.NEW_PROJECT }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Setup fake test org/project names if project didn't change
if: env.TEMPLATE_NAME == env.NEW_PROJECT
run: |
echo "TEST_RUN=true" >> $GITHUB_ENV
echo "NEW_ORG=${{ github.repository_owner }}" >> $GITHUB_ENV
echo "NEW_PROJECT=TEST_PROJECT" >> $GITHUB_ENV
echo "NEW_REPOSITORY=TEST_REPOSITORY" >> $GITHUB_ENV
# Rename all cpp_starter_project occurrences to current repository and remove this workflow
- name: Update repository to match new template information
run: |
# Update the README and template files to match the new org / repository names
sed -i "s|${{ env.TEMPLATE_REPOSITORY }}|${{ env.NEW_REPOSITORY }}|g" README.md ${{ env.TEMPLATES_PATH }}/template_repository
sed -i "s|${{ env.TEMPLATE_NAME }}|${{ env.NEW_PROJECT }}|g" README.md ${{ env.TEMPLATES_PATH }}/template_name
- name: Print diff after template name replacement
run: |
git diff
- name: Setup Cpp
uses: aminya/setup-cpp@v1
with:
compiler: gcc
cmake: true
ninja: false
vcpkg: false
ccache: false
clangtidy: false
cppcheck: false
gcovr: false
opencppcoverage: false
- name: Test simple configuration to make sure nothing broke (default compiler,cmake,developer_mode OFF)
run: |
cmake -S . -B ./build -G "${{ matrix.generator }}" -DCMAKE_BUILD_TYPE:STRING=${{ matrix.build_type }} -D${{ env.PROJECT_NAME }}_PACKAGING_MAINTAINER_MODE:BOOL=ON
- uses: EndBug/add-and-commit@v9
# only commit and push if we are a template and project name has changed
if: fromJson(steps.get_repo_meta.outputs.data).is_template == true && env.TEST_RUN == 'false'
with:
add: -A
author_name: Template Janitor
author_email: [email protected]
message: 'Change Template Name'
pathspec_error_handling: exitImmediately
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}