Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
lpusok authored Dec 9, 2020
2 parents 09ac4e7 + 706b93d commit ae2e7ba
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 13 deletions.
63 changes: 55 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const (

mappingFileEnvKey = "BITRISE_MAPPING_PATH"
mappingFilePattern = "*build/*/mapping.txt"

newLine = `\n`
)

var (
ignoredSuffixes = [...]string{"Classes", "Resources", "UnitTestClasses", "AndroidTestClasses", "AndroidTestResources"}
)

// Configs ...
Expand Down Expand Up @@ -92,6 +98,36 @@ func exportArtifacts(artifacts []gradle.Artifact, deployDir string) ([]string, e
return paths, nil
}

func filterNonUtilityVariants(variants []string) []string {
var filteredVariants []string

for _, v := range variants {
shouldIgnore := false
for _, suffix := range ignoredSuffixes {
if strings.HasSuffix(v, suffix) {
shouldIgnore = true
break
}
}

if !shouldIgnore {
filteredVariants = append(filteredVariants, v)
}
}

return filteredVariants
}

func separateVariants(variantsAsOneLine string) []string {
variants := strings.Split(variantsAsOneLine, newLine)

for index, variant := range variants {
variants[index] = strings.TrimSpace(variant)
}

return variants
}

func filterVariants(module, variant string, variantsMap gradle.Variants) (gradle.Variants, error) {
// if module set: drop all the other modules
if module != "" {
Expand All @@ -102,23 +138,34 @@ func filterVariants(module, variant string, variantsMap gradle.Variants) (gradle
variantsMap = gradle.Variants{module: v}
}

// if variant not set: use all variants
// if variant not set: use all variants, except utility ones
if variant == "" {
for module, variants := range variantsMap {
variantsMap[module] = filterNonUtilityVariants(variants)
}

return variantsMap, nil
}

variants := separateVariants(variant)

filteredVariants := gradle.Variants{}
for m, variants := range variantsMap {
for _, v := range variants {
if strings.ToLower(v) == strings.ToLower(variant) {
filteredVariants[m] = append(filteredVariants[m], v)
for _, variant := range variants {
found := false
for m, moduleVariants := range variantsMap {
for _, v := range moduleVariants {
if strings.EqualFold(v, variant) {
filteredVariants[m] = append(filteredVariants[m], v)
found = true
}
}
}
}

if len(filteredVariants) == 0 {
return nil, fmt.Errorf("variant: %s not found in any module", variant)
if !found {
return nil, fmt.Errorf("variant: %s not found in any module", variant)
}
}

return filteredVariants, nil
}

Expand Down
107 changes: 104 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

func TestFilterVariants(t *testing.T) {
variants := gradle.Variants{
"module1": []string{"variant1", "variant2", "variant3", "variant4", "variant5", "shared"},
"module2": []string{"2variant1", "2variant2", "shared", "2variant3", "2variant4", "2variant5"},
"module1": []string{"variant1", "variant2", "variant3", "variant4", "variant5", "shared", "shared2"},
"module2": []string{"2variant1", "2variant2", "shared", "2variant3", "2variant4", "2variant5", "shared2"},
}

t.Log("exact match for module and variant")
Expand Down Expand Up @@ -41,7 +41,7 @@ func TestFilterVariants(t *testing.T) {
require.NoError(t, err)

expectedVariants := gradle.Variants{
"module1": []string{"variant1", "variant2", "variant3", "variant4", "variant5", "shared"},
"module1": []string{"variant1", "variant2", "variant3", "variant4", "variant5", "shared", "shared2"},
}

require.Equal(t, expectedVariants, filtered)
Expand Down Expand Up @@ -91,4 +91,105 @@ func TestFilterVariants(t *testing.T) {

require.Equal(t, expectedVariants, filtered)
}

t.Log("exact match for module and multiple variants")
{
filtered, err := filterVariants("module1", `variant1\nvariant2`, variants)
require.NoError(t, err)

expectedVariants := gradle.Variants{
"module1": []string{"variant1", "variant2"},
}

require.Equal(t, expectedVariants, filtered)
}

t.Log("exact match for multiple variants")
{
filtered, err := filterVariants("", `shared\nshared2`, variants)
require.NoError(t, err)

expectedVariants := gradle.Variants{
"module1": []string{"shared", "shared2"},
"module2": []string{"shared", "shared2"},
}

require.Equal(t, expectedVariants, filtered)
}

t.Log("filter out utility variants")
{
variants := gradle.Variants{
"module1": []string{
"DemoDebug", "DemoDebugAndroidTestClasses", "DemoDebugAndroidTestResources",
"DemoDebugClasses", "DemoDebugResources", "DemoDebugUnitTestClasses",
"DemoRelease", "DemoReleaseClasses", "DemoReleaseResources", "DemoReleaseUnitTestClasses",
},
}

filtered, err := filterVariants("module1", "", variants)
require.NoError(t, err)

expectedVariants := gradle.Variants{
"module1": []string{"DemoDebug", "DemoRelease"},
}

require.Equal(t, expectedVariants, filtered)
}

t.Log("exact match for module and single not existing variant")
{
_, err := filterVariants("module1", "not-existings-variant", variants)
require.Error(t, err)
}

t.Log("single not existing variant")
{
_, err := filterVariants("", "not-existings-variant", variants)
require.Error(t, err)
}

t.Log("exact match for module and multiple variants, single not existing")
{
_, err := filterVariants("module1", `variant1\nnot-existings-variant`, variants)
require.Error(t, err)
}

t.Log("multiple variants, single not existing")
{
_, err := filterVariants("", `variant2\nnot-existings-variant`, variants)
require.Error(t, err)
}
}

func TestVariantSeparation(t *testing.T) {
testCases := []struct {
title string
variantsAsOneLine string
want []string
}{
{
"1. Given multiple variants",
`variant1\nvariant2`,
[]string{"variant1", "variant2"},
},
{
"2. Given single variant",
`variant1`,
[]string{"variant1"},
},
{
"3. Given empty variant",
``,
[]string{""},
},
}

for _, testCase := range testCases {
// When
variants := separateVariants(testCase.variantsAsOneLine)

// Then
require.Equal(t, testCase.want, variants)
}
}
5 changes: 3 additions & 2 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ inputs:
opts:
title: Variant
summary: |
Set the variant that you want to build. To see your available variants, please open your project in Android Studio and go in [Project Structure] -> variants section.
Set the variant(s) that you want to build. To see your available variants, please open your project in Android Studio and go in [Project Structure] -> variants section.
description: |
Set the variant that you want to build. To see your available variants, please open your project in Android Studio and go in [Project Structure] -> variants section.
Set the variant(s) that you want to build. To see your available variants, please open your project in Android Studio and go in [Project Structure] -> variants section.
You can set multiple variants separated by `\n` character. For instance: `- variant: myvariant1\nmyvariant2`.
is_required: false
- build_type: apk
opts:
Expand Down

0 comments on commit ae2e7ba

Please sign in to comment.