Browse Source

cli: add-package version option

pull/4782/head
Yunus Emre Kalkan 6 years ago
parent
commit
df11564ac1
  1. 3
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddModuleCommand.cs
  2. 14
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddPackageCommand.cs
  3. 40
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs

3
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddModuleCommand.cs

@ -75,6 +75,7 @@ namespace Volo.Abp.Cli.Commands
sb.AppendLine(" -s|--solution <solution-file> Specify the solution file explicitly.");
sb.AppendLine(" --skip-db-migrations <boolean> Specify if a new migration will be added or not.");
sb.AppendLine(" -sp|--startup-project <startup-project-path> Relative path to the project folder of the startup project. Default value is the current folder.");
sb.AppendLine(" -v|--version <version> Specify the version of the module. Default is your project's ABP version.");
sb.AppendLine("");
sb.AppendLine("Examples:");
sb.AppendLine("");
@ -152,7 +153,7 @@ namespace Volo.Abp.Cli.Commands
public const string Skip = "skip-db-migrations";
}
public static class StartupProject
public static class StartupProject
{
public const string Short = "sp";
public const string Long = "startup-project";

14
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddPackageCommand.cs

@ -34,9 +34,12 @@ namespace Volo.Abp.Cli.Commands
);
}
var version = commandLineArgs.Options.GetOrNull(Options.Version.Short, Options.Version.Long);
await ProjectNugetPackageAdder.AddAsync(
GetProjectFile(commandLineArgs),
commandLineArgs.Target
commandLineArgs.Target,
version
);
}
@ -55,6 +58,7 @@ namespace Volo.Abp.Cli.Commands
sb.AppendLine("Options:");
sb.AppendLine("");
sb.AppendLine(" -p|--project <project-file> Specify the project file explicitly.");
sb.AppendLine(" -v|--version <version> Specify the version of the package. Default is your project's ABP version or latest ABP version.");
sb.AppendLine("");
sb.AppendLine("Examples:");
sb.AppendLine("");
@ -119,6 +123,12 @@ namespace Volo.Abp.Cli.Commands
public const string Short = "p";
public const string Long = "project";
}
public static class Version
{
public const string Short = "v";
public const string Long = "version";
}
}
}
}
}

40
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs

@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Cli.Http;
@ -40,26 +41,36 @@ namespace Volo.Abp.Cli.ProjectModification
Logger = NullLogger<ProjectNugetPackageAdder>.Instance;
}
public async Task AddAsync(string projectFile, string packageName)
public async Task AddAsync(string projectFile, string packageName, string version = null)
{
await AddAsync(
projectFile,
await FindNugetPackageInfoAsync(packageName)
await FindNugetPackageInfoAsync(packageName),
version
);
}
public Task AddAsync(string projectFile, NugetPackageInfo package)
public Task AddAsync(string projectFile, NugetPackageInfo package, string version = null)
{
if (File.ReadAllText(projectFile).Contains($"\"{package.Name}\""))
var projectFileContent = File.ReadAllText(projectFile);
if (projectFileContent.Contains($"\"{package.Name}\""))
{
return Task.CompletedTask;
}
if (version == null)
{
version = GetAbpVersionOrNull(projectFileContent);
}
using (DirectoryHelper.ChangeCurrentDirectory(Path.GetDirectoryName(projectFile)))
{
Logger.LogInformation($"Installing '{package.Name}' package to the project '{Path.GetFileNameWithoutExtension(projectFile)}'...");
CmdHelper.Run("dotnet", "add package " + package.Name);
var versionOption = version == null ? "" : $" -v {version}";
CmdHelper.Run("dotnet", $"add package {package.Name}{versionOption}");
var moduleFiles = ModuleClassFinder.Find(projectFile, "AbpModule");
if (moduleFiles.Count == 0)
@ -80,11 +91,22 @@ namespace Volo.Abp.Cli.ProjectModification
return Task.CompletedTask;
}
protected virtual async Task<NugetPackageInfo> FindNugetPackageInfoAsync(string moduleName)
private string GetAbpVersionOrNull(string projectFileContent)
{
var doc = new XmlDocument() { PreserveWhitespace = true };
doc.Load(StreamHelper.GenerateStreamFromString(projectFileContent));
var nodes = doc.SelectNodes("/Project/ItemGroup/PackageReference[starts-with(@Include, 'Volo.')]");
return nodes?[0]?.Attributes?["Version"]?.Value;
}
protected virtual async Task<NugetPackageInfo> FindNugetPackageInfoAsync(string packageName)
{
using (var client = new CliHttpClient())
{
var url = $"{CliUrls.WwwAbpIo}api/app/nugetPackage/byName/?name=" + moduleName;
var url = $"{CliUrls.WwwAbpIo}api/app/nugetPackage/byName/?name=" + packageName;
var response = await client.GetAsync(url);
@ -92,7 +114,7 @@ namespace Volo.Abp.Cli.ProjectModification
{
if (response.StatusCode == HttpStatusCode.NotFound)
{
throw new CliUsageException($"'{moduleName}' nuget package could not be found!");
throw new CliUsageException($"'{packageName}' nuget package could not be found!");
}
await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(response);
@ -103,4 +125,4 @@ namespace Volo.Abp.Cli.ProjectModification
}
}
}
}
}

Loading…
Cancel
Save