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

2148: Add dotnet nuget push command and arguments #2229

Merged
Merged
Show file tree
Hide file tree
Changes from 10 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
41 changes: 41 additions & 0 deletions src/app/Fake.DotNet.Cli/DotNet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module DotNet =
open Fake.Core
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.DotNet.NuGet
open System
open System.IO
open System.Security.Cryptography
Expand Down Expand Up @@ -1435,3 +1436,43 @@ module DotNet =
execWithBinLog project param.Common "test" args param.MSBuildParams
__.MarkSuccess()

let private buildNugetPushArgs (param : NuGet.NuGetPushParams) =
[
param.DisableBuffering |> argOption "disable-buffering"
param.ApiKey |> Option.toList |> argList2 "api-key"
param.NoSymbols |> argOption "no-symbols"
param.NoServiceEndpoint |> argOption "no-service-endpoint"
param.Source |> Option.toList |> argList2 "source"
param.SymbolApiKey |> Option.toList |> argList2 "symbol-api-key"
param.SymbolSource |> Option.toList |> argList2 "symbol-source"
param.Timeout |> Option.map string |> Option.toList |> argList2 "timeout"
]
|> List.concat
|> List.filter (not << String.IsNullOrEmpty)

type Options with
member internal x.ToNuGetToolOptions () =
NuGet.ToolOptions.Create x.DotNetCliPath "nuget push" x.WorkingDirectory false

type NuGetPushOptions =
{ Common: Options
Options: NuGet.NuGetPushParams }
static member Create() =
{ Common = Options.Create()
Options = NuGet.NuGetPushParams.Create() }

/// Execute dotnet nuget push command

/// ## Parameters
///
/// - 'setParams' - set nuget push command parameters
/// - 'nupkg' - nupkg to publish
let nugetPush setParams nupkg =
use __ = Trace.traceTask "DotNet:nuget:push" nupkg
let param = NuGetPushOptions.Create() |> setParams
let toolOptions = param.Common.ToNuGetToolOptions()

let toCliArgs nugetParam = Args.toWindowsCommandLine(buildNugetPushArgs nugetParam)

NuGet.Private.push toolOptions param.Options toCliArgs nupkg
__.MarkSuccess()
1 change: 1 addition & 0 deletions src/app/Fake.DotNet.Cli/Fake.DotNet.Cli.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ProjectReference Include="..\Fake.Core.Trace\Fake.Core.Trace.fsproj" />
<ProjectReference Include="..\Fake.Core.Process\Fake.Core.Process.fsproj" />
<ProjectReference Include="..\Fake.Core.String\Fake.Core.String.fsproj" />
<ProjectReference Include="..\Fake.DotNet.NuGet\Fake.DotNet.NuGet.fsproj" />
<ProjectReference Include="..\Fake.IO.FileSystem\Fake.IO.FileSystem.fsproj" />
<ProjectReference Include="..\Fake.DotNet.MSBuild\Fake.DotNet.MSBuild.fsproj" />
</ItemGroup>
Expand Down
153 changes: 119 additions & 34 deletions src/app/Fake.DotNet.NuGet/NuGet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ type NugetSymbolPackage =
/// Build a symbol package using the nuspec file
| Nuspec = 2

type ToolOptions =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add short docs for this or mark it internal/private?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make it internal, since I'm not reusing it in the DotNet module anymore.

{ ToolPath : string
Command : string
WorkingDir : string
IsFullFramework : bool }

static member Create toolPath command workingDir isFullFramework =
{ ToolPath = toolPath
Command = command
WorkingDir = workingDir
IsFullFramework = isFullFramework }

/// Nuget parameter type
type NuGetParams =
{ ToolPath : string
Expand Down Expand Up @@ -315,40 +327,113 @@ let private pack parameters nuspecFile =
parameters.Version outputPath nuspecFile packageAnalysis defaultExcludes includeReferencedProjects properties basePath
|> execute

/// push package (and try again if something fails)
let rec private publish parameters =
TraceSecrets.register parameters.AccessKey "<NuGetKey>"
TraceSecrets.register parameters.SymbolAccessKey "<NuGetSymbolKey>"
// Newer NuGet requires source to be always specified, so if PublishUrl is empty,
// ignore symbol source - the produced source is broken anyway.
let normalize str = if String.isNullOrEmpty str then None else Some str
let source = match parameters.PublishUrl |> normalize, parameters.SymbolPublishUrl |> normalize with
| None, _ -> ""
| Some source, None -> sprintf "-source %s" source
| Some source, Some symSource -> sprintf "-source %s -SymbolSource %s -SymbolApiKey %s"
source symSource parameters.SymbolAccessKey

let args = sprintf "push \"%s\" %s %s" (parameters.OutputPath @@ packageFileName parameters |> Path.getFullName)
parameters.AccessKey source
Trace.tracefn "%s %s in WorkingDir: %s Trials left: %d" parameters.ToolPath args
(Path.getFullName parameters.WorkingDir) parameters.PublishTrials
try
let result =
let tracing = Process.shouldEnableProcessTracing()
try
Process.setEnableProcessTracing false
Process.execSimple ((fun info ->
{ info with
FileName = parameters.ToolPath
WorkingDirectory = Path.getFullName parameters.WorkingDir
Arguments = args }) >> Process.withFramework) parameters.TimeOut
finally Process.setEnableProcessTracing tracing
if result <> 0 then
sprintf "Error during NuGet push. %s %s" parameters.ToolPath args
|> TraceSecrets.guardMessage
|> failwith
with exn when parameters.PublishTrials > 0 ->
publish { parameters with PublishTrials = parameters.PublishTrials - 1 }
/// dotnet nuget push command options
type NuGetPushParams =
{ /// Disables buffering when pushing to an HTTP(S) server to reduce memory usage.
DisableBuffering: bool
/// The API key for the server
ApiKey: string option
/// Doesn't push symbols (even if present).
NoSymbols: bool
/// Doesn't append "api/v2/package" to the source URL.
NoServiceEndpoint: bool
/// Specifies the server URL. This option is required unless DefaultPushSource config value is set in the NuGet config file.
Source: string option
/// The API key for the symbol server.
SymbolApiKey: string option
/// Specifies the symbol server URL.
SymbolSource: string option
/// Specifies the timeout for pushing to a server.
Timeout: TimeSpan option
/// Number of times to retry pushing the package
PushTrials: int}

static member Create() =
{ DisableBuffering = false
ApiKey = None
NoSymbols = false
NoServiceEndpoint = false
Source = None
SymbolApiKey = None
SymbolSource = None
Timeout = None
PushTrials = 5 }

type NuGetParams with
member internal x.NuGetPushOptions =
let normalize str = if String.isNullOrEmpty str then None else Some str

{ DisableBuffering = false
ApiKey = normalize x.AccessKey
NoSymbols = false
NoServiceEndpoint = false
Source = normalize x.PublishUrl
SymbolApiKey = normalize x.SymbolAccessKey
SymbolSource = normalize x.SymbolPublishUrl
Timeout = Some x.TimeOut
PushTrials = x.PublishTrials }

member internal x.ToolOptions = ToolOptions.Create x.ToolPath "push" x.WorkingDir true
member internal x.Nupkg = (x.OutputPath @@ packageFileName x |> Path.getFullName)

let private toPushCliArgs param =
let ifTrue x b =
if b then Some x
else None

[
param.ApiKey
param.DisableBuffering |> ifTrue "-DisableBuffering"
param.NoSymbols |> ifTrue "-NoSymbols"
param.NoServiceEndpoint |> ifTrue "-NoServiceEndpoint"
param.Source |> Option.map (sprintf "-Source %s")
param.SymbolApiKey |> Option.map (sprintf "-SymbolApiKey %s")
param.SymbolSource |> Option.map (sprintf "-SymbolSource %s")
param.Timeout |> Option.map string |> Option.map (sprintf "-Timeout %s")
]
|> List.choose id
|> String.concat " "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the proper APIs for this (Arguments.ofList for example)?


module Private =
/// For internal use
let rec push (options : ToolOptions) (parameters : NuGetPushParams) (toCliArgs : NuGetPushParams -> string) nupkg =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this only currently handles dotnet nuget push we should think about other commands as well.

  • Do we add all operations by adding new functions one by one on demand?
  • Should we use a similar model as dotnet uses where you have a "low"-level DotNet.exec function where others like dotnet test build on top.
  • Something different?

Personally I'd lean to something along the second option, but if there are good reasons to do it differently I'm all ears. Note that I'm not proposing to implement all options in this PR, but whatever we decide to release should be extendible later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second option sounds like a good idea so I'll not reuse this in the DotNet module, but rather use the exec in there instead. I'll for now make this NuGet.Private.push internal instead and maybe it could be generalized to be the low level one?

parameters.ApiKey |> Option.iter (fun key -> TraceSecrets.register key "<NuGetKey>")
parameters.SymbolApiKey |> Option.iter (fun key -> TraceSecrets.register key "<NuGetSymbolKey>")

let args = sprintf "%s \"%s\" %s" options.Command nupkg (toCliArgs parameters)

sprintf "%s %s in WorkingDir: %s Trials left: %d" options.ToolPath args (Path.getFullName options.WorkingDir) parameters.PushTrials
|> TraceSecrets.guardMessage
|> Trace.trace

try
let result =
let tracing = Process.shouldEnableProcessTracing()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we really want to disable tracing we should use the corresponding CreateProcess functionality. We should remove this class to Process.shouldEnableProcessTracing to reduce side effects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I just copied some other code in the same module. I can just remove it I guess.

try
Process.setEnableProcessTracing false

CreateProcess.fromRawCommandLine options.ToolPath args
|> CreateProcess.withWorkingDirectory options.WorkingDir
|> CreateProcess.withTimeout (parameters.Timeout |> Option.defaultValue (TimeSpan.FromMinutes 5.0))
|> (fun p ->
if options.IsFullFramework then
p |> CreateProcess.withFramework
else
p)
|> Proc.run

finally Process.setEnableProcessTracing tracing

if result.ExitCode <> 0 then
sprintf "Error during NuGet push. %s %s" options.ToolPath args
|> TraceSecrets.guardMessage
|> failwith

with _ when parameters.PushTrials > 0 ->
push options { parameters with PushTrials = parameters.PushTrials - 1 } toCliArgs nupkg

let private publish (parameters : NuGetParams) =
Private.push parameters.ToolOptions parameters.NuGetPushOptions toPushCliArgs parameters.Nupkg

/// push package to symbol server (and try again if something fails)
let rec private publishSymbols parameters =
Expand Down