-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
94 lines (88 loc) · 3.23 KB
/
Program.cs
File metadata and controls
94 lines (88 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Xml;
using System.Linq;
namespace FantasyGroundsPackager
{
class Program
{
static void Main(string[] args)
{
string rootDir = GetPackageDirectory(args);
string definitionFile = ValidatePackageDirectory(rootDir);
ISet<string> validFiles = ReadPackageContents(rootDir, definitionFile);
validFiles.UnionWith(Directory.EnumerateFiles(rootDir, "*.md").Select(mdFile => Path.GetFileName(mdFile)));
string packageFile = Path.Join(rootDir, $"{Path.GetFileName(rootDir)}.ext");
if (File.Exists(packageFile))
{
File.Delete(packageFile);
}
using ZipArchive archive = ZipFile.Open(packageFile, ZipArchiveMode.Create);
foreach (string file in validFiles)
{
archive.CreateEntryFromFile(Path.Join(rootDir, file), file);
}
Console.WriteLine("Success!");
}
static string GetPackageDirectory(string[] args)
{
if (args.Length > 0 && args[0].Trim().Length > 0)
{
return Path.GetFullPath(args[0].Trim());
}
else
{
return Environment.CurrentDirectory;
}
}
static string ValidatePackageDirectory(string directory)
{
string extensionFile = Path.Join(directory, "extension.xml");
if (!File.Exists(extensionFile))
{
throw new FileNotFoundException("No extension file found!", extensionFile);
}
return "extension.xml";
}
static ISet<string> ReadPackageContents(string dir, string packageFile)
{
Console.WriteLine(Path.Join(dir, packageFile));
HashSet<string> validFiles = new() { packageFile };
if (Path.GetExtension(packageFile) == ".xml")
{
XmlDocument xmlDoc = new();
xmlDoc.Load(Path.Join(dir, packageFile));
ISet<string> files = ReadNodesForFiles(xmlDoc.SelectNodes("/root/*"));
foreach (string file in files)
{
ISet<string> contents = ReadPackageContents(dir, file);
validFiles.UnionWith(contents);
}
}
return validFiles;
}
static ISet<string> ReadNodesForFiles(XmlNodeList nodes)
{
HashSet<string> validFiles = new() { };
foreach (XmlNode node in nodes)
{
if (node.Name == "includefile")
{
validFiles.Add(node.Attributes.GetNamedItem("source").Value);
}
else if (node.Name == "script" || node.Name == "icon")
{
string value = node.Attributes.GetNamedItem("file")?.Value ?? null;
if (value != null)
{
validFiles.Add(value);
}
}
validFiles.UnionWith(ReadNodesForFiles(node.ChildNodes));
}
return validFiles;
}
}
}