From 38e41607d4b266fcc591db574e51b9aafab337b9 Mon Sep 17 00:00:00 2001 From: Franz Busch Date: Fri, 9 Apr 2021 10:57:19 +0200 Subject: [PATCH] Support SourceFileLists in TargetActions (#2686) --- CHANGELOG.md | 1 + .../TargetAction+ManifestMapper.swift | 20 +++++++++++--- .../ProjectDescription+TestData.swift | 12 +++++++-- .../TargetAction+ManifestMapperTests.swift | 27 +++++++++++++++++++ .../ios_app_with_actions/App/Project.swift | 2 +- 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b91833526..73762ce5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Please, check out guidelines: https://keepachangelog.com/en/1.0.0/ ### Changed - Add missing disabling of swiftformat and swift-format [#2795](https://github.com/tuist/tuist/pull/2795) by [@fortmarek](https://github.com/fortmarek) +- Add support for globbing in build phase input file and file lists as well as output and output file lists. [#2686](https://github.com/tuist/tuist/pull/2686) by [@FranzBusch](https://github.com/FranzBusch) ### Fixed diff --git a/Sources/TuistLoader/Models+ManifestMappers/TargetAction+ManifestMapper.swift b/Sources/TuistLoader/Models+ManifestMappers/TargetAction+ManifestMapper.swift index 077c93d8f..0e505ff62 100644 --- a/Sources/TuistLoader/Models+ManifestMappers/TargetAction+ManifestMapper.swift +++ b/Sources/TuistLoader/Models+ManifestMappers/TargetAction+ManifestMapper.swift @@ -13,10 +13,10 @@ extension TuistGraph.TargetAction { static func from(manifest: ProjectDescription.TargetAction, generatorPaths: GeneratorPaths) throws -> TuistGraph.TargetAction { let name = manifest.name let order = TuistGraph.TargetAction.Order.from(manifest: manifest.order) - let inputPaths = try manifest.inputPaths.map { try generatorPaths.resolve(path: $0) } - let inputFileListPaths = try manifest.inputFileListPaths.map { try generatorPaths.resolve(path: $0) } - let outputPaths = try manifest.outputPaths.map { try generatorPaths.resolve(path: $0) } - let outputFileListPaths = try manifest.outputFileListPaths.map { try generatorPaths.resolve(path: $0) } + let inputPaths = try absolutePaths(for: manifest.inputPaths, generatorPaths: generatorPaths) + let inputFileListPaths = try absolutePaths(for: manifest.inputFileListPaths, generatorPaths: generatorPaths) + let outputPaths = try absolutePaths(for: manifest.outputPaths, generatorPaths: generatorPaths) + let outputFileListPaths = try absolutePaths(for: manifest.outputFileListPaths, generatorPaths: generatorPaths) let basedOnDependencyAnalysis = manifest.basedOnDependencyAnalysis let script: TuistGraph.TargetAction.Script @@ -43,6 +43,18 @@ extension TuistGraph.TargetAction { basedOnDependencyAnalysis: basedOnDependencyAnalysis ) } + + private static func absolutePaths(for paths: [Path], generatorPaths: GeneratorPaths) throws -> [AbsolutePath] { + try paths.map { (path: Path) -> [AbsolutePath] in + // avoid globbing paths that contain variables + if path.pathString.contains("$") { + return [try generatorPaths.resolve(path: path)] + } + let absolutePath = try generatorPaths.resolve(path: path) + let base = AbsolutePath(absolutePath.dirname) + return try base.throwingGlob(absolutePath.basename) + }.reduce([], +) + } } extension TuistGraph.TargetAction.Order { diff --git a/Sources/TuistLoaderTesting/Loaders/TestData/ProjectDescription+TestData.swift b/Sources/TuistLoaderTesting/Loaders/TestData/ProjectDescription+TestData.swift index 9e0f69b16..dcb6a8178 100644 --- a/Sources/TuistLoaderTesting/Loaders/TestData/ProjectDescription+TestData.swift +++ b/Sources/TuistLoaderTesting/Loaders/TestData/ProjectDescription+TestData.swift @@ -93,12 +93,20 @@ extension TargetAction { public static func test(name: String = "Action", tool: String = "", order: Order = .pre, - arguments: [String] = []) -> TargetAction + arguments: [String] = [], + inputPaths: [Path] = [], + inputFileListPaths: [Path] = [], + outputPaths: [Path] = [], + outputFileListPaths: [Path] = []) -> TargetAction { TargetAction( name: name, script: .tool(tool, arguments), - order: order + order: order, + inputPaths: inputPaths, + inputFileListPaths: inputFileListPaths, + outputPaths: outputPaths, + outputFileListPaths: outputFileListPaths ) } } diff --git a/Tests/TuistLoaderTests/Models+ManifestMappers/TargetAction+ManifestMapperTests.swift b/Tests/TuistLoaderTests/Models+ManifestMappers/TargetAction+ManifestMapperTests.swift index b1ec6cbfb..05a3a83aa 100644 --- a/Tests/TuistLoaderTests/Models+ManifestMappers/TargetAction+ManifestMapperTests.swift +++ b/Tests/TuistLoaderTests/Models+ManifestMappers/TargetAction+ManifestMapperTests.swift @@ -28,4 +28,31 @@ final class TargetActionManifestMapperTests: TuistUnitTestCase { XCTAssertEqual(model.script, .tool("my_tool", ["arg1", "arg2"])) XCTAssertEqual(model.order, .pre) } + + func test_doesntGlob_whenVariable() throws { + // Given + let temporaryPath = try self.temporaryPath() + let generatorPaths = GeneratorPaths(manifestDirectory: temporaryPath) + let manifest = ProjectDescription.TargetAction.test( + name: "MyScript", + tool: "my_tool", + order: .pre, + arguments: ["arg1", "arg2"], + inputPaths: ["$(SRCROOT)/foo/bar/**/*.swift"], + inputFileListPaths: ["$(SRCROOT)/foo/bar/**/*.swift"], + outputPaths: ["$(SRCROOT)/foo/bar/**/*.swift"], + outputFileListPaths: ["$(SRCROOT)/foo/bar/**/*.swift"] + ) + // When + let model = try TuistGraph.TargetAction.from(manifest: manifest, generatorPaths: generatorPaths) + + // Then + XCTAssertEqual(model.name, "MyScript") + XCTAssertEqual(model.script, .tool("my_tool", ["arg1", "arg2"])) + XCTAssertEqual(model.order, .pre) + XCTAssertEqual(model.inputPaths, [temporaryPath.appending(RelativePath("$(SRCROOT)/foo/bar/**/*.swift"))]) + XCTAssertEqual(model.inputFileListPaths, [temporaryPath.appending(RelativePath("$(SRCROOT)/foo/bar/**/*.swift"))]) + XCTAssertEqual(model.outputPaths, [temporaryPath.appending(RelativePath("$(SRCROOT)/foo/bar/**/*.swift"))]) + XCTAssertEqual(model.outputFileListPaths, [temporaryPath.appending(RelativePath("$(SRCROOT)/foo/bar/**/*.swift"))]) + } } diff --git a/projects/tuist/fixtures/ios_app_with_actions/App/Project.swift b/projects/tuist/fixtures/ios_app_with_actions/App/Project.swift index d39ef2e85..79ab62868 100644 --- a/projects/tuist/fixtures/ios_app_with_actions/App/Project.swift +++ b/projects/tuist/fixtures/ios_app_with_actions/App/Project.swift @@ -9,7 +9,7 @@ let project = Project(name: "App", infoPlist: "Info.plist", sources: ["Sources/**"], actions: [ - .pre(tool: "/bin/echo", arguments: ["\"tuist\""], name: "Tuist"), + .pre(tool: "/bin/echo", arguments: ["\"tuist\""], name: "Tuist", inputPaths: ["Sources/**/*.swift"]), .post(tool: "/bin/echo", arguments: ["rocks"], name: "Rocks"), .pre(path: "script.sh", name: "Run script"), .pre(script: "echo 'Hello World'", name: "Embedded script"),