Skip to content

Add support for Netrc for Downloader #2833

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

Merged
merged 5 commits into from
Sep 3, 2020
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
4 changes: 4 additions & 0 deletions Sources/Commands/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,9 @@ public struct SwiftToolOptions: ParsableArguments {
archs.count > 1 ? .xcode : _buildSystem
}

/// The path to the netrc file which should be use for authentication when downloading binary target artifacts.
@Option(name: .customLong("netrc-file"), completion: .file())
var netrcFilePath: AbsolutePath?

public init() {}
}
26 changes: 22 additions & 4 deletions Sources/Commands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,23 +353,36 @@ public class SwiftTool {
verbosity = Verbosity(rawValue: options.verbosity)
Process.verbose = verbosity != .concise
}

static func postprocessArgParserResult(options: SwiftToolOptions, diagnostics: DiagnosticsEngine) throws {
if options.chdir != nil {
diagnostics.emit(warning: "'--chdir/-C' option is deprecated; use '--package-path' instead")
}

if options.multirootPackageDataFile != nil {
diagnostics.emit(.unsupportedFlag("--multiroot-data-file"))
}

if options.useExplicitModuleBuild && !options.useIntegratedSwiftDriver {
diagnostics.emit(error: "'--experimental-explicit-module-build' option requires '--use-integrated-swift-driver'")
}

if !options.archs.isEmpty && options.customCompileTriple != nil {
diagnostics.emit(.mutuallyExclusiveArgumentsError(arguments: ["--arch", "--triple"]))
}

if options.netrcFilePath != nil {
// --netrc-file option only supported on macOS >=10.13
#if os(macOS)
if #available(macOS 10.13, *) {
// ok, check succeeds
} else {
diagnostics.emit(error: "'--netrc-file' option is only supported on macOS >=10.13")
}
#else
diagnostics.emit(error: "'--netrc-file' option is only supported on macOS >=10.13")
#endif
}
}

func editablesPath() throws -> AbsolutePath {
Expand Down Expand Up @@ -405,6 +418,10 @@ public class SwiftTool {
private lazy var _swiftpmConfig: Result<SwiftPMConfig, Swift.Error> = {
return Result(catching: { SwiftPMConfig(path: try configFilePath()) })
}()

func resolvedNetrcFilePath() -> AbsolutePath? {
return options.netrcFilePath
}

/// Holds the currently active workspace.
///
Expand All @@ -430,6 +447,7 @@ public class SwiftTool {
delegate: delegate,
config: try getSwiftPMConfig(),
repositoryProvider: provider,
netrcFilePath: resolvedNetrcFilePath(),
isResolverPrefetchingEnabled: options.shouldEnableResolverPrefetching,
skipUpdate: options.skipDependencyUpdate,
enableResolverTrace: options.enableResolverTrace
Expand Down
2 changes: 1 addition & 1 deletion Sources/SPMTestSupport/MockDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class MockDownloader: Downloader {
public func downloadFile(
at url: Foundation.URL,
to destinationPath: AbsolutePath,
withAuthorizationProvider: AuthorizationProviding? = nil,
withAuthorizationProvider authorizationProvider: AuthorizationProviding? = nil,
progress: @escaping Downloader.Progress,
completion: @escaping Downloader.Completion
) {
Expand Down
18 changes: 16 additions & 2 deletions Sources/Workspace/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ public class Workspace {

/// The downloader used for downloading binary artifacts.
fileprivate let downloader: Downloader

fileprivate let netrcFilePath: AbsolutePath?

/// The downloader used for unarchiving binary artifacts.
fileprivate let archiver: Archiver
Expand Down Expand Up @@ -396,6 +398,7 @@ public class Workspace {
fileSystem: FileSystem = localFileSystem,
repositoryProvider: RepositoryProvider = GitRepositoryProvider(),
downloader: Downloader = FoundationDownloader(),
netrcFilePath: AbsolutePath? = nil,
archiver: Archiver = ZipArchiver(),
checksumAlgorithm: HashAlgorithm = SHA256(),
additionalFileRules: [FileRuleDescription] = [],
Expand All @@ -412,6 +415,7 @@ public class Workspace {
self.currentToolsVersion = currentToolsVersion
self.toolsVersionLoader = toolsVersionLoader
self.downloader = downloader
self.netrcFilePath = netrcFilePath
self.archiver = archiver
self.checksumAlgorithm = checksumAlgorithm
self.isResolverPrefetchingEnabled = isResolverPrefetchingEnabled
Expand Down Expand Up @@ -1400,7 +1404,15 @@ extension Workspace {
private func download(_ artifacts: [ManagedArtifact], diagnostics: DiagnosticsEngine) {
let group = DispatchGroup()
let tempDiagnostics = DiagnosticsEngine()


var authProvider: AuthorizationProviding? = nil
#if os(macOS)
// Netrc feature currently only supported on macOS 10.13+ due to dependency
// on NSTextCheckingResult.range(with:)
if #available(macOS 10.13, *) {
authProvider = try? Netrc.load(fromFileAtPath: netrcFilePath).get()
}
#endif
for artifact in artifacts {
group.enter()

Expand All @@ -1419,10 +1431,12 @@ extension Workspace {

let parsedURL = URL(string: url)!
let archivePath = parentDirectory.appending(component: parsedURL.lastPathComponent)


downloader.downloadFile(
at: parsedURL,
to: archivePath,
withAuthorizationProvider: nil,
withAuthorizationProvider: authProvider,
progress: { bytesDownloaded, totalBytesToDownload in
self.delegate?.downloadingBinaryArtifact(
from: url,
Expand Down
22 changes: 22 additions & 0 deletions Tests/CommandsTests/PackageToolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ final class PackageToolTests: XCTestCase {
func testVersion() throws {
XCTAssert(try execute(["--version"]).stdout.contains("Swift Package Manager"))
}

func testNetrcFile() throws {
func verifyUnsupportedOSThrows() {
do {
// should throw and be caught
try execute(["update", "--netrc-file", "/Users/me/.hidden/.netrc"])
XCTFail()
} catch {
XCTAssert(true)
}
}
#if os(macOS)
if #available(macOS 10.13, *) {
// should succeed
XCTAssert(try execute(["--netrc-file", "/Users/me/.hidden/.netrc"]).stdout.contains("USAGE: swift package"))
} else {
verifyUnsupportedOSThrows()
}
#else
verifyUnsupportedOSThrows()
#endif
}

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