Browse Source

refactor

pull/1295/head
Yunus Emre Kalkan 7 years ago
parent
commit
6d25b20998
  1. 160
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs
  2. 7
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs

160
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs

@ -40,6 +40,7 @@ namespace Volo.Abp.Cli
public async Task RunAsync(string[] args) public async Task RunAsync(string[] args)
{ {
Logger.LogInformation("ABP CLI (https://abp.io)"); Logger.LogInformation("ABP CLI (https://abp.io)");
Logger.LogInformation("https://abp.io");
await CheckCliVersionAsync(); await CheckCliVersionAsync();
CheckDependencies(); CheckDependencies();
@ -95,20 +96,54 @@ namespace Volo.Abp.Cli
private async Task CheckCliVersionAsync() private async Task CheckCliVersionAsync()
{ {
var assembly = typeof(CliService).Assembly; var assembly = typeof(CliService).Assembly;
var toolPath = GetToolPath(assembly);
var currentCliVersion = await GetCurrentCliVersion(toolPath, assembly);
var updateChannel = GetUpdateChannel(currentCliVersion);
var toolPath = assembly.Location.Contains(".store") ? assembly.Location.Substring(0, assembly.Location.IndexOf(".store")) : null; Logger.LogInformation($"Version {currentCliVersion} ({updateChannel} channel)");
SemanticVersion currentCliVersion = default;
try
{
var latestVersion = await GetLatestVersion(updateChannel);
if (latestVersion != null & latestVersion > currentCliVersion)
{
LogNewVersionInfo(updateChannel, latestVersion, toolPath);
}
}
catch (Exception e)
{
Logger.LogWarning("Unable to retrieve the latest version");
Logger.LogWarning(e.Message);
}
}
private static string GetToolPath(Assembly assembly)
{
if (!assembly.Location.Contains(".store"))
{
return null;
}
return assembly.Location.Substring(0, assembly.Location.IndexOf(".store", StringComparison.Ordinal));
}
private async Task<SemanticVersion> GetCurrentCliVersion(string toolPath, Assembly assembly)
{
SemanticVersion currentCliVersion = default;
if (!string.IsNullOrEmpty(toolPath)) if (!string.IsNullOrEmpty(toolPath))
{ {
var strReader = new StringReader(CmdHelper.RunCmdAndGetOutput($"dotnet tool list --tool-path {toolPath}")); var consoleOutput = new StringReader(CmdHelper.RunCmdAndGetOutput($"dotnet tool list --tool-path {toolPath}"));
string output; string line;
while ((output = await strReader.ReadLineAsync()) != null) while ((line = await consoleOutput.ReadLineAsync()) != null)
{ {
if (output.StartsWith("Volo.Abp.Cli", StringComparison.InvariantCultureIgnoreCase) && if (line.StartsWith("Volo.Abp.Cli", StringComparison.InvariantCultureIgnoreCase))
SemanticVersion.TryParse(output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries)[1], out var detectedVersion))
{ {
currentCliVersion = detectedVersion; var version = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries)[1];
SemanticVersion.TryParse(version, out currentCliVersion);
break;
} }
} }
} }
@ -118,73 +153,76 @@ namespace Volo.Abp.Cli
// If not a tool executable, fallback to assembly version and treat as dev without updates // If not a tool executable, fallback to assembly version and treat as dev without updates
// Assembly revisions are not supported by SemVer scheme required for NuGet, trim to {major}.{minor}.{patch} // Assembly revisions are not supported by SemVer scheme required for NuGet, trim to {major}.{minor}.{patch}
var assemblyVersion = string.Join(".", assembly.GetFileVersion().Split('.').Take(3)); var assemblyVersion = string.Join(".", assembly.GetFileVersion().Split('.').Take(3));
currentCliVersion = SemanticVersion.Parse(assemblyVersion + "-dev"); return SemanticVersion.Parse(assemblyVersion + "-dev");
} }
var updateChannel = return currentCliVersion;
currentCliVersion.IsPrerelease ? }
currentCliVersion.Release.Contains("preview") ?
UpdateChannel.Nightly
: currentCliVersion.Release.Contains("dev") ?
UpdateChannel.Development
: UpdateChannel.Prerelease
: UpdateChannel.Stable;
Logger.LogInformation($"Version {currentCliVersion} ({updateChannel} channel)"); private static UpdateChannel GetUpdateChannel(SemanticVersion currentCliVersion)
{
if (!currentCliVersion.IsPrerelease)
{
return UpdateChannel.Stable;
}
try if (currentCliVersion.Release.Contains("preview"))
{ {
SemanticVersion latestVersion = default; return UpdateChannel.Nightly;
}
switch (updateChannel) if (currentCliVersion.Release.Contains("dev"))
{ {
case UpdateChannel.Stable: return UpdateChannel.Development;
latestVersion = await NuGetService.GetLatestVersionOrNullAsync("Volo.Abp.Cli"); }
break;
case UpdateChannel.Prerelease: return UpdateChannel.Prerelease;
latestVersion = await NuGetService.GetLatestVersionOrNullAsync("Volo.Abp.Cli", includePreviews: true); }
break;
case UpdateChannel.Nightly: private async Task<SemanticVersion> GetLatestVersion(UpdateChannel updateChannel)
latestVersion = await NuGetService.GetLatestVersionOrNullAsync("Volo.Abp.Cli", includeNightly: true); {
break; switch (updateChannel)
} {
case UpdateChannel.Stable:
return await NuGetService.GetLatestVersionOrNullAsync("Volo.Abp.Cli");
if (latestVersion != null & latestVersion > currentCliVersion) case UpdateChannel.Prerelease:
{ return await NuGetService.GetLatestVersionOrNullAsync("Volo.Abp.Cli", includePreviews: true);
Logger.LogWarning($"ABP CLI has a newer {updateChannel.ToString().ToLowerInvariant()} version {latestVersion}, please update to get the latest features and fixes.");
Logger.LogWarning("");
Logger.LogWarning("Update Command: ");
// Update command doesn't support prerelease versions https://github.com/dotnet/sdk/issues/2551 workaround is to uninstall & install case UpdateChannel.Nightly:
switch (updateChannel) return await NuGetService.GetLatestVersionOrNullAsync("Volo.Abp.Cli", includeNightly: true);
{ default:
case UpdateChannel.Stable: return default;
Logger.LogWarning($" dotnet tool update --tool-path {toolPath} Volo.Abp.Cli"); }
break; }
case UpdateChannel.Prerelease:
Logger.LogWarning($" dotnet tool uninstall --tool-path {toolPath} Volo.Abp.Cli");
Logger.LogWarning($" dotnet tool install --tool-path {toolPath} --version {latestVersion} Volo.Abp.Cli");
break;
case UpdateChannel.Nightly:
Logger.LogWarning($" dotnet tool uninstall --tool-path {toolPath} Volo.Abp.Cli");
Logger.LogWarning($" dotnet tool install --tool-path {toolPath} --add-source https://www.myget.org/F/abp-nightly/api/v3/index.json --version {latestVersion} Volo.Abp.Cli");
break;
}
Logger.LogWarning(""); private void LogNewVersionInfo(UpdateChannel updateChannel, SemanticVersion latestVersion, string toolPath)
} {
Logger.LogWarning(
$"ABP CLI has a newer {updateChannel.ToString().ToLowerInvariant()} version {latestVersion}, please update to get the latest features and fixes.");
Logger.LogWarning("");
Logger.LogWarning("Update Command: ");
return; // Update command doesn't support prerelease versions https://github.com/dotnet/sdk/issues/2551 workaround is to uninstall & install
} switch (updateChannel)
catch (Exception e)
{ {
Logger.LogWarning("Unable to retrieve the latest version"); case UpdateChannel.Stable:
Logger.LogWarning(e.Message); Logger.LogWarning($" dotnet tool update --tool-path {toolPath} Volo.Abp.Cli");
break;
case UpdateChannel.Prerelease:
Logger.LogWarning($" dotnet tool uninstall --tool-path {toolPath} Volo.Abp.Cli");
Logger.LogWarning($" dotnet tool install --tool-path {toolPath} --version {latestVersion} Volo.Abp.Cli");
break;
case UpdateChannel.Nightly:
Logger.LogWarning($" dotnet tool uninstall --tool-path {toolPath} Volo.Abp.Cli");
Logger.LogWarning(
$" dotnet tool install --tool-path {toolPath} --add-source https://www.myget.org/F/abp-nightly/api/v3/index.json --version {latestVersion} Volo.Abp.Cli");
break;
} }
Logger.LogWarning("");
} }
protected enum UpdateChannel protected enum UpdateChannel

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

@ -3,6 +3,7 @@ using System.IO;
using System.Text; using System.Text;
using Volo.Abp.Cli.NuGet; using Volo.Abp.Cli.NuGet;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
namespace Volo.Abp.Cli.ProjectModification namespace Volo.Abp.Cli.ProjectModification
{ {
@ -75,9 +76,9 @@ namespace Volo.Abp.Cli.ProjectModification
content = content.Substring(indexAfterSecondQuote); content = content.Substring(indexAfterSecondQuote);
var indexOfThirdQuote = content.IndexOf("\"", StringComparison.Ordinal); var indexOfThirdQuote = content.IndexOf("\"", StringComparison.Ordinal);
var version = _nuGetService.GetLatestVersionOrNullAsync(packageId, includePreviews).GetAwaiter().GetResult(); var version = AsyncHelper.RunSync(() => _nuGetService.GetLatestVersionOrNullAsync(packageId, includePreviews));
returningText.Append(version.ToString()); returningText.Append(version);
index = indexOfPackageReference + packageReferenceStartText.Length + indexAfterQuote + indexAfterSecondQuote + indexOfThirdQuote; index = indexOfPackageReference + packageReferenceStartText.Length + indexAfterQuote + indexAfterSecondQuote + indexOfThirdQuote;

Loading…
Cancel
Save