-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathDotnetScriptExecutor.cs
More file actions
74 lines (61 loc) · 3.49 KB
/
DotnetScriptExecutor.cs
File metadata and controls
74 lines (61 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Calamari.Common.Features.Processes;
using Calamari.Common.Plumbing;
using Calamari.Common.Plumbing.Logging;
using Calamari.Common.Plumbing.Variables;
namespace Calamari.Common.Features.Scripting.DotnetScript
{
public class DotnetScriptExecutor : ScriptExecutor
{
readonly ICommandLineRunner commandLineRunner;
readonly DotnetScriptCompilationWarningOutputSink outputSink;
public DotnetScriptExecutor(ICommandLineRunner commandLineRunner, ILog log, DotnetScriptCompilationWarningOutputSink outputSink): base(log)
{
this.commandLineRunner = commandLineRunner;
this.outputSink = outputSink;
}
protected override IEnumerable<ScriptExecution> PrepareExecution(Script script,
IVariables variables,
Dictionary<string, string>? environmentVars = null)
{
var workingDirectory = Path.GetDirectoryName(script.File);
var localDotnetScriptPath = DotnetScriptBootstrapper.DotnetScriptPath(commandLineRunner, environmentVars);
var bundledExecutable = DotnetScriptBootstrapper.FindBundledExecutable();
var executable = GetExecutable(localDotnetScriptPath, bundledExecutable);
LogExecutionInfo(localDotnetScriptPath);
var configurationFile = DotnetScriptBootstrapper.PrepareConfigurationFile(workingDirectory, variables);
var (bootstrapFile, otherTemporaryFiles) = DotnetScriptBootstrapper.PrepareBootstrapFile(script.File, configurationFile, workingDirectory, variables);
var arguments = DotnetScriptBootstrapper.FormatCommandArguments(bootstrapFile, script.Parameters);
bool.TryParse(variables.Get("Octopus.Action.Script.CSharp.BypassIsolation", "false"), out var bypassDotnetScriptIsolation);
var cli = CreateCommandLineInvocation(executable, arguments, !string.IsNullOrWhiteSpace(localDotnetScriptPath));
cli.EnvironmentVars = environmentVars;
cli.WorkingDirectory = workingDirectory;
cli.Isolate = !bypassDotnetScriptIsolation;
cli.UseUTF8 = true; /* TODO Make UTF-8 encoding an opt-out setting */
cli.AdditionalInvocationOutputSink = outputSink;
yield return new ScriptExecution(cli, otherTemporaryFiles.Concat(new[] { bootstrapFile, configurationFile }));
}
private string GetExecutable(string? localDotnetScriptPath, string bundledExecutable)
{
return string.IsNullOrWhiteSpace(localDotnetScriptPath)
? bundledExecutable
: localDotnetScriptPath;
}
void LogExecutionInfo(string? localDotnetScriptPath)
{
Log.Verbose(string.IsNullOrEmpty(localDotnetScriptPath)
? "dotnet-script was not found, executing the bundled version"
: $"Found dotnet-script executable at {localDotnetScriptPath}");
}
CommandLineInvocation CreateCommandLineInvocation(string executable, string arguments, bool hasDotnetToolOnPath)
{
var extension = Path.GetExtension(executable);
return (CalamariEnvironment.IsRunningOnWindows || (hasDotnetToolOnPath && extension != ".dll"))
? new CommandLineInvocation(executable, arguments)
: new CommandLineInvocation("dotnet", $"\"{executable}\"", arguments);
}
}
}