public void CreateNewScriptFile(string fileName, string currentDirectory) { _scriptConsole.WriteNormal($"Creating '{fileName}'"); if (!Path.HasExtension(fileName)) { fileName = Path.ChangeExtension(fileName, ".csx"); } var pathToScriptFile = Path.Combine(currentDirectory, fileName); if (!File.Exists(pathToScriptFile)) { var scriptFileTemplate = TemplateLoader.ReadTemplate("helloworld.csx.template"); File.WriteAllText(pathToScriptFile, scriptFileTemplate); _scriptConsole.WriteSuccess($"...'{pathToScriptFile}' [Created]"); } else { _scriptConsole.WriteHighlighted($"...'{pathToScriptFile}' already exists [Skipping]"); } }
public void CreateNewScriptFile(string fileName, string currentDirectory) { _scriptConsole.WriteNormal($"Creating '{fileName}'"); if (!Path.HasExtension(fileName)) { fileName = Path.ChangeExtension(fileName, ".csx"); } var pathToScriptFile = Path.Combine(currentDirectory, fileName); if (!File.Exists(pathToScriptFile)) { var scriptFileTemplate = TemplateLoader.ReadTemplate("helloworld.csx.template"); if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { // add a shebang to set dotnet-script as the interpreter for .csx files // and make sure we are using environment newlines, because shebang won't work with windows cr\lf scriptFileTemplate = $"#!/usr/bin/env dotnet-script" + Environment.NewLine + scriptFileTemplate.Replace("\r\n", Environment.NewLine); } File.WriteAllText(pathToScriptFile, scriptFileTemplate, new UTF8Encoding(false /* Linux shebang can't handle BOM */)); if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { // mark .csx file as executable, this activates the shebang to run dotnet-script as interpreter _commandRunner.Execute($"/bin/chmod", $"+x {pathToScriptFile}"); } _scriptConsole.WriteSuccess($"...'{pathToScriptFile}' [Created]"); } else { _scriptConsole.WriteHighlighted($"...'{pathToScriptFile}' already exists [Skipping]"); } }