This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackage.cs
140 lines (131 loc) · 4.99 KB
/
Package.cs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace wlpm
{
public class Package
{
public const string DefaultTarget = "war3map.lua";
public const string DefaultSourceExtensions = "*.lua";
public string Title { get; set; }
public string Author { get; set; }
public string License { get; set; }
public List<PackageDependency> Dependencies { get; set; }
public List<string> Sources { get; set; }
public string Target { get; set; }
public string AfterBuild { get; set; }
public bool InsertModuleLoader { get; set; }
public List<string> AllowHosts { get; set; }
public string SourceExtensions { get; set; }
public Package()
{
initDefaults();
}
public Package(string jsonStr)
{
initDefaults();
fromJson(jsonStr);
}
private void initDefaults()
{
Title = "project";
Author = "";
License = "";
Dependencies = new List<PackageDependency>();
Sources = new List<string>();
Target = DefaultTarget;
AfterBuild = "";
InsertModuleLoader = true;
SourceExtensions = DefaultSourceExtensions;
AllowHosts = new List<string>();
}
private void fromJson(string jsonStr)
{
JObject json = JObject.Parse(jsonStr);
if(json["title"] != null) {
Title = (string)json["title"];
}
if(json["author"] != null) {
Author = (string)json["author"];
}
if(json["license"] != null) {
License = (string)json["license"];
}
if(json["target"] != null) {
Target = (string)json["target"];
}
if(json["afterBuild"] != null) {
AfterBuild = (string)json["afterBuild"];
}
if(json["sourceExtensions"] != null) {
SourceExtensions = (string)json["sourceExtensions"];
}
if(json["insertModuleLoader"] != null) {
InsertModuleLoader = (bool)json["insertModuleLoader"];
}
if(json["dependencies"] != null) {
if(json["dependencies"].Type != JTokenType.Object) {
throw new PackageException("Cannot parse \"" + Title + "\" package. The value of the property 'dependencies' must be an object, if exists");
}
foreach(JProperty d in json["dependencies"]) {
Dependencies.Add(new PackageDependency(d, Title));
}
}
if(json["allowHosts"] != null) {
if(json["allowHosts"].Type != JTokenType.Array) {
throw new PackageException("Cannot parse \"" + Title + "\" package. The value of the property 'allowHosts' must be an array, if exists");
}
foreach(string h in json["allowHosts"]) {
AllowHosts.Add(h);
}
}
if(json["sources"] != null && json["sources"].Type == JTokenType.Array) {
foreach(string src in json["sources"]) {
Sources.Add(src);
}
}
}
public JObject ToJson()
{
var result = new JObject();
if(Title.Length > 0) {
result.Add(new JProperty("title", Title));
}
if(Author.Length > 0) {
result.Add(new JProperty("author", Author));
}
if(License.Length > 0) {
result.Add(new JProperty("license", License));
}
var deps = new JObject();
foreach(PackageDependency dep in Dependencies) {
deps.Add(dep.ToJson());
}
result.Add(new JProperty("dependencies", deps));
if(Sources.Count > 0) {
result.Add(new JProperty("sources", new JArray(Sources.ToArray())));
}
if(Target.Length > 0 && Target != DefaultTarget) {
result.Add(new JProperty("target", Target));
}
if(AfterBuild.Length > 0) {
result.Add(new JProperty("afterBuild", AfterBuild));
}
if(!InsertModuleLoader) {
result.Add(new JProperty("insertModuleLoader", false));
}
if(SourceExtensions.Length > 0 && SourceExtensions != DefaultSourceExtensions) {
result.Add(new JProperty("sourceExtensions", SourceExtensions));
}
if(AllowHosts.Count > 0) {
result.Add(new JProperty("allowHosts", AllowHosts.ToArray()));
}
return result;
}
public static string getDefaultConfiguration()
{
return "{\n \"title\": \"project\",\n \"dependencies\": {}\n}";
}
}
}