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

Commit

Permalink
add comments explaining the perl script
Browse files Browse the repository at this point in the history
  • Loading branch information
galargh committed Dec 22, 2021
1 parent 9706e9c commit 486f183
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/copy-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,20 @@ jobs:
config=$(echo -e "$config" | jq -c '.')
github="{\"default_branch\":\"${{ env.DEFAULT_BRANCH }}\"}"
context="{\"config\":$config,\"github\":$github}"
# matches trimmed string between template context delimiters
regexp='\$\{\{\{\s*(.*?)\s*\}\}\}'
# replacement is a perl script that:
# 1. prepends . to the matched string to create a filter
# 2. escapes single quotes by replacing ' with '"'"' in filter
# 3. prints context escaped in the same manner
# 4. applies jq with filter to the stdout
replacement="\$filter = \".\$1\"; \$filter =~ s/'/'\"'\"'/g; \`echo '${context//\'/\'\"\'\"\'}' | jq -jc '\$filter'\`"
for f in $(jq -r '.[]' <<< ${{ toJson(env.FILES) }}); do
echo -e "\nProcessing $f."
# add DO NOT EDIT header
tmp=$(mktemp)
cat $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/header.yml $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/$f > $tmp
# replace template expressions with values from the JSON context object
# replace template contexts with values from the JSON context object
perl -pi -e "s#$regexp#$replacement#ge" $tmp
mv $tmp $TEMPLATE_REPO_DIR/$TEMPLATE_DIR/$f
# create commit, if necessary
Expand Down
8 changes: 6 additions & 2 deletions templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## Header

The [header](header.yml) is prepended to all the files before they are copied to the target repositories.

## Contexts

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

| Property name | Type | Always present | Description |
| --- | --- | --- |
| --- | --- | --- | --- |
| `github` | `object` | `true` | The top-level context. |
| `github.default_branch` | `string` | `true` | The name of the default branch of the target repository. |

Expand All @@ -53,7 +57,7 @@ The `github` context contains information about the target repository the file i
"greeting": "Hello"
},
"files": [],
"extra_files": [".github/workflows/example.yml"]
"extra_files": [".github/workflows/example.yml"],
"target": "protocol/.github-test-target"
},
"github": {
Expand Down

0 comments on commit 486f183

Please sign in to comment.