Skip to content

Commit

Permalink
Tool 1126 swiftpm (#25)
Browse files Browse the repository at this point in the history
* Adding swift pm cache support

* dep update

* using absolute project path

* Adding swiftpm cache input

* Bitrise yml fix

* Removing unused step param
  • Loading branch information
lpusok authored and trapacska committed Oct 30, 2019
1 parent 8b565bf commit 87a8b36
Show file tree
Hide file tree
Showing 72 changed files with 12,128 additions and 2,295 deletions.
40 changes: 25 additions & 15 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@

[[constraint]]
branch = "master"
name = "github.com/bitrise-io/go-steputils"

[prune]
go-tests = true
unused-packages = true

[[constraint]]
branch = "master"
name = "github.com/bitrise-io/go-utils"

[[constraint]]
branch = "master"
name = "github.com/bitrise-io/go-xcode"

[[constraint]]
name = "github.com/bitrise-steplib/steps-xcode-archive"
branch = "master"

[prune]
go-tests = true
unused-packages = true
54 changes: 54 additions & 0 deletions analyze.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"bytes"
"fmt"
"io"
"os"
"strings"

"github.com/bitrise-io/go-utils/colorstring"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-xcode/xcodebuild"
cache "github.com/bitrise-io/go-xcode/xcodecache"
"github.com/bitrise-io/go-xcode/xcpretty"
)

func runCommandWithRetry(cmd *xcodebuild.CommandBuilder, useXcpretty bool, swiftPackagesPath string) (string, error) {
output, err := runCommand(cmd, useXcpretty)
if err != nil && swiftPackagesPath != "" && strings.Contains(output, cache.SwiftPackagesStateInvalid) {
log.Warnf("Build failed, swift packages cache is in an invalid state, error: %s", err)
log.RWarnf("xcode-analyze", "swift-packages-cache-invalid", nil, "swift packages cache is in an invalid state")
if err := os.RemoveAll(swiftPackagesPath); err != nil {
return output, fmt.Errorf("failed to remove invalid Swift package caches, error: %s", err)
}
return runCommand(cmd, useXcpretty)
}
return output, err
}

func prepareCommand(xcodeCmd *xcodebuild.CommandBuilder, useXcpretty bool, output *bytes.Buffer) (*command.Model, *xcpretty.CommandModel) {
if useXcpretty {
return nil, xcpretty.New(*xcodeCmd)
}

buildRootCmd := xcodeCmd.Command()
buildRootCmd.SetStdout(io.MultiWriter(os.Stdout, output))
buildRootCmd.SetStderr(io.MultiWriter(os.Stderr, output))
return buildRootCmd, nil
}

func runCommand(buildCmd *xcodebuild.CommandBuilder, useXcpretty bool) (string, error) {
var output bytes.Buffer
xcodebuildCmd, xcprettyCmd := prepareCommand(buildCmd, useXcpretty, &output)

if xcprettyCmd != nil {
logWithTimestamp(colorstring.Green, "$ %s", xcprettyCmd.PrintableCmd())
fmt.Println()
return xcprettyCmd.Run()
}
logWithTimestamp(colorstring.Green, "$ %s", xcodebuildCmd.PrintableCommandArgs())
fmt.Println()
return output.String(), xcodebuildCmd.Run()
}
31 changes: 24 additions & 7 deletions bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,42 @@ workflows:
# ----------------------------------------------------------------
# --- workflow to Step Test
test:
envs:
- SAMPLE_APP_URL: https://github.com/bitrise-io/sample-apps-ios-simple-objc.git
- BITRISE_PROJECT_PATH: ios-simple-objc/ios-simple-objc.xcodeproj
- BITRISE_SCHEME: ios-simple-objc
steps:
- go-list:
- golint:
- errcheck:
- go-test:
after_run:
- test_objc
- test_swiftpm

test_objc:
envs:
- SAMPLE_APP_URL: https://github.com/bitrise-io/sample-apps-ios-simple-objc.git
- BITRISE_PROJECT_PATH: ios-simple-objc/ios-simple-objc.xcodeproj
- BITRISE_SCHEME: ios-simple-objc
after_run:
- _run_and_test_output

test_swiftpm:
envs:
- SAMPLE_APP_URL: https://github.com/bitrise-io/sample-apps-ios-swiftpm.git
- BITRISE_PROJECT_PATH: sample-swiftpm.xcodeproj
- BITRISE_SCHEME: "sample swiftpm"
after_run:
- _run_and_test_output

_run_and_test_output:
steps:
- script:
title: Remove temporary directory
inputs:
- content: rm -rf "$ORIG_BITRISE_SOURCE_DIR/_tmp"
- content: rm -rf "$BITRISE_SOURCE_DIR/_tmp"
- change-workdir:
title: Switch working dir to _tmp dir
run_if: true
inputs:
- path: "$ORIG_BITRISE_SOURCE_DIR/_tmp"
- path: "$BITRISE_SOURCE_DIR/_tmp"
- is_create_path: true
- script:
inputs:
Expand All @@ -43,7 +61,6 @@ workflows:
- path::./:
title: Test - xcodebuild output
inputs:
- is_force_code_sign: "no"
- is_clean_build: "no"
- workdir: $PROJECT_WORKDIR
- output_tool: xcodebuild
Expand Down
49 changes: 27 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"time"

"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/colorstring"
"github.com/bitrise-io/go-utils/errorutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/stringutil"
"github.com/bitrise-io/go-xcode/utility"
"github.com/bitrise-io/go-xcode/xcodebuild"
cache "github.com/bitrise-io/go-xcode/xcodecache"
"github.com/bitrise-io/go-xcode/xcpretty"
"github.com/bitrise-steplib/steps-xcode-archive/utils"
)
Expand All @@ -33,6 +33,7 @@ type Config struct {
ForceCodeSignIdentity string `env:"force_code_sign_identity"`
DisableCodesign bool `env:"disable_codesign,opt[yes,no]"`
DisableIndexWhileBuilding bool `env:"disable_index_while_building,opt[yes,no]"`
CacheLevel string `env:"cache_level,opt[none,swift_packages]"`
OutputTool string `env:"output_tool,opt[xcpretty,xcodebuild]"`
OutputDir string `env:"output_dir,dir"`

Expand All @@ -51,6 +52,11 @@ func main() {
fmt.Println()
log.Infof("Step determined configs:")

absProjectPath, err := filepath.Abs(conf.ProjectPath)
if err != nil {
fail("Failed to expand project path (%s), error: %s", conf.ProjectPath, err)
}

// Detect Xcode major version
xcodebuildVersion, err := utility.GetXcodeVersion()
if err != nil {
Expand Down Expand Up @@ -134,7 +140,7 @@ func main() {
log.Infof("Analyzing the project")

isWorkspace := false
ext := filepath.Ext(conf.ProjectPath)
ext := filepath.Ext(absProjectPath)
if ext == ".xcodeproj" {
isWorkspace = false
} else if ext == ".xcworkspace" {
Expand All @@ -143,7 +149,7 @@ func main() {
fail("Project file extension should be .xcodeproj or .xcworkspace, but got: %s", ext)
}

analyzeCmd := xcodebuild.NewCommandBuilder(conf.ProjectPath, isWorkspace, xcodebuild.AnalyzeAction)
analyzeCmd := xcodebuild.NewCommandBuilder(absProjectPath, isWorkspace, xcodebuild.AnalyzeAction)

analyzeCmd.SetDisableIndexWhileBuilding(conf.DisableIndexWhileBuilding)
analyzeCmd.SetScheme(conf.Scheme)
Expand All @@ -152,36 +158,35 @@ func main() {
analyzeCmd.SetDisableCodesign(true)
}

if outputTool == "xcpretty" {
xcprettyCmd := xcpretty.New(analyzeCmd)

logWithTimestamp(colorstring.Green, "$ %s", xcprettyCmd.PrintableCmd())
fmt.Println()
var swiftPackagesPath string
if xcodeMajorVersion >= 11 {
var err error
if swiftPackagesPath, err = cache.SwiftPackagesPath(absProjectPath); err != nil {
fail("Failed to get Swift Packages path, error: %s", err)
}
}

if rawXcodebuildOut, err := xcprettyCmd.Run(); err != nil {
rawXcodebuildOut, err := runCommandWithRetry(analyzeCmd, outputTool == "xcpretty", swiftPackagesPath)
if err != nil {
if outputTool == "xcpretty" {
log.Errorf("\nLast lines of the Xcode's build log:")
fmt.Println(stringutil.LastNLines(rawXcodebuildOut, 10))

if err := utils.ExportOutputFileContent(rawXcodebuildOut, rawXcodebuildOutputLogPath, bitriseXcodeRawResultTextEnvKey); err != nil {
log.Warnf("Failed to export %s, error: %s", bitriseXcodeRawResultTextEnvKey, err)
} else {
log.Warnf(`You can find the last couple of lines of Xcode's build log above, but the full log is also available in the raw-xcodebuild-output.log
The log file is stored in $BITRISE_DEPLOY_DIR, and its full path is available in the $BITRISE_XCODE_RAW_RESULT_TEXT_PATH environment variable
(value: %s)`, rawXcodebuildOutputLogPath)
The log file is stored in $BITRISE_DEPLOY_DIR, and its full path is available in the $BITRISE_XCODE_RAW_RESULT_TEXT_PATH environment variable
(value: %s)`, rawXcodebuildOutputLogPath)
}

fail("Analyze failed, error: %s", err)
}
} else {
logWithTimestamp(colorstring.Green, "$ %s", analyzeCmd.PrintableCmd())
fmt.Println()

analyzeRootCmd := analyzeCmd.Command()
analyzeRootCmd.SetStdout(os.Stdout)
analyzeRootCmd.SetStderr(os.Stderr)
fail("Analyze failed, error: %s", err)
}

if err := analyzeRootCmd.Run(); err != nil {
fail("Analyze failed, error: %s", err)
// Cache swift PM
if xcodeMajorVersion >= 11 && conf.CacheLevel == "swift_packages" {
if err := cache.CollectSwiftPackages(absProjectPath); err != nil {
log.Warnf("Failed to mark swift packages for caching, error: %s", err)
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ inputs:
value_options:
- "yes"
- "no"
- cache_level: swift_packages
opts:
title: Enable caching of Swift Package Manager packages
description: |-
Available options:
- `none` : Disable caching
- `swift_packages` : Cache Swift PM packages added to the Xcode project
value_options:
- "none"
- "swift_packages"
is_required: true
- output_tool: xcpretty
opts:
category: Debug
Expand Down
Loading

0 comments on commit 87a8b36

Please sign in to comment.