static string[] WriteScriptModules(IVariables variables, string parentDirectory, StringBuilder output) { var scriptModules = new List <string>(); foreach (var variableName in variables.GetNames().Where(ScriptVariables.IsLibraryScriptModule)) { if (ScriptVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.PowerShell) { var libraryScriptModuleName = ScriptVariables.GetLibraryScriptModuleName(variableName); var name = "Library_" + new string(libraryScriptModuleName.Where(char.IsLetterOrDigit).ToArray()) + "_" + DateTime.Now.Ticks; var moduleFileName = $"{name}.psm1"; var moduleFilePath = Path.Combine(parentDirectory, moduleFileName); Log.VerboseFormat("Writing script module '{0}' as PowerShell module {1}. This module will be automatically imported - functions will automatically be in scope.", libraryScriptModuleName, moduleFileName, name); var contents = variables.Get(variableName); if (contents == null) { throw new InvalidOperationException($"Value for variable {variableName} could not be found."); } CalamariFileSystem.OverwriteFile(moduleFilePath, contents, Encoding.UTF8); output.AppendLine($"Import-ScriptModule '{libraryScriptModuleName.EscapeSingleQuotedString()}' '{moduleFilePath.EscapeSingleQuotedString()}'"); output.AppendLine(); scriptModules.Add(moduleFilePath); } } return(scriptModules.ToArray()); }
static IEnumerable <string> PrepareScriptModules(IVariables variables, string workingDirectory) { foreach (var variableName in variables.GetNames().Where(ScriptVariables.IsLibraryScriptModule)) { if (ScriptVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.Python) { var libraryScriptModuleName = ScriptVariables.GetLibraryScriptModuleName(variableName); var name = new string(libraryScriptModuleName.Where(x => char.IsLetterOrDigit(x) || x == '_').ToArray()); var moduleFileName = $"{name}.py"; Log.VerboseFormat("Writing script module '{0}' as python module {1}. Import this module via `import {2}`.", libraryScriptModuleName, moduleFileName, name); var moduleFilePath = Path.Combine(workingDirectory, moduleFileName); CalamariFileSystem.OverwriteFile(moduleFilePath, variables.Get(variableName), Encoding.UTF8); yield return(name); } } }
static IEnumerable <string> PrepareScriptModules(IVariables variables, string workingDirectory) { foreach (var variableName in variables.GetNames().Where(ScriptVariables.IsLibraryScriptModule)) { if (ScriptVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.Bash) { var libraryScriptModuleName = ScriptVariables.GetLibraryScriptModuleName(variableName); var name = new string(libraryScriptModuleName.Where(char.IsLetterOrDigit).ToArray()); var moduleFileName = $"{name}.sh"; var moduleFilePath = Path.Combine(workingDirectory, moduleFileName); Log.VerboseFormat("Writing script module '{0}' as bash script {1}. Import this via `source {1}`.", libraryScriptModuleName, moduleFileName, name); Encoding utf8WithoutBom = new UTF8Encoding(false); CalamariFileSystem.OverwriteFile(moduleFilePath, variables.Get(variableName), utf8WithoutBom); EnsureValidUnixFile(moduleFilePath); yield return(moduleFilePath); } } }
static IEnumerable <string> PrepareScriptModules(IVariables variables, string workingDirectory) { foreach (var variableName in variables.GetNames().Where(ScriptVariables.IsLibraryScriptModule)) { if (ScriptVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.FSharp) { var libraryScriptModuleName = ScriptVariables.GetLibraryScriptModuleName(variableName); var name = new string(libraryScriptModuleName.Where(char.IsLetterOrDigit).ToArray()); var moduleFileName = $"{name}.fsx"; var moduleFilePath = Path.Combine(workingDirectory, moduleFileName); Log.VerboseFormat("Writing script module '{0}' as f# module {1}. Import this module via `#load \"{1}\"`.", libraryScriptModuleName, moduleFileName, name); var contents = variables.Get(variableName); if (contents == null) { throw new InvalidOperationException($"Value for variable {variableName} could not be found."); } CalamariFileSystem.OverwriteFile(moduleFilePath, contents, Encoding.UTF8); yield return(moduleFileName); } } }