-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
148 lines (121 loc) · 6.08 KB
/
Program.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
using Newtonsoft.Json.Linq;
using RootsBuilder;
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
namespace builder
{
class Program
{
private const string BUILDER_TOOLS = "BuilderTools";
private const string NODE_JS = "NodeJs";
private const string NODE_JS_URL = "https://nodejs.org/dist/v14.16.1/node-v14.16.1-win-x64.zip";
private const string APP_TEMPLATE = "AppTemplate";
private const string APP = "App";
private const string APP_TEMPLATE_GIT = "https://api.github.com/repos/WhiteOlivierus/app-template/zipball";
private const string PROJECT_NAME = "Project";
static void Main(string[] args)
{
/*
Setting up NodeJs
*/
Console.WriteLine("Downloading NodeJs");
if (!Directory.Exists(BUILDER_TOOLS))
{
WebClient webClient = new();
webClient.DownloadFile(NODE_JS_URL, $".\\{BUILDER_TOOLS}.zip");
Console.WriteLine("Downloaded NodeJs");
Console.WriteLine("Extracting NodeJs");
ZipFile.ExtractToDirectory($".\\{BUILDER_TOOLS}.zip", BUILDER_TOOLS, true);
// Renaming the folder in the builder tools so its easier accessible
DirectoryInfo node = new($".\\{BUILDER_TOOLS}");
if (!Directory.Exists($"{node.FullName}\\{NODE_JS}"))
{
Directory.Move(node.GetDirectories()[0].FullName, $"{node.FullName}\\{NODE_JS}");
Directory.Delete(node.GetDirectories()[0].FullName);
}
File.Delete($".\\{BUILDER_TOOLS}.zip");
}
Console.WriteLine("Extracted NodeJs");
/*
Setting up App template
*/
Console.WriteLine("Downloading App template");
if (!Directory.Exists(APP_TEMPLATE))
{
using WebClient client = new();
client.Headers.Add("user-agent", "Anything");
client.DownloadFile(APP_TEMPLATE_GIT, $".\\{APP_TEMPLATE}.zip");
Console.WriteLine("Downloaded App template");
Console.WriteLine("Extracting App template");
ZipFile.ExtractToDirectory($"{APP_TEMPLATE}.zip", APP_TEMPLATE, true);
DirectoryInfo app = new($".\\{APP_TEMPLATE}");
if (!Directory.Exists($"{app.FullName}\\{APP}"))
{
Directory.Move(app.GetDirectories()[0].FullName, $"{app.FullName}\\{APP}");
Directory.Delete(app.GetDirectories()[0].FullName);
}
//Clean up
File.Delete($"{APP_TEMPLATE}.zip");
}
Console.WriteLine("Extracted App template");
/*
Move the project files to the app template and overwrite if they allready exist
*/
Console.WriteLine("Setup App template");
if (!Directory.Exists($".\\{APP_TEMPLATE}\\{APP}\\src\\img"))
Directory.CreateDirectory($".\\{APP_TEMPLATE}\\{APP}\\src\\img");
CopyFilesRecursively(".\\img", $".\\{APP_TEMPLATE}\\{APP}\\src\\img");
if (File.Exists($".\\{APP_TEMPLATE}\\{APP}\\src\\projectData.json"))
File.Delete($".\\{APP_TEMPLATE}\\{APP}\\src\\projectData.json");
File.Copy(".\\projectData.json", $".\\{APP_TEMPLATE}\\{APP}\\src\\projectData.json");
Console.WriteLine("Setup done App template");
/*
Setup the package.json of the app
*/
Console.WriteLine("Configure App template");
string path = $".\\{APP_TEMPLATE}\\{APP}\\package.json";
string json = File.ReadAllText(path);
JObject package = JObject.Parse(json);
SaveJsonKeyEdit(package, "author", "dutchskull");
SaveJsonKeyEdit(package, "description", "A exported executable from the web");
SaveJsonKeyEdit(package, "name", PROJECT_NAME);
File.WriteAllText(path, package.ToString());
Console.WriteLine("Configure done App template");
/*
The building step
*/
Console.WriteLine("Building App template");
CommandExecuter.ExecuteCommand($"cd .\\{APP_TEMPLATE}\\{APP} && .\\..\\..\\{BUILDER_TOOLS}\\{NODE_JS}\\npm.cmd i");
CommandExecuter.ExecuteCommand($"cd .\\{APP_TEMPLATE}\\{APP} && .\\..\\..\\{BUILDER_TOOLS}\\{NODE_JS}\\npm.cmd link engine");
CommandExecuter.ExecuteCommand($"cd .\\{APP_TEMPLATE}\\{APP}\\ && .\\..\\..\\{BUILDER_TOOLS}\\{NODE_JS}\\npm.cmd run make");
Console.WriteLine("Build App template");
/*
Move the build to the root of the project
*/
DirectoryInfo buildOutputPath = new($".\\{APP_TEMPLATE}\\{APP}\\out\\make\\squirrel.windows\\x64\\");
if (File.Exists($"./{PROJECT_NAME}.exe"))
File.Delete($"./{PROJECT_NAME}.exe");
File.Move(buildOutputPath.GetFiles().Where(file => file.Name.Contains(".exe")).FirstOrDefault().FullName, $"./{PROJECT_NAME}.exe");
if (File.Exists($"{PROJECT_NAME}.exe"))
Process.Start("explorer.exe", $"{ PROJECT_NAME}.exe");
}
private static void CopyFilesRecursively(string sourcePath, string targetPath)
{
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
}
private static void SaveJsonKeyEdit(JObject package, string key, object value)
{
if (package.ContainsKey(key))
package[key] = value.ToString();
else
package.Add(key, value.ToString());
}
}
}