Skip to content

Commit e5ad585

Browse files
committed
script considering input, test all cases
1 parent 17eefad commit e5ad585

File tree

3 files changed

+96
-18
lines changed

3 files changed

+96
-18
lines changed

.github/workflows/test.yml

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ jobs:
2020
echo " }" >> app/build.gradle.kts
2121
echo "}" >> app/build.gradle.kts
2222
23-
- name: Run action
23+
- name: Run action with EXPOSE_CODE true and EXPOSE_NAME true
2424
uses: ./
2525
with:
2626
EXPOSE_CODE: 'true'
2727
EXPOSE_NAME: 'true'
2828

29-
- name: Validate versionName and versionCode
29+
- name: Validate versionName and versionCode with EXPOSE_CODE and EXPOSE_NAME true
3030
run: |
3131
content=$(cat app/build.gradle.kts)
3232
if [[ $content == *"versionName = \"4.22.0\""* && $content == *"versionCode = 123"* ]]; then
@@ -36,7 +36,7 @@ jobs:
3636
exit 1
3737
fi
3838
39-
- name: Verify Environment Variables
39+
- name: Verify Environment Variables with EXPOSE_CODE and EXPOSE_NAME true
4040
run: |
4141
echo "ANDROID_VERSION_NAME: $ANDROID_VERSION_NAME"
4242
echo "ANDROID_VERSION_CODE: $ANDROID_VERSION_CODE"
@@ -46,3 +46,70 @@ jobs:
4646
echo "Environment variables are not set as expected."
4747
exit 1
4848
fi
49+
50+
- name: Run action with EXPOSE_CODE false and EXPOSE_NAME true
51+
uses: ./
52+
with:
53+
EXPOSE_CODE: 'false'
54+
EXPOSE_NAME: 'true'
55+
56+
- name: Validate versionName with EXPOSE_CODE false and EXPOSE_NAME true
57+
run: |
58+
content=$(cat app/build.gradle.kts)
59+
if [[ $content == *"versionName = \"4.22.0\""* ]]; then
60+
echo "Mock build.gradle.kts contains versionName, NAME is $ANDROID_VERSION_NAME"
61+
else
62+
echo "Mock build.gradle.kts does not contain expected versionName"
63+
exit 1
64+
fi
65+
66+
- name: Verify Environment Variables with EXPOSE_CODE false and EXPOSE_NAME true
67+
run: |
68+
echo "ANDROID_VERSION_NAME: $ANDROID_VERSION_NAME"
69+
if [[ $ANDROID_VERSION_NAME == 4.22.0 ]]; then
70+
echo "Environment variable for name is set correctly."
71+
else
72+
echo "Environment variable for name is not set as expected."
73+
exit 1
74+
fi
75+
76+
- name: Run action with EXPOSE_CODE true and EXPOSE_NAME false
77+
uses: ./
78+
with:
79+
EXPOSE_CODE: 'true'
80+
EXPOSE_NAME: 'false'
81+
82+
- name: Validate versionCode with EXPOSE_CODE true and EXPOSE_NAME false
83+
run: |
84+
content=$(cat app/build.gradle.kts)
85+
if [[ $content == *"versionCode = 123"* ]]; then
86+
echo "Mock build.gradle.kts contains versionCode, CODE is $ANDROID_VERSION_CODE"
87+
else
88+
echo "Mock build.gradle.kts does not contain expected versionCode"
89+
exit 1
90+
fi
91+
92+
- name: Verify Environment Variables with EXPOSE_CODE true and EXPOSE_NAME false
93+
run: |
94+
echo "ANDROID_VERSION_CODE: $ANDROID_VERSION_CODE"
95+
if [[ $ANDROID_VERSION_CODE == 123 ]]; then
96+
echo "Environment variable for code is set correctly."
97+
else
98+
echo "Environment variable for code is not set as expected."
99+
exit 1
100+
fi
101+
102+
- name: Run action with EXPOSE_CODE false and EXPOSE_NAME false
103+
uses: ./
104+
with:
105+
EXPOSE_CODE: 'false'
106+
EXPOSE_NAME: 'false'
107+
108+
- name: Validate no version information extracted with EXPOSE_CODE false and EXPOSE_NAME false
109+
run: |
110+
if [[ -z $ANDROID_VERSION_NAME && -z $ANDROID_VERSION_CODE ]]; then
111+
echo "No version information extracted as expected."
112+
else
113+
echo "Version information extracted when it should not have been."
114+
exit 1
115+
fi

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Expose Android version code and name
2-
description: Finds the version code and version name in your build.gradle.kts file and publishes it as environment variables.
2+
description: Finds the version code and version name in your build.gradle.kts file and set them as environment variables.
33
author: Maria Ivanovskaya
44
inputs:
55
EXPOSE_CODE:

extract-expose-version.sh

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
#!/bin/bash
22

3-
# Extract Android version code as string
4-
ANDROID_VERSION_CODE=$(grep "versionCode" app/build.gradle.kts | sed 's/.*versionCode\s*=\s*\([0-9]*\).*/\1/' | awk '{print $0+0}')
5-
if [ -z "$ANDROID_VERSION_CODE" ]; then
6-
echo "Error: Unable to extract version code."
3+
# Check if the build.gradle.kts file exists
4+
if [ ! -f "app/build.gradle.kts" ]; then
5+
echo "Error: app/build.gradle.kts file not found."
76
exit 1
87
fi
98

10-
# Extract Android version name
11-
ANDROID_VERSION_NAME=$(grep "versionName" app/build.gradle.kts | sed "s/.*versionName\\s*=\\s*'\\(.*\\)'.*/\\1/" | tr -d '[:space:]' | sed 's/"//g' | sed 's/versionName=//')
12-
if [ -z "$ANDROID_VERSION_NAME" ]; then
13-
echo "Error: Unable to extract version name."
14-
exit 1
9+
# Check if EXPOSE_CODE is true for extracting Android version code
10+
if [ "$EXPOSE_CODE" = 'true' ]; then
11+
# Extract Android version code as string
12+
ANDROID_VERSION_CODE=$(grep "versionCode" app/build.gradle.kts | sed 's/.*versionCode\s*=\s*\([0-9]*\).*/\1/' | awk '{print $0+0}')
13+
if [ -z "$ANDROID_VERSION_CODE" ]; then
14+
echo "Error: Unable to extract version code."
15+
exit 1
16+
fi
17+
18+
# Write the extracted Android version code to a temporary file
19+
echo "ANDROID_VERSION_CODE=$ANDROID_VERSION_CODE" > extracted_values.txt
1520
fi
1621

17-
# Write the extracted values to a temporary file
18-
echo "ANDROID_VERSION_CODE=$ANDROID_VERSION_CODE" > extracted_values.txt
19-
echo "ANDROID_VERSION_NAME=$ANDROID_VERSION_NAME" >> extracted_values.txt
22+
# Check if EXPOSE_NAME is true for extracting version name
23+
if [ "$EXPOSE_NAME" = 'true' ]; then
24+
# Extract Android version name
25+
ANDROID_VERSION_NAME=$(grep "versionName" app/build.gradle.kts | sed "s/.*versionName\\s*=\\s*'\\(.*\\)'.*/\\1/" | tr -d '[:space:]' | sed 's/"//g' | sed 's/versionName=//')
26+
if [ -z "$ANDROID_VERSION_NAME" ]; then
27+
echo "Error: Unable to extract version name."
28+
exit 1
29+
fi
2030

21-
# Debugging output to indicate script completion
22-
echo "Script execution complete."
31+
# Append the extracted Android version name to the temporary file
32+
echo "ANDROID_VERSION_NAME=$ANDROID_VERSION_NAME" >> extracted_values.txt
33+
fi

0 commit comments

Comments
 (0)