Skip to content

Commit 79c3cd9

Browse files
authored
fix(amplify-xcode): add files to primary target (#1313)
* fix(amplify-xcode): add files to primary target * fix(amplify-xcode): skip if file already in build phase * fix(amplify-xcode): fix typo * fix(amplify-xcode): update binary name
1 parent 481b700 commit 79c3cd9

File tree

12 files changed

+93
-32
lines changed

12 files changed

+93
-32
lines changed

AmplifyTools/AmplifyXcode/AmplifyXcode.xcodeproj/project.pbxproj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@
11171117

11181118
/* Begin PBXFileReference section */
11191119
"AEXML::AEXML::Product" /* AEXML.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = AEXML.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1120-
"AmplifyXcode::AmplifyXcode::Product" /* AmplifyXcode */ = {isa = PBXFileReference; lastKnownFileType = text; path = AmplifyXcode; sourceTree = BUILT_PRODUCTS_DIR; };
1120+
"AmplifyXcode::AmplifyXcode::Product" /* amplify-xcode */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; path = "amplify-xcode"; sourceTree = BUILT_PRODUCTS_DIR; };
11211121
"AmplifyXcode::AmplifyXcodeCore::Product" /* AmplifyXcodeCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = AmplifyXcodeCore.framework; sourceTree = BUILT_PRODUCTS_DIR; };
11221122
"AmplifyXcode::AmplifyXcodeCoreTests::Product" /* AmplifyXcodeCoreTests.xctest */ = {isa = PBXFileReference; lastKnownFileType = file; path = AmplifyXcodeCoreTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
11231123
"AmplifyXcode::AmplifyXcodeTests::Product" /* AmplifyXcodeTests.xctest */ = {isa = PBXFileReference; lastKnownFileType = file; path = AmplifyXcodeTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -2353,7 +2353,7 @@
23532353
"PathKit::PathKit::Product" /* PathKit.framework */,
23542354
"Version::Version::Product" /* Version.framework */,
23552355
"GraphViz::GraphViz::Product" /* GraphViz.framework */,
2356-
"AmplifyXcode::AmplifyXcode::Product" /* AmplifyXcode */,
2356+
"AmplifyXcode::AmplifyXcode::Product" /* amplify-xcode */,
23572357
"XcodeGen::ProjectSpec::Product" /* ProjectSpec.framework */,
23582358
"JSONUtilities::JSONUtilities::Product" /* JSONUtilities.framework */,
23592359
"AEXML::AEXML::Product" /* AEXML.framework */,
@@ -2413,7 +2413,7 @@
24132413
path = Mocks;
24142414
sourceTree = "<group>";
24152415
};
2416-
OBJ_5 /* */ = {
2416+
OBJ_5 = {
24172417
isa = PBXGroup;
24182418
children = (
24192419
OBJ_6 /* Package.swift */,
@@ -2424,7 +2424,6 @@
24242424
OBJ_400 /* BuildTools */,
24252425
OBJ_401 /* README.md */,
24262426
);
2427-
name = "";
24282427
sourceTree = "<group>";
24292428
};
24302429
OBJ_53 /* Support */ = {
@@ -2675,7 +2674,7 @@
26752674
);
26762675
name = AmplifyXcode;
26772676
productName = AmplifyXcode;
2678-
productReference = "AmplifyXcode::AmplifyXcode::Product" /* AmplifyXcode */;
2677+
productReference = "AmplifyXcode::AmplifyXcode::Product" /* amplify-xcode */;
26792678
productType = "com.apple.product-type.tool";
26802679
};
26812680
"AmplifyXcode::AmplifyXcodeCore" /* AmplifyXcodeCore */ = {
@@ -3186,7 +3185,7 @@
31863185
knownRegions = (
31873186
en,
31883187
);
3189-
mainGroup = OBJ_5 /* */;
3188+
mainGroup = OBJ_5;
31903189
productRefGroup = OBJ_380 /* Products */;
31913190
projectDirPath = "";
31923191
projectRoot = "";
@@ -4622,6 +4621,7 @@
46224621
OTHER_CFLAGS = "$(inherited)";
46234622
OTHER_LDFLAGS = "$(inherited)";
46244623
OTHER_SWIFT_FLAGS = "$(inherited)";
4624+
PRODUCT_NAME = "amplify-xcode";
46254625
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited)";
46264626
SWIFT_FORCE_DYNAMIC_LINK_STDLIB = YES;
46274627
SWIFT_FORCE_STATIC_LINK_STDLIB = NO;
@@ -4651,6 +4651,7 @@
46514651
OTHER_CFLAGS = "$(inherited)";
46524652
OTHER_LDFLAGS = "$(inherited)";
46534653
OTHER_SWIFT_FLAGS = "$(inherited)";
4654+
PRODUCT_NAME = "amplify-xcode";
46544655
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited)";
46554656
SWIFT_FORCE_DYNAMIC_LINK_STDLIB = YES;
46564657
SWIFT_FORCE_STATIC_LINK_STDLIB = NO;

AmplifyTools/AmplifyXcode/AmplifyXcode.xcodeproj/xcshareddata/xcschemes/AmplifyXcode-Package.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<BuildableReference
1616
BuildableIdentifier = "primary"
1717
BlueprintIdentifier = "AmplifyXcode::AmplifyXcode"
18-
BuildableName = "AmplifyXcode"
18+
BuildableName = "amplify-xcode"
1919
BlueprintName = "AmplifyXcode"
2020
ReferencedContainer = "container:AmplifyXcode.xcodeproj">
2121
</BuildableReference>

AmplifyTools/AmplifyXcode/AmplifyXcode.xcodeproj/xcshareddata/xcschemes/AmplifyXcode.xcscheme

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<BuildableReference
1616
BuildableIdentifier = "primary"
1717
BlueprintIdentifier = "AmplifyXcode::AmplifyXcode"
18-
BuildableName = "AmplifyXcode"
18+
BuildableName = "amplify-xcode"
1919
BlueprintName = "AmplifyXcode"
2020
ReferencedContainer = "container:AmplifyXcode.xcodeproj">
2121
</BuildableReference>
@@ -26,7 +26,8 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29-
shouldUseLaunchSchemeArgsEnv = "YES">
29+
shouldUseLaunchSchemeArgsEnv = "YES"
30+
codeCoverageEnabled = "YES">
3031
<Testables>
3132
<TestableReference
3233
skipped = "NO">
@@ -65,7 +66,7 @@
6566
<BuildableReference
6667
BuildableIdentifier = "primary"
6768
BlueprintIdentifier = "AmplifyXcode::AmplifyXcode"
68-
BuildableName = "AmplifyXcode"
69+
BuildableName = "amplify-xcode"
6970
BlueprintName = "AmplifyXcode"
7071
ReferencedContainer = "container:AmplifyXcode.xcodeproj">
7172
</BuildableReference>

AmplifyTools/AmplifyXcode/Sources/AmplifyXcode/CLICommandReportable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ extension CLICommandReportable {
1717
for task in tasks {
1818
switch task {
1919
case .success(let message):
20-
print("-- \(message)")
20+
print("-- \(message)")
2121
case .failure(let error):
22-
print("-- 🚫 \(error)")
22+
print("-- \(error)")
2323
}
2424
}
2525
}

AmplifyTools/AmplifyXcode/Sources/AmplifyXcodeCore/Commands/CommandImportConfig.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ enum ImportConfigTasks {
4444
do {
4545
try environment.addFilesToXcodeProject(projectPath: projectPath,
4646
files: configFiles,
47-
toGroup: args.configGroup)
47+
toGroup: args.configGroup,
48+
inTarget: .primary)
4849
return .success("Successfully updated project \(projectPath).")
4950
} catch {
5051
if let underlyingError = error as? AmplifyCommandError {

AmplifyTools/AmplifyXcode/Sources/AmplifyXcodeCore/Commands/CommandImportModels.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ enum CommandImportModelsTasks {
3333
try environment.addFilesToXcodeProject(
3434
projectPath: environment.basePath,
3535
files: models,
36-
toGroup: args.modelsGroup)
36+
toGroup: args.modelsGroup,
37+
inTarget: .primary)
3738

3839
let addedModels = models.map { Path($0.path).lastComponent }
3940
return .success("Successfully added models \(addedModels) to '\(args.modelsGroup)' group.")

AmplifyTools/AmplifyXcode/Sources/AmplifyXcodeCore/Environment/AmplifyCommandEnvironment.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ public protocol AmplifyCommandEnvironmentXcode {
4545
/// Given a file path, returns an XcodeProjectFile reference
4646
func createXcodeFile(withPath path: String, ofType type: XcodeProjectFileType) -> XcodeProjectFile
4747

48-
/// Reads an Xcode project file at `projectPath`, read or create a group `group` if it doesn't exist
48+
/// Reads an Xcode project file at `projectPath`, retrieves or creates a group `group` if it doesn't exist
4949
/// and adds `files` to it
50-
func addFilesToXcodeProject(projectPath: String, files: [XcodeProjectFile], toGroup group: String) throws
50+
func addFilesToXcodeProject(projectPath: String,
51+
files: [XcodeProjectFile],
52+
toGroup group: String,
53+
inTarget target: XcodeProjectTarget) throws
5154
}
5255

5356
public typealias AmplifyCommandEnvironment = AmplifyCommandEnvironmentFileManager & AmplifyCommandEnvironmentXcode

AmplifyTools/AmplifyXcode/Sources/AmplifyXcodeCore/Environment/CommandEnvironment.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,18 @@ extension CommandEnvironment {
103103
public func addFilesToXcodeProject(
104104
projectPath path: String,
105105
files: [XcodeProjectFile],
106-
toGroup group: String) throws {
106+
toGroup group: String,
107+
inTarget target: XcodeProjectTarget) throws {
107108
do {
108109
let xcodeProject = try loadFirstXcodeProject(fromDirectory: path)
109-
try xcodeProject.add(files: files, toGroup: group)
110+
try xcodeProject.add(files: files, toGroup: group, inTarget: target)
110111
try xcodeProject.synchronize()
111112
} catch {
113+
if case let XcodeProjectError.targetNotFound(name: targetName) = error {
114+
throw AmplifyCommandError(.xcodeProject,
115+
errorDescription: "Target \(targetName) not found",
116+
recoverySuggestion: "Manually add Amplify files to your Xcode project.")
117+
}
112118
throw AmplifyCommandError(.xcodeProject, error: error)
113119
}
114120
}

AmplifyTools/AmplifyXcode/Sources/AmplifyXcodeCore/Support/XcodeProj+Helpers.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,24 @@ extension XcodeProj {
3636
}
3737
}
3838

39-
// MARK: addBuildFileTo
39+
// MARK: Add files to project
4040
extension XcodeProj {
41-
func addBuildFileToResources(_ file: PBXBuildFile) {
42-
// TODO: should we find the proper build phase?
43-
pbxproj.resourcesBuildPhases.first?.files?.append(file)
41+
func targets(named targetName: String,
42+
ofType productType: PBXProductType) -> [PBXTarget] {
43+
pbxproj.targets(named: targetName).filter { $0.productType == productType }
4444
}
4545

46-
func addBuildFileToSources(_ file: PBXBuildFile) {
47-
// TODO: should we find the proper build phase?
48-
pbxproj.sourcesBuildPhases.first?.files?.append(file)
46+
func addResourceFile(_ file: PBXBuildFile, toTarget target: PBXTarget) throws {
47+
if let files = try target.resourcesBuildPhase()?.files, files.contains(file) {
48+
return
49+
}
50+
try target.resourcesBuildPhase()?.files?.append(file)
51+
}
52+
53+
func addSourceFile(_ file: PBXBuildFile, toTarget target: PBXTarget) throws {
54+
if let files = try target.sourcesBuildPhase()?.files, files.contains(file) {
55+
return
56+
}
57+
try target.sourcesBuildPhase()?.files?.append(file)
4958
}
5059
}

AmplifyTools/AmplifyXcode/Sources/AmplifyXcodeCore/Support/XcodeProject.swift

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,29 @@ import Foundation
99
import PathKit
1010
import XcodeProj
1111

12+
/// Xcode project error
1213
public enum XcodeProjectError: Error {
1314
case notFound(path: String)
1415
case noPbxProjFound
1516
case groupNotFound(group: String)
1617
case addFileFailed
18+
case targetNotFound(name: String)
1719
}
1820

21+
/// Xcode project target
22+
public enum XcodeProjectTarget {
23+
/// primary target (name matches project file)
24+
case primary
25+
case named(String)
26+
}
27+
28+
/// Xcode project file type
1929
public enum XcodeProjectFileType {
20-
case resource, source
30+
/// resource file
31+
case resource
32+
33+
/// source file (i.e. Swift, Obj-C)
34+
case source
2135
}
2236

2337
public struct XcodeProjectFile: Equatable {
@@ -48,22 +62,41 @@ struct XcodeProject {
4862
func synchronize() throws {
4963
try project.pbxproj.write(path: Path(pbxProjFilePath), override: true)
5064
}
65+
66+
private func resolveTarget(_ target: XcodeProjectTarget) throws -> PBXTarget {
67+
let targetName: String
68+
switch target {
69+
case .primary:
70+
targetName = project.mainProject()?.name ?? "primary"
71+
case .named(let name):
72+
targetName = name
73+
}
74+
75+
if let targetRef = project.targets(named: targetName, ofType: .application).first {
76+
return targetRef
77+
}
78+
79+
throw XcodeProjectError.targetNotFound(name: targetName)
80+
}
5181
}
5282

5383
// MARK: Add files
5484
extension XcodeProject {
55-
func add(files: [XcodeProjectFile], toGroup group: String) throws {
85+
func add(files: [XcodeProjectFile],
86+
toGroup group: String,
87+
inTarget target: XcodeProjectTarget) throws {
5688
guard let mainProject = project.mainProject() else {
5789
throw XcodeProjectError.noPbxProjFound
5890
}
5991

6092
let sourceRoot = Path(basePath)
6193

62-
// TODO: unwrap and check targetGroup before proceeding
6394
guard let targetGroup = try project.getOrCreateGroup(named: group, in: mainProject.mainGroup) else {
6495
throw XcodeProjectError.groupNotFound(group: group)
6596
}
6697

98+
let targetRef = try resolveTarget(target)
99+
67100
for file in files {
68101
let path = Path(file.path)
69102

@@ -73,9 +106,9 @@ extension XcodeProject {
73106

74107
switch file.type {
75108
case .resource:
76-
project.addBuildFileToResources(buildFile)
109+
try project.addResourceFile(buildFile, toTarget: targetRef)
77110
case .source:
78-
project.addBuildFileToSources(buildFile)
111+
try project.addSourceFile(buildFile, toTarget: targetRef)
79112
}
80113
}
81114
}

0 commit comments

Comments
 (0)