Skip to content

Commit

Permalink
using LeoConsole's built-in log capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcoder04 committed Jul 15, 2022
1 parent 5a8b8ba commit 6cc60e4
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 104 deletions.
3 changes: 2 additions & 1 deletion Apkg/integrity.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ILeoConsole.Core;
using System.Linq;

namespace LeoConsole_apkg {
Expand All @@ -17,7 +18,7 @@ public static string CheckPkgConflicts(string[] files, string savePath) {

// Register() {{{
public static void Register(string p, string pVersion, string[] f, string savePath) {
ApkgOutput.MessageSuc0($"registering package {p} v{pVersion}");
LConsole.MessageSuc0($"registering package {p} v{pVersion}");
string baseDir = Path.Join(savePath, "var", "apkg", "installed", p);
Directory.CreateDirectory(baseDir);
File.WriteAllLines(Path.Join(baseDir, "files"), f);
Expand Down
42 changes: 21 additions & 21 deletions Apkg/repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public ApkgRepository(string sp, string v) {
Index = Enumerable.Empty<RepoPackage>().ToList();

if (!Directory.Exists(ReposFolder)) {
ApkgOutput.MessageWarn1("could not find repo cache, package list cannot be loaded");
ApkgOutput.MessageWarn1("please run 'apkg reload'");
LConsole.MessageWarn1("could not find repo cache, package list cannot be loaded");
LConsole.MessageWarn1("please run 'apkg reload'");
return;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public IList<string> AvailablePlugins() {
public void Reload(ConfigRepo[] repos) {
IList<RepoPackage> newIndex = Enumerable.Empty<RepoPackage>().ToList();
foreach (ConfigRepo repo in repos) {
ApkgOutput.MessageSuc0("loading " + repo.name);
LConsole.MessageSuc0("loading " + repo.name);
string tempFile = Path.Join(DownloadPath, repo.name + ".json");
if (!ApkgUtils.DownloadFile(repo.url, tempFile)) {
throw new Exception("error downloading " + repo.name);
Expand Down Expand Up @@ -106,48 +106,48 @@ private string ExtractLcp(string path) {
}
Directory.CreateDirectory(extractPath);
// extract package archive
ApkgOutput.MessageSuc0("extracting package");
LConsole.MessageSuc0("extracting package");
ZipFile.ExtractToDirectory(path, extractPath);
return extractPath;
} // }}}

// InstallFiles() {{{
private void InstallFiles(string[] files, string packageDir) {
foreach (string file in files) {
ApkgOutput.MessageSuc1("copying " + file);
LConsole.MessageSuc1("copying " + file);
Directory.CreateDirectory(Directory.GetParent(Path.Join(SavePath, file)).FullName);
// copy file
File.Copy(Path.Join(packageDir, file), Path.Join(SavePath, file), true);
if (
(file.StartsWith("share/scripts") || file.StartsWith("share/go-plugin"))
&& ApkgUtils.GetRunningOS() == "lnx64"
) {
ApkgOutput.MessageSuc1($"marking {file} as executable");
LConsole.MessageSuc1($"marking {file} as executable");
if (!ApkgUtils.RunProcess("chmod", "+x " + Path.Join(SavePath, file), SavePath)) {
ApkgOutput.MessageWarn1($"cannot mark {file} as executable");
LConsole.MessageWarn1($"cannot mark {file} as executable");
}
}
}
} // }}}

// InstallLcpkg() {{{
public void InstallLcpkg(string archiveFile) {
ApkgOutput.MessageSuc0("installing package");
ApkgOutput.MessageSuc1("preparing to extract package");
LConsole.MessageSuc0("installing package");
LConsole.MessageSuc1("preparing to extract package");

string tempFolder = "";
try {
tempFolder = ExtractLcp(archiveFile);
} catch (Exception e) {
ApkgOutput.MessageErr0($"cannot extract lcp archive: {e.Message}");
LConsole.MessageErr0($"cannot extract lcp archive: {e.Message}");
return;
}

ApkgOutput.MessageSuc0("checking package compatibility");
LConsole.MessageSuc0("checking package compatibility");
PkgArchiveManifest manifest = FileUtils.ReadManifest(tempFolder);

if (!Array.Exists(manifest.compatibleVersions, e => e == LeoConsoleVersion)) {
ApkgOutput.MessageErr1("your LeoConsole version is incompatible with this plugin");
LConsole.MessageErr1("your LeoConsole version is incompatible with this plugin");
return;
}

Expand All @@ -157,7 +157,7 @@ public void InstallLcpkg(string archiveFile) {
try {
url = GetUrlFor(pack);
} catch (Exception e) {
ApkgOutput.MessageErr1("cannot find a dependency");
LConsole.MessageErr1("cannot find a dependency");
return;
}
string dlPath = Path.Join(DownloadPath, $"{pack}.lcp");
Expand All @@ -171,46 +171,46 @@ public void InstallLcpkg(string archiveFile) {
string conflictsWith = ApkgIntegrity.CheckPkgConflicts(manifest.files, SavePath);
if (conflictsWith != "") {
if (conflictsWith != manifest.packageName) {
ApkgOutput.MessageErr0($"{manifest.packageName} conflicts with {conflictsWith}, aborting install");
LConsole.MessageErr0($"{manifest.packageName} conflicts with {conflictsWith}, aborting install");
return;
}
// conflicting with itself ask about reinstalling {{{
string installedVersion = File.ReadAllText(Path.Join(ConfigDir, "installed", manifest.packageName, "version")).Trim();
if (installedVersion == manifest.packageVersion) {
if (!LConsole.YesNoDialog("reinstall same package version?", true)) {
ApkgOutput.MessageErr1("installation aborted");
LConsole.MessageErr1("installation aborted");
return;
}
}
if (ApkgUtils.VersionGreater(installedVersion, manifest.packageVersion)) {
if (!LConsole.YesNoDialog($"downgrade package ({installedVersion}->{manifest.packageVersion})?", false)) {
ApkgOutput.MessageErr1("installation aborted");
LConsole.MessageErr1("installation aborted");
return;
}
} // }}}
RemovePackage(manifest.packageName);
} // }}}

ApkgOutput.MessageSuc0($"installing files for {manifest.project.maintainer}/{manifest.packageName}");
LConsole.MessageSuc0($"installing files for {manifest.project.maintainer}/{manifest.packageName}");
InstallFiles(manifest.files, tempFolder);

ApkgIntegrity.Register(
manifest.packageName, manifest.packageVersion, manifest.files, SavePath
);
ApkgOutput.MessageSuc0("successfully installed " + manifest.packageName);
LConsole.MessageSuc0("successfully installed " + manifest.packageName);
} // }}}

// RemovePackage() {{{
public void RemovePackage(string p) {
ApkgOutput.MessageSuc0("removing " + p);
LConsole.MessageSuc0("removing " + p);
if (!Directory.Exists(Path.Join(ConfigDir, "installed", p))) {
ApkgOutput.MessageErr0($"{p} is not installed");
LConsole.MessageErr0($"{p} is not installed");
return;
}
try {
FileUtils.DeleteFiles(File.ReadLines(Path.Join(ConfigDir, "installed", p, "files")).ToArray(), SavePath);
} catch (Exception e) {
ApkgOutput.MessageErr0("removing package failed: " + e.Message);
LConsole.MessageErr0("removing package failed: " + e.Message);
return;
}
ApkgIntegrity.Unregister(p, SavePath);
Expand Down
3 changes: 2 additions & 1 deletion Utils/fileutils.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ILeoConsole.Core;
using System.Text.Json;

namespace LeoConsole_apkg {
Expand All @@ -18,7 +19,7 @@ public static void DeleteParentDirs(string file, string savePath) {
public static void DeleteFiles(string[] files, string savePath) {
foreach (string f in files) {
string path = Path.Join(savePath, f);
ApkgOutput.MessageSuc1("deleting " + path);
LConsole.MessageSuc1("deleting " + path);
File.Delete(path);
FileUtils.DeleteParentDirs(path, savePath);
}
Expand Down
30 changes: 0 additions & 30 deletions Utils/output.cs

This file was deleted.

9 changes: 5 additions & 4 deletions Utils/utils.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ILeoConsole.Core;
using System.Diagnostics;
using System.IO;
using System.Net;
Expand Down Expand Up @@ -48,24 +49,24 @@ public static bool RunProcess(string name, string args, string pwd) {
p.Start();
p.WaitForExit();
if (p.ExitCode != 0) {
ApkgOutput.MessageErr1($"{name} returned an error");
LConsole.MessageErr1($"{name} returned an error");
return false;
}
} catch (Exception e) {
ApkgOutput.MessageErr1($"cannot run {name}: {e.Message}");
LConsole.MessageErr1($"cannot run {name}: {e.Message}");
return false;
}
return true;
} // }}}

// DownloadFile() {{{
public static bool DownloadFile(string url, string location) {
ApkgOutput.MessageSuc1($"downloading {url} to {location}...");
LConsole.MessageSuc1($"downloading {url} to {location}...");
try {
WebClient webClient = new WebClient();
webClient.DownloadFile(url, location);
} catch (Exception e) {
ApkgOutput.MessageErr1("cannot download: " + e.Message);
LConsole.MessageErr1("cannot download: " + e.Message);
return false;
}
return true;
Expand Down
Loading

0 comments on commit 6cc60e4

Please sign in to comment.