mirror of
https://onedev.site.tesses.net/crosslang/crosslangdevstudio
synced 2026-02-08 09:15:45 +00:00
New dev build
This commit is contained in:
42
Models/IPackageManager.cs
Normal file
42
Models/IPackageManager.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
|
||||
namespace CrossLangDevStudio.Models;
|
||||
|
||||
public interface IPackageManager
|
||||
{
|
||||
Task InstallPackageAsync(string name, CrossLangVersion version);
|
||||
string Filter { get; }
|
||||
void UninstallPackage(string name);
|
||||
|
||||
IEnumerable<CrossLangDependency> GetInstalledPackages();
|
||||
}
|
||||
|
||||
public static class PackageManagerExtensions
|
||||
{
|
||||
public static string Summarize(this string txt)
|
||||
{
|
||||
var lines = txt.Replace("\r", "").Split('\n');
|
||||
bool needsthreedots = false;
|
||||
if (lines.Length > 1) needsthreedots = true;
|
||||
if (lines.Length == 0) return "";
|
||||
if (lines[0].Length > 120) { needsthreedots = true; lines[0] = lines[0].Substring(0, 120); }
|
||||
if (needsthreedots) return $"{lines[0]}...";
|
||||
return lines[0];
|
||||
}
|
||||
public static bool IsPackageInstalled(this IPackageManager pm, string name)
|
||||
{
|
||||
foreach (var item in pm.GetInstalledPackages())
|
||||
{
|
||||
if (item.Name == name) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static async Task InstallPackageAsync(this IPackageManager pm, CrossLangDependency dep)
|
||||
{
|
||||
await pm.InstallPackageAsync(dep.Name, dep.Version);
|
||||
}
|
||||
}
|
||||
70
Models/ProjectPackageManager.cs
Normal file
70
Models/ProjectPackageManager.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CrossLangDevStudio.Models;
|
||||
|
||||
public class ProjectPackageManager : IPackageManager
|
||||
{
|
||||
public ProjectPackageManager(string path)
|
||||
{
|
||||
Path = path;
|
||||
}
|
||||
public string Path { get; init; }
|
||||
public string Filter => "lib,compile_tool";
|
||||
|
||||
public IEnumerable<CrossLangDependency> GetInstalledPackages()
|
||||
{
|
||||
var cfg = JsonConvert.DeserializeObject<CrossLangConfig>(File.ReadAllText(Path));
|
||||
if (cfg is not null)
|
||||
{
|
||||
foreach (var item in cfg.Dependencies)
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task InstallPackageAsync(string name, CrossLangVersion version)
|
||||
{
|
||||
var cfg = JsonConvert.DeserializeObject<CrossLangConfig>(await File.ReadAllTextAsync(Path)) ?? new CrossLangConfig();
|
||||
cfg.Dependencies ??= new List<CrossLangDependency>();
|
||||
bool has = false;
|
||||
foreach (var item in cfg.Dependencies)
|
||||
{
|
||||
if (item.Name == name)
|
||||
{
|
||||
has = true;
|
||||
if(version.CompareTo(item.Version) > 0)
|
||||
item.Version = version;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!has) cfg.Dependencies.Add(new CrossLangDependency { Name = name, Version = version });
|
||||
await File.WriteAllTextAsync(Path, JsonConvert.SerializeObject(cfg, Formatting.Indented, new JsonSerializerSettings(){NullValueHandling= NullValueHandling.Ignore}));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void UninstallPackage(string name)
|
||||
{
|
||||
var cfg = JsonConvert.DeserializeObject<CrossLangConfig>(File.ReadAllText(Path)) ?? new CrossLangConfig();
|
||||
cfg.Dependencies ??= new List<CrossLangDependency>();
|
||||
|
||||
foreach (var item in cfg.Dependencies)
|
||||
{
|
||||
if (item.Name == name)
|
||||
{
|
||||
cfg.Dependencies.Remove(item);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
File.WriteAllText(Path, JsonConvert.SerializeObject(cfg, Formatting.Indented,new JsonSerializerSettings(){NullValueHandling= NullValueHandling.Ignore}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user