-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.New.cs
More file actions
50 lines (44 loc) · 1.89 KB
/
Program.New.cs
File metadata and controls
50 lines (44 loc) · 1.89 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
using System;
using System.IO;
using System.Linq;
using System.Text;
namespace CSharpScriptRunner
{
static partial class Program
{
static void CreateNew(string template)
{
var templates = typeof(Program).Assembly.GetManifestResourceNames()
.Where(x => string.Equals(Path.GetExtension(x), ".csx", StringComparison.OrdinalIgnoreCase))
.ToDictionary(x => Path.GetExtension(Path.GetFileNameWithoutExtension(x)).Substring(1), StringComparer.OrdinalIgnoreCase);
if (template == null)
{
Console.Write("Available templates: ");
Console.WriteLine(string.Join(", ", templates.Keys));
Console.Write("Enter the name of the template you wish to create: ");
template = Console.ReadLine().Trim();
}
if (!templates.TryGetValue(template, out var resName))
{
WriteLineError($"The template '{template}' does not exists.", ConsoleColor.Red);
return;
}
var filename = template + ".csx";
if (File.Exists(filename))
{
WriteLineError($"The file '{filename} already exists.", ConsoleColor.Red);
return;
}
using (var res = new StreamReader(typeof(Program).Assembly.GetManifestResourceStream(resName)))
using (var file = new StreamWriter(File.OpenWrite(filename), Encoding.UTF8))
{
var text = res.ReadToEnd();
text = text
.Replace("{PowershellCommand}", PowershellUpdateCommand)
.Replace("{PowershellCommandEscaped}", PowershellUpdateCommand.Replace("\\", "\\\\").Replace("\"", "\\\""));
file.Write(text);
}
WriteLine($"The file '{filename}' was created.", ConsoleColor.Green);
}
}
}