psake设置执行​​目录

时间:2015-05-18 15:37:29

标签: powershell working-directory psake

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

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

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

我想在构建之前清理存储库(使用git clean -xdf)。 但我找不到一种方法(希望用.Net类)来设置git的执行目录。

首先,我搜索了一种在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?

1 个答案:

答案 0 :(得分:2)

我使用构建脚本在psake中遇到了与您相同的问题。 "设置位置" 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

您可能需要做的是在调用本机命令之前更改Win32当前目录,例如" git":

$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
}