From 5377563d4a0639a3ec9b6296f343595a137771e2 Mon Sep 17 00:00:00 2001 From: Valentin Knabel Date: Wed, 14 Jul 2021 17:21:36 +0200 Subject: [PATCH] Change directory to nested Package.swift --- Sources/Komondor/Commands/install.swift | 12 +++++++++++- Sources/Komondor/Installation/renderScript.swift | 6 ++++-- Tests/KomondorTests/KomondorTests.swift | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Sources/Komondor/Commands/install.swift b/Sources/Komondor/Commands/install.swift index 34592c9..df1f643 100644 --- a/Sources/Komondor/Commands/install.swift +++ b/Sources/Komondor/Commands/install.swift @@ -72,6 +72,16 @@ public func install(logger _: Logger) throws { // TODO: What if Package.swift isn't in the CWD? let swiftPackagePath = "Package.swift" + // Relative path to folder containing Package.swift + let topLevelString = try shellOut(to: "git rev-parse --show-toplevel").trimmingCharacters(in: .whitespaces) + let cwd = fileManager.currentDirectoryPath + let swiftPackagePrefix: String? + if cwd.hasPrefix(topLevelString), cwd != topLevelString { + swiftPackagePrefix = "." + cwd.dropFirst(topLevelString.count) + } else { + swiftPackagePrefix = nil + } + // Copy in the komondor templates try hookList.forEach { hookName in let hookPath = hooksRoot.appendingPathComponent(hookName) @@ -79,7 +89,7 @@ public func install(logger _: Logger) throws { // Separate header from script so we can // update if the script updates let header = renderScriptHeader(hookName) - let script = renderScript(hookName, swiftPackagePath) + let script = renderScript(hookName, swiftPackagePath, swiftPackagePrefix) let hook = header + script // This is the same permissions that husky uses diff --git a/Sources/Komondor/Installation/renderScript.swift b/Sources/Komondor/Installation/renderScript.swift index 931c002..a7f39ee 100644 --- a/Sources/Komondor/Installation/renderScript.swift +++ b/Sources/Komondor/Installation/renderScript.swift @@ -3,12 +3,14 @@ /// /// If *this* changes then the template should be updated /// -public func renderScript(_ hookName: String, _ swiftPackagePath: String) -> String { +public func renderScript(_ hookName: String, _ swiftPackagePath: String, _ swiftPackagePrefix: String?) -> String { + let changeDir = swiftPackagePrefix.map { "cd \($0)\n" } + ?? "" return """ hookName=`basename "$0"` gitParams="$*" - + \(changeDir) if grep -q \(hookName) \(swiftPackagePath); then # use prebuilt binary if one exists, preferring release builds=( '.build/release/komondor' '.build/debug/komondor' ) diff --git a/Tests/KomondorTests/KomondorTests.swift b/Tests/KomondorTests/KomondorTests.swift index faee4a9..7b5377f 100644 --- a/Tests/KomondorTests/KomondorTests.swift +++ b/Tests/KomondorTests/KomondorTests.swift @@ -1,4 +1,4 @@ -@testable import komondor +@testable import Komondor import XCTest final class KomondorTests: XCTestCase {