Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d862c0d

Browse filesBrowse files
authored
Merge pull request #467 from filipw/feature/unified-launch-config
Unified launch config
2 parents 139ba2f + 75872aa commit d862c0d
Copy full SHA for d862c0d

File tree

4 files changed

+113
-31
lines changed
Filter options

4 files changed

+113
-31
lines changed

‎src/Dotnet.Script.Core/Dotnet.Script.Core.csproj

Copy file name to clipboardExpand all lines: src/Dotnet.Script.Core/Dotnet.Script.Core.csproj
+1-11Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,7 @@
1919
</PropertyGroup>
2020

2121
<ItemGroup>
22-
<None Remove="Templates\helloworld.csx.template" />
23-
<None Remove="Templates\launch.json.template" />
24-
<None Remove="Templates\omnisharp.json.template" />
25-
<None Remove="Templates\program.publish.template" />
26-
</ItemGroup>
27-
28-
<ItemGroup>
29-
<EmbeddedResource Include="Templates\helloworld.csx.template" />
30-
<EmbeddedResource Include="Templates\launch.json.template" />
31-
<EmbeddedResource Include="Templates\omnisharp.json.template" />
32-
<EmbeddedResource Include="Templates\program.publish.template" />
22+
<EmbeddedResource Include="**/*.template" />
3323
</ItemGroup>
3424

3525
<ItemGroup>

‎src/Dotnet.Script.Core/Scaffolder.cs

Copy file name to clipboardExpand all lines: src/Dotnet.Script.Core/Scaffolder.cs
+48-15Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@ namespace Dotnet.Script.Core
1414
{
1515
public class Scaffolder
1616
{
17-
private ScriptEnvironment _scriptEnvironment;
17+
private readonly ScriptEnvironment _scriptEnvironment;
1818
private const string DefaultScriptFileName = "main.csx";
19-
private ScriptConsole _scriptConsole = ScriptConsole.Default;
20-
private CommandRunner _commandRunner;
19+
private readonly ScriptConsole _scriptConsole;
20+
private readonly CommandRunner _commandRunner;
2121

22-
public Scaffolder(LogFactory logFactory)
22+
public Scaffolder(LogFactory logFactory) : this(logFactory, ScriptConsole.Default, ScriptEnvironment.Default)
23+
{
24+
}
25+
26+
public Scaffolder(LogFactory logFactory, ScriptConsole scriptConsole, ScriptEnvironment scriptEnvironment)
2327
{
2428
_commandRunner = new CommandRunner(logFactory);
25-
_scriptEnvironment = ScriptEnvironment.Default;
29+
_scriptConsole = scriptConsole;
30+
_scriptEnvironment = scriptEnvironment;
2631
}
2732

2833
public void InitializerFolder(string fileName, string currentWorkingDirectory)
@@ -75,7 +80,7 @@ public void RegisterFileHandler()
7580
{
7681
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
7782
{
78-
// register dotnet-script as the tool to process .csx files
83+
// register dotnet-script as the tool to process .csx files
7984
_commandRunner.Execute("reg", @"add HKCU\Software\classes\.csx /f /ve /t REG_SZ /d dotnetscript");
8085
_commandRunner.Execute("reg", $@"add HKCU\Software\Classes\dotnetscript\Shell\Open\Command /f /ve /t REG_EXPAND_SZ /d ""\""%ProgramFiles%\dotnet\dotnet.exe\"" script \""%1\"" -- %*""");
8186
}
@@ -136,29 +141,57 @@ private void CreateLaunchConfiguration(string currentWorkingDirectory)
136141
_scriptConsole.WriteNormal("Creating VS Code launch configuration file");
137142
string pathToLaunchFile = Path.Combine(vsCodeDirectory, "launch.json");
138143
string installLocation = _scriptEnvironment.InstallLocation;
144+
bool isInstalledAsGlobalTool = installLocation.Contains(".dotnet/tools", StringComparison.OrdinalIgnoreCase);
139145
string dotnetScriptPath = Path.Combine(installLocation, "dotnet-script.dll").Replace(@"\", "/");
146+
string launchFileContent = null;
140147
if (!File.Exists(pathToLaunchFile))
141148
{
142-
string launchFileTemplate = TemplateLoader.ReadTemplate("launch.json.template");
143-
string launchFileContent = launchFileTemplate.Replace("PATH_TO_DOTNET-SCRIPT", dotnetScriptPath);
149+
if (isInstalledAsGlobalTool)
150+
{
151+
launchFileContent = TemplateLoader.ReadTemplate("globaltool.launch.json.template");
152+
}
153+
else
154+
{
155+
string launchFileTemplate = TemplateLoader.ReadTemplate("launch.json.template");
156+
launchFileContent = launchFileTemplate.Replace("PATH_TO_DOTNET-SCRIPT", dotnetScriptPath);
157+
}
158+
144159
File.WriteAllText(pathToLaunchFile, launchFileContent);
145160
_scriptConsole.WriteSuccess($"...'{pathToLaunchFile}' [Created]");
146161
}
147162
else
148163
{
149164
_scriptConsole.WriteHighlighted($"...'{pathToLaunchFile}' already exists' [Skipping]");
150-
var launchFileContent = File.ReadAllText(pathToLaunchFile);
151-
string pattern = @"^(\s*"")(.*dotnet-script.dll)("").*$";
152-
if (Regex.IsMatch(launchFileContent, pattern, RegexOptions.Multiline))
165+
launchFileContent = File.ReadAllText(pathToLaunchFile);
166+
if (isInstalledAsGlobalTool)
153167
{
154-
var newLaunchFileContent = Regex.Replace(launchFileContent, pattern, $"$1{dotnetScriptPath}$3", RegexOptions.Multiline);
155-
if (launchFileContent != newLaunchFileContent)
168+
var template = TemplateLoader.ReadTemplate("globaltool.launch.json.template");
169+
if (template != launchFileContent)
156170
{
157-
_scriptConsole.WriteHighlighted($"...Fixed path to dotnet-script: '{dotnetScriptPath}' [Updated]");
158-
File.WriteAllText(pathToLaunchFile, newLaunchFileContent);
171+
File.WriteAllText(pathToLaunchFile, template);
172+
_scriptConsole.WriteHighlighted("...Use global tool launch config [Updated]");
159173
}
160174
}
175+
else
176+
{
177+
string pattern = @"^(\s*"")(.*dotnet-script.dll)(""\s*,).*$";
178+
if (Regex.IsMatch(launchFileContent, pattern, RegexOptions.Multiline))
179+
{
180+
var newLaunchFileContent = Regex.Replace(launchFileContent, pattern, $"$1{dotnetScriptPath}$3", RegexOptions.Multiline);
181+
if (launchFileContent != newLaunchFileContent)
182+
{
183+
_scriptConsole.WriteHighlighted($"...Fixed path to dotnet-script: '{dotnetScriptPath}' [Updated]");
184+
File.WriteAllText(pathToLaunchFile, newLaunchFileContent);
185+
}
186+
}
187+
}
188+
161189
}
162190
}
191+
192+
private bool IsInstalledAsGlobalTool()
193+
{
194+
return _scriptEnvironment.InstallLocation.Contains(".dotnet/tools", StringComparison.OrdinalIgnoreCase);
195+
}
163196
}
164197
}
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": ".NET Script Debug",
6+
"type": "coreclr",
7+
"request": "launch",
8+
"program": "${env:HOME}/.dotnet/tools/dotnet-script",
9+
"args": ["${file}"],
10+
"windows": {
11+
"program": "${env:USERPROFILE}/.dotnet/tools/dotnet-script.exe",
12+
},
13+
"cwd": "${workspaceFolder}",
14+
"stopAtEntry": true,
15+
}
16+
]
17+
}

‎src/Dotnet.Script.Tests/ScaffoldingTests.cs

Copy file name to clipboardExpand all lines: src/Dotnet.Script.Tests/ScaffoldingTests.cs
+47-5Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
using Dotnet.Script.DependencyModel.Environment;
1+
using Dotnet.Script.Core;
2+
using Dotnet.Script.DependencyModel.Environment;
23
using Dotnet.Script.Shared.Tests;
34
using Newtonsoft.Json.Linq;
5+
using System;
46
using System.IO;
5-
using System.Text.RegularExpressions;
7+
using System.Reflection;
68
using Xunit;
7-
9+
using Xunit.Abstractions;
810

911
namespace Dotnet.Script.Tests
1012
{
1113
public class ScaffoldingTests
1214
{
1315
private readonly ScriptEnvironment _scriptEnvironment;
1416

15-
public ScaffoldingTests()
17+
public ScaffoldingTests(ITestOutputHelper testOutputHelper)
1618
{
1719
_scriptEnvironment = ScriptEnvironment.Default;
20+
testOutputHelper.Capture();
1821
}
1922

2023
[Fact]
@@ -168,7 +171,7 @@ public void ShouldUpdatePathToDotnetScript()
168171
var pathToLaunchConfiguration = Path.Combine(scriptFolder.Path, ".vscode/launch.json");
169172
var config = JObject.Parse(File.ReadAllText(pathToLaunchConfiguration));
170173

171-
config.SelectToken("configurations[0].args[1]").Replace("InvalidPath/dotnet-script.dll,");
174+
config.SelectToken("configurations[0].args[1]").Replace("InvalidPath/dotnet-script.dll");
172175

173176
File.WriteAllText(pathToLaunchConfiguration, config.ToString());
174177

@@ -178,5 +181,44 @@ public void ShouldUpdatePathToDotnetScript()
178181
Assert.NotEqual("InvalidPath/dotnet-script.dll", config.SelectToken("configurations[0].args[1]").Value<string>());
179182
}
180183
}
184+
185+
[Fact]
186+
public void ShouldCreateUnifiedLaunchFileWhenInstalledAsGlobalTool()
187+
{
188+
Scaffolder scaffolder = CreateTestScaffolder("somefolder/.dotnet/tools/dotnet-script");
189+
190+
using (var scriptFolder = new DisposableFolder())
191+
{
192+
scaffolder.InitializerFolder("main.csx", scriptFolder.Path);
193+
var fileContent = File.ReadAllText(Path.Combine(scriptFolder.Path, ".vscode", "launch.json"));
194+
Assert.Contains("{env:HOME}/.dotnet/tools/dotnet-script", fileContent);
195+
}
196+
}
197+
198+
[Fact]
199+
public void ShouldUpdateToUnifiedLaunchFileWhenInstalledAsGlobalTool()
200+
{
201+
Scaffolder scaffolder = CreateTestScaffolder("some-install-folder");
202+
Scaffolder globalToolScaffolder = CreateTestScaffolder("somefolder/.dotnet/tools/dotnet-script");
203+
using (var scriptFolder = new DisposableFolder())
204+
{
205+
scaffolder.InitializerFolder("main.csx", scriptFolder.Path);
206+
var fileContent = File.ReadAllText(Path.Combine(scriptFolder.Path, ".vscode", "launch.json"));
207+
Assert.Contains("some-install-folder", fileContent);
208+
globalToolScaffolder.InitializerFolder("main.csx", scriptFolder.Path);
209+
fileContent = File.ReadAllText(Path.Combine(scriptFolder.Path, ".vscode", "launch.json"));
210+
Assert.Contains("{env:HOME}/.dotnet/tools/dotnet-script", fileContent);
211+
}
212+
}
213+
214+
private static Scaffolder CreateTestScaffolder(string installLocation)
215+
{
216+
var scriptEnvironment = (ScriptEnvironment)Activator.CreateInstance(typeof(ScriptEnvironment), nonPublic: true);
217+
var installLocationField = typeof(ScriptEnvironment).GetField("_installLocation", BindingFlags.NonPublic | BindingFlags.Instance);
218+
installLocationField.SetValue(scriptEnvironment, new Lazy<string>(() => installLocation));
219+
var scriptConsole = new ScriptConsole(StringWriter.Null, StringReader.Null, StreamWriter.Null);
220+
var scaffolder = new Scaffolder(TestOutputHelper.CreateTestLogFactory(), scriptConsole, scriptEnvironment);
221+
return scaffolder;
222+
}
181223
}
182224
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.