Skip to content

Commit

Permalink
Add a new CopyFilesBuildPhase, "Embed ExtensionKit Extensions" (#1230)
Browse files Browse the repository at this point in the history
* Embed ExtensionKit Extensions

* Fix explicitFileType for extensionKit

* Update ChangeLog

* Fix if statement structure

* Add a new example extension to Tests/Fixtures/TestProject/

* Update Tests/Fixtures/TestProject/Project.xcodeproj

* Comment out example for extension kit extension in Tests/Fixtures/TestProject/

* Update Tests/Fixtures/TestProject/Project.xcodeproj
  • Loading branch information
mtj0928 authored Jul 21, 2022
1 parent c1d5c65 commit da8aad0
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 7 deletions.
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
24 changes: 19 additions & 5 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 @@ -736,8 +737,13 @@ public class PBXProjGenerator {
// custom copy takes precedence
customCopyDependenciesReferences.append(embedFile)
} else if dependencyTarget.type.isExtension {
// embed app extension
extensions.append(embedFile)
if dependencyTarget.type == .extensionKitExtension {
// embed extension kit extension
extensionKitExtensions.append(embedFile)
} else {
// embed app extension
extensions.append(embedFile)
}
} else if dependencyTarget.type.isSystemExtension {
// embed system extension
systemExtensions.append(embedFile)
Expand Down Expand Up @@ -1156,13 +1162,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import AppIntents

@main
struct EntryPoint: AppIntentsExtension {
}
11 changes: 11 additions & 0 deletions Tests/Fixtures/TestProject/ExtensionKit Extension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EXAppExtensionAttributes</key>
<dict>
<key>EXExtensionPointIdentifier</key>
<string>com.apple.appintents-extension</string>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import AppIntents

struct Intent: AppIntent {
static var title: LocalizedStringResource = "Intent"

func perform() async throws -> some IntentResult {
return .result()
}
}
9 changes: 9 additions & 0 deletions Tests/Fixtures/TestProject/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ targets:
- package: Swinject
product: Swinject
platformFilter: iOS
# https://github.com/yonaskolb/XcodeGen/issues/1232
# After GitHub Actions start supporting Xcode 14, an example for extensionKit should be added.
# - target: ExtensionKitExtension
onlyCopyFilesOnInstall: true
scheme:
testTargets:
Expand Down Expand Up @@ -390,6 +393,12 @@ targets:
sources: App_Clip_UITests
dependencies:
- target: App_Clip
# https://github.com/yonaskolb/XcodeGen/issues/1232
# After GitHub Actions start supporting Xcode 14, an example for extensionKit should be added.
# ExtensionKitExtension:
# type: extensionkit-extension
# platform: iOS
# sources: ExtensionKit Extension

schemes:
Framework:
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

0 comments on commit da8aad0

Please sign in to comment.