Skip to content

Commit

Permalink
Implementation of dotnet nuget add|remove|list|etc... source (#3206)
Browse files Browse the repository at this point in the history
Fixes: NuGet/Home#4126
Fixes: NuGet/Home#8668

Spec link: https://github.com/NuGet/Home/blob/dev/designs/DotNet-Sources-Support.md

## Main Work - `dotnet nuget add|remove|* source` implementation
Moves and refactors existing `NuGet.exe sources add|remove|*` command into NuGet.Commands.csproj, so that it can be called by NuGet.exe and by DotNet.exe.

## Localization fixes were then necessary
Dealt with some localization challenges as part of the strings that moved into NuGet.Commands.dll. Needed to make the ILMerge command that makes NuGet.exe to get localization strings from the 13 NuGet.Commands.resources.dll.

## CodeGeneration templates and datafile for commands/verbs
Also implemented a CodeGeneration engine (see .tt files) based on data in commands.xml - which automated code creation of much of the infrastructure. As we add more commands into dotnet.exe, this should be useful.
  • Loading branch information
Rob Relyea authored Feb 6, 2020
1 parent 319fa10 commit a746cd6
Show file tree
Hide file tree
Showing 56 changed files with 2,840 additions and 1,355 deletions.
2 changes: 1 addition & 1 deletion build/common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
TaskParameter="TargetOutputs"
ItemName="DllsToSign" />
</MSBuild>
<MsBuild Condition="'$(BuildRTM)' != 'true'"
<MsBuild
Projects="$(MSBuildProjectFullPath)"
Targets="_GetLocalizedDllsForSigning"
Properties="BuildProjectReferences=false;">
Expand Down
10 changes: 5 additions & 5 deletions build/loc.proj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<IntermediateOutputPath>$(ArtifactsDirectory)</IntermediateOutputPath>
</PropertyGroup>

<Target Name="LocalizeNonProjectFiles" Condition="'$(BuildRTM)' != 'true'">
<Target Name="LocalizeNonProjectFiles">
<ItemGroup>
<NonProjectFilesToMove Include="$(SolutionPackagesFolder)microsoft.web.xdt\2.1.2\lib\net40\Microsoft.Web.XmlTransform.dll">
<DestinationDir>$(ArtifactsDirectory)microsoft.web.xdt\2.1.2\lib\net40\Microsoft.Web.XmlTransform.dll</DestinationDir>
Expand All @@ -37,7 +37,7 @@
</ItemGroup>
</Target>

<Target Name="BatchLocalize" Condition="'$(BuildRTM)' != 'true'">
<Target Name="BatchLocalize">
<MSBuild
Projects="@(SolutionProjectsWithoutVSIX)"
Properties="BuildProjectReferences=false;"
Expand All @@ -50,7 +50,7 @@
<Message Text="Files to localize : @(FilesToLocalize, '%0a')" Importance="High"/>
</Target>

<Target Name="MoveLocalizedFilesToProjectSpecificArtifacts" AfterTargets="Localize" Condition="'$(BuildRTM)' != 'true'">
<Target Name="MoveLocalizedFilesToProjectSpecificArtifacts" AfterTargets="Localize">
<ItemGroup>
<_MoveLocalizedFiles Include="@(LocalizedUserFiles)">
<DestinationDir>$(ArtifactsDirectory)$([MSBuild]::MakeRelative($(ArtifactsDirectory)localize\%(LocalizedUserFiles.lang), %(LocalizedUserFiles.RootDir)%(LocalizedUserFiles.Directory)))%(LocalizedUserFiles.Culture)\%(LocalizedUserFiles.Filename)%(LocalizedUserFiles.Extension)</DestinationDir>
Expand All @@ -60,7 +60,7 @@
<Delete Files="$(ArtifactsDirectory)vsixlangpack\extension.vsixlangpack"/>
</Target>

<Target Name="MoveEulasToArtifacts" AfterTargets="Localize" Condition="'$(BuildRTM)' != 'true'">
<Target Name="MoveEulasToArtifacts" AfterTargets="Localize">
<ItemGroup>
<!-- SupportedCultures is defined in MicroBuild.Plugins.Localization.targets which is installed as a plugin
in VSTS MicroBuild -->
Expand All @@ -72,7 +72,7 @@
<Copy SourceFiles="@(EulaFiles->'%(Identity)')" DestinationFiles="@(EulaFiles->'%(DestinationDir)')"/>
</Target>

<Target Name="MoveLcgFilesToArtifacts" AfterTargets="Localize" Condition="'$(BuildRTM)' != 'true'">
<Target Name="MoveLcgFilesToArtifacts" AfterTargets="Localize">
<ItemGroup>
<_MoveLcgFiles Include="@(LocalizedToolFiles)" Condition="'%(LocalizedToolFiles.Extension)'== '.lcg'">
<DestinationDir>$(ArtifactsDirectory)$([MSBuild]::MakeRelative($(ArtifactsDirectory)localize\%(LocalizedToolFiles.lang), %(LocalizedToolFiles.RootDir)%(LocalizedToolFiles.Directory)))%(LocalizedToolFiles.Culture)\%(LocalizedToolFiles.Filename)%(LocalizedToolFiles.Extension)</DestinationDir>
Expand Down
41 changes: 30 additions & 11 deletions build/vsts_build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ phases:
FullVstsBuildNumber: $[dependencies.Initialize_Build.outputs['updatebuildnumber.FullVstsBuildNumber']]
VsTargetChannel: $[dependencies.Initialize_Build.outputs['updatebuildnumber.VsTargetChannel']]
VsTargetMajorVersion: $[dependencies.Initialize_Build.outputs['updatebuildnumber.VsTargetMajorVersion']]
LocalizedLanguageCount: "13"
DesktopTargetFramework: "net472"

queue:
name: VSEng-MicroBuildVS2019
timeoutInMinutes: 90
Expand Down Expand Up @@ -178,7 +181,7 @@ phases:
solution: "build\\build.proj"
msbuildVersion: "16.0"
configuration: "$(BuildConfiguration)"
msbuildArguments: "/t:BuildNoVSIX /p:BuildRTM=$(BuildRTM) /p:BuildNumber=$(BuildNumber)"
msbuildArguments: "/t:BuildNoVSIX /p:BuildRTM=$(BuildRTM) /p:BuildNumber=$(BuildNumber) /p:SkipILMergeOfNuGetExe=true"

- task: MSBuild@1
displayName: "Ensure msbuild.exe can parse nuget.sln"
Expand All @@ -189,14 +192,39 @@ phases:
msbuildArguments: "/t:EnsureNewtonsoftJsonVersion"
condition: "succeeded()"

- task: MSBuild@1
displayName: "Localize Assemblies"
inputs:
solution: "build\\loc.proj"
msbuildVersion: "16.0"
configuration: "$(BuildConfiguration)"
msbuildArguments: "/t:AfterBuild"

- task: MSBuild@1
displayName: "Build Final NuGet.exe (via ILMerge)"
inputs:
solution: "src\\NuGet.Clients\\NuGet.CommandLine\\NuGet.CommandLine.csproj"
msbuildVersion: "16.0"
configuration: "$(BuildConfiguration)"
msbuildArguments: "/t:ILMergeNuGetExe /p:ExpectedLocalizedArtifactCount=$(LocalizedLanguageCount)"

- task: MSBuild@1
displayName: "Publish NuGet.exe (ILMerged) into NuGet.CommandLine.Test (Mac tests use this)"
inputs:
solution: "test\\NuGet.Clients.Tests\\NuGet.CommandLine.Test\\NuGet.CommandLine.Test.csproj"
msbuildVersion: "16.0"
configuration: "$(BuildConfiguration)"
msbuildArguments: "/t:CopyFinalNuGetExeToOutputPath"
condition: "and(succeeded(),eq(variables['BuildRTM'], 'true'))"

- task: MSBuild@1
displayName: "Run unit tests"
continueOnError: "true"
inputs:
solution: "build\\build.proj"
msbuildVersion: "16.0"
configuration: "$(BuildConfiguration)"
msbuildArguments: "/t:CoreUnitTests;UnitTestsVS /p:BuildRTM=$(BuildRTM) /p:BuildNumber=$(BuildNumber) /p:TestResultOutputFormat=xml"
msbuildArguments: "/t:CoreUnitTests;UnitTestsVS /p:BuildRTM=$(BuildRTM) /p:BuildNumber=$(BuildNumber) /p:TestResultOutputFormat=xml /p:SkipILMergeOfNuGetExe=true"
condition: "and(succeeded(),eq(variables['BuildRTM'], 'true'))"

- task: PublishTestResults@2
Expand Down Expand Up @@ -228,15 +256,6 @@ phases:
ArtifactType: "Container"
condition: "and(succeeded(),eq(variables['BuildRTM'], 'true'))"

- task: MSBuild@1
displayName: "Localize Assemblies"
inputs:
solution: "build\\loc.proj"
msbuildVersion: "16.0"
configuration: "$(BuildConfiguration)"
msbuildArguments: "/t:AfterBuild"
condition: " and(succeeded(),eq(variables['BuildRTM'], 'false')) "

- task: MSBuild@1
displayName: "Sign Assemblies"
inputs:
Expand Down
16 changes: 10 additions & 6 deletions src/NuGet.Clients/NuGet.CommandLine/CommandLineParser.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
using NuGet.Commands;
using NuGet.Packaging;

namespace NuGet.CommandLine
Expand Down Expand Up @@ -66,7 +70,7 @@ public void ExtractOptions(ICommand command, IEnumerator<string> argsEnumerator)

if (value == null)
{
throw new CommandLineException(LocalizedResourceManager.GetString("MissingOptionValueError"), option);
throw new CommandException(LocalizedResourceManager.GetString("MissingOptionValueError"), option);
}

AssignValue(command, propInfo, option, value);
Expand Down Expand Up @@ -118,13 +122,13 @@ internal static void AssignValue(object command, PropertyInfo property, string o
property.SetValue(command, TypeHelper.ChangeType(value, property.PropertyType), index: null);
}
}
catch (CommandLineException)
catch (CommandException)
{
throw;
}
catch
{
throw new CommandLineException(LocalizedResourceManager.GetString("InvalidOptionValueError"), option, value);
throw new CommandException(LocalizedResourceManager.GetString("InvalidOptionValueError"), option, value);
}
}

Expand All @@ -143,7 +147,7 @@ public ICommand ParseCommandLine(IEnumerable<string> commandLineArgs)
ICommand cmd = _commandManager.GetCommand(cmdName);
if (cmd == null)
{
throw new CommandLineException(LocalizedResourceManager.GetString("UnknowCommandError"), cmdName);
throw new CommandException(LocalizedResourceManager.GetString("UnknowCommandError"), cmdName);
}

ExtractOptions(cmd, argsEnumerator);
Expand All @@ -168,7 +172,7 @@ where getDisplayName(item).StartsWith(value, StringComparison.OrdinalIgnoreCase)
var result = results.FirstOrDefault();
if (!results.Any())
{
throw new CommandLineException(LocalizedResourceManager.GetString("UnknownOptionError"), option);
throw new CommandException(LocalizedResourceManager.GetString("UnknownOptionError"), option);
}
else if (results.Skip(1).Any())
{
Expand All @@ -180,7 +184,7 @@ where getDisplayName(item).StartsWith(value, StringComparison.OrdinalIgnoreCase)
}
catch (InvalidOperationException)
{
throw new CommandLineException(String.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("AmbiguousOption"), value,
throw new CommandException(String.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("AmbiguousOption"), value,
String.Join(" ", from c in results select getDisplayName(c))));
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/NuGet.Clients/NuGet.CommandLine/CommandManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Globalization;
using System.Linq;
using System.Reflection;
using NuGet.Commands;

namespace NuGet.CommandLine
{
Expand All @@ -26,7 +30,7 @@ where command.CommandAttribute.CommandName.StartsWith(commandName, StringCompari

if (!results.Any())
{
throw new CommandLineException(LocalizedResourceManager.GetString("UnknowCommandError"), commandName);
throw new CommandException(LocalizedResourceManager.GetString("UnknowCommandError"), commandName);
}

var matchedCommand = results.First();
Expand All @@ -39,7 +43,7 @@ where command.CommandAttribute.CommandName.StartsWith(commandName, StringCompari
if (matchedCommand == null)
{
// No exact match was found and the result returned multiple prefixes.
throw new CommandLineException(String.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("AmbiguousCommand"), commandName,
throw new CommandException(String.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("AmbiguousCommand"), commandName,
String.Join(" ", from c in results select c.CommandAttribute.CommandName)));
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/NuGet.Clients/NuGet.CommandLine/Commands/AddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Threading;
using System.Threading.Tasks;
using NuGet.Commands;
using NuGet.Packaging;
using NuGet.Packaging.PackageExtraction;
using NuGet.Packaging.Signing;
Expand All @@ -29,7 +30,7 @@ public override async Task ExecuteCommandAsync()

if (string.IsNullOrEmpty(Source))
{
throw new CommandLineException(
throw new CommandException(
LocalizedResourceManager.GetString(nameof(NuGetResources.AddCommand_SourceNotProvided)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using NuGet.Commands;
using NuGet.Configuration;
using NuGet.Packaging;
using NuGet.Protocol;
Expand Down Expand Up @@ -111,7 +112,7 @@ internal void CalculateEffectivePackageSaveMode()
projectConfigFilePath,
ex.Message);

throw new CommandLineException(message);
throw new CommandException(message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Commands;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Frameworks;
Expand Down Expand Up @@ -250,7 +251,7 @@ private DependencyBehavior TryGetDependencyBehavior(string behaviorStr)

if (!Enum.TryParse<DependencyBehavior>(behaviorStr, ignoreCase: true, result: out dependencyBehavior) || !Enum.IsDefined(typeof(DependencyBehavior), dependencyBehavior))
{
throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("InstallCommandUnknownDependencyVersion"), behaviorStr));
throw new CommandException(string.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("InstallCommandUnknownDependencyVersion"), behaviorStr));
}

return dependencyBehavior;
Expand Down Expand Up @@ -341,7 +342,7 @@ private async Task InstallPackageAsync(
LocalizedResourceManager.GetString("InstallCommandUnableToFindPackage"),
packageId);

throw new CommandLineException(message);
throw new CommandException(message);
}

version = resolvePackage.LatestVersion;
Expand Down
5 changes: 4 additions & 1 deletion src/NuGet.Clients/NuGet.CommandLine/Commands/PackCommand.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -129,7 +132,7 @@ public override void ExecuteCommand()
{
if (!System.Version.TryParse(MinClientVersion, out _minClientVersionValue))
{
throw new CommandLineException(LocalizedResourceManager.GetString("PackageCommandInvalidMinClientVersion"));
throw new CommandException(LocalizedResourceManager.GetString("PackageCommandInvalidMinClientVersion"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ private void CheckRequireConsent()
LocalizedResourceManager.GetString("InstallCommandPackageRestoreConsentNotFound"),
NuGetResources.PackageRestoreConsentCheckBoxText.Replace("&", ""));

throw new CommandLineException(message);
throw new CommandException(message);
}
}
}
Expand Down
Loading

0 comments on commit a746cd6

Please sign in to comment.