Skip to content

Commit

Permalink
Made configuration optional by using archive configuration as default (
Browse files Browse the repository at this point in the history
…#22)

* Made configuration optional by using archive configuration as default

If the configuration input is not set we select the scheme's archive
action's configuration.

[PLAT-51]

* Gave variables more meaningful names

* Added clarification to the default value selection
  • Loading branch information
Mate Herber authored Sep 10, 2020
1 parent 74fb252 commit 24dca8a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
60 changes: 41 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
type Config struct {
ProjectPath string `env:"project_path,required"`
Scheme string `env:"scheme,required"`
Configuration string `env:"configuration,required"`
Configuration string `env:"configuration"`
ArtifactName string `env:"artifact_name"`
XcodebuildOptions string `env:"xcodebuild_options"`
Workdir string `env:"workdir"`
Expand Down Expand Up @@ -183,6 +183,25 @@ func main() {
failf("Failed to get absolute project path: %s", err)
}

//
// Read the Scheme
var scheme *xcscheme.Scheme
var schemeContainerDir string
var conf string
{
scheme, schemeContainerDir, err = readScheme(absProjectPath, cfg.Scheme)

if err != nil {
failf("Failed to read schema: %s", err)
}

if cfg.Configuration != "" {
conf = cfg.Configuration
} else {
conf = scheme.ArchiveAction.BuildConfiguration
}
}

//
// Create the app with Xcode Command Line tools
{
Expand All @@ -199,7 +218,7 @@ func main() {
// Build for simulator command
xcodeBuildCmd := xcodebuild.NewCommandBuilder(absProjectPath, isWorkspace, xcodebuild.BuildAction)
xcodeBuildCmd.SetScheme(cfg.Scheme)
xcodeBuildCmd.SetConfiguration(cfg.Configuration)
xcodeBuildCmd.SetConfiguration(conf)

// Disable the code signing for simulator build
xcodeBuildCmd.SetDisableCodesign(true)
Expand Down Expand Up @@ -257,7 +276,7 @@ The log file is stored in $BITRISE_DEPLOY_DIR, and its full path is available in
fmt.Println()
log.Infof("Copy artifacts from Derived Data to %s", absOutputDir)

proj, _, err := findBuiltProject(absProjectPath, cfg.Scheme, cfg.Configuration)
proj, err := findBuiltProject(scheme, schemeContainerDir, conf)
if err != nil {
failf("Failed to open xcproj - (%s), error:", absProjectPath, err)
}
Expand All @@ -278,15 +297,15 @@ The log file is stored in $BITRISE_DEPLOY_DIR, and its full path is available in
customOptions = append(customOptions, simulatorName)
}

schemeBuildDir, err := buildTargetDirForScheme(proj, absProjectPath, cfg.Scheme, cfg.Configuration, customOptions...)
schemeBuildDir, err := buildTargetDirForScheme(proj, absProjectPath, cfg.Scheme, conf, customOptions...)
if err != nil {
failf("Failed to get scheme (%s) build target dir, error: %s", err)
}

log.Debugf("Scheme build dir: %s", schemeBuildDir)

// Export the artifact from the build dir to the output_dir
if exportedArtifacts, err = exportArtifacts(proj, cfg.Scheme, schemeBuildDir, cfg.Configuration, cfg.SimulatorPlatform, absOutputDir); err != nil {
if exportedArtifacts, err = exportArtifacts(proj, cfg.Scheme, schemeBuildDir, conf, cfg.SimulatorPlatform, absOutputDir); err != nil {
failf("Failed to export the artifacts, error: %s", err)
}
}
Expand Down Expand Up @@ -331,45 +350,48 @@ func exportOutput(artifacts []string) (string, string, error) {
return artifacts[0], pathMap, nil
}

// findBuiltProject returns the Xcode project which will be built for the provided scheme
func findBuiltProject(pth, schemeName, configurationName string) (xcodeproj.XcodeProj, string, error) {
func readScheme(pth, schemeName string) (*xcscheme.Scheme, string, error) {
var scheme *xcscheme.Scheme
var schemeContainerDir string

if xcodeproj.IsXcodeProj(pth) {
project, err := xcodeproj.Open(pth)
if err != nil {
return xcodeproj.XcodeProj{}, "", err
return nil, "", err
}

scheme, _, err = project.Scheme(schemeName)
if err != nil {
return xcodeproj.XcodeProj{}, "", fmt.Errorf("failed to get scheme (%s) from project (%s), error: %s", schemeName, pth, err)
return nil, "", fmt.Errorf("failed to get scheme (%s) from project (%s), error: %s", schemeName, pth, err)
}
schemeContainerDir = filepath.Dir(pth)
} else if xcworkspace.IsWorkspace(pth) {
workspace, err := xcworkspace.Open(pth)
if err != nil {
return xcodeproj.XcodeProj{}, "", err
return nil, "", err
}

var containerProject string
scheme, containerProject, err = workspace.Scheme(schemeName)

if err != nil {
return xcodeproj.XcodeProj{}, "", fmt.Errorf("no scheme found with name: %s in workspace: %s, error: %s", schemeName, pth, err)
return nil, "", fmt.Errorf("no scheme found with name: %s in workspace: %s, error: %s", schemeName, pth, err)
}
schemeContainerDir = filepath.Dir(containerProject)
} else {
return xcodeproj.XcodeProj{}, "", fmt.Errorf("unknown project extension: %s", filepath.Ext(pth))
return nil, "", fmt.Errorf("unknown project extension: %s", filepath.Ext(pth))
}
return scheme, schemeContainerDir, nil
}

// findBuiltProject returns the Xcode project which will be built for the provided scheme
func findBuiltProject(scheme *xcscheme.Scheme, schemeContainerDir, configurationName string) (xcodeproj.XcodeProj, error) {
if configurationName == "" {
configurationName = scheme.ArchiveAction.BuildConfiguration
}

if configurationName == "" {
return xcodeproj.XcodeProj{}, "", fmt.Errorf("no configuration provided nor default defined for the scheme's (%s) archive action", schemeName)
return xcodeproj.XcodeProj{}, fmt.Errorf("no configuration provided nor default defined for the scheme's (%s) archive action", scheme.Name)
}

var archiveEntry xcscheme.BuildActionEntry
Expand All @@ -382,20 +404,20 @@ func findBuiltProject(pth, schemeName, configurationName string) (xcodeproj.Xcod
}

if archiveEntry.BuildableReference.BlueprintIdentifier == "" {
return xcodeproj.XcodeProj{}, "", fmt.Errorf("archivable entry not found")
return xcodeproj.XcodeProj{}, fmt.Errorf("archivable entry not found")
}

projectPth, err := archiveEntry.BuildableReference.ReferencedContainerAbsPath(schemeContainerDir)
if err != nil {
return xcodeproj.XcodeProj{}, "", err
return xcodeproj.XcodeProj{}, err
}

project, err := xcodeproj.Open(projectPth)
if err != nil {
return xcodeproj.XcodeProj{}, "", err
return xcodeproj.XcodeProj{}, err
}

return project, scheme.Name, nil
return project, nil
}

// buildTargetDirForScheme returns the TARGET_BUILD_DIR for the provided scheme
Expand Down Expand Up @@ -463,7 +485,7 @@ func wrapperNameForScheme(proj xcodeproj.XcodeProj, projectPath, scheme, configu
return "", fmt.Errorf("project file extension should be .xcodeproj or .xcworkspace, but got: %s", filepath.Ext(projectPath))

}

wrapperName, err := buildSettings.String("WRAPPER_NAME")

if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,18 @@ inputs:
- iOS
- tvOS
is_required: "true"
- configuration: Debug
- configuration:
opts:
title: "Configuration name"
description: |-
(optional) The configuration to use. By default your Scheme
(optional) The configuration to use. By default your Scheme's archive action
defines which configuration (Debug, Release, ...) should be used,
but you can overwrite it with this option.
**Make sure that the Configuration you specify actually exists
in your Xcode Project**. If it does not, if you have a typo
in the value of this input Xcode will simply use the Configuration
specified by the Scheme and will silently ignore this parameter!
is_required: "true"
- disable_index_while_building: "yes"
opts:
title: Disable indexing during the build
Expand Down

0 comments on commit 24dca8a

Please sign in to comment.