Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 486f183

Browse files
committed
add comments explaining the perl script
1 parent 9706e9c commit 486f183

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

.github/workflows/copy-workflow.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,20 @@ jobs:
111111
config=$(echo -e "$config" | jq -c '.')
112112
github="{\"default_branch\":\"${{ env.DEFAULT_BRANCH }}\"}"
113113
context="{\"config\":$config,\"github\":$github}"
114+
# matches trimmed string between template context delimiters
114115
regexp='\$\{\{\{\s*(.*?)\s*\}\}\}'
116+
# replacement is a perl script that:
117+
# 1. prepends . to the matched string to create a filter
118+
# 2. escapes single quotes by replacing ' with '"'"' in filter
119+
# 3. prints context escaped in the same manner
120+
# 4. applies jq with filter to the stdout
115121
replacement="\$filter = \".\$1\"; \$filter =~ s/'/'\"'\"'/g; \`echo '${context//\'/\'\"\'\"\'}' | jq -jc '\$filter'\`"
116122
for f in $(jq -r '.[]' <<< ${{ toJson(env.FILES) }}); do
117123
echo -e "\nProcessing $f."
118124
# add DO NOT EDIT header
119125
tmp=$(mktemp)
120126
cat $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/header.yml $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/$f > $tmp
121-
# replace template expressions with values from the JSON context object
127+
# replace template contexts with values from the JSON context object
122128
perl -pi -e "s#$regexp#$replacement#ge" $tmp
123129
mv $tmp $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/$f
124130
# create commit, if necessary

templates/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This directory contains template files that can be expanded and copied over to the configured repositories.
44

5+
## Header
6+
7+
The [header](header.yml) is prepended to all the files before they are copied to the target repositories.
8+
59
## Contexts
610

711
You can access context information during template expansion(before copy is performed).
@@ -38,7 +42,7 @@ The `config` context is the configuration object because of which the file is be
3842
The `github` context contains information about the target repository the file is being copied to.
3943

4044
| Property name | Type | Always present | Description |
41-
| --- | --- | --- |
45+
| --- | --- | --- | --- |
4246
| `github` | `object` | `true` | The top-level context. |
4347
| `github.default_branch` | `string` | `true` | The name of the default branch of the target repository. |
4448

@@ -53,7 +57,7 @@ The `github` context contains information about the target repository the file i
5357
"greeting": "Hello"
5458
},
5559
"files": [],
56-
"extra_files": [".github/workflows/example.yml"]
60+
"extra_files": [".github/workflows/example.yml"],
5761
"target": "protocol/.github-test-target"
5862
},
5963
"github": {

0 commit comments

Comments
 (0)