【问题标题】:psake set execution directorypsake 设置执行目录
【发布时间】:2015-05-18 15:37:29
【问题描述】:

我正在尝试将 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?

【问题讨论】:

    标签: powershell working-directory psake


    【解决方案1】:

    我在构建脚本的 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多