Skip to content

Commit 04f92b9

Browse files
committed
working with custom netrc file location
1 parent ea0fde3 commit 04f92b9

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let macOSPlatform: SupportedPlatform
2020
if let deploymentTarget = ProcessInfo.processInfo.environment["SWIFTPM_MACOS_DEPLOYMENT_TARGET"] {
2121
macOSPlatform = .macOS(deploymentTarget)
2222
} else {
23-
macOSPlatform = .macOS(.v10_10)
23+
macOSPlatform = .macOS(.v10_13)
2424
}
2525

2626
let package = Package(

Sources/Commands/Options.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,9 @@ public struct SwiftToolOptions: ParsableArguments {
255255
archs.count > 1 ? .xcode : _buildSystem
256256
}
257257

258+
/// The path to the netrc file which should be use for authentication when downloading binary target artifacts.
259+
@Option(name: .customLong("netrc-file"), completion: .file())
260+
var netrcFilePath: AbsolutePath?
261+
258262
public init() {}
259263
}

Sources/Commands/SwiftTool.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import func Foundation.NSUserName
1212
import class Foundation.ProcessInfo
13+
import func Foundation.NSHomeDirectory
1314
import Dispatch
1415

1516
import ArgumentParser
@@ -370,6 +371,18 @@ public class SwiftTool {
370371
if !options.archs.isEmpty && options.customCompileTriple != nil {
371372
diagnostics.emit(.mutuallyExclusiveArgumentsError(arguments: ["--arch", "--triple"]))
372373
}
374+
375+
if options.netrcFilePath != nil {
376+
// --netrc-file option only supported on macOS >=10.13
377+
#if os(macOS)
378+
if #available(macOS 10.13, *) {
379+
// ok, check succeeds
380+
} else {
381+
diagnostics.emit(error: "--netrc-file option is only supported on macOS >=10.13")
382+
}
383+
#else
384+
diagnostics.emit(error: "--netrc-file option is only supported on macOS >=10.13")
385+
}
373386
}
374387

375388
func editablesPath() throws -> AbsolutePath {
@@ -405,6 +418,10 @@ public class SwiftTool {
405418
private lazy var _swiftpmConfig: Result<SwiftPMConfig, Swift.Error> = {
406419
return Result(catching: { SwiftPMConfig(path: try configFilePath()) })
407420
}()
421+
422+
func resolvedNetrcFilePath() -> AbsolutePath? {
423+
return options.netrcFilePath
424+
}
408425

409426
/// Holds the currently active workspace.
410427
///
@@ -430,6 +447,7 @@ public class SwiftTool {
430447
delegate: delegate,
431448
config: try getSwiftPMConfig(),
432449
repositoryProvider: provider,
450+
netrcFilePath: resolvedNetrcFilePath(),
433451
isResolverPrefetchingEnabled: options.shouldEnableResolverPrefetching,
434452
skipUpdate: options.skipDependencyUpdate,
435453
enableResolverTrace: options.enableResolverTrace

Sources/SPMTestSupport/MockDownloader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class MockDownloader: Downloader {
4949
public func downloadFile(
5050
at url: Foundation.URL,
5151
to destinationPath: AbsolutePath,
52-
withAuthorizationProvider: AuthorizationProviding? = nil,
52+
withAuthorizationProvider authorizationProvider: AuthorizationProviding? = nil,
5353
progress: @escaping Downloader.Progress,
5454
completion: @escaping Downloader.Completion
5555
) {

Sources/Workspace/Workspace.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ public class Workspace {
346346

347347
/// The downloader used for downloading binary artifacts.
348348
fileprivate let downloader: Downloader
349+
350+
fileprivate let netrcFilePath: AbsolutePath?
349351

350352
/// The downloader used for unarchiving binary artifacts.
351353
fileprivate let archiver: Archiver
@@ -396,6 +398,7 @@ public class Workspace {
396398
fileSystem: FileSystem = localFileSystem,
397399
repositoryProvider: RepositoryProvider = GitRepositoryProvider(),
398400
downloader: Downloader = FoundationDownloader(),
401+
netrcFilePath: AbsolutePath? = nil,
399402
archiver: Archiver = ZipArchiver(),
400403
checksumAlgorithm: HashAlgorithm = SHA256(),
401404
additionalFileRules: [FileRuleDescription] = [],
@@ -412,6 +415,7 @@ public class Workspace {
412415
self.currentToolsVersion = currentToolsVersion
413416
self.toolsVersionLoader = toolsVersionLoader
414417
self.downloader = downloader
418+
self.netrcFilePath = netrcFilePath
415419
self.archiver = archiver
416420
self.checksumAlgorithm = checksumAlgorithm
417421
self.isResolverPrefetchingEnabled = isResolverPrefetchingEnabled
@@ -1401,6 +1405,8 @@ extension Workspace {
14011405
let group = DispatchGroup()
14021406
let tempDiagnostics = DiagnosticsEngine()
14031407

1408+
let netrc = try? Netrc.load(fromFileAtPath: netrcFilePath).get()
1409+
14041410
for artifact in artifacts {
14051411
group.enter()
14061412

@@ -1419,10 +1425,12 @@ extension Workspace {
14191425

14201426
let parsedURL = URL(string: url)!
14211427
let archivePath = parentDirectory.appending(component: parsedURL.lastPathComponent)
1428+
1429+
14221430
downloader.downloadFile(
14231431
at: parsedURL,
14241432
to: archivePath,
1425-
withAuthorizationProvider: nil,
1433+
withAuthorizationProvider: netrc,
14261434
progress: { bytesDownloaded, totalBytesToDownload in
14271435
self.delegate?.downloadingBinaryArtifact(
14281436
from: url,

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ final class PackageToolTests: XCTestCase {
4141
func testVersion() throws {
4242
XCTAssert(try execute(["--version"]).stdout.contains("Swift Package Manager"))
4343
}
44+
45+
func testNetrcFile() throws {
46+
XCTAssert(try execute(["--netrc-file", "/Users/me/.hidden/.netrc"]).stdout.contains("Absolute path to netrc file"))
47+
}
4448

4549
func testResolve() throws {
4650
fixture(name: "DependencyResolution/External/Simple") { prefix in

0 commit comments

Comments
 (0)