-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
281 lines (241 loc) · 10.9 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
namespace AWRP {
public class Program {
private static string CONFIG_PATH;
private static void Usage(Assembly asm) {
Console.Write("Usage: ");
Console.Write(Path.ChangeExtension(asm.GetName().Name,".exe"));
Console.WriteLine(" [init|add|push|remove|help] [options]");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("DESCRIPTION");
Console.WriteLine($" {asm.GetName().Name} is a tool to upload PowerApps WebResources dynamically to a solution.");
Console.WriteLine($" Initialize a new connection for your password using your host (organization>.crm.dynamics.com), a user, and his user secret.");
Console.WriteLine($" Adding files using the 'add' command and push them dynamically to your existing solution.");
Console.WriteLine();
Console.WriteLine("COMMANDS");
Console.WriteLine(" init");
Console.WriteLine(" Initialize a new connection in the current working directory");
Console.WriteLine();
Console.WriteLine(" add");
Console.WriteLine(" Add a file to the tracking list");
Console.WriteLine();
Console.WriteLine(" OPTIONS");
Console.WriteLine(" -f <file>");
Console.WriteLine(" The file relative to the config file that should be uploaded");
Console.WriteLine(" -s <solution>");
Console.WriteLine(" The Solutions unique name");
Console.WriteLine(" [-p <prefix>]");
Console.WriteLine(" When the resource path prefix is not set, the default of the config file is used");
Console.WriteLine(" [-d <description>]");
Console.WriteLine(" The description of the WebResource");
Console.WriteLine();
Console.WriteLine(" remove");
Console.WriteLine(" Removes a file from the tracking list");
Console.WriteLine();
Console.WriteLine(" OPTIONS");
Console.WriteLine(" -f <file>");
Console.WriteLine(" The file relative to the config file that should be uploaded");
Console.WriteLine();
Console.WriteLine(" push");
Console.WriteLine(" Upload all tracked files to your environment");
Console.WriteLine();
Console.WriteLine(" help");
Console.WriteLine(" Prints this text");
Console.WriteLine();
}
public static int Main(string[] args) {
if (args.Length == 0) {
Assembly asm = Assembly.GetEntryAssembly();
Console.Write(asm.GetName().Name);
Console.Write(" ");
Console.WriteLine(asm.GetName().Version);
Console.WriteLine();
Usage(asm);
return 0;
}
CONFIG_PATH = Path.Combine(Env.CWD, Config.CONFIG_NAME);
string task = args[0];
if (task == "init") {
return Init();
}
if (task == "push") {
return Push();
}
if (task == "add") {
string file = null;
string prefix = null;
string solution = null;
string description = "";
for (uint i = 1; i < args.Length; i++)
{
if (args[i] == "-f") {
if (i + 1 >= args.Length) {break;}
file = args[++i];
} else if (args[i] == "-d") {
if (i + 1 >= args.Length) {break;}
description = args[++i];
} else if (args[i] == "-p") {
if (i + 1 >= args.Length) {break;}
prefix = args[++i];
} else if (args[i] == "-s") {
if (i + 1 >= args.Length) {break;}
solution = args[++i];
}
}
if (file == null) {
Console.WriteLine("Missing parameter '-f <file>'");
return 1;
}
if (solution == null) {
Console.WriteLine("Missing parameter '-s <solution>'");
return 1;
}
return Add(file, description, prefix, solution);
}
if (task == "remove") {
string file = null;
for (uint i = 0; i < args.Length; i++) {
if (args[i] == "-f") {
if (i + 1 >= args.Length) {break;}
file = args[++i];
}
}
if (file == null) {
Console.WriteLine("Missing parameter '-f <file>'");
return 1;
}
return Remove(file);
}
{
Assembly asm = Assembly.GetEntryAssembly();
Console.Write(asm.GetName().Name);
Console.Write(" ");
Console.WriteLine(asm.GetName().Version);
Console.WriteLine();
Usage(asm);
return 0;
}
}
private static int Init() {
Config config = new Config();
config.Uploads = new List<Config.UploadItem>();
Console.Write("Host: ");
config.Host = Console.ReadLine();
config.Host = config.Host.StartsWith("http") ? config.Host : "https://" + config.Host;
Console.Write("User: ");
config.User = Console.ReadLine();
Console.Write("Password: ");
config.Password = Console.In.ReadPassword();
Console.Write("Resource prefix: ");
config.Prefix = Console.ReadLine();
if (config.Prefix.Length == 0) {
Console.WriteLine($"Unallowed prefix: '{config.Prefix}'");
}
if (!config.Prefix.EndsWith("_")) {
config.Prefix += "_";
}
if (config.Password == Config.PASSWORD_NULL) {
Console.WriteLine("Password cannot be null");
return 1;
}
Console.WriteLine($"Try to connect to host '{config.Host}'...");
Connector connector = new Connector(config.Host, config.User, config.Password);
if (connector.IsReady) {
Console.WriteLine("Successfully connected to Server");
config.Password = Console.In.Decision("Save the password? (Not secure)") ? config.Password : Config.PASSWORD_NULL;
Config.Write(CONFIG_PATH, config);
} else {
Console.WriteLine($"Connection to the host '{config.Host}' failed");
return 1;
}
return 0;
}
private static int Push() {
if (File.Exists(CONFIG_PATH)) {
if (!Config.Load(CONFIG_PATH, out Config config)) {
Console.WriteLine("Could not load config file. Please re-init the project");
return 1;
}
if (config.Password == Config.PASSWORD_NULL) {
Console.Write("Password: ");
config.Password = Console.In.ReadPassword();
}
Connector connector = new Connector(config.Host, config.User, config.Password);
if (!connector.IsReady) {
Console.WriteLine($"Connection to the host '{config.Host}' failed");
return 1;
}
foreach(Config.UploadItem item in config.Uploads) {
Console.WriteLine($"Try uploading '{item.Src}'");
connector.UploadFile(Path.Combine(Env.CWD, item.Src), item.ResoucrePath, item.Description, item.SolutionUniqueName);
}
} else {
Console.WriteLine("Nothing is initilized use the 'init' commnad");
return 1;
}
return 0;
}
private static int Add(string file, string description, string prefix, string solutionUniqueName) {
string filePath = Path.Combine(Env.CWD, file);
if (!File.Exists(filePath)) {
Console.WriteLine($"Cannot find file '{file}'");
return 1;
}
if (File.Exists(CONFIG_PATH)) {
if (!Config.Load(CONFIG_PATH, out Config config)) {
Console.WriteLine("ERROR: Could not load config file. Please re-init the project");
return 1;
}
file = file.Replace('\\', '/');
file = file.StartsWith("./") ? file.Substring(2) : file;
Config.UploadItem item = new Config.UploadItem() {
Src = file,
ResoucrePath = prefix ?? config.Prefix,
Description = description,
SolutionUniqueName = solutionUniqueName
};
config.Uploads.Add(item);
Config.Write(CONFIG_PATH, config);
Console.WriteLine($"Added '{file}' to upload list");
} else {
Console.WriteLine("Nothing is initilized use the 'init' command");
return 1;
}
return 0;
}
private static int Remove(string file) {
if (File.Exists(CONFIG_PATH)) {
if (!Config.Load(CONFIG_PATH, out Config config)) {
Console.WriteLine("ERROR: Could not load config file. Please re-init the project");
return 1;
}
int toRemove = -1;
file = file.Replace('\\', '/');
file = file.StartsWith("./") ? file.Substring(2) : file;
for (int i = 0; i < config.Uploads.Count; i++)
{
if (config.Uploads[i].Src == file) {
toRemove = i;
break;
}
}
if (toRemove != -1) {
Console.WriteLine($"Successfully removed '{file}'");
config.Uploads.RemoveAt(toRemove);
Config.Write(CONFIG_PATH, config);
} else {
Console.WriteLine($"Could not find file '{file}' in your config");
return 1;
}
} else {
Console.WriteLine("Nothing is initilized use the 'init' command");
return 1;
}
return 0;
}
}
}