|
2 | 2 | using System.IO;
|
3 | 3 | using System.Linq;
|
4 | 4 | using System.Reflection;
|
| 5 | +using System.Text.RegularExpressions; |
5 | 6 | using Dotnet.Script.Core.Templates;
|
6 | 7 | using Dotnet.Script.DependencyModel.Environment;
|
| 8 | +using Dotnet.Script.DependencyModel.ProjectSystem; |
7 | 9 | using Newtonsoft.Json.Linq;
|
8 | 10 |
|
9 | 11 | namespace Dotnet.Script.Core
|
10 | 12 | {
|
11 | 13 | public class Scaffolder
|
12 | 14 | {
|
13 | 15 | private ScriptEnvironment _scriptEnvironment;
|
| 16 | + private readonly ScriptLogger _logger; |
| 17 | + private const string DefaultScriptFileName = "main.csx"; |
14 | 18 |
|
15 |
| - public Scaffolder() |
| 19 | + public Scaffolder(ScriptLogger logger) |
16 | 20 | {
|
17 | 21 | _scriptEnvironment = ScriptEnvironment.Default;
|
| 22 | + _logger = logger; |
18 | 23 | }
|
19 | 24 |
|
20 |
| - public void InitializerFolder(string fileName) |
| 25 | + public void InitializerFolder(string fileName, string currentWorkingDirectory) |
| 26 | + { |
| 27 | + CreateLaunchConfiguration(currentWorkingDirectory); |
| 28 | + CreateOmniSharpConfigurationFile(currentWorkingDirectory); |
| 29 | + CreateScriptFile(fileName, currentWorkingDirectory); |
| 30 | + } |
| 31 | + |
| 32 | + public void CreateNewScriptFile(string fileName, string currentDirectory) |
21 | 33 | {
|
22 |
| - string currentDirectory = Directory.GetCurrentDirectory(); |
23 |
| - string vsCodeDirectory = Path.Combine(currentDirectory, ".vscode"); |
24 |
| - if (!Directory.Exists(vsCodeDirectory)) |
| 34 | + _logger.Log($"Creating '{fileName}'"); |
| 35 | + var pathToScriptFile = Path.Combine(currentDirectory, fileName); |
| 36 | + if (!File.Exists(pathToScriptFile)) |
25 | 37 | {
|
26 |
| - Directory.CreateDirectory(vsCodeDirectory); |
| 38 | + var scriptFileTemplate = TemplateLoader.ReadTemplate("helloworld.csx.template"); |
| 39 | + FileUtils.WriteFile(pathToScriptFile, scriptFileTemplate); |
| 40 | + _logger.Log($"...'{pathToScriptFile}' [Created]"); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + _logger.Log($"...'{pathToScriptFile}' already exists [Skipping]"); |
27 | 45 | }
|
| 46 | + } |
28 | 47 |
|
29 |
| - string pathToLaunchFile = Path.Combine(vsCodeDirectory, "launch.json"); |
30 |
| - if (!File.Exists(pathToLaunchFile)) |
| 48 | + private void CreateScriptFile(string fileName, string currentWorkingDirectory) |
| 49 | + { |
| 50 | + if (string.IsNullOrWhiteSpace(fileName)) |
31 | 51 | {
|
32 |
| - string installLocation = _scriptEnvironment.InstallLocation; |
33 |
| - string dotnetScriptPath = Path.Combine(installLocation, "dotnet-script.dll").Replace(@"\", "/"); |
34 |
| - |
35 |
| - string lauchFileTemplate = TemplateLoader.ReadTemplate("launch.json.template"); |
| 52 | + CreateDefaultScriptFile(currentWorkingDirectory); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + CreateNewScriptFile(fileName,currentWorkingDirectory); |
| 57 | + } |
| 58 | + } |
36 | 59 |
|
37 |
| - string launchFileContent = lauchFileTemplate.Replace("PATH_TO_DOTNET-SCRIPT", dotnetScriptPath); |
38 |
| - WriteFile(pathToLaunchFile, launchFileContent); |
| 60 | + private void CreateDefaultScriptFile(string currentWorkingDirectory) |
| 61 | + { |
| 62 | + _logger.Log($"Creating default script file '{DefaultScriptFileName}'"); |
| 63 | + if (Directory.GetFiles(currentWorkingDirectory, "*.csx").Any()) |
| 64 | + { |
| 65 | + _logger.Log("...Folder already contains one or more script files [Skipping]"); |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + CreateNewScriptFile(DefaultScriptFileName, currentWorkingDirectory); |
39 | 70 | }
|
40 |
| - |
41 |
| - string pathToOmniSharpJson = Path.Combine(currentDirectory, "omnisharp.json"); |
| 71 | + } |
| 72 | + |
| 73 | + private void CreateOmniSharpConfigurationFile(string currentWorkingDirectory) |
| 74 | + { |
| 75 | + _logger.Log("Creating OmniSharp configuration file"); |
| 76 | + string pathToOmniSharpJson = Path.Combine(currentWorkingDirectory, "omnisharp.json"); |
42 | 77 | if (!File.Exists(pathToOmniSharpJson))
|
43 | 78 | {
|
44 | 79 | var omniSharpFileTemplate = TemplateLoader.ReadTemplate("omnisharp.json.template");
|
45 | 80 | JObject settings = JObject.Parse(omniSharpFileTemplate);
|
46 | 81 | settings["script"]["defaultTargetFramework"] = _scriptEnvironment.TargetFramework;
|
47 |
| - WriteFile(pathToOmniSharpJson, settings.ToString()); |
| 82 | + FileUtils.WriteFile(pathToOmniSharpJson, settings.ToString()); |
| 83 | + _logger.Log($"...'{pathToOmniSharpJson}' [Created]"); |
48 | 84 | }
|
49 |
| - |
50 |
| - if (Directory.GetFiles(currentDirectory, "*.csx").Any() |
51 |
| - && string.IsNullOrWhiteSpace(fileName)) |
| 85 | + else |
52 | 86 | {
|
53 |
| - return; |
| 87 | + _logger.Log($"...'{pathToOmniSharpJson} already exists' [Skipping]"); |
54 | 88 | }
|
55 |
| - |
56 |
| - CreateNewScriptFile(fileName ?? "main.csx"); |
57 | 89 | }
|
58 | 90 |
|
59 |
| - public void CreateNewScriptFile(string file) |
| 91 | + private void CreateLaunchConfiguration(string currentWorkingDirectory) |
60 | 92 | {
|
61 |
| - string currentDirectory = Directory.GetCurrentDirectory(); |
62 |
| - var pathToScriptFile = Path.Combine(currentDirectory, file); |
63 |
| - if (!File.Exists(pathToScriptFile)) |
| 93 | + string vsCodeDirectory = Path.Combine(currentWorkingDirectory, ".vscode"); |
| 94 | + if (!Directory.Exists(vsCodeDirectory)) |
64 | 95 | {
|
65 |
| - var scriptFileTemplate = TemplateLoader.ReadTemplate("helloworld.csx.template"); |
66 |
| - WriteFile(pathToScriptFile, scriptFileTemplate); |
| 96 | + Directory.CreateDirectory(vsCodeDirectory); |
67 | 97 | }
|
68 |
| - } |
69 | 98 |
|
70 |
| - private void WriteFile(string path, string content) |
71 |
| - { |
72 |
| - using (var fileStream = new FileStream(path, FileMode.Create)) |
| 99 | + _logger.Log("Creating VS Code launch configuration file"); |
| 100 | + string pathToLaunchFile = Path.Combine(vsCodeDirectory, "launch.json"); |
| 101 | + string installLocation = _scriptEnvironment.InstallLocation; |
| 102 | + string dotnetScriptPath = Path.Combine(installLocation, "dotnet-script.dll").Replace(@"\", "/"); |
| 103 | + if (!File.Exists(pathToLaunchFile)) |
| 104 | + { |
| 105 | + string lauchFileTemplate = TemplateLoader.ReadTemplate("launch.json.template"); |
| 106 | + string launchFileContent = lauchFileTemplate.Replace("PATH_TO_DOTNET-SCRIPT", dotnetScriptPath); |
| 107 | + FileUtils.WriteFile(pathToLaunchFile, launchFileContent); |
| 108 | + _logger.Log($"...'{pathToLaunchFile}' [Created]"); |
| 109 | + } |
| 110 | + else |
73 | 111 | {
|
74 |
| - using (var streamWriter = new StreamWriter(fileStream)) |
| 112 | + _logger.Log($"...'{pathToLaunchFile}' already exists' [Skipping]"); |
| 113 | + var launchFileContent = FileUtils.ReadFile(pathToLaunchFile); |
| 114 | + string pattern = @"^(\s*"")(.*dotnet-script.dll)("").*$"; |
| 115 | + if (Regex.IsMatch(launchFileContent, pattern, RegexOptions.Multiline)) |
75 | 116 | {
|
76 |
| - streamWriter.Write(content); |
| 117 | + var newLaunchFileContent = Regex.Replace(launchFileContent, pattern, $"$1{dotnetScriptPath}$3", RegexOptions.Multiline); |
| 118 | + if (launchFileContent != newLaunchFileContent) |
| 119 | + { |
| 120 | + _logger.Log($"...Fixed path to dotnet-script: '{dotnetScriptPath}' [Updated]"); |
| 121 | + FileUtils.WriteFile(pathToLaunchFile, newLaunchFileContent); |
| 122 | + } |
77 | 123 | }
|
78 | 124 | }
|
79 | 125 | }
|
|
0 commit comments