timelapsenow/Timelapse.Pack/Program.cs

272 lines
9.2 KiB
C#

using TimelapseApi;
using System.Diagnostics;
using System.IO;
using System;
using System.IO.Compression;
using System.Xml;
using System.Xml.Linq;
public class TimelapsePack
{
public static string? BuildProject()
{
/*<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>*/
string curDirName=Path.GetFileName(Environment.CurrentDirectory);
string fname = File.Exists($"{curDirName}.csproj") ? $"{curDirName}.csproj" : (File.Exists($"{curDirName}.vbproj") ? $"{curDirName}.vbproj" : $"{curDirName}.fsproj");
string? outType="";
bool containsRef=false;
XmlReader reader = XmlReader.Create(fname);
XElement el = XElement.Load(reader);
reader.Close();
foreach(var item in el.Descendants())
{
if(item.Name=="PropertyGroup")
{
foreach(var desend in item.Descendants())
{
if(desend.Name== "OutputType")
{
outType=desend.Value;
}
}
}
if(item.Name == "ItemGroup")
{
foreach (var desend in item.Descendants())
{
if (desend.Name == "ProjectReference")
{
foreach (var a in desend.Attributes())
{
if (a.Name == "Include")
{
if (a.Value.Contains("TimelapseApi"))
{
containsRef = true;
}
}
}
}
if (desend.Name == "PackageReference")
{
foreach (var a in desend.Attributes())
{
if (a.Name == "Include")
{
if (a.Value.Contains("Tesses.TimelapseApi"))
{
containsRef = true;
}
}
}
}
}
}
}
if(!string.IsNullOrWhiteSpace(outType) && outType == "Library" && containsRef)
{
//build the library
Process p = new Process();
p.StartInfo = new ProcessStartInfo("dotnet","publish --configuration Release");
p.StartInfo.UseShellExecute=false;
if(p.Start())
{
p.WaitForExit();
return Path.Combine(Environment.CurrentDirectory,"bin","Release","net6.0","publish",$"{curDirName}.dll");
}else{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("ERROR: process not started");
}
}else{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("ERROR: Project is not a library");
}
return null;
}
public static void DirCopy(string src,string dest,Func<string,bool>? copyItem)
{
foreach(var f in Directory.GetDirectories(src))
{
if(copyItem != null && copyItem(f) == false)
{
continue;
}
string fname =Path.GetFileName(f);
DirCopy(f,Path.Combine(dest,fname),copyItem);
}
foreach(var f in Directory.GetFiles(src))
{
if(copyItem != null && copyItem(f) == false)
{
continue;
}
string fname =Path.GetFileName(f);
File.Copy(f,Path.Combine(dest,fname),true);
}
}
public static string? Pack(string? outname)
{
string[] _hasDeps=new string[] {
"AtkSharp.dll",
"TimelapseApi.dll",
"Eto.dll",
"Newtonsoft.Json.dll",
"FlashCap.dll",
"CarioSharp.dll",
"GtkSharp.dll",
"GdkSharp.dll",
"GLibSharp.dll",
"SixLabors.ImageSharp.dll"
};
var outPut=BuildProject();
if(!string.IsNullOrWhiteSpace(outPut))
{
Directory.CreateDirectory("_build/extension");
File.Copy("extension_info.json","_build/extension_info.json",true);
string? dirname=Path.GetDirectoryName(outPut);
if(!string.IsNullOrWhiteSpace(dirname)){
DirCopy(dirname,"_build/extension",(e)=>{
if(e==outPut)
{
return false;
}
string cF = Path.GetFileName(e);
return !_hasDeps.Contains(cF);
});
string fname = $"{Path.GetFileNameWithoutExtension(outPut)}.zip";
File.Copy(outPut,"_build/extension/extension.dll",true);
if(!string.IsNullOrWhiteSpace(outname))
{
fname=outname;
}
if(File.Exists(fname))
{
File.Delete(fname);
}
ZipFile.CreateFromDirectory ("_build",fname,CompressionLevel.SmallestSize, false);
Directory.Delete("_build",true);
return fname;
}
}
return null;
}
public static void Uninstall(string name)
{
string dir= Api.GetInternalFile("ExtensionBinaries",name);
if(Directory.Exists(dir))
{
Console.Write($"Are you sure you want to delete {name} (Yes/No): ");
j:
var read=Console.ReadLine();
if(string.IsNullOrWhiteSpace(read)) read="";
read=read.ToLower();
if(read == "yes")
{
Console.WriteLine("I asked ya, you said yes so deleting");
Directory.Delete(dir,true);
} else if (read == "no")
{
Console.WriteLine("Not deleting");
}
else {
Console.Write("Please type yes or no: ");
goto j;
}
}else{
Console.WriteLine($"No such extension {name}");
}
}
public static void ListExtensions(bool load)
{
if(load)
{
foreach(var item in ExtensionLoader.GetTimelapseExtensions())
{
Console.WriteLine($"Name=\"{item.Name}\", Id=\"{item.Id}\"");
}
}else{
string dirtoitterateon= Api.GetInternalFile("ExtensionBinaries");
if(Directory.Exists(dirtoitterateon))
{
foreach(var dir in Directory.GetDirectories(dirtoitterateon))
{
Console.WriteLine(Path.GetFileName(dir));
}
}
}
}
public static void Install(string? file)
{
if(!string.IsNullOrWhiteSpace(file))
{
TimelapseApi.ExtensionLoader.InstallExtension(file).Wait();
return;
}
string? outPut=Pack(null);
if(!string.IsNullOrWhiteSpace(outPut))
{
TimelapseApi.ExtensionLoader.InstallExtension(outPut).Wait();
File.Delete(outPut);
}
}
public static void Main(string[] args)
{
Console.WriteLine("Timelapse Extension Packager");
if(args.Length < 1)
{
Console.WriteLine("Usage: <command> args...");
Console.WriteLine();
Console.WriteLine("Commands:");
Console.WriteLine(" pack [optional-filename]");
Console.WriteLine(" install [optional-filename]");
Console.WriteLine(" list [--get-id (requires loading extensions partially)]");
Console.WriteLine(" uninstall <filename>");
return;
}
Console.WriteLine();
switch(args[0])
{
case "pack":
string? fname = "";
if(args.Length >= 2) fname=args[1];
Pack(fname);
break;
case "install":
string? fname2 = "";
if(args.Length >= 2) fname2=args[1];
Install(fname2);
break;
case "list":
ListExtensions(args.Length >= 2 && args[1] == "--get-id");
break;
case "uninstall":
if(args.Length >= 2) Uninstall(args[1]);
break;
}
}
}