-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
slugify.sh
executable file
·84 lines (76 loc) · 2.43 KB
/
slugify.sh
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
#!/usr/bin/env bash
if [[ "$OSTYPE" == "darwin"* ]]; then
# On MacOS,
# bash don't support substitution, so we use 'tr'
KEY=$(echo "$INPUT_KEY" | tr '[:lower:]' '[:upper:]')
CS_VALUE=${INPUT_VALUE:-${!INPUT_KEY}}
VALUE=$(echo "$CS_VALUE" | tr '[:upper:]' '[:lower:]')
PREFIX=$(echo "$INPUT_PREFIX" | tr '[:lower:]' '[:upper:]')
else
KEY=${INPUT_KEY^^}
CS_VALUE=${INPUT_VALUE:-${!INPUT_KEY}}
VALUE=${CS_VALUE,,}
PREFIX=${INPUT_PREFIX^^}
fi
MAX_LENGTH=""
if [ -z "${INPUT_SLUG_MAXLENGTH}" ]; then
echo "::error ::slug-maxlength cannot be empty"
exit 1
elif [ "${INPUT_SLUG_MAXLENGTH}" -eq "${INPUT_SLUG_MAXLENGTH}" ] 2>/dev/null; then
MAX_LENGTH="${INPUT_SLUG_MAXLENGTH}"
elif [ "${INPUT_SLUG_MAXLENGTH}" == "nolimit" ]; then
MAX_LENGTH="${INPUT_SLUG_MAXLENGTH}"
else
echo "::error ::slug-maxlength must be a number or equals to 'nolimit'"
exit 1
fi
slug() {
# 1st : Remove refs prefix
# 2d : Replace unwanted characters
# 3d : Remove leading hypens
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9._-]+/-/g;s/^-*//' <<<"$1")
reduce "$output"
}
slug_url() {
# 1st : Remove refs prefix
# 2d : Replace unwanted characters
# 3d : Remove leading hypens
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9-]+/-/g;s/^-*//' <<<"$1")
reduce "$output"
}
reduce() {
reduced_value="$1"
if [ "${MAX_LENGTH}" != "nolimit" ]; then
reduced_value=$(cut -c1-"${MAX_LENGTH}" <<<"$reduced_value")
fi
# 1st : Remove trailing hypens
sed -E 's/-*$//' <<<"$reduced_value"
}
SLUG_VALUE=$(slug "$VALUE")
SLUG_CS_VALUE=$(slug "$CS_VALUE")
SLUG_URL_VALUE=$(slug_url "$VALUE")
SLUG_URL_CS_VALUE=$(slug_url "$CS_VALUE")
if [ -f "$GITHUB_OUTPUT" ]; then
{
echo "value=${CS_VALUE}"
echo "slug=${SLUG_VALUE}"
echo "slug-cs=${SLUG_CS_VALUE}"
echo "slug-url=${SLUG_URL_VALUE}"
echo "slug-url-cs=${SLUG_URL_CS_VALUE}"
} >> "$GITHUB_OUTPUT"
else
echo "::set-output name=value::${CS_VALUE}"
echo "::set-output name=slug::${SLUG_VALUE}"
echo "::set-output name=slug-cs::${SLUG_CS_VALUE}"
echo "::set-output name=slug-url::${SLUG_URL_VALUE}"
echo "::set-output name=slug-url-cs::${SLUG_URL_CS_VALUE}"
fi
if [ "${INPUT_PUBLISH_ENV}" == "true" ]; then
{
echo "${PREFIX}${KEY}=${CS_VALUE}"
echo "${PREFIX}${KEY}_SLUG=${SLUG_VALUE}"
echo "${PREFIX}${KEY}_SLUG_CS=${SLUG_CS_VALUE}"
echo "${PREFIX}${KEY}_SLUG_URL=${SLUG_URL_VALUE}"
echo "${PREFIX}${KEY}_SLUG_URL_CS=${SLUG_URL_CS_VALUE}"
} >>"$GITHUB_ENV"
fi