Skip to content

Commit a291fdf

Browse files
committed
Added init-vscode argument (for debugging)
1 parent b5b3798 commit a291fdf

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

CSharpScriptRunner.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<EmbeddedResource Include="Templates/*.csx" />
13+
<EmbeddedResource Include="Templates/**/*.*" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

Program.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static class Verbs
3838
public const string Install = "install";
3939
public const string New = "new";
4040
public const string ClearCache = "clear-cache";
41+
public const string InitVSCode = "init-vscode";
4142
}
4243

4344
[STAThread]
@@ -57,7 +58,8 @@ static void Main(string[] args)
5758
{
5859
case Verbs.Install: Install(); break;
5960
case Verbs.New: CreateNew(args.Length > 1 ? args[1] : null); break;
60-
case Verbs.ClearCache: ClearCache(); break;
61+
case Verbs.ClearCache: ClearCache(); break;
62+
case Verbs.InitVSCode: InitVSCode(); break;
6163
default: RunScript(args); break;
6264
}
6365
}
@@ -251,6 +253,13 @@ static IEnumerable<string> LoadPackages(string scriptPath, out InteractiveAssemb
251253

252254
static void RunScript(string[] args)
253255
{
256+
if (!File.Exists(args[0]))
257+
{
258+
WriteLine($"Script file '{args[0]}' does not exist.", ConsoleColor.Red);
259+
System.Threading.Thread.Sleep(5000);
260+
return;
261+
}
262+
254263
var scriptPath = Path.GetFullPath(args[0]);
255264
var runtimeExt = Path.GetExtension(Path.GetFileNameWithoutExtension(scriptPath));
256265
if (!string.IsNullOrEmpty(runtimeExt))
@@ -390,6 +399,40 @@ static void ClearCache()
390399
WriteLine("Cache cleared", ConsoleColor.Green);
391400
}
392401

402+
static void InitVSCode()
403+
{
404+
const string VSCodeDir = ".vscode";
405+
const string Filter = ".Templates.vscode.";
406+
407+
var templates = typeof(Program).Assembly.GetManifestResourceNames()
408+
.Select(x => (i: x.IndexOf(Filter), s: x))
409+
.Where(x => x.i > -1)
410+
.Select(x => (name: x.s, File: Path.Combine(VSCodeDir, x.s.Substring(x.i + Filter.Length))));
411+
412+
var dir = new DirectoryInfo(VSCodeDir);
413+
if (!dir.Exists)
414+
{
415+
dir.Create();
416+
// dir.Attributes |= FileAttributes.Hidden;
417+
}
418+
419+
foreach (var template in templates)
420+
{
421+
if (File.Exists(template.File))
422+
{
423+
WriteLine($"File '{template.File}' already exists.", ConsoleColor.Yellow);
424+
continue;
425+
}
426+
427+
using (var stream = typeof(Program).Assembly.GetManifestResourceStream(template.name))
428+
using (var file = File.OpenWrite(template.File))
429+
{
430+
WriteLine($"Creating file '{template.File}'...", ConsoleColor.Green);
431+
stream.CopyTo(file);
432+
}
433+
}
434+
}
435+
393436
static void WriteLine(string line, ConsoleColor color = (ConsoleColor)(-1))
394437
{
395438
if ((int)color > -1)

Templates/vscode/launch.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
8+
{
9+
"name": ".NET Core Launch (console)",
10+
"type": "coreclr",
11+
"request": "launch",
12+
"program": "CSharpScriptRunner",
13+
"args": [ "${file}" ],
14+
"cwd": "${workspaceFolder}",
15+
"console": "internalConsole",
16+
"stopAtEntry": false,
17+
"requireExactSource": false
18+
},
19+
{
20+
"name": ".NET Core Attach",
21+
"type": "coreclr",
22+
"request": "attach",
23+
"processId": "${command:pickProcess}",
24+
"requireExactSource": false
25+
}
26+
]
27+
}

0 commit comments

Comments
 (0)