Browse Source

Merge pull request #6522 from abpframework/liangshiwei/abp-cli-update

Update command allow to specify version
pull/6533/head
Yunus Emre Kalkan 6 years ago
committed by GitHub
parent
commit
4e74a31fd0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      docs/en/CLI.md
  2. 1
      docs/zh-Hans/CLI.md
  3. 22
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/UpdateCommand.cs
  4. 71
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs
  5. 57
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs
  6. 94
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs

1
docs/en/CLI.md

@ -129,6 +129,7 @@ abp update [options]
* `--solution-path` or `-sp`: Specify the solution path. Use the current directory by default * `--solution-path` or `-sp`: Specify the solution path. Use the current directory by default
* `--solution-name` or `-sn`: Specify the solution name. Search `*.sln` files in the directory by default. * `--solution-name` or `-sn`: Specify the solution name. Search `*.sln` files in the directory by default.
* `--check-all`: Check the new version of each package separately. Default is `false`. * `--check-all`: Check the new version of each package separately. Default is `false`.
* `--version` or `-v`: Specifies the version to use for update. If not specified, latest version is used.
### add-package ### add-package

1
docs/zh-Hans/CLI.md

@ -126,6 +126,7 @@ abp update [options]
* `--solution-path` 或 `-sp`: 指定解决方案路径/目录. 默认使用当前目录 * `--solution-path` 或 `-sp`: 指定解决方案路径/目录. 默认使用当前目录
* `--solution-name` 或 `-sn`: 指定解决方案名称. 默认在目录中搜索`*.sln`文件. * `--solution-name` 或 `-sn`: 指定解决方案名称. 默认在目录中搜索`*.sln`文件.
* `--check-all`: 分别检查每个包的新版本. 默认是 `false`. * `--check-all`: 分别检查每个包的新版本. 默认是 `false`.
* `--version` or `-v`: 指定用于升级的版本. 如果没有指定,则使用最新版本.
### add-package ### add-package

22
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/UpdateCommand.cs

@ -34,24 +34,25 @@ namespace Volo.Abp.Cli.Commands
var directory = commandLineArgs.Options.GetOrNull(Options.SolutionPath.Short, Options.SolutionPath.Long) ?? var directory = commandLineArgs.Options.GetOrNull(Options.SolutionPath.Short, Options.SolutionPath.Long) ??
Directory.GetCurrentDirectory(); Directory.GetCurrentDirectory();
var version = commandLineArgs.Options.GetOrNull(Options.Version.Short, Options.Version.Long);
if (updateNuget || !updateNpm) if (updateNuget || !updateNpm)
{ {
await UpdateNugetPackages(commandLineArgs, directory); await UpdateNugetPackages(commandLineArgs, directory, version);
} }
if (updateNpm || !updateNuget) if (updateNpm || !updateNuget)
{ {
await UpdateNpmPackages(directory); await UpdateNpmPackages(directory, version);
} }
} }
private async Task UpdateNpmPackages(string directory) private async Task UpdateNpmPackages(string directory, string version)
{ {
await _npmPackagesUpdater.Update(directory); await _npmPackagesUpdater.Update(directory, version: version);
} }
private async Task UpdateNugetPackages(CommandLineArgs commandLineArgs, string directory) private async Task UpdateNugetPackages(CommandLineArgs commandLineArgs, string directory, string version)
{ {
var solution = commandLineArgs.Options.GetOrNull(Options.SolutionName.Short, Options.SolutionName.Long); var solution = commandLineArgs.Options.GetOrNull(Options.SolutionName.Short, Options.SolutionName.Long);
@ -66,7 +67,7 @@ namespace Volo.Abp.Cli.Commands
{ {
var solutionName = Path.GetFileName(solution).RemovePostFix(".sln"); var solutionName = Path.GetFileName(solution).RemovePostFix(".sln");
await _nugetPackagesVersionUpdater.UpdateSolutionAsync(solution, checkAll: checkAll); await _nugetPackagesVersionUpdater.UpdateSolutionAsync(solution, checkAll: checkAll, version: version);
Logger.LogInformation($"Volo packages are updated in {solutionName} solution."); Logger.LogInformation($"Volo packages are updated in {solutionName} solution.");
return; return;
@ -78,7 +79,7 @@ namespace Volo.Abp.Cli.Commands
{ {
var projectName = Path.GetFileName(project).RemovePostFix(".csproj"); var projectName = Path.GetFileName(project).RemovePostFix(".csproj");
await _nugetPackagesVersionUpdater.UpdateProjectAsync(project, checkAll: checkAll); await _nugetPackagesVersionUpdater.UpdateProjectAsync(project, checkAll: checkAll, version: version);
Logger.LogInformation($"Volo packages are updated in {projectName} project."); Logger.LogInformation($"Volo packages are updated in {projectName} project.");
return; return;
@ -107,6 +108,7 @@ namespace Volo.Abp.Cli.Commands
sb.AppendLine("-sp|--solution-path (Specify the solution path)"); sb.AppendLine("-sp|--solution-path (Specify the solution path)");
sb.AppendLine("-sn|--solution-name (Specify the solution name)"); sb.AppendLine("-sn|--solution-name (Specify the solution name)");
sb.AppendLine("--check-all (Check the new version of each package separately)"); sb.AppendLine("--check-all (Check the new version of each package separately)");
sb.AppendLine("-v|--version <version> (default: latest version)");
sb.AppendLine(""); sb.AppendLine("");
sb.AppendLine("Some examples:"); sb.AppendLine("Some examples:");
sb.AppendLine(""); sb.AppendLine("");
@ -148,6 +150,12 @@ namespace Volo.Abp.Cli.Commands
{ {
public const string Long = "check-all"; public const string Long = "check-all";
} }
public static class Version
{
public const string Short = "v";
public const string Long = "version";
}
} }
} }
} }

71
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs

@ -40,13 +40,43 @@ namespace Volo.Abp.Cli.NuGet
} }
public async Task<SemanticVersion> GetLatestVersionOrNullAsync(string packageId, bool includeNightly = false, bool includeReleaseCandidates = false) public async Task<SemanticVersion> GetLatestVersionOrNullAsync(string packageId, bool includeNightly = false, bool includeReleaseCandidates = false)
{
var versionList = await GetPackageVersionListAsync(packageId, includeNightly, includeReleaseCandidates);
List<SemanticVersion> versions;
if (!includeNightly && !includeReleaseCandidates)
{
versions = versionList
.Select(SemanticVersion.Parse)
.OrderByDescending(v=> v, new VersionComparer()).ToList();
versions = versions.Where(x => !x.IsPrerelease).ToList();
}
else if (!includeNightly && includeReleaseCandidates)
{
versions = versionList
.Where(v=> !v.Contains("-preview"))
.Select(SemanticVersion.Parse)
.OrderByDescending(v=> v, new VersionComparer()).ToList();
}
else
{
versions = versionList
.Select(SemanticVersion.Parse)
.OrderByDescending(v=> v, new VersionComparer()).ToList();
}
return versions.Any() ? versions.Max() : null;
}
public async Task<List<string>> GetPackageVersionListAsync(string packageId, bool includeNightly = false,
bool includeReleaseCandidates = false)
{ {
if (AuthService.IsLoggedIn()) if (AuthService.IsLoggedIn())
{ {
if (_proPackageList == null) _proPackageList ??= await GetProPackageListAsync();
{
_proPackageList = await GetProPackageListAsync();
}
} }
string url; string url;
@ -75,37 +105,8 @@ namespace Volo.Abp.Cli.NuGet
var responseContent = await responseMessage.Content.ReadAsStringAsync(); var responseContent = await responseMessage.Content.ReadAsStringAsync();
List<SemanticVersion> versions; return JsonSerializer
.Deserialize<NuGetVersionResultDto>(responseContent).Versions;
if (!includeNightly && !includeReleaseCandidates)
{
versions = JsonSerializer
.Deserialize<NuGetVersionResultDto>(responseContent)
.Versions
.Select(SemanticVersion.Parse)
.OrderByDescending(v=> v, new VersionComparer()).ToList();
versions = versions.Where(x => !x.IsPrerelease).ToList();
}
else if (!includeNightly && includeReleaseCandidates)
{
versions = JsonSerializer
.Deserialize<NuGetVersionResultDto>(responseContent)
.Versions
.Where(v=> !v.Contains("-preview"))
.Select(SemanticVersion.Parse)
.OrderByDescending(v=> v, new VersionComparer()).ToList();
}
else
{
versions = JsonSerializer
.Deserialize<NuGetVersionResultDto>(responseContent)
.Versions
.Select(SemanticVersion.Parse)
.OrderByDescending(v=> v, new VersionComparer()).ToList();
}
return versions.Any() ? versions.Max() : null;
} }
} }

57
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs

@ -43,7 +43,7 @@ namespace Volo.Abp.Cli.ProjectModification
Logger = NullLogger<NpmPackagesUpdater>.Instance; Logger = NullLogger<NpmPackagesUpdater>.Instance;
} }
public async Task Update(string rootDirectory, bool includePreviews = false, bool includeReleaseCandidates = false, bool switchToStable = false) public async Task Update(string rootDirectory, bool includePreviews = false, bool includeReleaseCandidates = false, bool switchToStable = false, string version = null)
{ {
var fileList = _packageJsonFileFinder.Find(rootDirectory); var fileList = _packageJsonFileFinder.Find(rootDirectory);
@ -58,7 +58,7 @@ namespace Volo.Abp.Cli.ProjectModification
async Task UpdateAsync(string file) async Task UpdateAsync(string file)
{ {
var updated = await UpdatePackagesInFile(file, includePreviews,includeReleaseCandidates, switchToStable); var updated = await UpdatePackagesInFile(file, includePreviews,includeReleaseCandidates, switchToStable, version);
packagesUpdated.TryAdd(file, updated); packagesUpdated.TryAdd(file, updated);
} }
@ -181,7 +181,8 @@ namespace Volo.Abp.Cli.ProjectModification
string filePath, string filePath,
bool includePreviews = false, bool includePreviews = false,
bool includeReleaseCandidates = false, bool includeReleaseCandidates = false,
bool switchToStable = false) bool switchToStable = false,
string specifiedVersion = null)
{ {
var packagesUpdated = false; var packagesUpdated = false;
var fileContent = File.ReadAllText(filePath); var fileContent = File.ReadAllText(filePath);
@ -195,7 +196,7 @@ namespace Volo.Abp.Cli.ProjectModification
foreach (var abpPackage in abpPackages) foreach (var abpPackage in abpPackages)
{ {
var updated = await TryUpdatingPackage(filePath, abpPackage, includePreviews, includeReleaseCandidates, switchToStable); var updated = await TryUpdatingPackage(filePath, abpPackage, includePreviews, includeReleaseCandidates, switchToStable, specifiedVersion);
if (updated) if (updated)
{ {
@ -215,27 +216,45 @@ namespace Volo.Abp.Cli.ProjectModification
JProperty package, JProperty package,
bool includePreviews = false, bool includePreviews = false,
bool includeReleaseCandidates = false, bool includeReleaseCandidates = false,
bool switchToStable = false) bool switchToStable = false,
string specifiedVersion = null)
{ {
var currentVersion = (string) package.Value; var currentVersion = (string) package.Value;
var version = ""; var version = "";
if ((includePreviews || (!switchToStable && currentVersion.Contains("-preview"))) && !includeReleaseCandidates) if (!specifiedVersion.IsNullOrWhiteSpace())
{ {
version = "preview"; if (!SpecifiedVersionExists(specifiedVersion, package))
{
return false;
}
if (SemanticVersion.Parse(specifiedVersion) <= SemanticVersion.Parse(currentVersion.RemovePreFix("~", "^")))
{
return false;
}
version = specifiedVersion.EnsureStartsWith('^');
} }
else else
{ {
if (!switchToStable && IsPrerelease(currentVersion)) if ((includePreviews || (!switchToStable && currentVersion.Contains("-preview"))) && !includeReleaseCandidates)
{ {
version = await GetLatestVersion(package, true); version = "preview";
} }
else else
{ {
version = await GetLatestVersion(package, includeReleaseCandidates); if (!switchToStable && IsPrerelease(currentVersion))
{
version = await GetLatestVersion(package, true);
}
else
{
version = await GetLatestVersion(package, includeReleaseCandidates);
}
} }
} }
if (string.IsNullOrEmpty(version) || version == currentVersion) if (string.IsNullOrEmpty(version) || version == currentVersion)
{ {
return false; return false;
@ -267,9 +286,7 @@ namespace Volo.Abp.Cli.ProjectModification
return _fileVersionStorage[package.Name]; return _fileVersionStorage[package.Name];
} }
var versionListAsJson = CmdHelper.RunCmdAndGetOutput($"npm show {package.Name} versions"); var versionList = GetPackageVersionList(package);
var versionList = JsonConvert.DeserializeObject<string[]>(versionListAsJson)
.OrderByDescending(SemanticVersion.Parse, new VersionComparer()).ToList();
var newVersion = includeReleaseCandidates var newVersion = includeReleaseCandidates
? versionList.First() ? versionList.First()
@ -327,5 +344,19 @@ namespace Volo.Abp.Cli.ProjectModification
Logger.LogInformation($"Running npm install on {fileDirectory}"); Logger.LogInformation($"Running npm install on {fileDirectory}");
CmdHelper.RunCmd($"cd {fileDirectory} && npm install"); CmdHelper.RunCmd($"cd {fileDirectory} && npm install");
} }
protected virtual List<string> GetPackageVersionList(JProperty package)
{
var versionListAsJson = CmdHelper.RunCmdAndGetOutput($"npm show {package.Name} versions");
return JsonConvert.DeserializeObject<string[]>(versionListAsJson)
.OrderByDescending(SemanticVersion.Parse, new VersionComparer()).ToList();
}
protected virtual bool SpecifiedVersionExists(string version, JProperty package)
{
var versionList = GetPackageVersionList(package);
return versionList.Any(v => v.Equals(version, StringComparison.OrdinalIgnoreCase));
}
} }
} }

94
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs

@ -25,11 +25,11 @@ namespace Volo.Abp.Cli.ProjectModification
Logger = NullLogger<VoloNugetPackagesVersionUpdater>.Instance; Logger = NullLogger<VoloNugetPackagesVersionUpdater>.Instance;
} }
public async Task UpdateSolutionAsync(string solutionPath, bool includePreviews = false, bool includeReleaseCandidates = false, bool switchToStable = false, bool checkAll = false) public async Task UpdateSolutionAsync(string solutionPath, bool includePreviews = false, bool includeReleaseCandidates = false, bool switchToStable = false, bool checkAll = false, string version = null)
{ {
var projectPaths = ProjectFinder.GetProjectFiles(solutionPath); var projectPaths = ProjectFinder.GetProjectFiles(solutionPath);
if (checkAll) if (checkAll && version.IsNullOrWhiteSpace())
{ {
Task.WaitAll(projectPaths.Select(projectPath => UpdateInternalAsync(projectPath, includePreviews, includeReleaseCandidates, switchToStable)).ToArray()); Task.WaitAll(projectPaths.Select(projectPath => UpdateInternalAsync(projectPath, includePreviews, includeReleaseCandidates, switchToStable)).ToArray());
} }
@ -48,7 +48,8 @@ namespace Volo.Abp.Cli.ProjectModification
switchToStable, switchToStable,
latestVersionFromNuget, latestVersionFromNuget,
latestReleaseCandidateVersionFromNuget, latestReleaseCandidateVersionFromNuget,
latestVersionFromMyGet); latestVersionFromMyGet,
version);
File.WriteAllText(filePath, updatedContent); File.WriteAllText(filePath, updatedContent);
} }
@ -57,9 +58,9 @@ namespace Volo.Abp.Cli.ProjectModification
} }
} }
public async Task UpdateProjectAsync(string projectPath, bool includeNightlyPreviews = false, bool includeReleaseCandidates = false, bool switchToStable = false, bool checkAll = false) public async Task UpdateProjectAsync(string projectPath, bool includeNightlyPreviews = false, bool includeReleaseCandidates = false, bool switchToStable = false, bool checkAll = false, string version = null)
{ {
if (checkAll) if (checkAll && version.IsNullOrWhiteSpace())
{ {
await UpdateInternalAsync(projectPath, includeNightlyPreviews, includeReleaseCandidates, switchToStable); await UpdateInternalAsync(projectPath, includeNightlyPreviews, includeReleaseCandidates, switchToStable);
} }
@ -77,7 +78,8 @@ namespace Volo.Abp.Cli.ProjectModification
switchToStable, switchToStable,
latestVersionFromNuget, latestVersionFromNuget,
latestReleaseCandidateVersionFromNuget, latestReleaseCandidateVersionFromNuget,
latestVersionFromMyGet); latestVersionFromMyGet,
version);
File.WriteAllText(projectPath, updatedContent); File.WriteAllText(projectPath, updatedContent);
} }
@ -91,13 +93,26 @@ namespace Volo.Abp.Cli.ProjectModification
File.WriteAllText(projectPath, updatedContent); File.WriteAllText(projectPath, updatedContent);
} }
protected virtual async Task<bool> SpecifiedVersionExists(string version, string packageId)
{
var versionList = await _nuGetService.GetPackageVersionListAsync(packageId);
if (versionList.All(v => !v.Equals(version, StringComparison.OrdinalIgnoreCase)))
{
versionList = await _nuGetService.GetPackageVersionListAsync(packageId, true);
}
return versionList.Any(v => v.Equals(version, StringComparison.OrdinalIgnoreCase));
}
private async Task<string> UpdateVoloPackagesAsync(string content, private async Task<string> UpdateVoloPackagesAsync(string content,
bool includeNightlyPreviews = false, bool includeNightlyPreviews = false,
bool includeReleaseCandidates = false, bool includeReleaseCandidates = false,
bool switchToStable = false, bool switchToStable = false,
SemanticVersion latestNugetVersion = null, SemanticVersion latestNugetVersion = null,
SemanticVersion latestNugetReleaseCandidateVersion = null, SemanticVersion latestNugetReleaseCandidateVersion = null,
string latestMyGetVersion = null) string latestMyGetVersion = null,
string specifiedVersion = null)
{ {
string packageId = null; string packageId = null;
@ -129,40 +144,63 @@ namespace Volo.Abp.Cli.ProjectModification
Logger.LogDebug("Checking package: \"{0}\" - Current version: {1}", packageId, currentSemanticVersion); Logger.LogDebug("Checking package: \"{0}\" - Current version: {1}", packageId, currentSemanticVersion);
if ((includeNightlyPreviews || (currentVersion.Contains("-preview") && !switchToStable)) && !includeReleaseCandidates) if (!specifiedVersion.IsNullOrWhiteSpace())
{ {
var latestVersion = latestMyGetVersion ?? await GetLatestVersionFromMyGet(packageId); if (await SpecifiedVersionExists(specifiedVersion, packageId))
if (currentVersion != latestVersion)
{ {
Logger.LogInformation("Updating package \"{0}\" from v{1} to v{2}.", packageId, currentVersion, latestVersion); var specifiedSemanticVersion = SemanticVersion.Parse(specifiedVersion);
versionAttribute.Value = latestVersion; if (specifiedSemanticVersion > currentSemanticVersion)
{
Logger.LogInformation("Updating package \"{0}\" from v{1} to v{2}.", packageId, currentVersion, specifiedVersion);
versionAttribute.Value = specifiedVersion;
}
else
{
Logger.LogWarning("Unable to update package \"{0}\" version v{1} to v{2}.", packageId, currentVersion, specifiedVersion);
}
} }
else else
{ {
Logger.LogDebug("Package: \"{0}-v{1}\" is up to date.", packageId, currentVersion); Logger.LogWarning("Package \"{0}\" specified version v{1} does not exist.", packageId, specifiedVersion);
} }
} }
else else
{ {
SemanticVersion latestVersion; if ((includeNightlyPreviews || (currentVersion.Contains("-preview") && !switchToStable)) && !includeReleaseCandidates)
if (currentSemanticVersion.IsPrerelease && !switchToStable)
{
latestVersion = latestNugetReleaseCandidateVersion ?? await _nuGetService.GetLatestVersionOrNullAsync(packageId, includeReleaseCandidates: true);
}
else
{
latestVersion = latestNugetVersion ?? await _nuGetService.GetLatestVersionOrNullAsync(packageId, includeReleaseCandidates: includeReleaseCandidates);
}
if (latestVersion != null && (currentSemanticVersion < latestVersion || (currentSemanticVersion.IsPrerelease && switchToStable)))
{ {
Logger.LogInformation("Updating package \"{0}\" from v{1} to v{2}.", packageId, currentSemanticVersion.ToString(), latestVersion.ToString()); var latestVersion = latestMyGetVersion ?? await GetLatestVersionFromMyGet(packageId);
versionAttribute.Value = latestVersion.ToString();
if (currentVersion != latestVersion)
{
Logger.LogInformation("Updating package \"{0}\" from v{1} to v{2}.", packageId, currentVersion, latestVersion);
versionAttribute.Value = latestVersion;
}
else
{
Logger.LogDebug("Package: \"{0}-v{1}\" is up to date.", packageId, currentVersion);
}
} }
else else
{ {
Logger.LogInformation("Package: \"{0}-v{1}\" is up to date.", packageId, currentSemanticVersion); SemanticVersion latestVersion;
if (currentSemanticVersion.IsPrerelease && !switchToStable)
{
latestVersion = latestNugetReleaseCandidateVersion ?? await _nuGetService.GetLatestVersionOrNullAsync(packageId, includeReleaseCandidates: true);
}
else
{
latestVersion = latestNugetVersion ?? await _nuGetService.GetLatestVersionOrNullAsync(packageId, includeReleaseCandidates: includeReleaseCandidates);
}
if (latestVersion != null && (currentSemanticVersion < latestVersion || (currentSemanticVersion.IsPrerelease && switchToStable)))
{
Logger.LogInformation("Updating package \"{0}\" from v{1} to v{2}.", packageId, currentSemanticVersion.ToString(), latestVersion.ToString());
versionAttribute.Value = latestVersion.ToString();
}
else
{
Logger.LogInformation("Package: \"{0}-v{1}\" is up to date.", packageId, currentSemanticVersion);
}
} }
} }
} }

Loading…
Cancel
Save