Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3/x] Fix package manager integrations #263

Merged
merged 8 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ jobs:
GH_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
run: Sources/MockingbirdAutomationCli/buildAndRun.sh test example ${{ matrix.type }}

test-cli-launcher:
name: Test CLI Launcher
runs-on: macOS-latest
steps:
- uses: actions/checkout@v2
- name: Test
env:
GH_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
MKB_ARTIFACTS_URL: https://github.com/birdrides/mockingbird/releases/download/0.19.0/<FILE>
run: ./mockingbird version

build-cocoapods:
name: Build CocoaPods
runs-on: macOS-latest
Expand Down
122 changes: 68 additions & 54 deletions Mockingbird.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

32 changes: 18 additions & 14 deletions MockingbirdFramework.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,28 @@ Pod::Spec.new do |s|
s.tvos.deployment_target = '9.0'
s.watchos.deployment_target = '7.4'
s.swift_version = '5.0'
s.preserve_paths = [
s.frameworks = 'XCTest'

s.user_target_xcconfig = {
'FRAMEWORK_SEARCH_PATHS' => '$(PLATFORM_DIR)/Developer/Library/Frameworks',
}

s.pod_target_xcconfig = {
'ENABLE_BITCODE' => 'NO',
'ENABLE_TESTABILITY' => 'YES',
}

s.source_files = [
'Sources/MockingbirdFramework/**/*.{swift,h,m,mm}',
'Sources/MockingbirdCommon/**/*.swift',
]

s.preserve_paths = [
'README.md',
'LICENSE.md',
'mockingbird',
'Sources/MockingbirdAutomationCli/Resources/CodesigningRequirements/*',
'Sources/MockingbirdFramework/Info.plist',
'Sources/MockingbirdCli/Info.plist',
]

s.subspec 'Common' do |common|
common.source_files = 'Sources/MockingbirdCommon/**/*.swift'
end

s.subspec 'Core' do |core|
core.dependency 'MockingbirdFramework/Common'
core.source_files = 'Sources/MockingbirdFramework/**/*.{swift,h,m,mm}'
core.exclude_files = 'Sources/MockingbirdFramework/Utilities/ExportedModules.swift'
core.frameworks = 'XCTest'
core.user_target_xcconfig = { 'FRAMEWORK_SEARCH_PATHS' => '$(PLATFORM_DIR)/Developer/Library/Frameworks' }
core.pod_target_xcconfig = { 'ENABLE_BITCODE' => 'NO', 'ENABLE_TESTABILITY' => 'YES' }
end
end
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ if ProcessInfo.processInfo.environment["MKB_BUILD_EXECUTABLES"] != "1" {
targets: [
.target(
name: "Mockingbird",
dependencies: ["MockingbirdBridge", "MockingbirdCommon"],
path: "Sources/MockingbirdFramework",
exclude: ["Objective-C"],
dependencies: ["MockingbirdBridge"],
path: "Sources",
exclude: ["MockingbirdFramework/Objective-C"],
sources: ["MockingbirdFramework", "MockingbirdCommon"],
swiftSettings: [.define("MKB_SWIFTPM")],
linkerSettings: [.linkedFramework("XCTest")]),
.target(
Expand Down Expand Up @@ -61,6 +62,7 @@ if ProcessInfo.processInfo.environment["MKB_BUILD_EXECUTABLES"] != "1" {
.package(url: "https://github.com/weichsel/ZIPFoundation.git", .exact("0.9.14")),
],
targets: [
.target(name: "MockingbirdCommon"),
.target(
name: "MockingbirdCli",
dependencies: [
Expand Down Expand Up @@ -102,5 +104,3 @@ if ProcessInfo.processInfo.environment["MKB_BUILD_EXECUTABLES"] != "1" {
]
)
}

package.targets.append(.target(name: "MockingbirdCommon", path: "Sources/MockingbirdCommon"))
12 changes: 12 additions & 0 deletions Sources/MockingbirdAutomation/Interop/Git.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import PathKit

public enum Git {
public static func getHEAD(short: Bool = false, repository: Path) throws -> String {
var options: [String] = []
if short { options.append("--short") }
let (rev, _) = try Subprocess("git", ["rev-parse", "HEAD"] + options,
workingDirectory: repository).runWithStringOutput()
return rev.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
5 changes: 4 additions & 1 deletion Sources/MockingbirdAutomation/Interop/SwiftPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ public enum SwiftPackage {
}
}

public static func update(package: Path) throws {
public static func update(package: Path,
environment: [String: String] = ProcessInfo.processInfo.environment,
packageConfiguration: PackageConfiguration? = nil) throws {
try Subprocess("xcrun", ["swift", "package", "update"],
environment: packageConfiguration?.getEnvironment(environment) ?? environment,
workingDirectory: package.parent()).run()
}

Expand Down
16 changes: 16 additions & 0 deletions Sources/MockingbirdAutomation/Utilities/Path+Backup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation
import PathKit

public extension Path {
func backup(fileExtension: String = "bak") throws {
let backupPath = Path(absolute().string + "." + fileExtension)
try? backupPath.delete()
try copy(backupPath)
}

func restore(fileExtension: String = "bak") throws {
let copy = Path(absolute().string)
try? copy.delete()
try Path(absolute().string + "." + fileExtension).move(self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ extension Build {
let frameworkPath = projectPath.parent() + "Carthage/Build/Mockingbird.xcframework"

if let location = globalOptions.archiveLocation {
try archive(artifacts: [("", frameworkPath)], destination: Path(location))
try archive(artifacts: [("", frameworkPath)],
destination: Path(location),
includeLicense: false)
}
}
}
Expand Down
106 changes: 91 additions & 15 deletions Sources/MockingbirdAutomationCli/Commands/TestExampleProject.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ArgumentParser
import MockingbirdAutomation
import MockingbirdCommon
import PathKit
import MockingbirdAutomation
import Foundation

extension Test {
Expand All @@ -23,6 +23,23 @@ extension Test {
case spmPackage = "spm-package"
}

static func applyLocallyBuiltCli(binPath: Path) throws {
try? binPath.delete()
try binPath.mkpath()
let cliPath = try SwiftPackage.build(target: .product(name: "mockingbird"),
configuration: .debug,
package: Path("Package.swift"))
try cliPath.copy(binPath + "mockingbird")
let cliLibrariesPath = Path("Sources/MockingbirdCli/Resources/Libraries")
try cliLibrariesPath.copy(binPath + cliLibrariesPath.lastComponent)
}

static func backup(_ files: [Path], block: () throws -> Void) throws {
try files.forEach({ try $0.backup() })
defer { files.forEach({ try? $0.restore() }) }
try block()
}

struct TestCocoaPods: ParsableCommand {
static var configuration = CommandConfiguration(
commandName: "cocoapods",
Expand All @@ -33,11 +50,33 @@ extension Test {
logError("Unable to create simulator")
return
}
let workspacePath = Path("Examples/CocoaPodsExample/CocoaPodsExample.xcworkspace")
try CocoaPods.install(workspace: workspacePath)
try XcodeBuild.test(target: .scheme(name: "CocoaPodsExample"),
project: .workspace(path: workspacePath),
destination: .iOSSimulator(deviceUUID: uuid))

let srcroot = Path("Examples/CocoaPodsExample")
let workspacePath = srcroot + "CocoaPodsExample.xcworkspace"
let podfilePath = srcroot + "Podfile"
try backup([podfilePath, srcroot + "Podfile.lock"]) {
// Point to the local revision.
let rev = try Git.getHEAD(repository: Path.current)
let podfileContents = try podfilePath.read()
.replacingOccurrences(of: #"pod 'MockingbirdFramework', '~> [\d\.]+'"#,
with: "pod 'MockingbirdFramework', " +
":git => '\(Path.current.absolute())', " +
":commit => '\(rev)'",
options: [.regularExpression])
try podfilePath.delete()
try podfilePath.write(podfileContents)

// Pull and build the framework.
try CocoaPods.install(workspace: workspacePath)

// Inject the local binary.
let binPath = srcroot + "Pods/MockingbirdFramework/bin/\(mockingbirdVersion)"
try applyLocallyBuiltCli(binPath: binPath)

try XcodeBuild.test(target: .scheme(name: "CocoaPodsExample"),
project: .workspace(path: workspacePath),
destination: .iOSSimulator(deviceUUID: uuid))
}
}
}
}
Expand All @@ -52,11 +91,28 @@ extension Test {
logError("Unable to create simulator")
return
}
let projectPath = Path("Examples/CarthageExample/CarthageExample.xcodeproj")
try Carthage.update(platforms: [.iOS], project: projectPath)
try XcodeBuild.test(target: .scheme(name: "CarthageExample"),
project: .project(path: projectPath),
destination: .iOSSimulator(deviceUUID: uuid))

let srcroot = Path("Examples/CarthageExample")
let cartfilePath = srcroot + "Cartfile"
try backup([cartfilePath, srcroot + "Cartfile.resolved"]) {
// Point to the local revision.
try cartfilePath.write("""
git "file://\(Path.current.absolute())" "HEAD"
""")

// Pull and build the framework.
try? (srcroot + "Carthage").delete()
let projectPath = srcroot + "CarthageExample.xcodeproj"
try Carthage.update(platforms: [.iOS], project: projectPath)

// Inject the local binary.
let binPath = srcroot + "Carthage/Checkouts/mockingbird/bin/\(mockingbirdVersion)"
try applyLocallyBuiltCli(binPath: binPath)

try XcodeBuild.test(target: .scheme(name: "CarthageExample"),
project: .project(path: projectPath),
destination: .iOSSimulator(deviceUUID: uuid))
}
}
}
}
Expand Down Expand Up @@ -85,10 +141,30 @@ extension Test {
commandName: "spm-package",
abstract: "Test the SwiftPM example package.")
func run() throws {
let packagePath = Path("Examples/SPMPackageExample/Package.swift")
try SwiftPackage.update(package: packagePath)
try Subprocess("./gen-mocks.sh", workingDirectory: packagePath.parent()).run()
try SwiftPackage.test(package: packagePath)
let srcroot = Path("Examples/SPMPackageExample")
let packagePath = srcroot + "Package.swift"
try backup([packagePath, srcroot + "Package.resolved"]) {
// Point to the local revision.
let rev = try Git.getHEAD(repository: Path.current)
let packageContents = try packagePath.read()
.replacingOccurrences(of: "https://github.com/birdrides/mockingbird.git",
with: Path.current.absolute().string)
.replacingOccurrences(of: #"\.upToNextMinor\(from: "[\d\.]+"\)"#,
with: ".revision(\(doubleQuoted: rev))",
options: [.regularExpression])
try packagePath.delete()
try packagePath.write(packageContents)

// Pull and build the framework.
try SwiftPackage.update(package: packagePath, packageConfiguration: .libraries)

// Inject the local binary.
let binPath = srcroot + ".build/checkouts/mockingbird/bin/\(mockingbirdVersion)"
try applyLocallyBuiltCli(binPath: binPath)

try Subprocess("./gen-mocks.sh", workingDirectory: packagePath.parent()).run()
try SwiftPackage.test(packageConfiguration: .libraries, package: packagePath)
}
}
}
}
Expand Down

This file was deleted.

8 changes: 6 additions & 2 deletions mockingbird
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
# - MKB_NO_VERIFY: Disable verifying the code signature of downloaded artifacts.

set -eu
cd "$(dirname "$0")"

readonly srcroot="$(dirname "$0")"
pushd .
cd "${srcroot}"

function getVersion {
/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' Sources/MockingbirdCli/Info.plist
Expand Down Expand Up @@ -54,4 +57,5 @@ if [[ ! -x "${executable}" ]]; then
chmod u+x "${executable}"
fi

MKB_LAUNCHER="$0" "${executable}" "$@"
popd
MKB_LAUNCHER="$0" "${srcroot}/${executable}" "$@"