-
Notifications
You must be signed in to change notification settings - Fork 7
/
action.yml
99 lines (83 loc) · 3.18 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
name: "Check formatting of Rust code with rustfmt"
description: |
Run `cargo fmt` and check Rust code.
Highlights places which are not correctly formatted.
branding:
icon: "check-square"
color: "yellow"
inputs:
manifest-path:
description: "Specify the --manifest-path argument to rustfmt"
required: false
default: "./Cargo.toml"
alias:
description: "Fmt alias to run"
required: false
default: "fmt"
runs:
using: composite
steps:
- name: Checkout the latest code
id: git_checkout
uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
- name: Setup Rust Toolchain
id: setup_rust_toolchain
uses: actions-rust-lang/setup-rust-toolchain@b113a30d27a8e59c969077c0a0168cc13dab5ffc # v1.8.0
with:
components: rustfmt
cache: false
- name: Rustfmt Job Summary
shell: bash
run: |
# Split alias command's options
alias=$(grep -e "${{ inputs.alias }}.*=" .cargo/config | tr -d '"' | awk '/^${{ inputs.alias }}[[:space:]]*=/ {sub(/^${{ inputs.alias }}[[:space:]]*=[[:space:]]*/, ""); print; exit}')
before_empty_dashes=""
after_empty_dashes=""
reached_empty_dashes=false
IFS=' '
read -ra args <<< "$alias"
if [[ "${args[0]}" != "fmt" && "${{ inputs.alias }}" != "fmt" ]]; then
echo "The provided alias is invalid!";
fi
for arg in "${args[@]}"; do
if [[ "$arg" == "--" ]]; then
reached_empty_dashes=true;
continue;
fi
if $reached_empty_dashes; then
after_empty_dashes="$after_empty_dashes $arg";
else
before_empty_dashes="$before_empty_dashes $arg";
fi
done
# Run cargo and store the original output
CARGO_STATUS=0
CARGO_OUTPUT=$(cargo ${before_empty_dashes:-fmt} --all --manifest-path=${{ inputs.manifest-path }} -- $after_empty_dashes --color=always --check 2>/dev/null) || CARGO_STATUS=$?
if [ ${CARGO_STATUS} -eq 0 ]; then
cat <<MARKDOWN_INTRO >> $GITHUB_STEP_SUMMARY
# Rustfmt Results
The code is formatted perfectly!
MARKDOWN_INTRO
else
cat <<MARKDOWN_INTRO >> $GITHUB_STEP_SUMMARY
# Rustfmt Results
\`cargo fmt\` reported formatting errors in the following locations.
You can fix them by executing the following command and committing the changes.
\`\`\`bash
cargo fmt --all
\`\`\`
MARKDOWN_INTRO
echo "${CARGO_OUTPUT}" |
# Strip color codes
sed 's/\x1B\[[0-9;]*[A-Za-z]//g' |
# Strip (some) cursor movements
sed 's/\x1B.[A-G]//g' |
tr "\n" "\r" |
# Wrap each location into a HTML details
sed -E 's#Diff in ([^\r]*?) at line ([[:digit:]]+):\r((:?[ +-][^\r]*\r)+)#<details>\n<summary>\1:\2</summary>\n\n```diff\n\3```\n\n</details>\n\n#g' |
tr "\r" "\n" >> $GITHUB_STEP_SUMMARY
fi
# Print the original cargo message
echo "${CARGO_OUTPUT}"
# Exit with the same status as cargo
exit "${CARGO_STATUS}"