From 40d45a89d8c2dc14bd561030692e7b16d2c1ce33 Mon Sep 17 00:00:00 2001 From: Booklordofthedings Date: Sun, 8 Sep 2024 23:49:17 +0200 Subject: [PATCH] rewrite for minimal usage --- pkkg | 19 ++++++ src/Program.bf | 178 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 144 insertions(+), 53 deletions(-) create mode 100644 pkkg diff --git a/pkkg b/pkkg new file mode 100644 index 0000000..ecba703 --- /dev/null +++ b/pkkg @@ -0,0 +1,19 @@ +#This is the pkkg template file +l Name Pkkg + +t Desc The pkkg package is supposed to be used for basic package management. +- It only depdends on Bofa and git being installed as a commandline program on. +- The PC running it. + +l Url https:git.lybry.net + +a Authors + o Book + l Name Booklordofthedings + l Url https://Boooklordofthe.dev + +a Dependencies + o Bofa + l Name Bofa + l Url https://code.booklordofthe.dev/Booklordofthedings/Bofa.git + l Tag 1.0.0 \ No newline at end of file diff --git a/src/Program.bf b/src/Program.bf index b333875..40790b2 100644 --- a/src/Program.bf +++ b/src/Program.bf @@ -2,6 +2,7 @@ namespace pkkg; using System; using System.IO; +using System.Diagnostics; using System.Collections; using Bofa; @@ -9,6 +10,7 @@ using Bofa.Serialization; class Program { + public static int32 VersionMajor = 1; public static int32 VersionMinor = 0; public static int32 VersionPatch = 0; @@ -18,91 +20,161 @@ class Program """; public static String PkkgFile = "pkkg"; - public static String LogFile = "pkkg.log"; + public static String DepPath = "deps"; - public enum ReturnValues : int32 - { - //Errors - UnknownError = -1, - FileWriteError = -2, - FileReadError = -3, - //Sucesses - CreatedTemplateFile = 1, - } + //Runtime stuff + public static Dictionary Processed = new .() ~ DeleteDictionaryAndValues!(_); + public static List FilesToProcess = new .() ~ DeleteContainerAndItems!(_); + public static List DependenciesToProcess = new .() ~ DeleteContainerAndItems!(_); - public static int32 Main(String[] pArgs) + public static void Main(String[] pArgs) { - Log(scope $"Starting pkkg {VersionMajor}.{VersionMinor}.{VersionPatch}"); if(!File.Exists(PkkgFile)) { - if(File.WriteAllText(PkkgFile, TemplateFile) case .Err) + File.WriteAllText(PkkgFile, TemplateFile).IgnoreError(); + return; + } + + FilesToProcess.Add(new .("pkkg")); + while(FilesToProcess.Count > 0) + { + var targetFile = FilesToProcess.PopBack(); + defer delete targetFile; + String targetFileContent = scope .(); + Package packageFile = new .(); + if(File.ReadAllText(targetFile, targetFileContent) case .Err) { - Log("Unable to write template file"); - return (.)ReturnValues.FileWriteError; + delete packageFile; + continue; + } + if(ParseBofa(targetFileContent, ref packageFile) case .Err) + { + delete packageFile; + continue; + } + Processed.Add(packageFile.Name, packageFile); + for(var i in packageFile.Dependencies) + DependenciesToProcess.Add(i); + + while(DependenciesToProcess.Count > 0) + { + var targetDependency = DependenciesToProcess.PopBack(); + if(Processed.ContainsKey(targetDependency.Name)) + continue; + + DownloadDependency(DepPath, targetDependency).IgnoreError(); + if(File.Exists(scope $"{DepPath}/{targetDependency.Name}/pkkg")) + FilesToProcess.Add(new $"{DepPath}/{targetDependency.Name}/pkkg"); } - Log("Wrote template file"); - return (.)ReturnValues.CreatedTemplateFile; } - //pkkg file already exists, trying to parse it - Log("Attempting to read pkkg file"); - var packageFile = scope String(); - if(File.ReadAllText(PkkgFile, packageFile) case .Err) - { - Log("Unable to read config file"); - return (.)ReturnValues.FileReadError; - } - Log("Read file attempting to parse it"); - - Package current = new .(); - if(current.Deserialize()) - { - - } + return; } - ///Log a message to the console and to a file - public static void Log(StringView pMessage) - { - Console.WriteLine(pMessage); - if(!LogFile.IsEmpty) - File.WriteAllText(LogFile, pMessage, true).IgnoreError(); - } - - public static Result ParseBofa(StringView pMessage) + public static Result ParseBofa(StringView pMessage, ref Package pPackage) { Dictionary results = new .(); defer { DeleteDictionaryAndValues!(results); } List errors = scope .(); BofaParser.Parse(pMessage, results, errors); if(errors.Count > 0) - return .Err; //We are hardcode like that + return .Err; //We are hardcore like that + if(!pPackage.Deserialize(results)) + return .Err; + return .Ok; + } + public static Result DownloadDependency(StringView pkgPath, Package.Dependency pDep) + { + ProcessStartInfo info = scope .(); + SpawnedProcess process = scope .(); + + + info.SetWorkingDirectory(DepPath); + info.SetFileNameAndArguments(scope $"git clone {pDep.Url} {pDep.Name}"); + + if(process.Start(info) case .Err) + return .Err; + + while(!process.HasExited) + continue; + + if(process.ExitCode != 0) + return .Err; + + if(pDep.Tag == "") + return .Ok; + //Reuse for setting the tag/branch + + info = scope .(); + process = scope .(); + + info.SetWorkingDirectory(scope $"{DepPath}/{pDep.Name}"); + info.SetFileNameAndArguments(scope $"git checkout tags/{pDep.Tag}"); + + if(process.Start(info) case .Err) + return .Err; + + while(!process.HasExited) + continue; + + if(process.ExitCode != 0) + return .Err; + + return .Ok; } } -[BofaSerialize] + class Package { - public String Name ~ delete _; - public String Description ~ delete _; - public String Url ~ delete _; + public String Name = null ~ delete _; + public String Description = null ~ delete _; + public String Url = null ~ delete _; [BofaSerialize] public class Author { - public String Name ~ delete _; - public String Url ~ delete _; + public String Name = new .() ~ delete _; + public String Url = new .() ~ delete _; } - public List Authors ~ DeleteContainerAndItems!(_); + public List Authors = null ~ DeleteContainerAndItems!(_); [BofaSerialize] public class Dependency { - public String Name ~ delete _; - public String GitUrl ~ delete _; - public String Branch ~ delete _; + public String Name = new .() ~ delete _; + public String Url = new .() ~ delete _; + public String Tag = new .() ~ delete _; + } + public List Dependencies = null ~ DeleteContainerAndItems!(_); + + + public bool Deserialize(Dictionary pInput) + { + if(!pInput.ContainsKey("Name") || pInput["Name"].Type != .Line) + return false; + Name = new .(pInput["Name"].Value.Line); + + if(!pInput.ContainsKey("Desc") || pInput["Desc"].Type != .Text) + return false; + Description = new .(pInput["Desc"].Value.Text); + + if(!pInput.ContainsKey("Url") || pInput["Url"].Type != .Line) + return false; + Url = new .(pInput["Url"].Value.Line); + + if(!pInput.ContainsKey("Authors") || pInput["Authors"].Type != .Array) + return false; + Authors = new .(); + Authors.Deserialize(pInput["Authors"]); + + if(!pInput.ContainsKey("Dependencies") || pInput["Dependencies"].Type != .Array) + return false; + Dependencies = new .(); + Dependencies.Deserialize(pInput["Dependencies"]); + + return true; } - public List Dependencies ~ DeleteContainerAndItems!(_); } \ No newline at end of file