From 6cc60e426cb3963b48dec3959806019a4b160dac Mon Sep 17 00:00:00 2001 From: alexcoder04 Date: Fri, 15 Jul 2022 13:02:05 +0200 Subject: [PATCH] using LeoConsole's built-in log capabilities --- Apkg/integrity.cs | 3 +- Apkg/repository.cs | 42 +++++++++++++-------------- Utils/fileutils.cs | 3 +- Utils/output.cs | 30 ------------------- Utils/utils.cs | 9 +++--- command.cs | 72 +++++++++++++++++++++++----------------------- manifest.json | 2 +- plugin.cs | 20 ++++++------- 8 files changed, 77 insertions(+), 104 deletions(-) delete mode 100644 Utils/output.cs diff --git a/Apkg/integrity.cs b/Apkg/integrity.cs index 1cf6105..8e57bc5 100644 --- a/Apkg/integrity.cs +++ b/Apkg/integrity.cs @@ -1,3 +1,4 @@ +using ILeoConsole.Core; using System.Linq; namespace LeoConsole_apkg { @@ -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); diff --git a/Apkg/repository.cs b/Apkg/repository.cs index e080205..8e5b666 100644 --- a/Apkg/repository.cs +++ b/Apkg/repository.cs @@ -21,8 +21,8 @@ public ApkgRepository(string sp, string v) { Index = Enumerable.Empty().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; } @@ -78,7 +78,7 @@ public IList AvailablePlugins() { public void Reload(ConfigRepo[] repos) { IList newIndex = Enumerable.Empty().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); @@ -106,7 +106,7 @@ 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; } // }}} @@ -114,7 +114,7 @@ private string ExtractLcp(string path) { // 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); @@ -122,9 +122,9 @@ private void InstallFiles(string[] files, string packageDir) { (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"); } } } @@ -132,22 +132,22 @@ private void InstallFiles(string[] files, string packageDir) { // 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; } @@ -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"); @@ -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); diff --git a/Utils/fileutils.cs b/Utils/fileutils.cs index 5306ab3..e668909 100644 --- a/Utils/fileutils.cs +++ b/Utils/fileutils.cs @@ -1,3 +1,4 @@ +using ILeoConsole.Core; using System.Text.Json; namespace LeoConsole_apkg { @@ -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); } diff --git a/Utils/output.cs b/Utils/output.cs deleted file mode 100644 index 0731747..0000000 --- a/Utils/output.cs +++ /dev/null @@ -1,30 +0,0 @@ -using ILeoConsole.Core; - -// TODO: this functionality will be integrated into ILeoConsole soon -namespace LeoConsole_apkg { - public class ApkgOutput { - // PRINTING NICE MESSAGES - public static void MessageSuc0(string msg) { - LConsole.WriteLine("§a==>§r " + msg); - } - public static void MessageSuc1(string msg) { - LConsole.WriteLine(" §a->§r " + msg); - } - - public static void MessageErr0(string msg) { - LConsole.WriteLine("§c==>§r error: " + msg); - } - public static void MessageErr1(string msg) { - LConsole.WriteLine(" §c->§r error: " + msg); - } - - public static void MessageWarn0(string msg) { - LConsole.WriteLine("§e==>§r warning: " + msg); - } - public static void MessageWarn1(string msg) { - LConsole.WriteLine(" §e->§r warning: " + msg); - } - } -} - -// vim: tabstop=2 softtabstop=2 shiftwidth=2 expandtab diff --git a/Utils/utils.cs b/Utils/utils.cs index 724b0a1..7c0de86 100644 --- a/Utils/utils.cs +++ b/Utils/utils.cs @@ -1,3 +1,4 @@ +using ILeoConsole.Core; using System.Diagnostics; using System.IO; using System.Net; @@ -48,11 +49,11 @@ 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; @@ -60,12 +61,12 @@ public static bool RunProcess(string name, string args, string pwd) { // 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; diff --git a/command.cs b/command.cs index b2208c5..e30d71c 100644 --- a/command.cs +++ b/command.cs @@ -32,7 +32,7 @@ public LeoConsoleApkgCommand(string savePath, string lcVersion) { // Command() {{{ public void Command() { if (_Arguments.Length < 2) { - ApkgOutput.MessageErr0("you need to provide a subcommand\n"); + LConsole.MessageErr0("you need to provide a subcommand\n"); apkg_do_help(); return; } @@ -51,7 +51,7 @@ public void Command() { case "build": apkg_do_build(); break; case "get-local": apkg_do_get_local(); break; default: - ApkgOutput.MessageErr0("apkg: unknown subcommand '" + _Arguments[1] + "'"); + LConsole.MessageErr0("apkg: unknown subcommand '" + _Arguments[1] + "'"); break; } } @@ -61,20 +61,20 @@ public void Command() { // get() {{{ private void apkg_do_get() { if (_Arguments.Length < 3){ - ApkgOutput.MessageErr0("you need to provide a package name"); + LConsole.MessageErr0("you need to provide a package name"); return; } try { Repository.Reload(Config.Repositories); } catch (Exception e) { - ApkgOutput.MessageErr0("error reloading package database"); + LConsole.MessageErr0("error reloading package database"); return; } string url; try { url = Repository.GetUrlFor(_Arguments[2]); } catch (Exception e) { - ApkgOutput.MessageErr1("cannot find your package"); + LConsole.MessageErr1("cannot find your package"); return; } string dlPath = Path.Join(data.DownloadPath, "apkg", $"{_Arguments[2]}.lcp"); @@ -87,10 +87,10 @@ private void apkg_do_get() { // get_local() {{{ private void apkg_do_get_local() { if (!Config.DebugMode) { - ApkgOutput.MessageErr0("this command is only available in debug mode"); return; + LConsole.MessageErr0("this command is only available in debug mode"); return; } if (_Arguments.Length < 3) { - ApkgOutput.MessageErr0("you need to pass a file or folder to install"); + LConsole.MessageErr0("you need to pass a file or folder to install"); return; } if (Directory.Exists(_Arguments[2])) { @@ -99,12 +99,12 @@ private void apkg_do_get_local() { _Arguments[2], data.CurrentWorkingPath )) { - ApkgOutput.MessageErr0("error building plugin"); + LConsole.MessageErr0("error building plugin"); return; } foreach (string f in Directory.GetFiles(_Arguments[2])) { if (f.EndsWith(".lcp")) { - ApkgOutput.MessageSuc0("installing built archive: " + f); + LConsole.MessageSuc0("installing built archive: " + f); Repository.InstallLcpkg(f); return; } @@ -112,7 +112,7 @@ private void apkg_do_get_local() { return; } if (!_Arguments[2].EndsWith(".lcp")) { - ApkgOutput.MessageErr0("looks not like an lcp file"); + LConsole.MessageErr0("looks not like an lcp file"); return; } Repository.InstallLcpkg(_Arguments[2]); @@ -121,15 +121,15 @@ private void apkg_do_get_local() { // build() {{{ private void apkg_do_build() { if (!Config.DebugMode) { - ApkgOutput.MessageErr0("this command is only available in debug mode"); + LConsole.MessageErr0("this command is only available in debug mode"); return; } if (_Arguments.Length < 3) { - ApkgOutput.MessageErr0("you need to pass a folder to build"); + LConsole.MessageErr0("you need to pass a folder to build"); return; } if (!Directory.Exists(_Arguments[2])) { - ApkgOutput.MessageErr0("the folder you passed doesn't exists"); + LConsole.MessageErr0("the folder you passed doesn't exists"); return; } ApkgUtils.RunProcess( @@ -142,7 +142,7 @@ private void apkg_do_build() { // update() {{{ private void apkg_do_update() { IList allPlugins = Repository.AvailablePlugins(); - ApkgOutput.MessageSuc0("updating plugins:"); + LConsole.MessageSuc0("updating plugins:"); foreach (string p in allPlugins) { if (!Directory.Exists(Path.Join(data.SavePath, "var", "apkg", "installed", p))) { continue; @@ -150,7 +150,7 @@ private void apkg_do_update() { string installedVersion = File.ReadAllText(Path.Join(data.SavePath, "var", "apkg", "installed", p, "version")).Trim(); string availableVersion = Repository.GetInfoFor(p).version; if (ApkgUtils.VersionGreater(availableVersion, installedVersion)) { - ApkgOutput.MessageSuc1($"update available for {p} (v{installedVersion} -> v{availableVersion})"); + LConsole.MessageSuc1($"update available for {p} (v{installedVersion} -> v{availableVersion})"); string url = Repository.GetUrlFor(_Arguments[2]); string dlPath = Path.Join(data.DownloadPath, "apkg", $"{_Arguments[2]}.lcp"); if (!ApkgUtils.DownloadFile(url, dlPath)) { @@ -160,25 +160,25 @@ private void apkg_do_update() { continue; } if (ApkgUtils.VersionGreater(installedVersion, availableVersion)) { - ApkgOutput.MessageWarn1($"{p} is newer than repository (installed {installedVersion}, repo {availableVersion})"); + LConsole.MessageWarn1($"{p} is newer than repository (installed {installedVersion}, repo {availableVersion})"); continue; } - ApkgOutput.MessageSuc1($"{p} is up-to-date"); + LConsole.MessageSuc1($"{p} is up-to-date"); } } // }}} // list_installed() {{{ private void apkg_do_list_installed() { - ApkgOutput.MessageSuc0("your installed packages:"); + LConsole.MessageSuc0("your installed packages:"); foreach (string filename in Directory.GetDirectories( Path.Join(ConfigFolder, "installed") )){ string p = Path.GetFileName(filename); try { RepoPackage info = Repository.GetInfoFor(p); - ApkgOutput.MessageSuc1($"{p} - {info.description}"); + LConsole.MessageSuc1($"{p} - {info.description}"); } catch (Exception e) { - ApkgOutput.MessageErr1($"{p} not found: {e.Message}"); + LConsole.MessageErr1($"{p} not found: {e.Message}"); } } } // }}} @@ -186,7 +186,7 @@ private void apkg_do_list_installed() { // remove() {{{ private void apkg_do_remove() { if (_Arguments.Length < 3){ - ApkgOutput.MessageErr0("you need to provide a package name to remove"); + LConsole.MessageErr0("you need to provide a package name to remove"); return; } Repository.RemovePackage(_Arguments[2]); @@ -195,10 +195,10 @@ private void apkg_do_remove() { // list_avaiable() {{{ private void apkg_do_list_available() { IList list = Repository.AvailablePlugins(); - ApkgOutput.MessageSuc0("available packages:"); + LConsole.MessageSuc0("available packages:"); foreach (string p in list) { RepoPackage info = Repository.GetInfoFor(p); - ApkgOutput.MessageSuc1($"{p} - {info.description}"); + LConsole.MessageSuc1($"{p} - {info.description}"); } } // }}} @@ -206,7 +206,7 @@ private void apkg_do_list_available() { private void apkg_do_search() { string keyword = _Arguments[2]; IList list = Repository.AvailablePlugins(); - ApkgOutput.MessageSuc0("results:"); + LConsole.MessageSuc0("results:"); IList matches = Enumerable.Empty().ToList(); foreach (string p in list) { if (p.ToLower().Contains(keyword.ToLower())) { @@ -214,32 +214,32 @@ private void apkg_do_search() { } } if (matches.Count < 1) { - ApkgOutput.MessageErr1("none"); + LConsole.MessageErr1("none"); return; } foreach (string p in matches) { - ApkgOutput.MessageSuc1(p); + LConsole.MessageSuc1(p); } } // }}} // info() {{{ private void apkg_do_info() { - ApkgOutput.MessageSuc0("apkg information"); - ApkgOutput.MessageSuc1( + LConsole.MessageSuc0("apkg information"); + LConsole.MessageSuc1( $"cache/download directory: {Path.Join(data.DownloadPath, "apkg")}"); - ApkgOutput.MessageSuc1( + LConsole.MessageSuc1( $"plugin installation directory: {Path.Join(data.SavePath, "plugins")}"); - ApkgOutput.MessageSuc1( + LConsole.MessageSuc1( $"share installation directory: {Path.Join(data.SavePath, "share")}"); - ApkgOutput.MessageSuc1( + LConsole.MessageSuc1( $"config/database directory: {ConfigFolder}"); - ApkgOutput.MessageSuc1( + LConsole.MessageSuc1( $"docs directory: {Path.Join(data.SavePath, "share", "docs", "apkg")}"); - ApkgOutput.MessageSuc1($"apkg version: {apkgVersion}"); + LConsole.MessageSuc1($"apkg version: {apkgVersion}"); if (Config.DebugMode) { - ApkgOutput.MessageWarn1("debug mode: ON"); + LConsole.MessageWarn1("debug mode: ON"); } else { - ApkgOutput.MessageSuc1("debug mode: off"); + LConsole.MessageSuc1("debug mode: off"); } } // }}} @@ -248,7 +248,7 @@ private void apkg_do_reload() { try { Repository.Reload(Config.Repositories); } catch (Exception e) { - ApkgOutput.MessageErr0("error reloading package database"); + LConsole.MessageErr0("error reloading package database"); } } // }}} diff --git a/manifest.json b/manifest.json index 4635a63..262637a 100644 --- a/manifest.json +++ b/manifest.json @@ -3,7 +3,7 @@ "packageName": "apkg", "packageVersion": "1.3.0", "depends": [], - "compatibleVersions": ["2.0.0"], + "compatibleVersions": ["2.1.0"], "build": { "command": "/bin/sh", "args": ["./build.sh"], diff --git a/plugin.cs b/plugin.cs index 19b76e2..f9305f6 100644 --- a/plugin.cs +++ b/plugin.cs @@ -42,7 +42,7 @@ public void RegisterCommands(){ } public void PluginMain() { - ApkgOutput.MessageSuc0("performing apkg self-check"); + LConsole.MessageSuc0("performing apkg self-check"); // check if folders exist {{{ string[] folders = { Path.Join(data.SavePath, "var"), @@ -56,8 +56,8 @@ public void PluginMain() { try { Directory.CreateDirectory(folder); } catch (Exception e) { - ApkgOutput.MessageErr1("cannot create apkg dir: " + e.Message); - ApkgOutput.MessageErr0("something seems to be broken, you can not use apkg"); + LConsole.MessageErr1("cannot create apkg dir: " + e.Message); + LConsole.MessageErr0("something seems to be broken, you can not use apkg"); return; } } @@ -72,8 +72,8 @@ public void PluginMain() { try { repository.Reload(config.Repositories); } catch (Exception e) { - ApkgOutput.MessageErr0("error reloading package database"); - ApkgOutput.MessageErr0("something seems to be broken, you can not use apkg"); + LConsole.MessageErr0("error reloading package database"); + LConsole.MessageErr0("something seems to be broken, you can not use apkg"); return; } // re-install itself @@ -81,19 +81,19 @@ public void PluginMain() { try { url = repository.GetUrlFor("apkg"); } catch (Exception e) { - ApkgOutput.MessageErr1("apkg not found in repository"); - ApkgOutput.MessageErr0("something seems to be broken, you can not use apkg"); + LConsole.MessageErr1("apkg not found in repository"); + LConsole.MessageErr0("something seems to be broken, you can not use apkg"); return; } string dlPath = Path.Join(data.SavePath, "tmp", "apkg.lcp"); if (!ApkgUtils.DownloadFile(url, dlPath)) { - ApkgOutput.MessageErr1("could not download apkg"); - ApkgOutput.MessageErr0("something seems to be broken, you can not use apkg"); + LConsole.MessageErr1("could not download apkg"); + LConsole.MessageErr0("something seems to be broken, you can not use apkg"); return; } repository.InstallLcpkg(dlPath); } - ApkgOutput.MessageSuc1("self-check successfull"); + LConsole.MessageSuc1("self-check successfull"); } // }}}