Skip to content

Commit

Permalink
Merge pull request #94 from finestructure/fix-product-clause
Browse files Browse the repository at this point in the history
Fix product clause
  • Loading branch information
finestructure authored Aug 15, 2023
2 parents db1b7a4 + 21f5ebc commit b5aac0c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 27 deletions.
17 changes: 9 additions & 8 deletions Sources/ArenaCore/ArenaCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,15 @@ extension Arena {
try shellOut(to: ShellOutCommand(string: "swift package resolve"), at: dependencyPackagePath)
}

let packageInfo: [(Dependency, PackageInfo)]
var packageInfo = [(Dependency, PackageInfo, PackageIdentifier)]()
do {
// find libraries
packageInfo = Array(
zip(dependencies,
try dependencies.compactMap { $0.sourceDir(packageDir: dependencyPackagePath) }
.compactMap { try getPackageInfo(in: $0) } )
)
for dep in dependencies {
guard let sourceDir = dep.sourceDir(packageDir: dependencyPackagePath) else { continue }
packageInfo.append(
(dep, try getPackageInfo(in: sourceDir), PackageIdentifier(url: dep.url.absoluteString))
)
}
let libs = packageInfo.flatMap { $0.1.libraries }
if libs.isEmpty { throw AppError.noLibrariesFound }
progress(.listLibraries, "📔 Libraries found: \(libs.joined(separator: ", "))")
Expand All @@ -152,7 +153,7 @@ extension Arena {
// update Package.swift dependencies again, adding in package `name:` and
// the `platforms` stanza (if required)
do {
let depsClause = packageInfo.map { (dep, pkg) in
let depsClause = packageInfo.map { (dep, pkg, _) in
" " + dep.packageClause(name: pkg.name)
}.joined(separator: ",\n")
let updatedDeps = "package.dependencies = [\n\(depsClause)\n]"
Expand All @@ -167,7 +168,7 @@ extension Arena {
package.targets = [
.target(name: "\(depdencyPackageName)",
dependencies: [
\(PackageGenerator.productsClause(packageInfo))
\(PackageGenerator.productsClause(packageInfo.map { ($1, $2) }))
]
)
]
Expand Down
52 changes: 44 additions & 8 deletions Sources/ArenaCore/PackageGenerator.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
import Path

struct PackageIdentifier {
var id: String

init(url: String) {
self.id = Self.computeDefaultName(fromLocation: url)
}

static func computeDefaultName(fromLocation url: String) -> String {
#if os(Windows)
let isSeparator: (Character) -> Bool = { $0 == "/" || $0 == "\\" }
#else
let isSeparator: (Character) -> Bool = { $0 == "/" }
#endif

// Get the last path component of the URL.
// Drop the last character in case it's a trailing slash.
var endIndex = url.endIndex
if let lastCharacter = url.last, isSeparator(lastCharacter) {
endIndex = url.index(before: endIndex)
}

let separatorIndex = url[..<endIndex].lastIndex(where: isSeparator)
let startIndex = separatorIndex.map { url.index(after: $0) } ?? url.startIndex
var lastComponent = url[startIndex ..< endIndex]

// Strip `.git` suffix if present.
if lastComponent.hasSuffix(".git") {
lastComponent = lastComponent.dropLast(4)
}

return String(lastComponent)
}

}

enum PackageGenerator {
static func productsClause(_ info: [(Dependency, PackageInfo)]) -> String {
info
.flatMap { pkg in pkg.1.libraries.map { (package: pkg.1.name, library: $0) } }
.map {
"""
.product(name: "\($0.library)", package: "\($0.package)")
"""
}.joined(separator: ",\n")
static func productsClause(_ info: [(PackageInfo, PackageIdentifier)]) -> String {
var clauses = [String]()
for (pkgInfo, pkgId) in info {
for lib in pkgInfo.libraries {
clauses.append("""
.product(name: "\(lib)", package: "\(pkgId.id)")
""")
}
}
return clauses.joined(separator: ",\n")
}
}

Expand Down
22 changes: 12 additions & 10 deletions Tests/ArenaTests/PackageGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import XCTest
class PackageGeneratorTests: XCTestCase {

func test_productsClause() throws {
let info: [(Dependency, PackageInfo)] = [
(Dependency(url: URL(string: "https://github.com/finestructure/parser")!,
refSpec: .branch("main")),
.init(name: "Parser", platforms: nil, libraries: ["Parser"])),
(Dependency(url: URL(string: "https://github.com/finestructure/gala")!,
refSpec: .branch("main")),
.init(name: "Gala", platforms: nil, libraries: ["Gala"])),
let info: [(PackageInfo, PackageIdentifier)] = [
(.init(name: "Parser", platforms: nil, libraries: ["Parser"]),
.init(url: "https://github.com/finestructure/Parser")),
(.init(name: "Gala", platforms: nil, libraries: ["Gala"]),
.init(url: "https://github.com/finestructure/gala")),
(.init(name: "Alias", platforms: nil, libraries: ["Alias"]),
.init(url: "https://github.com/p-x9/AliasMacro")),
]
assertSnapshot(matching: PackageGenerator.productsClause(info),
as: .lines,
record: false)
XCTAssertEqual(PackageGenerator.productsClause(info), """
.product(name: "Parser", package: "Parser"),
.product(name: "Gala", package: "gala"),
.product(name: "Alias", package: "AliasMacro")
""")
}

func test_mergePlatforms() throws {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.product(name: "Parser", package: "Parser"),
.product(name: "Gala", package: "Gala")
.product(name: "Gala", package: "Gala"),
.product(name: "Alias", package: "AliasMacro")

0 comments on commit b5aac0c

Please sign in to comment.