From 8303947b82144d3808d129c795befe7b896ce4f6 Mon Sep 17 00:00:00 2001 From: ryohey Date: Sun, 12 Jul 2020 17:25:15 +0900 Subject: [PATCH] Fix Build Script Order --- Sources/XcodeGenKit/SpecGenerator.swift | 32 ++++++++-- .../project.pbxproj | 60 +++++++++++++++++++ .../XcodeGenKitTests/SpecGeneratorTests.swift | 24 ++++++-- 3 files changed, 108 insertions(+), 8 deletions(-) diff --git a/Sources/XcodeGenKit/SpecGenerator.swift b/Sources/XcodeGenKit/SpecGenerator.swift index c01480f6..b9b5ea60 100644 --- a/Sources/XcodeGenKit/SpecGenerator.swift +++ b/Sources/XcodeGenKit/SpecGenerator.swift @@ -320,9 +320,31 @@ private func generateTargetSpec(target: PBXNativeTarget, mainGroup: PBXGroup, so let targetSources = sources + headers + implicitHeaders + resources - let buildScripts = target.buildPhases - .compactMap { $0 as? PBXShellScriptBuildPhase } - .map(BuildScript.init) + var preBuildScripts = [BuildScript]() + var postCompileScripts: [BuildScript]? + var postBuildScripts: [BuildScript]? + + for buildPhase in target.buildPhases { + if postBuildScripts != nil { + if let buildPhase = buildPhase as? PBXShellScriptBuildPhase { + postBuildScripts?.append(BuildScript(buildPhase: buildPhase)) + } + } else if postCompileScripts != nil { + // Scripts between the compile and non-script phases + if let buildPhase = buildPhase as? PBXShellScriptBuildPhase { + postCompileScripts?.append(BuildScript(buildPhase: buildPhase)) + } else { + postBuildScripts = [BuildScript]() + } + } else { + // Script before the compile phase + if buildPhase is PBXSourcesBuildPhase { + postCompileScripts = [BuildScript]() + } else if let buildPhase = buildPhase as? PBXShellScriptBuildPhase { + preBuildScripts.append(BuildScript(buildPhase: buildPhase)) + } + } + } let buildRules = target.buildRules.map(BuildRule.init) @@ -336,7 +358,9 @@ private func generateTargetSpec(target: PBXNativeTarget, mainGroup: PBXGroup, so settings: target.settings, sources: targetSources, dependencies: dependencies, - postBuildScripts: buildScripts, + preBuildScripts: preBuildScripts, + postCompileScripts: postCompileScripts ?? [], + postBuildScripts: postBuildScripts ?? [], buildRules: buildRules) } diff --git a/Tests/Fixtures/MigrationTestProject/MigrationTestProject.xcodeproj/project.pbxproj b/Tests/Fixtures/MigrationTestProject/MigrationTestProject.xcodeproj/project.pbxproj index 63a75599..56db424c 100644 --- a/Tests/Fixtures/MigrationTestProject/MigrationTestProject.xcodeproj/project.pbxproj +++ b/Tests/Fixtures/MigrationTestProject/MigrationTestProject.xcodeproj/project.pbxproj @@ -244,10 +244,13 @@ isa = PBXNativeTarget; buildConfigurationList = D9BD320524BAE14C0056ACD2 /* Build configuration list for PBXNativeTarget "MigrationTestProject" */; buildPhases = ( + D9BD326124BAF0DE0056ACD2 /* Pre Build Script */, D9BD31E224BAE14A0056ACD2 /* Sources */, + D9BD326224BAF48F0056ACD2 /* Post Compile Script */, D9BD31E324BAE14A0056ACD2 /* Frameworks */, D9BD31E424BAE14A0056ACD2 /* Resources */, D9BD323024BAE7340056ACD2 /* Embed Frameworks */, + D9BD326024BAF0720056ACD2 /* Post Build Script */, ); buildRules = ( ); @@ -395,6 +398,63 @@ }; /* End PBXResourcesBuildPhase section */ +/* Begin PBXShellScriptBuildPhase section */ + D9BD326024BAF0720056ACD2 /* Post Build Script */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + ); + name = "Post Build Script"; + outputFileListPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "# Type a script or drag a script file from your workspace to insert its path.\necho \"post build script\"\n"; + }; + D9BD326124BAF0DE0056ACD2 /* Pre Build Script */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + ); + name = "Pre Build Script"; + outputFileListPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "# Type a script or drag a script file from your workspace to insert its path.\necho \"pre build script\"\n"; + }; + D9BD326224BAF48F0056ACD2 /* Post Compile Script */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + ); + name = "Post Compile Script"; + outputFileListPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "# Type a script or drag a script file from your workspace to insert its path.\necho \"post compile script\"\n"; + }; +/* End PBXShellScriptBuildPhase section */ + /* Begin PBXSourcesBuildPhase section */ D9BD31E224BAE14A0056ACD2 /* Sources */ = { isa = PBXSourcesBuildPhase; diff --git a/Tests/XcodeGenKitTests/SpecGeneratorTests.swift b/Tests/XcodeGenKitTests/SpecGeneratorTests.swift index bf03cf43..0e154e40 100644 --- a/Tests/XcodeGenKitTests/SpecGeneratorTests.swift +++ b/Tests/XcodeGenKitTests/SpecGeneratorTests.swift @@ -4,8 +4,22 @@ import TestSupport import XcodeProj import PathKit import Yams +import ProjectSpec class SpecGeneratorTests: XCTestCase { + var target: Target! + + override func setUpWithError() throws { + let file = fixturePath + "MigrationTestProject/MigrationTestProject.xcodeproj" + let xcodeProj = try XcodeProj(path: file) + let project = try generateSpec(xcodeProj: xcodeProj, projectDirectory: file.parent())! + target = project.targets.first { $0.name == "MigrationTestProject" }! + + let projectDict = project.toJSONDictionary().removeEmpty() + let encodedYAML = try Yams.dump(object: projectDict) + print(encodedYAML) + } + func testRemoveEmpty() { let arr: [Any] = [[], [1, [2], []], [3]] let removed: [Any] = arr.removeEmpty() @@ -18,13 +32,15 @@ class SpecGeneratorTests: XCTestCase { } func testMigrateDependencies() throws { - let file = fixturePath + "MigrationTestProject/MigrationTestProject.xcodeproj" - let xcodeProj = try XcodeProj(path: file) - let project = try generateSpec(xcodeProj: xcodeProj, projectDirectory: file.parent())! - let target = project.targets.first { $0.name == "MigrationTestProject" }! XCTAssertEqual(target.dependencies[0].reference, "ExampleFramework") XCTAssertEqual(target.dependencies[0].type, .target) XCTAssertEqual(target.dependencies[1].reference, "AVKit.framework") XCTAssertEqual(target.dependencies[1].type, .sdk(root: "System/Library/Frameworks")) } + + func testBuildScript() throws { + XCTAssertEqual(target.preBuildScripts[0].name, "Pre Build Script") + XCTAssertEqual(target.postCompileScripts[0].name, "Post Compile Script") + XCTAssertEqual(target.postBuildScripts[0].name, "Post Build Script") + } }