Add possiblity to add resources before the sources build phase (#1351)

* feat: Add possiblity to add resources before the sources build phase;

* feat(ios): Add change to Docs + CHANGELOG;

* fix: rename resourcesBeforeSourcesBuildPhase to putResourcesBeforeSourcesBuildPhase to make it more clear;

* Update CHANGELOG.md

---------

Co-authored-by: Yonas Kolb <yonaskolb@users.noreply.github.com>
This commit is contained in:
mat1th 2023-04-25 01:57:44 +00:00 committed by GitHub
parent 988afa0f5b
commit 8256008778
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 74 additions and 7 deletions

View File

@ -5,6 +5,7 @@
### Added
- Added support for shared breakpoints #177 @alexruperez @myihsan
- Added support for `putResourcesBeforeSourcesBuildPhase` in a target #1351 @mat1th
### Fixed

View File

@ -356,6 +356,7 @@ Settings are merged in the following order: groups, base, configs.
- `DevelopmentTeam`: if all configurations have the same `DEVELOPMENT_TEAM` setting
- `ProvisioningStyle`: if all configurations have the same `CODE_SIGN_STYLE` setting
- `TestTargetID`: if all configurations have the same `TEST_TARGET_NAME` setting
- [ ] **putResourcesBeforeSourcesBuildPhase**: **Bool** - If this is `true` the `Copy Resources` step will be placed before the `Compile Sources` build step.
### Product Type

View File

@ -56,6 +56,7 @@ public struct Target: ProjectTarget {
public var attributes: [String: Any]
public var productName: String
public var onlyCopyFilesOnInstall: Bool
public var putResourcesBeforeSourcesBuildPhase: Bool
public var isLegacy: Bool {
legacy != nil
@ -94,7 +95,8 @@ public struct Target: ProjectTarget {
scheme: TargetScheme? = nil,
legacy: LegacyTarget? = nil,
attributes: [String: Any] = [:],
onlyCopyFilesOnInstall: Bool = false
onlyCopyFilesOnInstall: Bool = false,
putResourcesBeforeSourcesBuildPhase: Bool = false
) {
self.name = name
self.type = type
@ -118,6 +120,7 @@ public struct Target: ProjectTarget {
self.legacy = legacy
self.attributes = attributes
self.onlyCopyFilesOnInstall = onlyCopyFilesOnInstall
self.putResourcesBeforeSourcesBuildPhase = putResourcesBeforeSourcesBuildPhase
}
}
@ -329,6 +332,7 @@ extension Target: NamedJSONDictionaryConvertible {
legacy = jsonDictionary.json(atKeyPath: "legacy")
attributes = jsonDictionary.json(atKeyPath: "attributes") ?? [:]
onlyCopyFilesOnInstall = jsonDictionary.json(atKeyPath: "onlyCopyFilesOnInstall") ?? false
putResourcesBeforeSourcesBuildPhase = jsonDictionary.json(atKeyPath: "putResourcesBeforeSourcesBuildPhase") ?? false
}
}
@ -364,6 +368,10 @@ extension Target: JSONEncodable {
dict["onlyCopyFilesOnInstall"] = true
}
if putResourcesBeforeSourcesBuildPhase {
dict["putResourcesBeforeSourcesBuildPhase"] = true
}
return dict
}
}

View File

@ -1083,6 +1083,18 @@ public class PBXProjGenerator {
}
}
func addResourcesBuildPhase() {
let resourcesBuildPhaseFiles = getBuildFilesForPhase(.resources) + copyResourcesReferences
if !resourcesBuildPhaseFiles.isEmpty {
let resourcesBuildPhase = addObject(PBXResourcesBuildPhase(files: resourcesBuildPhaseFiles))
buildPhases.append(resourcesBuildPhase)
}
}
if target.putResourcesBeforeSourcesBuildPhase {
addResourcesBuildPhase()
}
let sourcesBuildPhaseFiles = getBuildFilesForPhase(.sources)
let shouldSkipSourcesBuildPhase = sourcesBuildPhaseFiles.isEmpty && target.type.canSkipCompileSourcesBuildPhase
if !shouldSkipSourcesBuildPhase {
@ -1092,10 +1104,8 @@ public class PBXProjGenerator {
buildPhases += try target.postCompileScripts.map { try generateBuildScript(targetName: target.name, buildScript: $0) }
let resourcesBuildPhaseFiles = getBuildFilesForPhase(.resources) + copyResourcesReferences
if !resourcesBuildPhaseFiles.isEmpty {
let resourcesBuildPhase = addObject(PBXResourcesBuildPhase(files: resourcesBuildPhaseFiles))
buildPhases.append(resourcesBuildPhase)
if !target.putResourcesBeforeSourcesBuildPhase {
addResourcesBuildPhase()
}
let swiftObjCInterfaceHeader = project.getCombinedBuildSetting("SWIFT_OBJC_INTERFACE_HEADER_NAME", target: target, config: project.configs[0]) as? String

View File

@ -1652,8 +1652,8 @@
isa = PBXNativeTarget;
buildConfigurationList = 77CE5B5E5DEAC820254D484C /* Build configuration list for PBXNativeTarget "App_macOS" */;
buildPhases = (
96BB43F4706B031DA45166E8 /* Sources */,
77D35586228BF8AB74152BB5 /* Resources */,
96BB43F4706B031DA45166E8 /* Sources */,
FB79B30FEA6073A29B4D9FCC /* CopyFiles */,
A6E1C88C073F8CC6B5B072B6 /* Frameworks */,
DE875E9A37F7CB9C347AEFA0 /* Embed System Extensions */,

View File

@ -111,6 +111,7 @@ targets:
- sdk: Contacts.framework
- sdk: libc++.tbd
- sdk: libz.dylib
putResourcesBeforeSourcesBuildPhase: true
App_iOS:
type: application

View File

@ -575,7 +575,8 @@ class ProjectSpecTests: XCTestCase {
passSettings: true,
arguments: "bar",
workingDirectory: "foo"),
attributes: ["foo": "bar"])],
attributes: ["foo": "bar"],
putResourcesBeforeSourcesBuildPhase: true)],
aggregateTargets: [AggregateTarget(name: "aggregate target",
targets: ["App"],
settings: Settings(buildSettings: ["buildSettings": "bar"],

View File

@ -1277,6 +1277,13 @@ class SpecLoadingTests: XCTestCase {
try expect(target.onlyCopyFilesOnInstall) == true
}
$0.it("parses put resources before Sources Build Phase") {
var targetSource = validTarget
targetSource["putResourcesBeforeSourcesBuildPhase"] = true
let target = try Target(name: "Embed Frameworks", jsonDictionary: targetSource)
try expect(target.putResourcesBeforeSourcesBuildPhase) == true
}
$0.it("parses settings") {
let project = try Project(path: fixturePath + "settings_test.yml")
let buildSettings: BuildSettings = ["SETTING": "value"]

View File

@ -375,6 +375,44 @@ class PBXProjGeneratorTests: XCTestCase {
try expect(testTarget?.frameworksBuildPhase()?.files?.count) == 1
try expect(testTarget?.frameworksBuildPhase()?.files?[0].platformFilter) == "ios"
}
$0.it("places resources before sources buildPhase") {
let directories = """
Sources:
- MainScreen:
- Entities:
- file.swift
- image.jpg
"""
try createDirectories(directories)
let target1 = Target(
name: "TestAll",
type: .application,
platform: .iOS,
sources: ["Sources"],
putResourcesBeforeSourcesBuildPhase: true
)
let target2 = Target(
name: "TestiOS",
type: .application,
platform: .iOS,
sources: ["Sources"],
putResourcesBeforeSourcesBuildPhase: false
)
let project = Project(basePath: directoryPath, name: "Test", targets: [target1, target2])
let pbxProj = try project.generatePbxProj()
let targets = pbxProj.projects.first?.targets
try expect(targets?.count) == 2
try expect(targets?.first?.buildPhases.first).to.beOfType(PBXResourcesBuildPhase.self)
try expect(targets?.first?.buildPhases.last).to.beOfType(PBXSourcesBuildPhase.self)
try expect(targets?.last?.buildPhases.first).to.beOfType(PBXSourcesBuildPhase.self)
try expect(targets?.last?.buildPhases.last).to.beOfType(PBXResourcesBuildPhase.self)
}
}
}
}