Skip to content

Commit 9416c56

Browse files
committed
Change JavaRepositoryDescriptor to enum
1 parent 5a6616b commit 9416c56

File tree

2 files changed

+64
-23
lines changed

2 files changed

+64
-23
lines changed

Sources/SwiftJavaConfigurationShared/Configuration.swift

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ public struct Configuration: Codable {
8080

8181
// Java dependencies we need to fetch for this target.
8282
public var dependencies: [JavaDependencyDescriptor]?
83-
// Java repositories for this target when fetching dependencies.
83+
/// Maven repositories for this target when fetching dependencies.
84+
///
85+
/// `mavenCentral()` will always be used.
86+
///
87+
/// Reference: [Repository Types](https://docs.gradle.org/current/userguide/supported_repository_types.html)
8488
public var repositories: [JavaRepositoryDescriptor]?
8589

8690
public init() {
@@ -136,36 +140,73 @@ public struct JavaDependencyDescriptor: Hashable, Codable {
136140
}
137141

138142
/// Descriptor for [repositories](https://docs.gradle.org/current/userguide/supported_repository_types.html#sec:maven-repo)
139-
public struct JavaRepositoryDescriptor: Hashable, Codable {
140-
public enum RepositoryType: String, Codable {
141-
case mavenLocal, mavenCentral
142-
case maven // TODO: ivy .. https://docs.gradle.org/current/userguide/supported_repository_types.html#sec:maven-repo
143-
}
143+
public enum JavaRepositoryDescriptor: Hashable, Codable, Equatable {
144144

145-
public var type: RepositoryType
146-
public var url: String?
147-
public var artifactUrls: [String]?
145+
/// Haven't found a proper way to test credentials, packages that need to download from private repo can be downloaded by maven and then use local repo instead
146+
case maven(url: String, artifactUrls: [String]? = nil)
147+
case mavenLocal(includeGroups: [String]? = nil)
148+
case other(_ type: String)
148149

149-
public init(type: RepositoryType, url: String? = nil, artifactUrls: [String]? = nil) {
150-
self.type = type
151-
self.url = url
152-
self.artifactUrls = artifactUrls
153-
}
150+
enum CodingKeys: String, CodingKey { case type, url, artifactUrls, credentials, includeGroups }
154151

155-
public func renderGradleRepository() -> String? {
152+
public init(from decoder: Decoder) throws {
153+
let c = try decoder.container(keyedBy: CodingKeys.self)
154+
let type = try c.decode(String.self, forKey: .type)
156155
switch type {
157-
case .mavenLocal, .mavenCentral:
158-
return "\(type.rawValue)()"
159-
case .maven:
160-
guard let url else {
161-
return nil
156+
case "maven":
157+
self = try .maven(
158+
url: c.decode(String.self, forKey: .url),
159+
artifactUrls: try? c.decode([String].self, forKey: .artifactUrls),
160+
)
161+
case "mavenLocal":
162+
self = .mavenLocal(includeGroups: try? c.decode([String].self, forKey: .includeGroups))
163+
default:
164+
self = .other(type)
165+
}
166+
}
167+
168+
public func encode(to encoder: Encoder) throws {
169+
var c = encoder.container(keyedBy: CodingKeys.self)
170+
switch self {
171+
case let .maven(url, artifactUrls/*, creds*/):
172+
try c.encode("maven", forKey: .type)
173+
try c.encode(url, forKey: .url)
174+
if let artifactUrls = artifactUrls {
175+
try c.encode(artifactUrls, forKey: .artifactUrls)
162176
}
177+
case let .mavenLocal(includeGroups):
178+
try c.encode("mavenLocal", forKey: .type)
179+
if let gs = includeGroups {
180+
try c.encode(gs, forKey: .includeGroups)
181+
}
182+
case let .other(type):
183+
try c.encode("\(type)", forKey: .type)
184+
}
185+
}
186+
187+
public func renderGradleRepository() -> String? {
188+
switch self {
189+
case let .maven(url, artifactUrls):
163190
return """
164191
maven {
165-
url "\(url)"
192+
url = uri("\(url)")
166193
\((artifactUrls ?? []).map({ "artifactUrls(\"\($0)\")" }).joined(separator: "\n"))
167194
}
168195
"""
196+
case let .mavenLocal(groups):
197+
if let gs = groups {
198+
return """
199+
mavenLocal {
200+
content {
201+
\(gs.map({ "includeGroup(\"\($0)\")" }).joined(separator: "\n"))
202+
}
203+
}
204+
"""
205+
} else {
206+
return "mavenLocal()"
207+
}
208+
case let .other(type):
209+
return "\(type)()"
169210
}
170211
}
171212
}

Sources/SwiftJavaTool/Commands/ResolveCommand.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ extension SwiftJava.ResolveCommand {
8181
configuredRepositories += repositories
8282
}
8383

84-
if !configuredRepositories.contains(where: { $0.type == .mavenCentral }) {
84+
if !configuredRepositories.contains(where: { $0 == .other("mavenCentral") }) {
8585
// swift-java dependencies are originally located in mavenCentral
86-
configuredRepositories.append(JavaRepositoryDescriptor(type: .mavenCentral))
86+
configuredRepositories.append(.other("mavenCentral"))
8787
}
8888

8989
let dependenciesClasspath =

0 commit comments

Comments
 (0)