示例#1
0
        private bool TryGetScriptFromVariables(out string scriptBody, out string scriptFileName, out ScriptSyntax syntax)
        {
            scriptBody = variables.Get(SpecialVariables.Action.Script.ScriptBody);
            if (WasProvided(scriptBody))
            {
                scriptFileName = variables.Get(SpecialVariables.Action.Script.ScriptFileName);
                if (WasProvided(scriptFileName))
                {
                    syntax = ScriptTypeExtensions.FileNameToScriptType(scriptFileName);

                    return(true);
                }

                var scriptSyntax = variables.Get(SpecialVariables.Action.Script.Syntax);
                if (scriptSyntax == null)
                {
                    syntax = scriptEngine.GetSupportedTypes().FirstOrDefault();
                    Log.Warn($"No script syntax provided. Defaulting to first known supported type {syntax}");
                }
                else if (!Enum.TryParse(scriptSyntax, out syntax))
                {
                    throw new CommandException($"Unknown script syntax `{scriptSyntax}` provided");
                }

                scriptFileName = "Script." + syntax.FileExtension();
                return(true);
            }

            // Try get any supported script body variable
            foreach (var supportedSyntax in scriptEngine.GetSupportedTypes())
            {
                scriptBody = variables.Get(SpecialVariables.Action.Script.ScriptBodyBySyntax(supportedSyntax));
                if (scriptBody == null)
                {
                    continue;
                }

                scriptFileName = "Script." + supportedSyntax.FileExtension();
                syntax         = supportedSyntax;
                return(true);
            }

            scriptBody     = null;
            syntax         = 0;
            scriptFileName = null;
            return(false);
        }
示例#2
0
        protected (CalamariResult result, VariableDictionary variables) RunScript(string scriptName,
                                                                                  Dictionary <string, string> additionalVariables  = null,
                                                                                  Dictionary <string, string> additionalParameters = null,
                                                                                  string sensitiveVariablesPassword = null)
        {
            var variablesFile = Path.GetTempFileName();
            var variables     = new VariableDictionary();

            variables.Set(SpecialVariables.Action.Script.ScriptFileName, scriptName);
            variables.Set(SpecialVariables.Action.Script.ScriptBody, File.ReadAllText(GetFixtureResouce("Scripts", scriptName)));
            variables.Set(SpecialVariables.Action.Script.Syntax, ScriptTypeExtensions.FileNameToScriptType(scriptName).ToString());

            additionalVariables?.ToList().ForEach(v => variables[v.Key] = v.Value);

            using (new TemporaryFile(variablesFile))
            {
                var cmdBase = Calamari()
                              .Action("run-script");

                if (sensitiveVariablesPassword == null)
                {
                    variables.Save(variablesFile);
                    cmdBase = cmdBase.Argument("variables", variablesFile);
                }
                else
                {
                    variables.SaveEncrypted(sensitiveVariablesPassword, variablesFile);
                    cmdBase = cmdBase.Argument("sensitiveVariables", variablesFile)
                              .Argument("sensitiveVariablesPassword", sensitiveVariablesPassword);
                }

                cmdBase = (additionalParameters ?? new Dictionary <string, string>()).Aggregate(cmdBase, (cmd, param) => cmd.Argument(param.Key, param.Value));

                var output = Invoke(cmdBase, variables);

                return(output, variables);
            }
        }