psake 设置执行目录

psake set execution directory

我正在尝试将 MSBuild 移动到 psake。

我的存储库结构如下所示:

.build
 | buildscript.ps1
.tools
packages
MyProject
MyProject.Testing
MyProject.sln

我想在构建之前清理存储库(使用 git clean -xdf)。 但是我找不到设置git.

执行目录的方法(期待.Net 类)

首先我搜索了一种在psakes exec中设置工作目录的方法:

exec { git clean -xdf }
exec { Set-Location $root
       git clean -xdf }

Set-Location 有效,但在 exec 块完成后,位置仍设置为 $root。

然后我尝试了:

Start-Process git -Argumentlist "clean -xdf" -WorkingDirectory $root

这有效但保持 git 打开并且未来的任务不会被执行。

如何在 $root 中执行 git?

我在 psake 中使用我的构建脚本遇到了与您完全相同的问题。 "Set-Location" cmdlet 不会影响 Powershell 会话的 Win32 工作目录。

这是一个例子:

# Start a new PS session at "C:\Windows\system32"
Set-Location C:\temp
"PS Location = $(Get-Location)"
"CurrentDirectory = $([Environment]::CurrentDirectory)"

输出将是:

PS Location = C:\temp
CurrentDirectory = C:\Windows\system32

您可能需要做的是在调用 "git":

等本机命令之前更改 Win32 当前目录
$root = "C:\Temp"
exec {
  # remember previous directory so we can restore it at end
  $prev = [Environment]::CurrentDirectory
  [Environment]::CurrentDirectory = $root

  git clean -xdf

  # you might need try/finally in case of exceptions...
  [Environment]::CurrentDirectory = $prev
}