-
Notifications
You must be signed in to change notification settings - Fork 695
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
Implemented nuget.exe add and nuget.exe init commands #14
Changes from all commits
9872689
e89fc5b
f5ea7ef
2e8af14
5538980
545d48a
84fac0d
81f552e
b41eda6
39521b5
280762e
85556a3
d6e75a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace NuGet.CommandLine | ||
{ | ||
[Command(typeof(NuGetCommand), "add", "AddCommandDescription", | ||
MinArgs = 1, MaxArgs = 1, UsageDescriptionResourceName = "AddCommandUsageDescription", | ||
UsageSummaryResourceName = "AddCommandUsageSummary", UsageExampleResourceName = "AddCommandUsageExamples")] | ||
public class AddCommand : Command | ||
{ | ||
[Option(typeof(NuGetCommand), "AddCommandSourceDescription", AltName = "src")] | ||
public string Source { get; set; } | ||
|
||
[Option(typeof(NuGetCommand), "ExpandDescription")] | ||
public bool Expand { get; set; } | ||
|
||
public override async Task ExecuteCommandAsync() | ||
{ | ||
// Arguments[0] will not be null at this point. | ||
// Because, this command has MinArgs set to 1. | ||
var packagePath = Arguments[0]; | ||
|
||
if (string.IsNullOrEmpty(Source)) | ||
{ | ||
throw new CommandLineException( | ||
LocalizedResourceManager.GetString(nameof(NuGetResources.AddCommand_SourceNotProvided))); | ||
} | ||
|
||
OfflineFeedUtility.ThrowIfInvalidOrNotFound( | ||
packagePath, | ||
isDirectory: false, | ||
nameOfNotFoundErrorResource: nameof(NuGetResources.NupkgPath_NotFound)); | ||
|
||
// If the Source Feed Folder does not exist, it will be created. | ||
OfflineFeedUtility.ThrowIfInvalid(Source); | ||
|
||
var offlineFeedAddContext = new OfflineFeedAddContext( | ||
packagePath, | ||
Source, | ||
Console, // IConsole is an ILogger | ||
throwIfSourcePackageIsInvalid: true, | ||
throwIfPackageExistsAndInvalid: true, | ||
throwIfPackageExists: false, | ||
expand: Expand); | ||
|
||
await OfflineFeedUtility.AddPackageToSource(offlineFeedAddContext, CancellationToken.None); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this throw? if so which exception the user will get? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OfflineFeedAddContext, which is passed in to the method, has that information. It will always throw CommandLineException |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace NuGet.CommandLine | ||
{ | ||
[Command(typeof(NuGetCommand), "init", "InitCommandDescription", | ||
MinArgs = 2, MaxArgs = 2, UsageDescriptionResourceName = "InitCommandUsageDescription", | ||
UsageSummaryResourceName = "InitCommandUsageSummary", UsageExampleResourceName = "InitCommandUsageExamples")] | ||
public class InitCommand : Command | ||
{ | ||
[Option(typeof(NuGetCommand), "ExpandDescription")] | ||
public bool Expand { get; set; } | ||
|
||
public override async Task ExecuteCommandAsync() | ||
{ | ||
// Arguments[0] or Arguments[1] will not be null at this point. | ||
// Because, this command has MinArgs set to 2. | ||
var source = Arguments[0]; | ||
var destination = Arguments[1]; | ||
|
||
OfflineFeedUtility.ThrowIfInvalidOrNotFound( | ||
source, | ||
isDirectory: true, | ||
nameOfNotFoundErrorResource: nameof(NuGetResources.InitCommand_FeedIsNotFound)); | ||
|
||
// If the Destination Feed Folder does not exist, it will be created. | ||
OfflineFeedUtility.ThrowIfInvalid(destination); | ||
|
||
var packagePaths = GetPackageFilePaths(source, "*" + ProjectManagement.Constants.PackageExtension); | ||
|
||
if (packagePaths.Count > 0) | ||
{ | ||
foreach (var packagePath in packagePaths) | ||
{ | ||
var offlineFeedAddContext = new OfflineFeedAddContext( | ||
packagePath, | ||
destination, | ||
Console, // IConsole is an ILogger | ||
throwIfSourcePackageIsInvalid: false, | ||
throwIfPackageExistsAndInvalid: false, | ||
throwIfPackageExists: false, | ||
expand: Expand); | ||
|
||
await OfflineFeedUtility.AddPackageToSource(offlineFeedAddContext, CancellationToken.None); | ||
} | ||
} | ||
else | ||
{ | ||
var message = string.Format( | ||
CultureInfo.CurrentCulture, | ||
LocalizedResourceManager.GetString(nameof(NuGetResources.InitCommand_FeedContainsNoPackages)), | ||
source); | ||
|
||
Console.LogInformation(message); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Helper method based on LocalPackageRepository and ExpandedPackageRepository | ||
/// to avoid dependency on NuGet.Core. Links to the classes are | ||
/// https://github.com/NuGet/NuGet2/blob/2.9/src/Core/Repositories/LocalPackageRepository.cs | ||
/// AND | ||
/// https://github.com/NuGet/NuGet2/blob/2.9/src/Core/Repositories/ExpandedPackageRepository.cs | ||
/// </summary> | ||
private static IReadOnlyList<string> GetPackageFilePaths(string source, string nupkgFilter) | ||
{ | ||
var packagePaths = new List<string>(); | ||
var isV2StyleFolderSource = IsV2StyleFolderSource(source, nupkgFilter); | ||
|
||
if (!isV2StyleFolderSource.HasValue) | ||
{ | ||
// There are no nupkg files, v2-style or v3-style, under 'source'. | ||
return packagePaths; | ||
} | ||
|
||
if (isV2StyleFolderSource.Value) | ||
{ | ||
foreach (var idDirectory in Directory.EnumerateDirectories(source)) | ||
{ | ||
// Since we need the .nupkg file for nuget.exe init, PackageSaveMode.Nuspec is not supported. | ||
// And, Default search option for EnumerateFiles is top directory only. | ||
var packagesAtIdDirectory = Directory.EnumerateFiles(idDirectory, nupkgFilter); | ||
|
||
packagePaths.AddRange(packagesAtIdDirectory); | ||
} | ||
|
||
var packagesAtRoot = Directory.EnumerateFiles(source, nupkgFilter); | ||
packagePaths.AddRange(packagesAtRoot); | ||
} | ||
else | ||
{ | ||
foreach (var idDirectory in Directory.EnumerateDirectories(source)) | ||
{ | ||
var packageId = Path.GetFileName(idDirectory); | ||
|
||
foreach (var versionDirectory in Directory.EnumerateDirectories(idDirectory)) | ||
{ | ||
var packagesAtVersionDirectory = Directory.EnumerateFiles(versionDirectory, nupkgFilter); | ||
packagePaths.AddRange(packagesAtVersionDirectory); | ||
} | ||
} | ||
} | ||
|
||
return packagePaths; | ||
} | ||
|
||
/// <summary> | ||
/// Helper method based on the LazyLocalPackageRepository.cs to avoid dependency on NuGet.Core | ||
/// https://github.com/NuGet/NuGet2/blob/2.9/src/Core/Repositories/LazyLocalPackageRepository.cs#L74 | ||
/// </summary> | ||
/// <returns>Return true if source v2 style folder. Otherwise, false. | ||
/// If no nupkgs were found under the source, returns null</returns> | ||
private static bool? IsV2StyleFolderSource(string source, string nupkgFilter) | ||
{ | ||
var packagesAtRoot = Directory.EnumerateFiles(source, nupkgFilter); | ||
|
||
if (packagesAtRoot.Any()) | ||
{ | ||
return true; | ||
} | ||
|
||
foreach (var idDirectory in Directory.EnumerateDirectories(source)) | ||
{ | ||
// Since we need the .nupkg file for nuget.exe init, PackageSaveMode.Nuspec is not supported. | ||
// And, Default search option for EnumerateFiles is top directory only. | ||
var packagesAtIdDirectory = Directory.EnumerateFiles(idDirectory, nupkgFilter); | ||
|
||
if (packagesAtIdDirectory.Any()) | ||
{ | ||
return true; | ||
} | ||
|
||
foreach (var versionDirectory in Directory.EnumerateDirectories(idDirectory)) | ||
{ | ||
var packagesAtVersionDirectory = Directory.EnumerateFiles(versionDirectory, nupkgFilter); | ||
|
||
if (packagesAtVersionDirectory.Any()) | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check if we have a way to declare mandatory parameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. We do not have a way today to declare mandatory parameters.