timelapsenow/Timelapse.Pack/Program.cs

272 lines
9.2 KiB
C#
Raw Permalink Normal View History

2022-05-20 02:06:35 +00:00
using TimelapseApi;
using System.Diagnostics;
using System.IO;
using System;
using System.IO.Compression;
2022-05-20 02:08:13 +00:00
2022-05-20 02:06:35 +00:00
using System.Xml;
using System.Xml.Linq;
2022-05-20 02:08:13 +00:00
2022-05-20 02:06:35 +00:00
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;
2022-05-20 02:08:13 +00:00
2022-05-20 02:06:35 +00:00
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;
}
}
}
}
}
}
}
2022-05-20 02:08:13 +00:00
2022-05-20 02:06:35 +00:00
if(!string.IsNullOrWhiteSpace(outType) && outType == "Library" && containsRef)
{
//build the library
Process p = new Process();
p.StartInfo = new ProcessStartInfo("dotnet","publish --configuration Release");
2022-05-20 02:06:35 +00:00
p.StartInfo.UseShellExecute=false;
if(p.Start())
{
p.WaitForExit();
return Path.Combine(Environment.CurrentDirectory,"bin","Release","net6.0","publish",$"{curDirName}.dll");
2022-05-20 02:06:35 +00:00
}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;
}
2022-05-20 06:44:35 +00:00
public static void DirCopy(string src,string dest,Func<string,bool>? copyItem)
2022-05-20 02:06:35 +00:00
{
foreach(var f in Directory.GetDirectories(src))
{
if(copyItem != null && copyItem(f) == false)
{
continue;
}
string fname =Path.GetFileName(f);
2022-05-20 06:44:35 +00:00
2022-05-20 07:30:59 +00:00
DirCopy(f,Path.Combine(dest,fname),copyItem);
2022-05-20 02:06:35 +00:00
}
foreach(var f in Directory.GetFiles(src))
{
if(copyItem != null && copyItem(f) == false)
{
continue;
}
string fname =Path.GetFileName(f);
2022-05-20 07:30:59 +00:00
File.Copy(f,Path.Combine(dest,fname),true);
2022-05-20 02:06:35 +00:00
}
}
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");
2022-05-20 07:30:59 +00:00
File.Copy("extension_info.json","_build/extension_info.json",true);
2022-05-20 06:44:35 +00:00
string? dirname=Path.GetDirectoryName(outPut);
2022-05-20 02:06:35 +00:00
2022-05-20 06:44:35 +00:00
if(!string.IsNullOrWhiteSpace(dirname)){
DirCopy(dirname,"_build/extension",(e)=>{
2022-05-20 02:06:35 +00:00
if(e==outPut)
{
return false;
}
string cF = Path.GetFileName(e);
return !_hasDeps.Contains(cF);
});
string fname = $"{Path.GetFileNameWithoutExtension(outPut)}.zip";
2022-05-20 07:30:59 +00:00
File.Copy(outPut,"_build/extension/extension.dll",true);
2022-05-20 02:06:35 +00:00
if(!string.IsNullOrWhiteSpace(outname))
{
fname=outname;
}
if(File.Exists(fname))
{
File.Delete(fname);
}
ZipFile.CreateFromDirectory ("_build",fname,CompressionLevel.SmallestSize, false);
2022-05-20 07:30:59 +00:00
Directory.Delete("_build",true);
2022-05-20 02:06:35 +00:00
return fname;
2022-05-20 06:44:35 +00:00
}
2022-05-20 02:06:35 +00:00
}
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:
2022-05-20 06:44:35 +00:00
var read=Console.ReadLine();
if(string.IsNullOrWhiteSpace(read)) read="";
read=read.ToLower();
2022-05-20 02:06:35 +00:00
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":
2022-05-20 02:07:41 +00:00
string? fname2 = "";
if(args.Length >= 2) fname2=args[1];
Install(fname2);
2022-05-20 02:06:35 +00:00
break;
case "list":
ListExtensions(args.Length >= 2 && args[1] == "--get-id");
break;
case "uninstall":
if(args.Length >= 2) Uninstall(args[1]);
break;
}
}
}