From 513b98a19770ac0c0c46c14cbce91a1efeb17be5 Mon Sep 17 00:00:00 2001 From: ericoporto Date: Sun, 26 Jan 2020 00:05:27 +0000 Subject: [PATCH] a bit convoluted but got a core and a console interface. --- .../agsget/CommandLine/Apply/ApplyCommand.cs | 2 +- agsget/agsget/CommandLine/Get/GetCommand.cs | 2 +- agsget/agsget/CommandLine/Pack/PackCommand.cs | 2 +- .../CommandLine/Search/SearchCommand.cs | 2 +- .../CommandLine/Update/UpdateCommand.cs | 2 +- agsget/agsget/Core/Actions/ApplyDo.cs | 7 ++-- agsget/agsget/Core/Actions/GetDo.cs | 5 +-- agsget/agsget/Core/Actions/PackDo.cs | 2 +- agsget/agsget/Core/Actions/SearchDo.cs | 2 +- agsget/agsget/Core/Actions/UpdateDo.cs | 4 +-- agsget/agsget/Core/AgsGetCore.cs | 36 +++++++++++++++++++ agsget/agsget/Core/BaseFiles.cs | 4 +-- agsget/agsget/Core/Configuration.cs | 4 +-- agsget/agsget/Core/GameAgfIO.cs | 4 +-- agsget/agsget/Core/PackageCacheIO.cs | 14 ++++---- .../agsget/{ => Core}/_Utils/ConsoleExtra.cs | 4 +-- .../{ => Core}/_Utils/DownloadPretty.cs | 10 +++--- .../{ => Core}/_Utils/WebClientExtensios.cs | 4 +-- 18 files changed, 75 insertions(+), 35 deletions(-) create mode 100644 agsget/agsget/Core/AgsGetCore.cs rename agsget/agsget/{ => Core}/_Utils/ConsoleExtra.cs (94%) rename agsget/agsget/{ => Core}/_Utils/DownloadPretty.cs (65%) rename agsget/agsget/{ => Core}/_Utils/WebClientExtensios.cs (98%) diff --git a/agsget/agsget/CommandLine/Apply/ApplyCommand.cs b/agsget/agsget/CommandLine/Apply/ApplyCommand.cs index ae8f3bd..5e48720 100644 --- a/agsget/agsget/CommandLine/Apply/ApplyCommand.cs +++ b/agsget/agsget/CommandLine/Apply/ApplyCommand.cs @@ -15,7 +15,7 @@ public static int Run(ApplyOptions ApplyOptions) throw new ArgumentNullException(nameof(ApplyOptions)); } - return ApplyDo.Do(Console.WriteLine, ApplyOptions.changeRunDir, ApplyOptions.PackageName); + return AgsGetCore.AgsGetCore.Apply(Console.WriteLine, ApplyOptions.changeRunDir, ApplyOptions.PackageName); } } } diff --git a/agsget/agsget/CommandLine/Get/GetCommand.cs b/agsget/agsget/CommandLine/Get/GetCommand.cs index b9000ad..4263d17 100644 --- a/agsget/agsget/CommandLine/Get/GetCommand.cs +++ b/agsget/agsget/CommandLine/Get/GetCommand.cs @@ -14,7 +14,7 @@ public static int Run(GetOptions GetOptions) throw new ArgumentNullException(nameof(GetOptions)); } - return GetDo.Do(Console.WriteLine, GetOptions.changeRunDir, GetOptions.PackageName); + return AgsGetCore.AgsGetCore.Get(Console.WriteLine, GetOptions.changeRunDir, GetOptions.PackageName); } } } diff --git a/agsget/agsget/CommandLine/Pack/PackCommand.cs b/agsget/agsget/CommandLine/Pack/PackCommand.cs index c095861..54f5539 100644 --- a/agsget/agsget/CommandLine/Pack/PackCommand.cs +++ b/agsget/agsget/CommandLine/Pack/PackCommand.cs @@ -8,7 +8,7 @@ class PackCommand : Command { public static int RunPackCommand(PackOptions PackOptions) { - return PackDo.Do(Console.WriteLine, PackOptions.changeRunDir, PackOptions.PairName); + return AgsGetCore.AgsGetCore.Pack(Console.WriteLine, PackOptions.changeRunDir, PackOptions.PairName); } } } diff --git a/agsget/agsget/CommandLine/Search/SearchCommand.cs b/agsget/agsget/CommandLine/Search/SearchCommand.cs index c8f2ad7..2dfe6e1 100644 --- a/agsget/agsget/CommandLine/Search/SearchCommand.cs +++ b/agsget/agsget/CommandLine/Search/SearchCommand.cs @@ -9,7 +9,7 @@ class SearchCommand : Command { public static int Run(SearchOptions SearchOptions) { - return SearchDo.Do(Console.WriteLine, SearchOptions.changeRunDir, SearchOptions.SearchQuery); + return AgsGetCore.AgsGetCore.Search(Console.WriteLine, SearchOptions.changeRunDir, SearchOptions.SearchQuery); } } } diff --git a/agsget/agsget/CommandLine/Update/UpdateCommand.cs b/agsget/agsget/CommandLine/Update/UpdateCommand.cs index a85c3e6..6f99e32 100644 --- a/agsget/agsget/CommandLine/Update/UpdateCommand.cs +++ b/agsget/agsget/CommandLine/Update/UpdateCommand.cs @@ -12,7 +12,7 @@ class UpdateCommand : Command //to a general option available for all commands, but not sure yet public static int Run(UpdateOptions UpdateOptions) { - return UpdateDo.Do(Console.WriteLine, UpdateOptions.changeRunDir, UpdateOptions.PackageIndexURL); + return AgsGetCore.AgsGetCore.Update(Console.WriteLine, UpdateOptions.changeRunDir, UpdateOptions.PackageIndexURL); } } } diff --git a/agsget/agsget/Core/Actions/ApplyDo.cs b/agsget/agsget/Core/Actions/ApplyDo.cs index 1554069..58fb08e 100644 --- a/agsget/agsget/Core/Actions/ApplyDo.cs +++ b/agsget/agsget/Core/Actions/ApplyDo.cs @@ -1,6 +1,6 @@ using System; -namespace agsget +namespace AgsGetCore.Actions { public class ApplyDo { @@ -41,7 +41,7 @@ public static int Do(Action writerMethod, string changeRunDir, string pa //Update.3.Downloads the index of packages to `./ ags_packages_cache / package_index`. //If it already exists, overwrites it. - PackageCacheIO.GetPackageIndex(null); + PackageCacheIO.GetPackageIndex(writerMethod, null); } //5. Check if PACKAGE_NAME exists on `./ ags_packages_cache / PACKAGE_NAME`, @@ -59,6 +59,7 @@ public static int Do(Action writerMethod, string changeRunDir, string pa //Get.5. Download PACKAGE_NAME to `./ags_packages_cache/PACKAGE_NAME`. if (!PackageCacheIO.GetPackage( + writerMethod, Configuration.PackageIndexURL, packageName)) { @@ -73,6 +74,8 @@ public static int Do(Action writerMethod, string changeRunDir, string pa if (GameAgfIO.IsScriptPairInserted(packageName)) { Console.WriteLine("Script already found on Game.agf."); + + //TODO: This question must be console on console or a callback to an yes/no GUI, need to figure out how if (!ConsoleExtra.ConfirmYN("Are you sure you want to replace?")) { writerMethod("Package already inserted and will not be replaced."); diff --git a/agsget/agsget/Core/Actions/GetDo.cs b/agsget/agsget/Core/Actions/GetDo.cs index c95e918..422ec32 100644 --- a/agsget/agsget/Core/Actions/GetDo.cs +++ b/agsget/agsget/Core/Actions/GetDo.cs @@ -1,6 +1,6 @@ using System; -namespace agsget +namespace AgsGetCore.Actions { public class GetDo { @@ -32,7 +32,7 @@ public static int Do(Action writerMethod, string changeRunDir, string pa //Update.3.Downloads the index of packages to `./ ags_packages_cache / package_index`. //If it already exists, overwrites it. - PackageCacheIO.GetPackageIndex(null); + PackageCacheIO.GetPackageIndex(writerMethod, null); } //4.Check if PACKAGE_NAME exists on `./ ags_packages_cache / package_index`, if not, exit with error. @@ -45,6 +45,7 @@ public static int Do(Action writerMethod, string changeRunDir, string pa //5. Download PACKAGE_NAME to `./ags_packages_cache/PACKAGE_NAME`. if(!PackageCacheIO.GetPackage( + writerMethod, Configuration.PackageIndexURL, packageName)) { diff --git a/agsget/agsget/Core/Actions/PackDo.cs b/agsget/agsget/Core/Actions/PackDo.cs index 72e5651..be5a40b 100644 --- a/agsget/agsget/Core/Actions/PackDo.cs +++ b/agsget/agsget/Core/Actions/PackDo.cs @@ -1,6 +1,6 @@ using System; -namespace agsget +namespace AgsGetCore.Actions { public class PackDo { diff --git a/agsget/agsget/Core/Actions/SearchDo.cs b/agsget/agsget/Core/Actions/SearchDo.cs index bddb6df..ca73986 100644 --- a/agsget/agsget/Core/Actions/SearchDo.cs +++ b/agsget/agsget/Core/Actions/SearchDo.cs @@ -1,6 +1,6 @@ using System; -namespace agsget +namespace AgsGetCore.Actions { public class SearchDo { diff --git a/agsget/agsget/Core/Actions/UpdateDo.cs b/agsget/agsget/Core/Actions/UpdateDo.cs index 3829f92..cd2e83c 100644 --- a/agsget/agsget/Core/Actions/UpdateDo.cs +++ b/agsget/agsget/Core/Actions/UpdateDo.cs @@ -1,6 +1,6 @@ using System; -namespace agsget +namespace AgsGetCore.Actions { public class UpdateDo { @@ -24,7 +24,7 @@ public static int Do(Action writerMethod, string changeRunDir, string pa //3.Downloads the index of packages to `./ ags_packages_cache / package_index`. //If it already exists, overwrites it. - PackageCacheIO.GetPackageIndex(packageIndexURL); + PackageCacheIO.GetPackageIndex(writerMethod, packageIndexURL); writerMethod("Success."); return 0; diff --git a/agsget/agsget/Core/AgsGetCore.cs b/agsget/agsget/Core/AgsGetCore.cs new file mode 100644 index 0000000..f8d4cf7 --- /dev/null +++ b/agsget/agsget/Core/AgsGetCore.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; + +using AgsGetCore.Actions; +namespace AgsGetCore +{ + class AgsGetCore + { + public static int Apply(Action writerMethod, string changeRunDir, string packageName) + { + + return ApplyDo.Do(writerMethod, changeRunDir, packageName); + } + + public static int Get(Action writerMethod, string changeRunDir, string packageName) + { + return GetDo.Do(writerMethod, changeRunDir, packageName); + } + + public static int Pack(Action writerMethod, string changeRunDir, string pairName) + { + return PackDo.Do(writerMethod, changeRunDir, pairName); + } + + public static int Search(Action writerMethod, string changeRunDir, string searchQuery) + { + return SearchDo.Do(writerMethod, changeRunDir, searchQuery); + } + + public static int Update(Action writerMethod, string changeRunDir, string packageIndexURL) + { + return UpdateDo.Do(writerMethod, changeRunDir, packageIndexURL); + } + } +} diff --git a/agsget/agsget/Core/BaseFiles.cs b/agsget/agsget/Core/BaseFiles.cs index 9b91a0e..b6391b9 100644 --- a/agsget/agsget/Core/BaseFiles.cs +++ b/agsget/agsget/Core/BaseFiles.cs @@ -1,10 +1,10 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using System.IO; -namespace agsget +namespace AgsGetCore { public class BaseFiles { diff --git a/agsget/agsget/Core/Configuration.cs b/agsget/agsget/Core/Configuration.cs index eeaece6..e0a8b29 100644 --- a/agsget/agsget/Core/Configuration.cs +++ b/agsget/agsget/Core/Configuration.cs @@ -1,8 +1,8 @@ -using System; +using System; using System.Collections.Generic; using System.Text; -namespace agsget +namespace AgsGetCore { class Configuration { diff --git a/agsget/agsget/Core/GameAgfIO.cs b/agsget/agsget/Core/GameAgfIO.cs index 7ddc716..5f349f4 100644 --- a/agsget/agsget/Core/GameAgfIO.cs +++ b/agsget/agsget/Core/GameAgfIO.cs @@ -1,11 +1,11 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Linq; using System.Xml.Linq; -namespace agsget +namespace AgsGetCore { //I think I may need to refactor this for GameProjectIO public class GameAgfIO diff --git a/agsget/agsget/Core/PackageCacheIO.cs b/agsget/agsget/Core/PackageCacheIO.cs index 88cf560..266f964 100644 --- a/agsget/agsget/Core/PackageCacheIO.cs +++ b/agsget/agsget/Core/PackageCacheIO.cs @@ -1,25 +1,25 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -namespace agsget +namespace AgsGetCore { // This class should be the interface when interacting with the package cache // some commands are distributed on BaseFiles, maybe they need to be here and not there? public class PackageCacheIO { - public static void GetPackageIndex(string packageIndexUrl) + public static void GetPackageIndex(Action writerMethod, string packageIndexUrl) { if (string.IsNullOrEmpty(packageIndexUrl)) { - DownloadPretty.File(Configuration.PackageIndexURL + "index/package_index.json", BaseFiles.GetIndexFilePath()); + DownloadPretty.File(writerMethod, Configuration.PackageIndexURL + "index/package_index.json", BaseFiles.GetIndexFilePath()); } else { - DownloadPretty.File(packageIndexUrl + "index/package_index.json", BaseFiles.GetIndexFilePath()); + DownloadPretty.File(writerMethod, packageIndexUrl + "index/package_index.json", BaseFiles.GetIndexFilePath()); } } @@ -52,7 +52,7 @@ public static bool IsPackageOnLocalCache(string packageName) return File.Exists(scriptModuleFile); } - public static bool GetPackage(string packageIndexUrl, string packageName) + public static bool GetPackage(Action writerMethod, string packageIndexUrl, string packageName) { var packageDirPath = Path.Combine(BaseFiles.GetCacheDirectoryPath(), packageName); @@ -67,7 +67,7 @@ public static bool GetPackage(string packageIndexUrl, string packageName) // for the package. var destinationFile = Path.Combine(packageDirPath, packageName + ".scm"); - if (!DownloadPretty.File( + if (!DownloadPretty.File(writerMethod, packageIndexUrl + "pkgs/" + packageName + "/" + packageName + ".scm", destinationFile)) { diff --git a/agsget/agsget/_Utils/ConsoleExtra.cs b/agsget/agsget/Core/_Utils/ConsoleExtra.cs similarity index 94% rename from agsget/agsget/_Utils/ConsoleExtra.cs rename to agsget/agsget/Core/_Utils/ConsoleExtra.cs index c0d65a9..b0141c4 100644 --- a/agsget/agsget/_Utils/ConsoleExtra.cs +++ b/agsget/agsget/Core/_Utils/ConsoleExtra.cs @@ -1,8 +1,8 @@ -using System; +using System; using System.Collections.Generic; using System.Text; -namespace agsget +namespace AgsGetCore { public class ConsoleExtra { diff --git a/agsget/agsget/_Utils/DownloadPretty.cs b/agsget/agsget/Core/_Utils/DownloadPretty.cs similarity index 65% rename from agsget/agsget/_Utils/DownloadPretty.cs rename to agsget/agsget/Core/_Utils/DownloadPretty.cs index 2b59e5b..af93dbc 100644 --- a/agsget/agsget/_Utils/DownloadPretty.cs +++ b/agsget/agsget/Core/_Utils/DownloadPretty.cs @@ -1,23 +1,23 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using System.Net; -namespace agsget +namespace AgsGetCore { public class DownloadPretty { - public static bool File(string url, string filename) { + public static bool File(Action writerMethod, string url, string filename) { var webClient = new WebClient(); - Console.WriteLine("Will download from:"+ url); + writerMethod("Will download from:"+ url); webClient.DownloadFileTaskAsync( new Uri(url), filename, new Progress>(t => { - Console.Write($"\r({t.Item2,25:#,###}) Bytes received: {t.Item1,25:#,###}/{t.Item3,25:#,###}" ); + writerMethod(string.Format($"\r({t.Item2,25:#,###}) Bytes received: {t.Item1,25:#,###}/{t.Item3,25:#,###}" )); // Console.WriteLine($@" // Bytes received: {t.Item1,25:#,###} diff --git a/agsget/agsget/_Utils/WebClientExtensios.cs b/agsget/agsget/Core/_Utils/WebClientExtensios.cs similarity index 98% rename from agsget/agsget/_Utils/WebClientExtensios.cs rename to agsget/agsget/Core/_Utils/WebClientExtensios.cs index 9f909ca..6bec358 100644 --- a/agsget/agsget/_Utils/WebClientExtensios.cs +++ b/agsget/agsget/Core/_Utils/WebClientExtensios.cs @@ -1,11 +1,11 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using System.Net; using System.ComponentModel; using System.Threading.Tasks; -namespace agsget +namespace AgsGetCore { public static class WebClientExtensios {