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

Symlink Resource Artifacts #265

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
51 changes: 45 additions & 6 deletions Sources/MintKit/Mint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ public class Mint {
try linkPackage(package, executable: executable, overwrite: overwrite)
}
}
try linkResources(package)
checkLinkPath()
}
return false
Expand Down Expand Up @@ -390,6 +391,7 @@ public class Mint {
try linkPackage(package, executable: executable, overwrite: overwrite)
}
}
try linkResources(package)
checkLinkPath()
}

Expand All @@ -406,12 +408,7 @@ public class Mint {
pathsToCopy.append(executablePath)
}

let copiedExtensions: Set = ["bundle", "resources", "dylib"]
for path in try buildPath.children() {
if let ext = path.extension, copiedExtensions.contains(ext) {
pathsToCopy.append(path)
}
}
try pathsToCopy.append(contentsOf: getResources(from: buildPath))

for path in pathsToCopy {
let destinationPath = installPath + path.lastComponent
Expand All @@ -422,6 +419,18 @@ public class Mint {
try Task.run(bash: "cp -R \"\(path.string)\" \"\(destinationPath.string)\"")
}
}

private func getResources(from path: Path) throws -> [Path] {
let extensions: Set = ["bundle", "resources", "dylib"]

return try path.children().filter {
guard let ext = $0.extension, extensions.contains(ext) else {
return false
}

return true
}
}

private func checkLinkPath() {
if let path = ProcessInfo.processInfo.environment["PATH"], !path.contains(linkPath.string) {
Expand Down Expand Up @@ -491,6 +500,22 @@ public class Mint {
output(confirmation.green)
}

func linkResources(_ package: PackageReference) throws {
let packagePath = PackagePath(path: packagesPath, package: package)
let resources = try getResources(from: packagePath.installPath)

for resource in resources {
let installPath = linkPath + resource.lastComponent

do {
try Task.run(bash: "ln -s -f -h \"\(packagePath.installPath + resource.lastComponent)\" \"\(installPath.string)\"")
} catch {
errorOutput("Could not link \(resource.lastComponent) to \(installPath.string)".red)
return
}
}
}

public func bootstrap(link: Bool = false, overwrite: Bool? = nil) throws {

let mintFile = try Mintfile(path: mintFilePath)
Expand Down Expand Up @@ -539,6 +564,14 @@ public class Mint {
let option = Input.readOption(options: packages.map { $0.gitRepo }, prompt: "There are multiple packages matching '\(name)', which one would you like to uninstall?")
package = packages.first { $0.gitRepo == option }!
}

// get resources across all installed versions
let resources = Set(
try package.versionDirs
.map { try getResources(from: $0.path) }
.flatMap { $0 }
)

try package.path.delete()
output("\(package.name) was uninstalled")

Expand All @@ -551,5 +584,11 @@ public class Mint {
let installPath = linkPath + executable.name
try installPath.delete()
}

// remove all resource artifact links
for resource in resources {
let installPath = linkPath + resource.lastComponent
try installPath.delete()
}
}
}
Loading