-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackage.cs
185 lines (175 loc) · 7.07 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace imp
{
public class Package
{
public string Title { get; set; }
public string Author { get; set; }
public string License { get; set; }
public List<PackageDependency> Dependencies { get; set; }
public PackageSource Source { get; set; }
public string Target { get; set; }
public List<string> WatchExtra { get; set; }
public string BeforeBuild { get; set; }
public string AfterBuild { get; set; }
public List<string> AllowHosts { get; set; }
public Package(string targetFileName, string sourceDir, string remoteSourcesDir)
{
initDefaults(targetFileName, sourceDir, remoteSourcesDir);
}
public Package(string jsonStr)
{
initDefaults();
fromJson(jsonStr);
}
private void initDefaults(string targetFileName = "", string sourceDir = "", string remoteSourcesDir = "")
{
Title = "Just another IMP project";
Author = System.Environment.UserName;
License = "";
Dependencies = new List<PackageDependency>();
Target = targetFileName;
WatchExtra = new List<string>();
AfterBuild = "";
BeforeBuild = "";
Source = new PackageSource();
Source.RemoteSources = remoteSourcesDir;
Source.EntryPoint = "";
Source.Language = "";
Source.CustomExtensions = "";
Source.Sources = new List<string>();
if(sourceDir.Length > 0) {
Source.Sources.Add(sourceDir);
}
AllowHosts = new List<string>();
}
private void fromJson(string jsonStr)
{
JObject json = JObject.Parse(jsonStr);
if(json["language"] != null) {
Source.Language = (string)json["language"];
}
Title = json["title"] == null ? "" : (string)json["title"];
Author = json["author"] == null ? "" : (string)json["author"];
License = json["license"] == null ? "" : (string)json["license"];
Target = json["target"] == null ? "" : (string)json["target"];
WatchExtra.Clear();
if(json["watchExtra"] != null && json["watchExtra"].Type == JTokenType.Array) {
foreach(string extra in json["watchExtra"]) {
WatchExtra.Add(extra);
}
}
BeforeBuild = json["beforeBuild"] == null ? "" : (string)json["beforeBuild"];
AfterBuild = json["afterBuild"] == null ? "" : (string)json["afterBuild"];
if(json["sourceExtensions"] != null) {
Source.CustomExtensions = (string)json["sourceExtensions"];
}
if(json["entryPoint"] != null) {
Source.EntryPoint = (string)json["entryPoint"];
}
if(json["remoteSources"] != null) {
Source.RemoteSources = (string)json["remoteSources"];
}
Dependencies.Clear();
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));
}
}
AllowHosts.Clear();
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);
}
}
Source.Sources.Clear();
if(json["sources"] != null && json["sources"].Type == JTokenType.Array) {
foreach(string src in json["sources"]) {
Source.Sources.Add(src);
}
}
}
public JObject ToJson()
{
var result = new JObject();
result.Add(new JProperty("language", GetLanguage()));
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(Source.Sources.Count > 0) {
result.Add(new JProperty("sources", new JArray(Source.Sources.ToArray())));
}
if(Source.RemoteSources.Length > 0) {
result.Add(new JProperty("remoteSources", Source.RemoteSources));
}
if(Target.Length > 0) {
result.Add(new JProperty("target", Target));
}
if(WatchExtra.Count > 0) {
result.Add(new JProperty("watchExtra", new JArray(WatchExtra.ToArray())));
}
if(BeforeBuild.Length > 0) {
result.Add(new JProperty("beforeBuild", BeforeBuild));
}
if(AfterBuild.Length > 0) {
result.Add(new JProperty("afterBuild", AfterBuild));
}
if(Source.CustomExtensions.Length > 0) {
result.Add(new JProperty("sourceExtensions", Source.CustomExtensions));
}
if(Source.EntryPoint.Length > 0) {
result.Add(new JProperty("entryPoint", Source.EntryPoint));
}
if(AllowHosts.Count > 0) {
result.Add(new JProperty("allowHosts", AllowHosts.ToArray()));
}
return result;
}
public string GetLanguage()
{
if(Source.Language.Length == 0) {
string ext = Path.GetExtension(Target);
if(ext == null || ext.Length == 0) {
return "";
}
ext = ext.Substring(1);
switch(ext) {
case "as":
return "angelscript";
case "lua":
return "lua";
}
return ext;
}
return Source.Language;
}
public static string getDefaultConfiguration(string targetFileName, string sourceDir, string remoteSourcesDir)
{
return new Package(targetFileName, sourceDir, remoteSourcesDir).ToJson().ToString();
}
}
}