Skip to content

Commit 7c1773a

Browse files
Finished ez program
1 parent ca5b464 commit 7c1773a

1 file changed

Lines changed: 96 additions & 2 deletions

File tree

ez/Program.cs

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using EZCodeLanguage;
2+
using System.IO.Compression;
3+
using System.Net;
24

35
static class EZ
46
{
57
public static int error_code = 0;
68
public static void Main(string[] args)
79
{
8-
args = ["new", "package"];
910
if (args.Length == 0)
1011
{
1112
args = ["help"];
@@ -62,7 +63,9 @@ print Hello World
6263
"project.ezcode"
6364
],
6465
"Configuration":{
65-
"GlobalPackages":"main"
66+
"GlobalPackages":[
67+
"main"
68+
]
6669
}
6770
}
6871
""";
@@ -135,10 +138,87 @@ class example-object {
135138

136139
case "i":
137140
case "install":
141+
if (args.Length < 2)
142+
{
143+
Console.WriteLine("Error, Expected package name");
144+
error_code = 1;
145+
break;
146+
}
147+
string package = args[1];
148+
149+
if (args.Length > 2)
150+
{
151+
Console.WriteLine($"Error, unexpected '{string.Join(" ", args.Skip(1))}'");
152+
error_code = 1;
153+
break;
154+
}
155+
156+
Console.ForegroundColor = ConsoleColor.Green;
157+
Console.WriteLine($"Starting Install of '{package}' package");
158+
Console.ResetColor();
159+
160+
string downloadFilePath = Path.Combine(Package.PackagesDirectory, package),
161+
downloadUrl = $"https://github.com/EZCodeLanguage/Packages/releases/download/{package}-package/{package}.zip";
162+
163+
try
164+
{
165+
InstallAndExtract(downloadUrl, downloadFilePath);
166+
167+
Console.ForegroundColor = ConsoleColor.Green;
168+
Console.WriteLine("Install Complete");
169+
}
170+
catch (Exception ex)
171+
{
172+
error_code = 1;
173+
Console.ForegroundColor = ConsoleColor.DarkRed;
174+
Console.WriteLine(ex.Message);
175+
}
176+
Console.ResetColor();
138177
break;
139178

140179
case "u":
141180
case "uninstall":
181+
if (args.Length < 2)
182+
{
183+
Console.WriteLine("Error, Expected package name");
184+
error_code = 1;
185+
break;
186+
}
187+
package = args[1];
188+
189+
if (args.Length > 2)
190+
{
191+
Console.WriteLine($"Error, unexpected '{string.Join(" ", args.Skip(1))}'");
192+
error_code = 1;
193+
break;
194+
}
195+
196+
Console.ForegroundColor = ConsoleColor.Green;
197+
Console.WriteLine($"Starting uninstalling of '{package}' package");
198+
Console.ResetColor();
199+
200+
try
201+
{
202+
downloadFilePath = Path.Combine(Package.PackagesDirectory, package);
203+
if (!Directory.Exists(downloadFilePath))
204+
{
205+
throw new Exception($"Error, Package '{package}' can't be found");
206+
}
207+
else
208+
{
209+
Directory.Delete(downloadFilePath, true);
210+
}
211+
212+
Console.ForegroundColor = ConsoleColor.Green;
213+
Console.WriteLine("Uninstall Complete");
214+
}
215+
catch (Exception ex)
216+
{
217+
error_code = 1;
218+
Console.ForegroundColor = ConsoleColor.DarkRed;
219+
Console.WriteLine(ex.Message);
220+
}
221+
Console.ResetColor();
142222
break;
143223

144224
case "version":
@@ -190,6 +270,20 @@ class example-object {
190270

191271
Environment.Exit(error_code);
192272
}
273+
static void InstallAndExtract(string downloadUrl, string downloadFilePath)
274+
{
275+
using (WebClient webClient = new WebClient())
276+
{
277+
string tempDownloadFilePath = downloadFilePath + "_.zip";
278+
279+
webClient.DownloadFile(downloadUrl, tempDownloadFilePath);
280+
281+
ZipFile.ExtractToDirectory(tempDownloadFilePath, downloadFilePath, true);
282+
283+
File.Delete(tempDownloadFilePath);
284+
}
285+
}
286+
193287
static void EZEnvironment(string[] contents = null)
194288
{
195289
string help = "Commands: " +

0 commit comments

Comments
 (0)