mirror of https://github.com/abpframework/abp.git
committed by
GitHub
28 changed files with 499 additions and 183 deletions
@ -0,0 +1,44 @@ |
|||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Cli.Args; |
||||
|
using Volo.Abp.Cli.ProjectModification; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Cli.Commands |
||||
|
{ |
||||
|
public class SwitchToPreviewCommand : IConsoleCommand, ITransientDependency |
||||
|
{ |
||||
|
private readonly PackagePreviewSwitcher _packagePreviewSwitcher; |
||||
|
|
||||
|
public SwitchToPreviewCommand(PackagePreviewSwitcher packagePreviewSwitcher) |
||||
|
{ |
||||
|
_packagePreviewSwitcher = packagePreviewSwitcher; |
||||
|
} |
||||
|
|
||||
|
public async Task ExecuteAsync(CommandLineArgs commandLineArgs) |
||||
|
{ |
||||
|
await _packagePreviewSwitcher.SwitchToPreview(commandLineArgs); |
||||
|
} |
||||
|
|
||||
|
public string GetUsageInfo() |
||||
|
{ |
||||
|
var sb = new StringBuilder(); |
||||
|
|
||||
|
sb.AppendLine(""); |
||||
|
sb.AppendLine("Usage:"); |
||||
|
sb.AppendLine(" abp switch-to-preview [options]"); |
||||
|
sb.AppendLine(""); |
||||
|
sb.AppendLine("Options:"); |
||||
|
sb.AppendLine("-sd|--solution-directory"); |
||||
|
sb.AppendLine(""); |
||||
|
sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); |
||||
|
|
||||
|
return sb.ToString(); |
||||
|
} |
||||
|
|
||||
|
public string GetShortDescription() |
||||
|
{ |
||||
|
return "Switches packages to preview ABP version."; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,76 +0,0 @@ |
|||||
using System; |
|
||||
using System.IO; |
|
||||
using System.Xml; |
|
||||
using Microsoft.Extensions.Logging; |
|
||||
using Microsoft.Extensions.Logging.Abstractions; |
|
||||
using Volo.Abp.DependencyInjection; |
|
||||
|
|
||||
namespace Volo.Abp.Cli.ProjectModification |
|
||||
{ |
|
||||
public class PackageSourceAdder: ITransientDependency |
|
||||
{ |
|
||||
public ILogger<PackageSourceAdder> Logger { get; set; } |
|
||||
|
|
||||
public PackageSourceAdder() |
|
||||
{ |
|
||||
Logger = NullLogger<PackageSourceAdder>.Instance; |
|
||||
} |
|
||||
|
|
||||
public void Add(string sourceKey, string sourceValue) |
|
||||
{ |
|
||||
var nugetConfigPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), |
|
||||
"NuGet", "NuGet.Config"); |
|
||||
|
|
||||
if (!File.Exists(nugetConfigPath)) |
|
||||
{ |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
var fileContent = File.ReadAllText(nugetConfigPath); |
|
||||
|
|
||||
if (fileContent.Contains($"\"{sourceValue}\"")) |
|
||||
{ |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
Logger.LogInformation($"Adding \"{sourceValue}\" ({sourceKey}) to nuget sources..."); |
|
||||
|
|
||||
try |
|
||||
{ |
|
||||
var doc = new XmlDocument() { PreserveWhitespace = true }; |
|
||||
|
|
||||
doc.Load(GenerateStreamFromString(fileContent)); |
|
||||
|
|
||||
var sourceNodes = doc.SelectNodes("/configuration/packageSources"); |
|
||||
|
|
||||
var newNode = doc.CreateElement("add"); |
|
||||
|
|
||||
var includeAttr = doc.CreateAttribute("key"); |
|
||||
includeAttr.Value = sourceKey; |
|
||||
newNode.Attributes.Append(includeAttr); |
|
||||
|
|
||||
var versionAttr = doc.CreateAttribute("value"); |
|
||||
versionAttr.Value = sourceValue; |
|
||||
newNode.Attributes.Append(versionAttr); |
|
||||
|
|
||||
sourceNodes?[0]?.AppendChild(newNode); |
|
||||
|
|
||||
File.WriteAllText(nugetConfigPath, doc.OuterXml); |
|
||||
} |
|
||||
catch |
|
||||
{ |
|
||||
Logger.LogWarning($"Adding \"{sourceValue}\" ({sourceKey}) to nuget sources FAILED."); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
private static Stream GenerateStreamFromString(string s) |
|
||||
{ |
|
||||
var stream = new MemoryStream(); |
|
||||
var writer = new StreamWriter(stream); |
|
||||
writer.Write(s); |
|
||||
writer.Flush(); |
|
||||
stream.Position = 0; |
|
||||
return stream; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,126 @@ |
|||||
|
using System; |
||||
|
using System.IO; |
||||
|
using System.Xml; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Microsoft.Extensions.Logging.Abstractions; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Cli.ProjectModification |
||||
|
{ |
||||
|
public class PackageSourceManager: ITransientDependency |
||||
|
{ |
||||
|
public ILogger<PackageSourceManager> Logger { get; set; } |
||||
|
|
||||
|
public PackageSourceManager() |
||||
|
{ |
||||
|
Logger = NullLogger<PackageSourceManager>.Instance; |
||||
|
} |
||||
|
|
||||
|
public void Add(string solutionFolder, string sourceKey, string sourceValue) |
||||
|
{ |
||||
|
var nugetConfigPath = GetNugetConfigPath(solutionFolder); |
||||
|
|
||||
|
Logger.LogInformation($"Adding \"{sourceValue}\" ({sourceKey}) to nuget sources..."); |
||||
|
|
||||
|
if (!File.Exists(nugetConfigPath)) |
||||
|
{ |
||||
|
File.WriteAllText(nugetConfigPath, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + |
||||
|
"<configuration>\n" + |
||||
|
" <packageSources>\n" + |
||||
|
" <add key=\"nuget.org\" value=\"https://api.nuget.org/v3/index.json\" />\n" + |
||||
|
$" <add key=\"{sourceKey}\" value=\"{sourceValue}\" />\n" + |
||||
|
" </packageSources>\n" + |
||||
|
"</configuration>"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
var fileContent = File.ReadAllText(nugetConfigPath); |
||||
|
|
||||
|
if (fileContent.Contains($"\"{sourceValue}\"")) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
var doc = new XmlDocument() { PreserveWhitespace = true }; |
||||
|
|
||||
|
doc.Load(GenerateStreamFromString(fileContent)); |
||||
|
|
||||
|
var sourceNodes = doc.SelectNodes("/configuration/packageSources"); |
||||
|
|
||||
|
var newNode = doc.CreateElement("add"); |
||||
|
|
||||
|
var includeAttr = doc.CreateAttribute("key"); |
||||
|
includeAttr.Value = sourceKey; |
||||
|
newNode.Attributes.Append(includeAttr); |
||||
|
|
||||
|
var versionAttr = doc.CreateAttribute("value"); |
||||
|
versionAttr.Value = sourceValue; |
||||
|
newNode.Attributes.Append(versionAttr); |
||||
|
|
||||
|
sourceNodes?[0]?.AppendChild(newNode); |
||||
|
|
||||
|
File.WriteAllText(nugetConfigPath, doc.OuterXml); |
||||
|
} |
||||
|
catch |
||||
|
{ |
||||
|
Logger.LogWarning($"Adding \"{sourceValue}\" ({sourceKey}) to nuget sources FAILED."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Remove(string solutionFolder, string sourceKey) |
||||
|
{ |
||||
|
var nugetConfigPath = GetNugetConfigPath(solutionFolder); |
||||
|
|
||||
|
if (!File.Exists(nugetConfigPath)) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
var fileContent = File.ReadAllText(nugetConfigPath); |
||||
|
|
||||
|
if (!fileContent.Contains($"\"{sourceKey}\"")) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
Logger.LogInformation($"Removing \"{sourceKey}\" from nuget sources..."); |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
var doc = new XmlDocument() { PreserveWhitespace = true }; |
||||
|
|
||||
|
doc.Load(GenerateStreamFromString(fileContent)); |
||||
|
|
||||
|
var nodes = doc.SelectNodes($"/configuration/packageSources/add[@key='{sourceKey}']"); |
||||
|
|
||||
|
if (nodes != null && nodes.Count > 0) |
||||
|
{ |
||||
|
nodes[0].ParentNode.RemoveChild(nodes[0]); |
||||
|
} |
||||
|
|
||||
|
File.WriteAllText(nugetConfigPath, doc.OuterXml); |
||||
|
} |
||||
|
catch |
||||
|
{ |
||||
|
Logger.LogWarning($"Removing \"{sourceKey}\" from nuget sources FAILED."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static string GetNugetConfigPath(string solutionFolder) |
||||
|
{ |
||||
|
return Path.Combine(solutionFolder, "NuGet.Config"); |
||||
|
} |
||||
|
|
||||
|
private static Stream GenerateStreamFromString(string s) |
||||
|
{ |
||||
|
var stream = new MemoryStream(); |
||||
|
var writer = new StreamWriter(stream); |
||||
|
writer.Write(s); |
||||
|
writer.Flush(); |
||||
|
stream.Position = 0; |
||||
|
return stream; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using Volo.Docs.Projects; |
||||
|
|
||||
|
namespace Volo.Docs.Version |
||||
|
{ |
||||
|
public interface IVersionHelper |
||||
|
{ |
||||
|
List<string> OrderByDescending(List<string> versions); |
||||
|
|
||||
|
List<VersionInfoDto> OrderByDescending(List<VersionInfoDto> versions); |
||||
|
|
||||
|
bool IsPreRelease(string version); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,52 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using NuGet.Versioning; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Docs.Projects; |
||||
|
|
||||
|
namespace Volo.Docs.Version |
||||
|
{ |
||||
|
public class SemanticVersionHelper : IVersionHelper, ITransientDependency |
||||
|
{ |
||||
|
public List<string> OrderByDescending(List<string> versions) |
||||
|
{ |
||||
|
return versions.OrderByDescending(v=> SemanticVersion.Parse(NormalizeVersion(v)), new VersionComparer()).ToList(); |
||||
|
} |
||||
|
|
||||
|
public List<VersionInfoDto> OrderByDescending(List<VersionInfoDto> versions) |
||||
|
{ |
||||
|
return versions.OrderByDescending(v => SemanticVersion.Parse(NormalizeVersion(v.Name)), new VersionComparer()).ToList(); |
||||
|
} |
||||
|
|
||||
|
public bool IsPreRelease(string version) |
||||
|
{ |
||||
|
return SemanticVersion.Parse(NormalizeVersion(version)).IsPrerelease; |
||||
|
} |
||||
|
|
||||
|
private string NormalizeVersion(string version) |
||||
|
{ |
||||
|
version = version.RemovePreFix("v"); |
||||
|
|
||||
|
var normalizedVersion = ""; |
||||
|
|
||||
|
var versionParts = version.Split("-"); |
||||
|
|
||||
|
if (versionParts[0].Split(".").Length > 3) |
||||
|
{ |
||||
|
normalizedVersion = string.Join(".",versionParts[0].Split(".").Take(3)); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
normalizedVersion = versionParts[0]; |
||||
|
} |
||||
|
|
||||
|
if (versionParts.Length > 1) |
||||
|
{ |
||||
|
return normalizedVersion + "-" + string.Join("-", versionParts.Skip(1)); |
||||
|
} |
||||
|
|
||||
|
return normalizedVersion; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue