forked from dotnet/try
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
146 lines (122 loc) · 4.42 KB
/
Copy pathProgram.cs
File metadata and controls
146 lines (122 loc) · 4.42 KB
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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MLS.PackageTool
{
public class PackageToolConstants
{
public const string LocateProjectAsset = "locate-project-asset";
public const string LocateWasmAsset = "locate-wasm-asset";
public const string PreparePackage = "prepare-package";
}
public class Program
{
static async Task Main(string[] args)
{
var parser = CommandLineParser.Create(
LocateBuildAssetHandler,
LocateWasmAssetHandler,
Prepare);
await parser.InvokeAsync(args);
}
private static async Task Prepare(IConsole console)
{
await UnzipProjectAsset(console);
}
private static async Task UnzipProjectAsset(IConsole console)
{
var directory = AssemblyDirectory();
var zipFilePath = Path.Combine(directory, "project.zip");
File.Delete(zipFilePath);
string targetDirectory = Path.Combine(directory, "project");
try
{
Directory.Delete(targetDirectory, recursive: true);
}
catch (DirectoryNotFoundException)
{
}
var resource = typeof(Program).Assembly.GetManifestResourceNames()[0];
using (var stream = typeof(Program).Assembly.GetManifestResourceStream(resource))
{
using (var zipFileStream = File.OpenWrite(zipFilePath))
{
await stream.CopyToAsync(zipFileStream);
await zipFileStream.FlushAsync();
}
}
ZipFile.ExtractToDirectory(zipFilePath, targetDirectory);
File.Delete(zipFilePath);
}
public static void LocateBuildAssetHandler(IConsole console)
{
console.Out.WriteLine(BuildDirectoryLocation());
}
public static void LocateWasmAssetHandler(IConsole console)
{
console.Out.WriteLine(WasmDirectoryLocation());
}
public static string BuildDirectoryLocation() =>
Path.Combine(Path.GetDirectoryName(AssemblyLocation()), "project", "build");
public static string WasmDirectoryLocation() =>
Path.Combine(Path.GetDirectoryName(AssemblyLocation()), "project", "wasm", "MLS.Blazor", "dist");
public static string AssemblyLocation()
{
return typeof(Program).Assembly.Location;
}
public static string AssemblyDirectory()
{
return Path.GetDirectoryName(AssemblyLocation());
}
}
public class CommandLineParser
{
public static Parser Create(
Action<IConsole> getBuildAsset,
Action<IConsole> getWasmAsset,
Func<IConsole, Task> prepare)
{
var rootCommand = new RootCommand
{
LocateBuildAsset(),
LocateWasmAsset(),
PreparePackage()
};
var parser = new CommandLineBuilder(rootCommand)
.UseDefaults()
.Build();
Command LocateBuildAsset()
{
return new Command(PackageToolConstants.LocateProjectAsset)
{
Handler = CommandHandler.Create(getBuildAsset)
};
}
Command LocateWasmAsset()
{
return new Command(PackageToolConstants.LocateWasmAsset)
{
Handler = CommandHandler.Create(getWasmAsset)
};
}
Command PreparePackage()
{
return new Command(PackageToolConstants.PreparePackage, "Prepares the packages")
{
Handler = CommandHandler.Create(prepare)
};
}
return parser;
}
}
}