Skip to content

Commit

Permalink
feat(sync-files): add pre-command and post-command (#149)
Browse files Browse the repository at this point in the history
* feat(sync-files): add pre-command and post-command

Signed-off-by: Kenji Miyake <[email protected]>

* add setup-sd

Signed-off-by: Kenji Miyake <[email protected]>

* fix README.md

Signed-off-by: Kenji Miyake <[email protected]>

* rename command to commands

Signed-off-by: Kenji Miyake <[email protected]>
  • Loading branch information
kenji-miyake authored May 16, 2022
1 parent 78694bf commit 41c9a65
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
17 changes: 17 additions & 0 deletions sync-files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ The specifications are:
| files/dest | false | The same as `files/source`. | The path where to place the synced file in the base repository. |
| files/replace | false | `true` | Whether to replace the synced file if it already exists. |
| files/delete-orphaned | false | `true` | Whether to delete the synced file if it does not exist in the target repository anymore. |
| files/pre-commands | false | `""` | The multi-line commands executed before copying the file. |
| files/post-commands | false | `""` | The multi-line commands executed after copying the file. |

In the `pre-commands` and `post-commands` options, the following special variables can be used:

- `{source}`: The sync source file
- `{dest}`: The sync dest file

Example:

```yaml
- repository: autowarefoundation/autoware
files:
- source: .pre-commit-config.yaml
post-commands: |
sd -f ms "[^\n]*shellcheck-py\n.*?\n\n" "" {dest}
```

## Inputs

Expand Down
29 changes: 24 additions & 5 deletions sync-files/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ runs:
with:
yq-version: v4.25.1

- name: Set up sd
uses: kenji-miyake/setup-sd@v1

- name: Parse config
run: |
pip3 install pyyaml
Expand Down Expand Up @@ -95,9 +98,17 @@ runs:
dest_path=$(yq ".dest" /tmp/file-config.yaml)
replace=$(yq ".replace" /tmp/file-config.yaml)
delete_orphaned=$(yq ".delete-orphaned" /tmp/file-config.yaml)
pre_commands=$(yq ".pre-commands" /tmp/file-config.yaml)
post_commands=$(yq ".post-commands" /tmp/file-config.yaml)
modified_source_path="/tmp/repository/$source_path"
source_file="/tmp/repository/$source_path"
if [ -f "$source_file" ]; then
pre_commands=$(echo "$pre_commands" | sed "s|{source}|$modified_source_path|g" | sed "s|{dest}|$dest_path|g")
post_commands=$(echo "$post_commands" | sed "s|{source}|$modified_source_path|g" | sed "s|{dest}|$dest_path|g")
[ -n "$pre_commands" ] && echo "pre_commands for $source_path: $pre_commands"
[ -n "$post_commands" ] && echo "post_commands for $source_path: $post_commands"
if [ -f "$modified_source_path" ]; then
if [ -f "$dest_path" ] && [ "$replace" != "true" ]; then
echo "Skip copying to $dest_path."
yq -i ".skipped += [\"$dest_path\"]" /tmp/result.yaml
Expand All @@ -107,11 +118,19 @@ runs:
if ! [ -f "$dest_path" ]; then
echo "Newly copy $source_path to $dest_path."
mkdir -p $(dirname "$dest_path")
cp "$source_file" "$dest_path"
eval "$pre_commands" || true
cp "$modified_source_path" "$dest_path"
eval "$post_commands"
yq -i ".added += [\"$dest_path\"]" /tmp/result.yaml
elif ! diff "$source_file" "$dest_path"; then
elif ! diff "$modified_source_path" "$dest_path"; then
echo "Copy $source_path to $dest_path."
cp "$source_file" "$dest_path"
eval "$pre_commands"
cp "$modified_source_path" "$dest_path"
eval "$post_commands"
yq -i ".changed += [\"$dest_path\"]" /tmp/result.yaml
else
echo "$source_path and $dest_path are the same."
Expand Down
6 changes: 6 additions & 0 deletions sync-files/parse_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def main():
if "delete-orphaned" not in item:
item["delete-orphaned"] = True

if "pre-commands" not in item:
item["pre-commands"] = ""

if "post-commands" not in item:
item["post-commands"] = ""

print(yaml.dump(config))


Expand Down

0 comments on commit 41c9a65

Please sign in to comment.