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

Add a new CopyFilesBuildPhase, "Embed ExtensionKit Extensions" #1230

Merged
merged 8 commits into from
Jul 21, 2022
Merged
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## Next Version
### Added
- Added a new CopyFilesBuildPhase, "Embed ExtensionKit Extensions" #1230 @mtj0928

## 2.30.0

Expand Down
19 changes: 15 additions & 4 deletions Sources/XcodeGenKit/PBXProjGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public class PBXProjGenerator {

var explicitFileType: String?
var lastKnownFileType: String?
let fileType = Xcode.fileType(path: Path(target.filename))
if target.platform == .macOS || target.platform == .watchOS || target.type == .framework {
let fileType = Xcode.fileType(path: Path(target.filename), productType: target.type)
if target.platform == .macOS || target.platform == .watchOS || target.type == .framework || target.type == .extensionKitExtension {
explicitFileType = fileType
} else {
lastKnownFileType = fileType
Expand Down Expand Up @@ -670,6 +670,7 @@ public class PBXProjGenerator {
var copyWatchReferences: [PBXBuildFile] = []
var packageDependencies: [XCSwiftPackageProductDependency] = []
var extensions: [PBXBuildFile] = []
var extensionKitExtensions: [PBXBuildFile] = []
var systemExtensions: [PBXBuildFile] = []
var appClips: [PBXBuildFile] = []
var carthageFrameworksToEmbed: [String] = []
Expand Down Expand Up @@ -735,9 +736,11 @@ public class PBXProjGenerator {
if dependency.copyPhase != nil {
// custom copy takes precedence
customCopyDependenciesReferences.append(embedFile)
} else if dependencyTarget.type.isExtension {
} else if dependencyTarget.type.isExtension && dependencyTarget.type != .extensionKitExtension {
// embed app extension
extensions.append(embedFile)
} else if dependencyTarget.type == .extensionKitExtension {
extensionKitExtensions.append(embedFile)
Copy link
Collaborator

@freddi-kit freddi-kit Jul 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to write like below to consider the other addtional future cases of dependencyTarget.type when dependencyTarget.type.isExtension is true

else if dependencyTarget.type.isExtension {
     // we may add other properties == 
     if dependencyTarget.type == .extensionKitExtension {
        extensionKitExtensions.append(embedFile)
     } else {
         extensions.append(embedFile)
     }
}

} else if dependencyTarget.type.isSystemExtension {
// embed system extension
systemExtensions.append(embedFile)
Expand Down Expand Up @@ -1156,13 +1159,21 @@ public class PBXProjGenerator {

if !extensions.isEmpty {

let copyFilesPhase = addObject(
let copyFilesPhase = addObject(
getPBXCopyFilesBuildPhase(dstSubfolderSpec: .plugins, name: "Embed App Extensions", files: extensions)
)

buildPhases.append(copyFilesPhase)
}

if !extensionKitExtensions.isEmpty {

let copyFilesPhase = addObject(
getPBXCopyFilesBuildPhase(dstSubfolderSpec: .productsDirectory, dstPath: "$(EXTENSIONS_FOLDER_PATH)", name: "Embed ExtensionKit Extensions", files: extensionKitExtensions)
)
buildPhases.append(copyFilesPhase)
}

if !systemExtensions.isEmpty {

let copyFilesPhase = addObject(
Expand Down
6 changes: 4 additions & 2 deletions Sources/XcodeGenKit/XCProjExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ extension Dictionary {

extension Xcode {

public static func fileType(path: Path) -> String? {
public static func fileType(path: Path, productType: PBXProductType? = nil) -> String? {
guard let fileExtension = path.extension else { return nil }
switch fileExtension {
switch (fileExtension, productType) {
// cases that aren't handled (yet) in XcodeProj.
case ("appex", .extensionKitExtension):
return "wrapper.extensionkit-extension"
default:
// fallback to XcodeProj defaults
return Xcode.filetype(extension: fileExtension)
Expand Down
44 changes: 44 additions & 0 deletions Tests/XcodeGenKitTests/ProjectGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,50 @@ class ProjectGeneratorTests: XCTestCase {
try expectCopyPhase(in: pbxProject, withFilePaths: ["extA.appex"], toSubFolder: .executables, dstPath: "test")
}
}

$0.context("extensionKit") {

let extA = Target(
name: "extA",
type: .extensionKitExtension,
platform: .macOS
)
let extB = Target(
name: "extB",
type: .extensionKitExtension,
platform: .macOS
)

$0.it("embeds them into plugins without copy phase spec") {

// given
let dependencies = [
Dependency(type: .target, reference: extA.name, embed: true),
Dependency(type: .target, reference: extB.name, embed: false),
]

// when
let pbxProject = try generateProjectForApp(withDependencies: dependencies, targets: [extA, extB])

// then
try expectCopyPhase(in: pbxProject, withFilePaths: ["extA.appex"], toSubFolder: .productsDirectory, dstPath: "$(EXTENSIONS_FOLDER_PATH)")
}

$0.it("embeds them into custom location with copy phase spec") {

// given
let dependencies = [
Dependency(type: .target, reference: extA.name, embed: true, copyPhase: BuildPhaseSpec.CopyFilesSettings(destination: .productsDirectory, subpath: "test", phaseOrder: .postCompile)),
Dependency(type: .target, reference: extB.name, embed: false, copyPhase: BuildPhaseSpec.CopyFilesSettings(destination: .productsDirectory, subpath: "test", phaseOrder: .postCompile)),
]

// when
let pbxProject = try generateProjectForApp(withDependencies: dependencies, targets: [extA, extB])

// then
try expectCopyPhase(in: pbxProject, withFilePaths: ["extA.appex"], toSubFolder: .productsDirectory, dstPath: "test")
}
}

$0.context("commandLineTool") {

Expand Down