Yunus Emre Kalkan 7 years ago
parent
commit
33aebc9ebc
  1. 35
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/UpdateCommand.cs
  2. 25
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloPackagesVersionUpdater.cs

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

@ -1,14 +1,10 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using Volo.Abp.Cli.Args; using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.ProjectBuilding.Github;
using Volo.Abp.Cli.ProjectModification; using Volo.Abp.Cli.ProjectModification;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -35,12 +31,11 @@ namespace Volo.Abp.Cli.Commands
if (solution != null) if (solution != null)
{ {
var version = await GetLatestAbpVersion(includePreviews);
var solutionName = Path.GetFileName(solution).RemovePostFix(".sln"); var solutionName = Path.GetFileName(solution).RemovePostFix(".sln");
_packagesVersionUpdater.UpdateSolution(solution, version); _packagesVersionUpdater.UpdateSolution(solution, includePreviews);
Logger.LogInformation($"Volo packages are updated to {version} in {solutionName} solution."); Logger.LogInformation($"Volo packages are updated in {solutionName} solution.");
return; return;
} }
@ -48,39 +43,17 @@ namespace Volo.Abp.Cli.Commands
if (project != null) if (project != null)
{ {
var version = await GetLatestAbpVersion(includePreviews);
var projectName = Path.GetFileName(project).RemovePostFix(".csproj"); var projectName = Path.GetFileName(project).RemovePostFix(".csproj");
_packagesVersionUpdater.UpdateProject(project, version); _packagesVersionUpdater.UpdateProject(project, includePreviews);
Logger.LogInformation($"Volo packages are updated to {version} in {projectName} project."); Logger.LogInformation($"Volo packages are updated in {projectName} project.");
return; return;
} }
Logger.LogError("No solution or project found in this directory."); Logger.LogError("No solution or project found in this directory.");
} }
protected virtual async Task<string> GetLatestAbpVersion(bool includePreviews)
{
var url = "https://api.github.com/repos/abpframework/abp/releases";
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromMinutes(30);
client.DefaultRequestHeaders.UserAgent.ParseAdd("MyAgent/1.0");
var result = await client.GetStringAsync(url);
var releases = JsonConvert.DeserializeObject<List<GithubRelease>>(result);
if (!includePreviews)
{
releases.RemoveAll(v => v.IsPrerelease);
}
return releases.First().Name.RemovePreFix("v");
}
}
public static class Options public static class Options
{ {
public static class IncludePreviews public static class IncludePreviews

25
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloPackagesVersionUpdater.cs

@ -1,28 +1,36 @@
using System; using System;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Volo.Abp.Cli.NuGet;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.ProjectModification namespace Volo.Abp.Cli.ProjectModification
{ {
public class VoloPackagesVersionUpdater : ITransientDependency public class VoloPackagesVersionUpdater : ITransientDependency
{ {
public void UpdateSolution(string solutionPath, string newVersion) private readonly NuGetService _nuGetService;
public VoloPackagesVersionUpdater(NuGetService nuGetService)
{
_nuGetService = nuGetService;
}
public void UpdateSolution(string solutionPath, bool includePreviews)
{ {
var projectPaths = ProjectFinder.GetProjectFiles(solutionPath); var projectPaths = ProjectFinder.GetProjectFiles(solutionPath);
foreach (var filePath in projectPaths) foreach (var filePath in projectPaths)
{ {
UpdateInternal(filePath, newVersion); UpdateInternal(filePath, includePreviews);
} }
} }
public void UpdateProject(string projectPath, string newVersion) public void UpdateProject(string projectPath, bool includePreviews)
{ {
UpdateInternal(projectPath, newVersion); UpdateInternal(projectPath, includePreviews);
} }
protected virtual void UpdateInternal(string projectPath, string newVersion) protected virtual void UpdateInternal(string projectPath, bool includePreviews)
{ {
var fileContent = File.ReadAllText(projectPath); var fileContent = File.ReadAllText(projectPath);
@ -32,13 +40,13 @@ namespace Volo.Abp.Cli.ProjectModification
while (index >= 0) while (index >= 0)
{ {
fileContent = fileContent.Substring(index); fileContent = fileContent.Substring(index);
content.Append(ReplaceAPackage(fileContent, newVersion, out index)); content.Append(ReplaceAPackage(fileContent, includePreviews, out index));
} }
File.WriteAllText(projectPath, content.ToString()); File.WriteAllText(projectPath, content.ToString());
} }
private string ReplaceAPackage(string content, string version, out int index) private string ReplaceAPackage(string content, bool includePreviews, out int index)
{ {
var packageReferenceStartText = "nclude=\"Volo."; var packageReferenceStartText = "nclude=\"Volo.";
var returningText = new StringBuilder(); var returningText = new StringBuilder();
@ -56,6 +64,8 @@ namespace Volo.Abp.Cli.ProjectModification
var indexAfterQuote = content.IndexOf("\"", StringComparison.Ordinal) + 1; var indexAfterQuote = content.IndexOf("\"", StringComparison.Ordinal) + 1;
var packageId = "Volo." + content.Substring(indexAfterQuote - 1);
returningText.Append(content.Substring(0, indexAfterQuote)); returningText.Append(content.Substring(0, indexAfterQuote));
content = content.Substring(indexAfterQuote); content = content.Substring(indexAfterQuote);
@ -66,6 +76,7 @@ namespace Volo.Abp.Cli.ProjectModification
var indexOfThirdQuote = content.IndexOf("\"", StringComparison.Ordinal); var indexOfThirdQuote = content.IndexOf("\"", StringComparison.Ordinal);
var version = _nuGetService.GetLatestVersionOrNullAsync(packageId, includePreviews);
returningText.Append(version); returningText.Append(version);
index = indexOfPackageReference + packageReferenceStartText.Length + indexAfterQuote + indexAfterSecondQuote + indexOfThirdQuote; index = indexOfPackageReference + packageReferenceStartText.Length + indexAfterQuote + indexAfterSecondQuote + indexOfThirdQuote;

Loading…
Cancel
Save