rewrite for minimal usage
This commit is contained in:
parent
71412a34fc
commit
40d45a89d8
2 changed files with 144 additions and 53 deletions
19
pkkg
Normal file
19
pkkg
Normal file
|
@ -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
|
172
src/Program.bf
172
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<StringView, Package> Processed = new .() ~ DeleteDictionaryAndValues!(_);
|
||||
public static List<String> FilesToProcess = new .() ~ DeleteContainerAndItems!(_);
|
||||
public static List<Package.Dependency> 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)
|
||||
{
|
||||
Log("Unable to write template file");
|
||||
return (.)ReturnValues.FileWriteError;
|
||||
}
|
||||
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)
|
||||
var targetFile = FilesToProcess.PopBack();
|
||||
defer delete targetFile;
|
||||
String targetFileContent = scope .();
|
||||
Package packageFile = new .();
|
||||
if(File.ReadAllText(targetFile, targetFileContent) case .Err)
|
||||
{
|
||||
Log("Unable to read config file");
|
||||
return (.)ReturnValues.FileReadError;
|
||||
delete packageFile;
|
||||
continue;
|
||||
}
|
||||
Log("Read file attempting to parse it");
|
||||
|
||||
Package current = new .();
|
||||
if(current.Deserialize())
|
||||
if(ParseBofa(targetFileContent, ref packageFile) case .Err)
|
||||
{
|
||||
|
||||
}
|
||||
delete packageFile;
|
||||
continue;
|
||||
}
|
||||
Processed.Add(packageFile.Name, packageFile);
|
||||
for(var i in packageFile.Dependencies)
|
||||
DependenciesToProcess.Add(i);
|
||||
|
||||
///Log a message to the console and to a file
|
||||
public static void Log(StringView pMessage)
|
||||
while(DependenciesToProcess.Count > 0)
|
||||
{
|
||||
Console.WriteLine(pMessage);
|
||||
if(!LogFile.IsEmpty)
|
||||
File.WriteAllText(LogFile, pMessage, true).IgnoreError();
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
public static Result<void> ParseBofa(StringView pMessage)
|
||||
return;
|
||||
}
|
||||
|
||||
public static Result<void> ParseBofa(StringView pMessage, ref Package pPackage)
|
||||
{
|
||||
Dictionary<StringView, Bofa> results = new .();
|
||||
defer { DeleteDictionaryAndValues!(results); }
|
||||
List<int64> 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<void> 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<Author> Authors ~ DeleteContainerAndItems!(_);
|
||||
public List<Author> 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<Dependency> Dependencies = null ~ DeleteContainerAndItems!(_);
|
||||
|
||||
|
||||
public bool Deserialize(Dictionary<StringView, Bofa> 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<Dependency> Dependencies ~ DeleteContainerAndItems!(_);
|
||||
}
|
Reference in a new issue