Compare commits

...

8 Commits

Author SHA1 Message Date
Vakhid Betrakhmadov
1415e57c87
Merge 9034be374c into f51719ce29 2024-06-03 19:04:50 +03:00
Yonas Kolb
f51719ce29
Update cache hook docs 2024-05-21 11:28:45 +10:00
Yonas Kolb
1b0720d139 Update to 2.41.0 2024-05-20 21:37:00 +10:00
Yonas Kolb
576739bcb5
Add cache command (#1476)
* add cache command

* docs: update git hook info
2024-05-20 21:32:24 +10:00
Tyler Milner
aa79a3ed0b
Fix typo in README (#1452) 2024-05-20 21:26:31 +10:00
John Flanagan
d99e448647
Include folder (SPM packages) in group sorting logic (#1466) 2024-05-17 23:14:20 +10:00
Tatsuki Otsuka
274ce7342c
Disallow the "watchOS" supported destination for multiplatform apps (#1470)
* Reject multiplatform apps that support the watchOS destination

This commit also fixes existing test cases.

* Add test cases

* Update docs

* Update changelog
2024-05-17 23:00:09 +10:00
Vakhid Betrakhmadov
9034be374c Support preferred screen capture format in scheme test action (resolves #1443) 2024-02-16 01:26:27 +00:00
22 changed files with 286 additions and 41 deletions

View File

@ -2,6 +2,20 @@
## Next Version
## 2.41.0
### Added
- Added `xcodegen cache` command that writes the cache. Useful for `post-commit` git hook integration #1476 @yonaskolb
### Changed
- Include folders in file sorting #1466 @jflan-dd
### Fixed
- Fixed `supportedDestinations` validation when it contains watchOS for multiplatform apps. #1470 @tatsuky
## 2.40.1
### Fixed

View File

@ -9,10 +9,14 @@ Absolutely. You will get the most out of XcodeGen by adding your project to your
>Note that you can run `xcodegen` as a step in your build process on CI.
## What happens when I switch branches
If files were added or removed in the new checkout you will most likely need to run `xcodegen` again so that your project will reference all your files. Unfortunately this is a manual step at the moment, but in the future this could be automated.
If files were added or removed in the new checkout you will most likely need to run `xcodegen` again so that your project will reference all your files.
For now you can always add xcodegen as a git `post-checkout` hook.
It's recommended to use `--use-cache` so that the project is not needlessly generated.
It's recommended to set up some [git hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) to automate the process:
- run `xcodegen generate --use-cache` on the following hooks. This will make sure the project is up to date when checking out, merging and rebasing
- `post-checkout`
- `post-rewrite`
- `post-merge`
- run `xcodegen cache` on `pre-commit`. This will make sure that when switching branches the cache will be updated in case you made local changes, or are ammending a commit that added a new file.
## Can I use CocoaPods
Yes, you will just need to run `pod install` after the project is generated to integrate Cocoapods changes.

View File

@ -500,7 +500,7 @@ targets:
destinationFilters: [iOS]
```
Note that the definition of supported destinations can be applied to every type of bundle making everything more easy to manage (app targets, unit tests, UI tests etc).
Note that the definition of supported destinations can be applied to almost every type of bundle making everything more easy to manage (app targets, unit tests, UI tests etc). App targets currently do not support the watchOS destination. Create a separate target using `platform` for watchOS apps. See Apple's [Configuring a multiplatform app](https://developer.apple.com/documentation/xcode/configuring-a-multiplatform-app-target) for details.
### Sources
@ -1064,6 +1064,7 @@ A multiline script can be written using the various YAML multiline methods, for
- [ ] **captureScreenshotsAutomatically**: **Bool** - indicates whether screenshots should be captured automatically while UI Testing. This defaults to true.
- [ ] **deleteScreenshotsWhenEachTestSucceeds**: **Bool** - whether successful UI tests should cause automatically-captured screenshots to be deleted. If `captureScreenshotsAutomatically` is false, this value is ignored. This defaults to true.
- [ ] **testPlans**: **[[Test Plan](#test-plan)]** - List of test plan locations that will be referenced in the scheme.
- [ ] **preferredScreenCaptureFormat**: **String** - automatic screen capture format to use while UI Testing. Possible values are `screenshots`, `screenRecording`. Default is `screenRecording`.
#### Test Target
A target can be one of a 2 types:

View File

@ -1,6 +1,6 @@
TOOL_NAME = XcodeGen
export EXECUTABLE_NAME = xcodegen
VERSION = 2.40.1
VERSION = 2.41.0
PREFIX = /usr/local
INSTALL_PATH = $(PREFIX)/bin/$(EXECUTABLE_NAME)

View File

@ -86,8 +86,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/tuist/XcodeProj.git",
"state" : {
"revision" : "6e60fb55271c80f83a186c9b1b4982fd991cfc0a",
"version" : "8.13.0"
"revision" : "3797181813ee963fe305d939232bc576d23ddbb0",
"version" : "8.15.0"
}
},
{

View File

@ -16,7 +16,7 @@ let package = Package(
.package(url: "https://github.com/yonaskolb/JSONUtilities.git", from: "4.2.0"),
.package(url: "https://github.com/kylef/Spectre.git", from: "0.9.2"),
.package(url: "https://github.com/onevcat/Rainbow.git", from: "4.0.0"),
.package(url: "https://github.com/tuist/XcodeProj.git", exact: "8.13.0"),
.package(url: "https://github.com/tuist/XcodeProj.git", exact: "8.15.0"),
.package(url: "https://github.com/jakeheis/SwiftCLI.git", from: "6.0.3"),
.package(url: "https://github.com/mxcl/Version", from: "2.0.0"),
.package(url: "https://github.com/SwiftDocOrg/GraphViz.git", exact: "0.2.0"),

View File

@ -113,7 +113,7 @@ swift run xcodegen
Add the following to your Package.swift file's dependencies:
```swift
.package(url: "https://github.com/yonaskolb/XcodeGen.git", from: "2.40.1"),
.package(url: "https://github.com/yonaskolb/XcodeGen.git", from: "2.41.0"),
```
And then import wherever needed: `import XcodeGenKit`
@ -136,7 +136,7 @@ Options:
- **--use-cache**: Used to prevent unnecessarily generating the project. If this is set, then a cache file will be written to when a project is generated. If `xcodegen` is later run but the spec and all the files it contains are the same, the project won't be generated.
- **--cache-path**: A custom path to use for your cache file. This defaults to `~/.xcodegen/cache/{PROJECT_SPEC_PATH_HASH}`
There are other commands as well such as `xcodegen dump` which lets out output the resolved spec in many different formats, or write it to a file. Use `xcodegen help` to see more detailed usage information.
There are other commands as well such as `xcodegen dump` which lets one output the resolved spec in many different formats, or write it to a file. Use `xcodegen help` to see more detailed usage information.
## Dependency Diagrams
<details>

View File

@ -203,6 +203,7 @@ public struct Scheme: Equatable {
public static let debugEnabledDefault = true
public static let captureScreenshotsAutomaticallyDefault = true
public static let deleteScreenshotsWhenEachTestSucceedsDefault = true
public static let preferredScreenCaptureFormatDefault = XCScheme.TestAction.ScreenCaptureFormat.screenRecording
public var config: String?
public var gatherCoverageData: Bool
@ -220,6 +221,7 @@ public struct Scheme: Equatable {
public var captureScreenshotsAutomatically: Bool
public var deleteScreenshotsWhenEachTestSucceeds: Bool
public var testPlans: [TestPlan]
public var preferredScreenCaptureFormat: XCScheme.TestAction.ScreenCaptureFormat
public struct TestTarget: Equatable, ExpressibleByStringLiteral {
@ -286,7 +288,8 @@ public struct Scheme: Equatable {
debugEnabled: Bool = debugEnabledDefault,
customLLDBInit: String? = nil,
captureScreenshotsAutomatically: Bool = captureScreenshotsAutomaticallyDefault,
deleteScreenshotsWhenEachTestSucceeds: Bool = deleteScreenshotsWhenEachTestSucceedsDefault
deleteScreenshotsWhenEachTestSucceeds: Bool = deleteScreenshotsWhenEachTestSucceedsDefault,
preferredScreenCaptureFormat: XCScheme.TestAction.ScreenCaptureFormat = preferredScreenCaptureFormatDefault
) {
self.config = config
self.gatherCoverageData = gatherCoverageData
@ -304,6 +307,7 @@ public struct Scheme: Equatable {
self.customLLDBInit = customLLDBInit
self.captureScreenshotsAutomatically = captureScreenshotsAutomatically
self.deleteScreenshotsWhenEachTestSucceeds = deleteScreenshotsWhenEachTestSucceeds
self.preferredScreenCaptureFormat = preferredScreenCaptureFormat
}
public var shouldUseLaunchSchemeArgsEnv: Bool {
@ -620,6 +624,7 @@ extension Scheme.Test: JSONObjectConvertible {
customLLDBInit = jsonDictionary.json(atKeyPath: "customLLDBInit")
captureScreenshotsAutomatically = jsonDictionary.json(atKeyPath: "captureScreenshotsAutomatically") ?? Scheme.Test.captureScreenshotsAutomaticallyDefault
deleteScreenshotsWhenEachTestSucceeds = jsonDictionary.json(atKeyPath: "deleteScreenshotsWhenEachTestSucceeds") ?? Scheme.Test.deleteScreenshotsWhenEachTestSucceedsDefault
preferredScreenCaptureFormat = jsonDictionary.json(atKeyPath: "preferredScreenCaptureFormat") ?? Scheme.Test.preferredScreenCaptureFormatDefault
}
}
@ -662,6 +667,10 @@ extension Scheme.Test: JSONEncodable {
dict["deleteScreenshotsWhenEachTestSucceeds"] = deleteScreenshotsWhenEachTestSucceeds
}
if preferredScreenCaptureFormat != Scheme.Test.preferredScreenCaptureFormatDefault {
dict["preferredScreenCaptureFormat"] = preferredScreenCaptureFormat.toJSONValue()
}
return dict
}
}
@ -1013,3 +1022,9 @@ extension XCScheme.LaunchAction.GPUValidationMode: JSONEncodable {
}
}
}
extension XCScheme.TestAction.ScreenCaptureFormat: JSONEncodable {
public func toJSONValue() -> Any {
rawValue
}
}

View File

@ -185,6 +185,12 @@ extension Project {
errors.append(.unexpectedTargetPlatformForSupportedDestinations(target: target.name, platform: target.platform))
}
if let supportedDestinations = target.supportedDestinations,
target.type.isApp,
supportedDestinations.contains(.watchOS) {
errors.append(.containsWatchOSDestinationForMultiplatformApp(target: target.name))
}
if target.supportedDestinations?.contains(.macOS) == true,
target.supportedDestinations?.contains(.macCatalyst) == true {

View File

@ -19,6 +19,7 @@ public struct SpecValidationError: Error, CustomStringConvertible {
case invalidTargetSchemeTest(target: String, testTarget: String)
case invalidTargetPlatformForSupportedDestinations(target: String)
case unexpectedTargetPlatformForSupportedDestinations(target: String, platform: Platform)
case containsWatchOSDestinationForMultiplatformApp(target: String)
case multipleMacPlatformsInSupportedDestinations(target: String)
case missingTargetPlatformInSupportedDestinations(target: String, platform: Platform)
case invalidSchemeTarget(scheme: String, target: String, action: String)
@ -66,6 +67,8 @@ public struct SpecValidationError: Error, CustomStringConvertible {
return "Target \(target.quoted) has multiple definitions of mac platforms in supported destinations"
case let .missingTargetPlatformInSupportedDestinations(target, platform):
return "Target \(target.quoted) has platform \(platform.rawValue.quoted) that is missing in supported destinations"
case let .containsWatchOSDestinationForMultiplatformApp(target):
return "Multiplatform app \(target.quoted) cannot contain watchOS in \"supportedDestinations\". Create a separate target using \"platform\" for watchOS apps"
case let .invalidConfigFile(configFile, config):
return "Invalid config file \(configFile.quoted) for config \(config.quoted)"
case let .invalidSchemeTarget(scheme, target, action):

View File

@ -3,6 +3,6 @@ import ProjectSpec
import XcodeGenCLI
import Version
let version = Version("2.40.1")
let version = Version("2.41.0")
let cli = XcodeGenCLI(version: version)
cli.execute()

View File

@ -0,0 +1,44 @@
import Foundation
import PathKit
import ProjectSpec
import SwiftCLI
import XcodeGenKit
import XcodeProj
import Version
class CacheCommand: ProjectCommand {
@Key("--cache-path", description: "Where the cache file will be loaded from and save to. Defaults to ~/.xcodegen/cache/{SPEC_PATH_HASH}")
var cacheFilePath: Path?
init(version: Version) {
super.init(version: version,
name: "cache",
shortDescription: "Write the project cache")
}
override func execute(specLoader: SpecLoader, projectSpecPath: Path, project: Project) throws {
let cacheFilePath = self.cacheFilePath ?? Path("~/.xcodegen/cache/\(projectSpecPath.absolute().string.md5)").absolute()
var cacheFile: CacheFile?
// generate cache
do {
cacheFile = try specLoader.generateCacheFile()
} catch {
throw GenerationError.projectSpecParsingError(error)
}
// write cache
if let cacheFile = cacheFile {
do {
try cacheFilePath.parent().mkpath()
try cacheFilePath.write(cacheFile.string)
success("Wrote cache to \(cacheFilePath)")
} catch {
info("Failed to write cache: \(error.localizedDescription)")
}
}
}
}

View File

@ -49,7 +49,7 @@ class DumpCommand: ProjectCommand {
try file.parent().mkpath()
try file.write(output)
} else {
stdout.print(output)
success(output)
}
}
}

View File

@ -8,9 +8,6 @@ import Version
class GenerateCommand: ProjectCommand {
@Flag("-q", "--quiet", description: "Suppress all informational and success output")
var quiet: Bool
@Flag("-c", "--use-cache", description: "Use a cache for the xcodegen spec. This will prevent unnecessarily generating the project if nothing has changed")
var useCache: Bool
@ -46,7 +43,7 @@ class GenerateCommand: ProjectCommand {
Path("~/.xcodegen/cache/\(projectSpecPath.absolute().string.md5)").absolute()
var cacheFile: CacheFile?
// read cache
// generate cache
if useCache || self.cacheFilePath != nil {
do {
cacheFile = try specLoader.generateCacheFile()
@ -138,22 +135,4 @@ class GenerateCommand: ProjectCommand {
try Task.run(bash: command, directory: projectDirectory.absolute().string)
}
}
func info(_ string: String) {
if !quiet {
stdout.print(string)
}
}
func warning(_ string: String) {
if !quiet {
stdout.print(string.yellow)
}
}
func success(_ string: String) {
if !quiet {
stdout.print(string.green)
}
}
}

View File

@ -12,6 +12,9 @@ class ProjectCommand: Command {
let name: String
let shortDescription: String
@Flag("-q", "--quiet", description: "Suppress all informational and success output")
var quiet: Bool
@Key("-s", "--spec", description: "The path to the project spec file. Defaults to project.yml. (It is also possible to link to multiple spec files by comma separating them. Note that all other flags will be the same.)")
var spec: String?
@ -58,4 +61,22 @@ class ProjectCommand: Command {
}
func execute(specLoader: SpecLoader, projectSpecPath: Path, project: Project) throws {}
func info(_ string: String) {
if !quiet {
stdout.print(string)
}
}
func warning(_ string: String) {
if !quiet {
stdout.print(string.yellow)
}
}
func success(_ string: String) {
if !quiet {
stdout.print(string.green)
}
}
}

View File

@ -15,6 +15,7 @@ public class XcodeGenCLI {
description: "Generates Xcode projects",
commands: [
generateCommand,
CacheCommand(version: version),
DumpCommand(version: version),
]
)

View File

@ -631,8 +631,8 @@ public class PBXProjGenerator {
}
if let order = groupOrdering?.order {
let files = group.children.filter { $0 is PBXFileReference }
var groups = group.children.filter { $0 is PBXGroup }
let files = group.children.filter { !$0.isGroupOrFolder }
var groups = group.children.filter { $0.isGroupOrFolder }
var filteredGroups = [PBXFileElement]()
@ -1626,6 +1626,10 @@ extension Platform {
}
extension PBXFileElement {
/// - returns: `true` if the element is a group or a folder reference. Likely an SPM package.
var isGroupOrFolder: Bool {
self is PBXGroup || (self as? PBXFileReference)?.lastKnownFileType == "folder"
}
public func getSortOrder(groupSortPosition: SpecOptions.GroupSortPosition) -> Int {
if type(of: self).isa == "PBXGroup" {

View File

@ -306,6 +306,7 @@ public class SchemeGenerator {
language: scheme.test?.language,
region: scheme.test?.region,
systemAttachmentLifetime: scheme.test?.systemAttachmentLifetime,
preferredScreenCaptureFormat: scheme.test?.preferredScreenCaptureFormat,
customLLDBInitFile: scheme.test?.customLLDBInit
)

View File

@ -203,6 +203,32 @@ class ProjectSpecTests: XCTestCase {
try expectValidationError(project, .unexpectedTargetPlatformForSupportedDestinations(target: "target1", platform: .watchOS))
}
$0.it("watchOS in multiplatform app's supported destinations") {
var project = baseProject
project.targets = [
Target(
name: "target1",
type: .application,
platform: .auto,
supportedDestinations: [.watchOS]
)
]
try expectValidationError(project, .containsWatchOSDestinationForMultiplatformApp(target: "target1"))
}
$0.it("watchOS in non-app's supported destinations") {
var project = baseProject
project.targets = [
Target(
name: "target1",
type: .framework,
platform: .auto,
supportedDestinations: [.watchOS]
)
]
try expectNoValidationError(project, .containsWatchOSDestinationForMultiplatformApp(target: "target1"))
}
$0.it("multiple definitions of mac platform in supported destinations") {
var project = baseProject
project.targets = [

View File

@ -259,6 +259,96 @@ class PBXProjGeneratorTests: XCTestCase {
.map { $0.nameOrPath }
try expect(screenGroups) == ["mainScreen1.swift", "mainScreen2.swift", "View", "Presenter", "Interactor", "Entities", "Assembly"]
}
$0.it("sorts SPM packages") {
var options = SpecOptions()
options.groupSortPosition = .top
options.groupOrdering = [
GroupOrdering(
order: [
"Sources",
"Resources",
"Tests",
"Packages",
"Support files",
"Configurations",
]
),
GroupOrdering(
pattern: "Packages",
order: [
"FeatureA",
"FeatureB",
"Common",
]
),
]
let directories = """
Configurations:
- file.swift
Resources:
- file.swift
Sources:
- MainScreen:
- mainScreen1.swift
- mainScreen2.swift
- Assembly:
- file.swift
- Entities:
- file.swift
- Interactor:
- file.swift
- Presenter:
- file.swift
- View:
- file.swift
Support files:
- file.swift
Packages:
- Common:
- Package.swift
- FeatureA:
- Package.swift
- FeatureB:
- Package.swift
Tests:
- file.swift
UITests:
- file.swift
"""
try createDirectories(directories)
let target = Target(name: "Test", type: .application, platform: .iOS, sources: ["Configurations", "Resources", "Sources", "Support files", "Tests", "UITests"])
let project = Project(
basePath: directoryPath,
name: "Test",
targets: [target],
packages: [
"Common": .local(path: "Packages/Common", group: nil),
"FeatureA": .local(path: "Packages/FeatureA", group: nil),
"FeatureB": .local(path: "Packages/FeatureB", group: nil),
],
options: options
)
let projGenerator = PBXProjGenerator(project: project)
let pbxProj = try project.generatePbxProj()
let group = try pbxProj.getMainGroup()
projGenerator.setupGroupOrdering(group: group)
let mainGroups = group.children.map { $0.nameOrPath }
try expect(mainGroups) == ["Sources", "Resources", "Tests", "Packages", "Support files", "Configurations", "UITests", "Products"]
let packages = group.children
.first { $0.nameOrPath == "Packages" }
.flatMap { $0 as? PBXGroup }?
.children
.map(\.nameOrPath)
try expect(packages) == ["FeatureA", "FeatureB", "Common"]
}
}
}

View File

@ -485,8 +485,8 @@ class ProjectGeneratorTests: XCTestCase {
try expect(targetConfig1.buildSettings["CODE_SIGN_IDENTITY"] as? String) == "iPhone Developer"
}
$0.it("supportedDestinations merges settings - iOS, watchOS") {
let target = Target(name: "Target", type: .application, platform: .auto, supportedDestinations: [.iOS, .watchOS])
$0.it("supportedDestinations merges settings - iOS, watchOS (framework)") {
let target = Target(name: "Target", type: .framework, platform: .auto, supportedDestinations: [.iOS, .watchOS])
let project = Project(name: "", targets: [target])
let pbxProject = try project.generatePbxProj()
@ -499,8 +499,8 @@ class ProjectGeneratorTests: XCTestCase {
try expect(targetConfig1.buildSettings["SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD"] as? Bool) == true
}
$0.it("supportedDestinations merges settings - visionOS, watchOS") {
let target = Target(name: "Target", type: .application, platform: .auto, supportedDestinations: [.visionOS, .watchOS])
$0.it("supportedDestinations merges settings - visionOS, watchOS (framework)") {
let target = Target(name: "Target", type: .framework, platform: .auto, supportedDestinations: [.visionOS, .watchOS])
let project = Project(name: "", targets: [target])
let pbxProject = try project.generatePbxProj()

View File

@ -568,6 +568,42 @@ class SchemeGeneratorTests: XCTestCase {
.init(reference: "container:\(testPlanPath2)", default: true),
]
}
$0.it("generates scheme with screenshots as preferred screen capture format") {
let scheme = Scheme(
name: "MyScheme",
build: Scheme.Build(targets: [buildTarget]),
run: Scheme.Run(config: "Debug"),
test: Scheme.Test(config: "Debug", preferredScreenCaptureFormat: .screenshots)
)
let project = Project(
name: "test",
targets: [app, framework],
schemes: [scheme]
)
let xcodeProject = try project.generateXcodeProject()
let xcscheme = try unwrap(xcodeProject.sharedData?.schemes.first)
try expect(xcscheme.testAction?.preferredScreenCaptureFormat) == .screenshots
}
$0.it("generates scheme with screen recording as preferred screen capture format") {
let scheme = Scheme(
name: "MyScheme",
build: Scheme.Build(targets: [buildTarget]),
run: Scheme.Run(config: "Debug"),
test: Scheme.Test(config: "Debug", preferredScreenCaptureFormat: .screenRecording)
)
let project = Project(
name: "test",
targets: [app, framework],
schemes: [scheme]
)
let xcodeProject = try project.generateXcodeProject()
let xcscheme = try unwrap(xcodeProject.sharedData?.schemes.first)
try expect(xcscheme.testAction?.preferredScreenCaptureFormat) == .screenRecording
}
}
}