Merge pull request #14182 from abpframework/Check-latest-version-one-time-per-day

Cli: Check latest version one time per day
pull/14183/head
Alper Ebiçoğlu 3 years ago committed by GitHub
commit 72fccf9acb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,11 +1,4 @@
using System.Collections.Generic;
using System.Security.Policy;
using Volo.Abp.Cli.ProjectBuilding.Templates.App;
using Volo.Abp.Cli.ProjectBuilding.Templates.Microservice;
using Volo.Abp.Cli.ProjectBuilding.Templates.Module;
using Volo.Abp.Cli.ProjectBuilding.Templates.MvcModule;
namespace Volo.Abp.Cli;
namespace Volo.Abp.Cli;
public static class CliConsts
{
@ -24,4 +17,9 @@ public static class CliConsts
public const string AppSettingsJsonFileName = "appsettings.json";
public const string AppSettingsSecretJsonFileName = "appsettings.secrets.json";
public static class MemoryKeys
{
public const string LatestCliVersionCheckDate = "LatestCliVersionCheckDate";
}
}

@ -10,6 +10,7 @@ public static class CliPaths
public static string Log => Path.Combine(AbpRootPath, "cli", "logs");
public static string Root => Path.Combine(AbpRootPath, "cli");
public static string AccessToken => Path.Combine(AbpRootPath, "cli", "access-token.bin");
public static string Memory => Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)!, "memory.bin");
public static string Build => Path.Combine(AbpRootPath, "build");
public static string Lic => Path.Combine(Path.GetTempPath(), Encoding.ASCII.GetString(new byte[] { 65, 98, 112, 76, 105, 99, 101, 110, 115, 101, 46, 98, 105, 110 }));

@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging.Abstractions;
using NuGet.Versioning;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
@ -11,14 +12,17 @@ using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Commands;
using Volo.Abp.Cli.Memory;
using Volo.Abp.Cli.NuGet;
using Volo.Abp.Cli.Utils;
using Volo.Abp.DependencyInjection;
using Volo.Abp.IO;
namespace Volo.Abp.Cli;
public class CliService : ITransientDependency
{
private readonly MemoryService _memoryService;
public ILogger<CliService> Logger { get; set; }
protected ICommandLineArgumentParser CommandLineArgumentParser { get; }
protected ICommandSelector CommandSelector { get; }
@ -31,8 +35,10 @@ public class CliService : ITransientDependency
ICommandSelector commandSelector,
IServiceScopeFactory serviceScopeFactory,
NuGetService nugetService,
ICmdHelper cmdHelper)
ICmdHelper cmdHelper,
MemoryService memoryService)
{
_memoryService = memoryService;
CommandLineArgumentParser = commandLineArgumentParser;
CommandSelector = commandSelector;
ServiceScopeFactory = serviceScopeFactory;
@ -164,6 +170,11 @@ public class CliService : ITransientDependency
private async Task CheckCliVersionAsync()
{
if (!await IsLatestVersionCheckExpiredAsync())
{
return;
}
var assembly = typeof(CliService).Assembly;
var toolPath = GetToolPath(assembly);
var currentCliVersion = await GetCurrentCliVersionInternalAsync(assembly);
@ -187,6 +198,27 @@ public class CliService : ITransientDependency
}
}
private async Task<bool> IsLatestVersionCheckExpiredAsync()
{
try
{
var latestTime = await _memoryService.GetAsync(CliConsts.MemoryKeys.LatestCliVersionCheckDate);
if (latestTime != null && DateTime.Now - DateTime.Parse(latestTime, CultureInfo.InvariantCulture) < TimeSpan.FromDays(1))
{
return false;
}
await _memoryService.SetAsync(CliConsts.MemoryKeys.LatestCliVersionCheckDate, DateTime.Now.ToString(CultureInfo.InvariantCulture));
return true;
}
catch (Exception)
{
return true;
}
}
private string GetToolPath(Assembly assembly)
{
if (!assembly.Location.Contains(".store"))

@ -39,13 +39,8 @@ public class GetSourceCommand : IConsoleCommand, ITransientDependency
}
var version = commandLineArgs.Options.GetOrNull(Options.Version.Short, Options.Version.Long);
if (version != null)
{
Logger.LogInformation("Version: " + version);
}
var outputFolder = GetOutPutFolder(commandLineArgs);
Logger.LogInformation("Output folder: " + outputFolder);
var gitHubAbpLocalRepositoryPath = commandLineArgs.Options.GetOrNull(Options.GitHubAbpLocalRepositoryPath.Long);
if (gitHubAbpLocalRepositoryPath != null)

@ -33,8 +33,7 @@ public class SourceCodeDownloadService : ITransientDependency
public async Task DownloadModuleAsync(string moduleName, string outputFolder, string version, string gitHubAbpLocalRepositoryPath, string gitHubVoloLocalRepositoryPath, AbpCommandLineOptions options)
{
Logger.LogInformation("Downloading source code of " + moduleName);
Logger.LogInformation("Version: " + (version ?? "Latest"));
Logger.LogInformation($"Downloading source code of {moduleName} (v{version ?? "Latest"})");
Logger.LogInformation("Output folder: " + outputFolder);
var result = await ModuleProjectBuilder.BuildAsync(

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.DependencyInjection;
using Volo.Abp.IO;
namespace Volo.Abp.Cli.Memory;
public class MemoryService : ITransientDependency
{
private const string KeyValueSeparator = "|||";
[ItemCanBeNull]
public async Task<string> GetAsync(string key)
{
if (!File.Exists(CliPaths.Memory))
{
return null;
}
return (await FileHelper.ReadAllTextAsync(CliPaths.Memory))
.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.None)
.FirstOrDefault(x => x.StartsWith($"{key} "))?.Split(KeyValueSeparator).Last().Trim();
}
public async Task SetAsync(string key, string value)
{
if (!File.Exists(CliPaths.Memory))
{
File.WriteAllText(CliPaths.Memory,
$"{key} {KeyValueSeparator} {value}"
);
return;
}
var memoryContentLines = (await FileHelper.ReadAllTextAsync(CliPaths.Memory))
.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.None)
.ToList();
memoryContentLines.RemoveAll(x => x.StartsWith(key));
memoryContentLines.Add($"{key} {KeyValueSeparator} {value}");
File.WriteAllText(CliPaths.Memory,
memoryContentLines.JoinAsString(Environment.NewLine)
);
}
}
Loading…
Cancel
Save