mirror of https://github.com/abpframework/abp.git
7 changed files with 223 additions and 40 deletions
@ -0,0 +1,11 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.Cli.ProjectModification |
||||
|
{ |
||||
|
public class MyGetApiResponse |
||||
|
{ |
||||
|
public string _date { get; set; } |
||||
|
|
||||
|
public List<MyGetPackage> Packages { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.Cli.ProjectModification |
||||
|
{ |
||||
|
public class MyGetPackage |
||||
|
{ |
||||
|
public string PackageType { get; set; } |
||||
|
|
||||
|
public string Id { get; set; } |
||||
|
|
||||
|
public List<string> Versions { get; set; } |
||||
|
|
||||
|
public List<string> Dates { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,50 @@ |
|||||
|
using System; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Microsoft.Extensions.Logging.Abstractions; |
||||
|
using Newtonsoft.Json; |
||||
|
using Volo.Abp.Cli.Http; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Cli.ProjectModification |
||||
|
{ |
||||
|
public class MyGetPackageListFinder : ISingletonDependency |
||||
|
{ |
||||
|
private MyGetApiResponse _response; |
||||
|
|
||||
|
public ILogger<MyGetPackageListFinder> Logger { get; set; } |
||||
|
|
||||
|
public MyGetPackageListFinder() |
||||
|
{ |
||||
|
Logger = NullLogger<MyGetPackageListFinder>.Instance; |
||||
|
} |
||||
|
|
||||
|
public async Task<MyGetApiResponse> GetPackages() |
||||
|
{ |
||||
|
if (_response != null) |
||||
|
{ |
||||
|
return _response; |
||||
|
} |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
using (var client = new CliHttpClient(TimeSpan.FromMinutes(10))) |
||||
|
{ |
||||
|
var responseMessage = await client.GetAsync( |
||||
|
$"{CliUrls.WwwAbpIo}api/myget/packages/" |
||||
|
); |
||||
|
|
||||
|
_response = JsonConvert.DeserializeObject<MyGetApiResponse>(Encoding.Default.GetString(await responseMessage.Content.ReadAsByteArrayAsync())); |
||||
|
} |
||||
|
} |
||||
|
catch (Exception) |
||||
|
{ |
||||
|
Logger.LogError("Unable to get latest preview version."); |
||||
|
throw; |
||||
|
} |
||||
|
|
||||
|
return _response; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,76 @@ |
|||||
|
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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue