Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change directory to nested Package.swift #44

Merged
merged 1 commit into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Sources/Komondor/Commands/install.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,24 @@ 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)

// 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
Expand Down
6 changes: 4 additions & 2 deletions Sources/Komondor/Installation/renderScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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' )
Expand Down
2 changes: 1 addition & 1 deletion Tests/KomondorTests/KomondorTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testable import komondor
@testable import Komondor
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When trying to commit I receive an error:

Komondor/Tests/KomondorTests/KomondorTests.swift:1:18: error: no such module 'Komondor'
@testable import Komondor
                 ^
note: module 'Komondor' is the main module of an executable, and cannot be imported by tests and other targets

I could extract all code except main.swift into a separate target (e.g. KomondorKit) if you like.

import XCTest

final class KomondorTests: XCTestCase {
Expand Down