diff --git a/main.go b/main.go index c59feed..b0e0df6 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,12 @@ const ( mappingFileEnvKey = "BITRISE_MAPPING_PATH" mappingFilePattern = "*build/*/mapping.txt" + + newLine = `\n` +) + +var ( + ignoredSuffixes = [...]string{"Classes", "Resources", "UnitTestClasses", "AndroidTestClasses", "AndroidTestResources"} ) // Configs ... @@ -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 != "" { @@ -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 } diff --git a/main_test.go b/main_test.go index a05ee18..218b72a 100644 --- a/main_test.go +++ b/main_test.go @@ -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") @@ -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) @@ -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) + } } diff --git a/step.yml b/step.yml index 5eb7860..a244257 100644 --- a/step.yml +++ b/step.yml @@ -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: