Skip to content

Commit

Permalink
Add APK/AAB config input (#49)
Browse files Browse the repository at this point in the history
* Gitignore .idea folder

* Add APK/AAB config input

STEP-1310
  • Loading branch information
ofalvai committed Aug 2, 2021
1 parent 28d256d commit 8cf3355
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.bitrise*
.gows*
_tmp/
.idea/
64 changes: 58 additions & 6 deletions cordova/cordova.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package cordova

import (
"fmt"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/sliceutil"
)

// Model ...
type Model struct {
platforms []string
configuration string
target string
buildConfig string
customOptions []string
platforms []string
configuration string
target string
buildConfig string
customOptions []string
androidAppType string
}

// New ...
Expand Down Expand Up @@ -42,6 +45,13 @@ func (builder *Model) SetBuildConfig(buildConfig string) *Model {
return builder
}

// SetAndroidAppType ...
// Possible app types: "apk", "aab"
func (builder *Model) SetAndroidAppType(appType string) *Model {
builder.androidAppType = appType
return builder
}

// SetCustomOptions ...
func (builder *Model) SetCustomOptions(customOptions ...string) *Model {
builder.customOptions = customOptions
Expand Down Expand Up @@ -69,7 +79,40 @@ func (builder *Model) commandSlice(cmd ...string) []string {
if builder.buildConfig != "" {
cmdSlice = append(cmdSlice, "--buildConfig", builder.buildConfig)
}
cmdSlice = append(cmdSlice, builder.customOptions...)

// Cordova CLI expects platform-specific arguments to be listed after a -- separator
// We parse user-specified options and group them separately
separator := "--"
separatorIndex := sliceutil.IndexOfStringInSlice(separator, builder.customOptions)
var generalOptions []string
var platformOptions []string

if builder.hasPlatformAndroid() {
// Package type is platform-specific
packageTypeValue := builder.androidAppType
if packageTypeValue == "aab" {
packageTypeValue = "bundle"
}
platformOptions = append(platformOptions, fmt.Sprintf("--packageType=%s", packageTypeValue))
}

for i, opt := range builder.customOptions {
if opt == separator {
continue
}
if separatorIndex >= 0 && i > separatorIndex {
platformOptions = append(platformOptions, opt)
} else {
generalOptions = append(generalOptions, opt)
}
}

cmdSlice = append(cmdSlice, generalOptions...)
if len(platformOptions) > 0 {
cmdSlice = append(cmdSlice, separator)
cmdSlice = append(cmdSlice, platformOptions...)
}

}

return cmdSlice
Expand All @@ -86,3 +129,12 @@ func (builder *Model) CompileCommand() *command.Model {
cmdSlice := builder.commandSlice("compile")
return command.New(cmdSlice[0], cmdSlice[1:]...)
}

func (builder *Model) hasPlatformAndroid() bool {
for _, platform := range builder.platforms {
if platform == "android" {
return true
}
}
return false
}
63 changes: 63 additions & 0 deletions cordova/cordova_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cordova

import (
"testing"
)

func Test_platformCustomOptions(t *testing.T) {
tests := []struct {
name string
model Model
wantCmd string
}{
{
"Project with generic option and AAB output",
*New().SetCustomOptions("--release").SetPlatforms("android").SetAndroidAppType("aab"),
`cordova "compile" "android" "--release" "--" "--packageType=bundle"`,
},
{
"Project with generic option and APK output",
*New().SetCustomOptions("--release").SetPlatforms("android").SetAndroidAppType("apk"),
`cordova "compile" "android" "--release" "--" "--packageType=apk"`,
},
{
"Project with no generic option and APK output",
*New().SetPlatforms("android").SetAndroidAppType("apk"),
`cordova "compile" "android" "--" "--packageType=apk"`,
},
{
"Project with no generic option and AAB output",
*New().SetPlatforms("android").SetAndroidAppType("aab"),
`cordova "compile" "android" "--" "--packageType=bundle"`,
},
{
"Project with platform-specific and generic options and AAB output",
*New().SetCustomOptions("--release", "--", "--keystore=android.keystore").SetPlatforms("android").SetAndroidAppType("aab"),
`cordova "compile" "android" "--release" "--" "--packageType=bundle" "--keystore=android.keystore"`,
},
{
"Project with platform-specific and generic options and APK output",
*New().SetCustomOptions("--release", "--", "--keystore=android.keystore").SetPlatforms("android").SetAndroidAppType("apk"),
`cordova "compile" "android" "--release" "--" "--packageType=apk" "--keystore=android.keystore"`,
},
{
"Project with only packageType option and APK output",
*New().SetCustomOptions("--", `--packageType="bundle"`).SetPlatforms("android").SetAndroidAppType("apk"),
`cordova "compile" "android" "--" "--packageType=apk" "--packageType=\"bundle\""`,
},
{
"Project with only packageType option and AAB output",
*New().SetCustomOptions("--", `--packageType="bundle"`).SetPlatforms("android").SetAndroidAppType("bundle"),
`cordova "compile" "android" "--" "--packageType=bundle" "--packageType=\"bundle\""`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := tt.model.CompileCommand().PrintableCommandArgs()
if tt.wantCmd != cmd {
t.Errorf("Cordova command doesn't match wanted value.\nWant: %v\nGot: %s", tt.wantCmd, cmd)
}
})
}
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"github.com/kballard/go-shellquote"
"os"
"path/filepath"
"strings"
Expand All @@ -19,7 +20,6 @@ import (
"github.com/bitrise-io/go-utils/sliceutil"
"github.com/bitrise-io/go-utils/ziputil"
"github.com/bitrise-steplib/steps-cordova-archive/cordova"
"github.com/kballard/go-shellquote"
)

const (
Expand All @@ -46,6 +46,7 @@ type config struct {
Options string `env:"options"`
DeployDir string `env:"BITRISE_DEPLOY_DIR"`
UseCache bool `env:"cache_local_deps,opt[true,false]"`
AndroidAppType string `env:"android_app_type,opt[apk,aab]"`
}

func installDependency(packageManager jsdependency.Tool, name string, version string) error {
Expand Down Expand Up @@ -245,6 +246,7 @@ func main() {
builder.SetPlatforms(platforms...)
}

builder.SetAndroidAppType(configs.AndroidAppType)
builder.SetConfiguration(configs.Configuration)
builder.SetTarget(configs.Target)

Expand Down
15 changes: 12 additions & 3 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ inputs:
- "true"
- "false"
is_required: true
- cordova_version:
- cordova_version:
opts:
title: "Cordova version"
description: |-
Expand All @@ -125,8 +125,6 @@ inputs:
description: |-
Use this input to specify custom options, to append to the end of the cordova-cli build command.
Add `-- --packageType="bundle"` to build an Android bundle (aab). (https://github.com/apache/cordova-android/pull/764)
The new Xcode build system is now supported in cordova-ios@5.0.0 (https://github.com/apache/cordova-ios/issues/407).
To use the legacy build system add `--buildFlag="-UseModernBuildSystem=0"` to the options string.
Expand All @@ -146,6 +144,17 @@ inputs:
value_options:
- "true"
- "false"
- android_app_type: apk
opts:
category: Android
title: Android app type
summary: Distribution type when building the Android app
description: Distribution type when building the Android app
is_required: true
value_options:
- apk
- aab

outputs:
- BITRISE_IPA_PATH:
opts:
Expand Down

0 comments on commit 8cf3355

Please sign in to comment.