Skip to content

Commit

Permalink
fix install package
Browse files Browse the repository at this point in the history
  • Loading branch information
LazuliKao committed Jun 23, 2023
1 parent daa7c10 commit cbc963d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 80 deletions.
154 changes: 76 additions & 78 deletions src/LipNETWrapper/LipConsoleWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,94 +1,93 @@
#nullable enable
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using LipNETWrapper.Class;
using Newtonsoft.Json;

namespace LipNETWrapper
namespace LipNETWrapper;

public class LipConsoleWrapper : ILipWrapper
{
public class LipConsoleWrapper : ILipWrapper
public LipConsoleWrapper(string executablePath = "lip.exe", string? workingDir = null)
{
public LipConsoleWrapper(string executablePath = "lip.exe", string? workingDir = null)
{
ExecutablePath = executablePath;
WorkingPath = workingDir;
}
public string ExecutablePath { get; set; }
public string? WorkingPath { get; set; }
public async Task<string> GetLipVersion(CancellationToken tk = default)
{
return (await new LipConsoleLoader(ExecutablePath, WorkingPath)
.RunString(LipCommand.Create("-V"), tk: tk)).Trim();
}
public async Task<(LipPackage[] packages, string message)> GetAllPackagesAsync(CancellationToken tk = default)
{
async Task<(LipPackage[] packages, string message)> GetInternal()
{
var json = await new LipConsoleLoader(ExecutablePath, WorkingPath)
.RunString(LipCommand.Create("list", true).WithJson(), tk: tk);
Debug.WriteLine(json);
var arr = JsonConvert.DeserializeObject<LipPackage[]>(json);
return (arr ?? Array.Empty<LipPackage>(), json);
}
try
{
return await GetInternal();
}
catch
{ //retry once
return await GetInternal();
}
}
public async Task<(bool success, LipPackageVersions? package, string message)> GetPackageInfoAsync(string packageId, CancellationToken tk = default, Action<string>? onOutput = null)
ExecutablePath = executablePath;
WorkingPath = workingDir;
}
public string ExecutablePath { get; set; }
public string? WorkingPath { get; set; }
public async Task<string> GetLipVersion(CancellationToken tk = default)
{
return (await new LipConsoleLoader(ExecutablePath, WorkingPath)
.RunString(LipCommand.Create("-V"), tk: tk)).Trim();
}
public async Task<(LipPackage[] packages, string message)> GetAllPackagesAsync(CancellationToken tk = default)
{
async Task<(LipPackage[] packages, string message)> GetInternal()
{
var json = await new LipConsoleLoader(ExecutablePath, WorkingPath)
.RunString(LipCommand.Create("show", onOutput is null).WithJson() + "--available" + packageId, onOutput, tk);
var obj = JsonConvert.DeserializeObject<LipPackageItem>(json)?.AvailableVersions;
return (obj is not null, obj, json);
.RunString(LipCommand.Create("list", true).WithJson(), tk: tk);
Debug.WriteLine(json);
var arr = JsonConvert.DeserializeObject<LipPackage[]>(json);
return (arr ?? Array.Empty<LipPackage>(), json);
}
public async Task<(bool success, LipPackage? package, string message)> GetLocalPackageInfoAsync(string packageId, CancellationToken tk = default)
try
{
var json = await new LipConsoleLoader(ExecutablePath, WorkingPath)
.RunString(LipCommand.Create("show", true).WithJson() + packageId, tk: tk);
var obj = JsonConvert.DeserializeObject<LipPackageItem>(json)?.Package;
return (obj is not null, obj, json);
return await GetInternal();
}
public Task<int> InstallPackageAsync(string packageId, bool upgrade = false, bool skipDependency = false, CancellationToken tk = default, Action<string, Action<string>>? onOutput = null)
{
var cmd = LipCommand.Create("install") + "-y" /*+ "--numeric-progress"*/;
if (upgrade)
cmd += "--upgrade";
if (skipDependency)
cmd += "--no-dependencies";
//todo 强制重装
//if (forceReinstall)
// cmd += "--force-reinstall";
return new LipConsoleLoader(ExecutablePath, WorkingPath)
.RunWithInput(cmd + packageId, onOutput, tk);
catch
{ //retry once
return await GetInternal();
}
}
public async Task<(bool success, LipPackageVersions? package, string message)> GetPackageInfoAsync(string packageId, CancellationToken tk = default, Action<string>? onOutput = null)
{
var json = await new LipConsoleLoader(ExecutablePath, WorkingPath)
.RunString(LipCommand.Create("show", onOutput is null).WithJson() + "--available" + packageId, onOutput, tk);
var obj = JsonConvert.DeserializeObject<LipPackageItem>(json)?.AvailableVersions;
return (obj is not null, obj, json);
}
public async Task<(bool success, LipPackage? package, string message)> GetLocalPackageInfoAsync(string packageId, CancellationToken tk = default)
{
var json = await new LipConsoleLoader(ExecutablePath, WorkingPath)
.RunString(LipCommand.Create("show", true).WithJson() + packageId, tk: tk);
var obj = JsonConvert.DeserializeObject<LipPackageItem>(json)?.Package;
return (obj is not null, obj, json);
}
public Task<int> InstallPackageAsync(string packageId, bool upgrade = false, bool skipDependency = false, CancellationToken tk = default, Action<string, Action<string>>? onOutput = null)
{
var cmd = LipCommand.Create("install")/* + "-y"*/ /*+ "--numeric-progress"*/;
if (upgrade)
cmd += "--upgrade";
if (skipDependency)
cmd += "--no-dependencies";
//todo 强制重装
//if (forceReinstall)
// cmd += "--force-reinstall";
return new LipConsoleLoader(ExecutablePath, WorkingPath)
.RunWithInput(cmd + packageId, onOutput, tk);
}

public Task<int> UninstallPackageAsync(string packageId, CancellationToken tk = default, Action<string>? onOutput = null)
{
return new LipConsoleLoader(ExecutablePath, WorkingPath)
.Run(LipCommand.Create("uninstall") + "-y" + packageId, onOutput, tk);
}
public async Task<LipRegistry> GetLipRegistryAsync(string registry, CancellationToken tk = default)
{
public Task<int> UninstallPackageAsync(string packageId, CancellationToken tk = default, Action<string>? onOutput = null)
{
return new LipConsoleLoader(ExecutablePath, WorkingPath)
.Run(LipCommand.Create("uninstall") + "-y" + packageId, onOutput, tk);
}
public async Task<LipRegistry> GetLipRegistryAsync(string registry, CancellationToken tk = default)
{
#if NET7_0 || NETCOREAPP
var client = new System.Net.Http.HttpClient() { };
var response = await client.GetAsync(registry, tk);
var client = new System.Net.Http.HttpClient() { };
var response = await client.GetAsync(registry, tk);

if (!response.IsSuccessStatusCode)
{
throw new Exception("Failed to get registry: " + response.StatusCode);
}
var content = await response.Content.ReadAsStringAsync(tk);
return JsonConvert.DeserializeObject<LipRegistry>(content)!;
if (!response.IsSuccessStatusCode)
{
throw new Exception("Failed to get registry: " + response.StatusCode);
}
var content = await response.Content.ReadAsStringAsync(tk);
return JsonConvert.DeserializeObject<LipRegistry>(content)!;
#else
using var client = new WebClient() { Encoding = Encoding.UTF8 };
var text = await client.DownloadStringTaskAsync(registry);
Expand All @@ -98,11 +97,10 @@ public async Task<LipRegistry> GetLipRegistryAsync(string registry, Cancellation
}
return JsonConvert.DeserializeObject<LipRegistry>(text)!;
#endif
}
public Task CachePurge()
{
return new LipConsoleLoader(ExecutablePath, WorkingPath)
.Run(LipCommand.Create("cache") + "purge", null, CancellationToken.None);
}
}
}
public Task CachePurge()
{
return new LipConsoleLoader(ExecutablePath, WorkingPath)
.Run(LipCommand.Create("cache") + "purge", null, CancellationToken.None);
}
}
4 changes: 2 additions & 2 deletions src/LipUI/ViewModels/InstallPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task Install()
{
if (!string.IsNullOrWhiteSpace(x))
{
if (x.EndsWith("(y/n)", true, CultureInfo.InvariantCulture))//条款
if (x.EndsWith("[y/N]", true, CultureInfo.InvariantCulture))//条款
{
Task.Delay(1000).ContinueWith(async _ =>
{
Expand All @@ -61,7 +61,7 @@ public async Task Install()
OutPut.Last()
.Replace("(http", Environment.NewLine + "http");
//.Replace("http", Environment.NewLine + "http");
if (fullEula.EndsWith("(y/n)", true, CultureInfo.InvariantCulture))
if (fullEula.EndsWith("[y/N]", true, CultureInfo.InvariantCulture))
{
//remove
fullEula = fullEula[..^5].Trim();
Expand Down

0 comments on commit cbc963d

Please sign in to comment.