Respect directory option for steps again (#4319)

This commit is contained in:
6543
2024-11-06 23:21:56 +01:00
committed by GitHub
parent c9752bb735
commit bf5405b6cc
10 changed files with 96 additions and 61 deletions

View File

@@ -18,10 +18,19 @@ import (
"bytes"
"fmt"
"strings"
"text/template"
)
func generateScriptWindows(commands []string) string {
func generateScriptWindows(commands []string, workDir string) string {
var buf bytes.Buffer
if err := setupScriptWinTmpl.Execute(&buf, map[string]string{
"WorkDir": workDir,
}); err != nil {
// should never happen but well we have an error to trance
return fmt.Sprintf("echo 'failed to generate posix script from commands: %s'; exit 1", err.Error())
}
for _, command := range commands {
escaped := fmt.Sprintf("%q", command)
escaped = strings.ReplaceAll(escaped, "$", `\$`)
@@ -31,16 +40,13 @@ func generateScriptWindows(commands []string) string {
command,
))
}
script := fmt.Sprintf(
setupScriptWin,
buf.String(),
)
return script
return buf.String()
}
const setupScriptWin = `
const setupScriptWinProto = `
$ErrorActionPreference = 'Stop';
if ([Environment]::GetEnvironmentVariable('CI_WORKSPACE')) { if (-not (Test-Path "$env:CI_WORKSPACE")) { New-Item -Path "$env:CI_WORKSPACE" -ItemType Directory -Force }};
if (-not (Test-Path "{{.WorkDir}}")) { New-Item -Path "{{.WorkDir}}" -ItemType Directory -Force };
if (-not [Environment]::GetEnvironmentVariable('HOME')) { [Environment]::SetEnvironmentVariable('HOME', 'c:\root') };
if (-not (Test-Path "$env:HOME")) { New-Item -Path "$env:HOME" -ItemType Directory -Force };
if ($Env:CI_NETRC_MACHINE) {
@@ -51,10 +57,11 @@ $netrc=[string]::Format("{0}\_netrc",$Env:HOME);
};
[Environment]::SetEnvironmentVariable("CI_NETRC_PASSWORD",$null);
[Environment]::SetEnvironmentVariable("CI_SCRIPT",$null);
if ([Environment]::GetEnvironmentVariable('CI_WORKSPACE')) { cd "$env:CI_WORKSPACE" };
%s
cd "{{.WorkDir}}";
`
var setupScriptWinTmpl, _ = template.New("").Parse(setupScriptWinProto)
// traceScript is a helper script that is added to the step script
// to trace a command.
const traceScriptWin = `