简体   繁体   English

如何调试 NuGet package 的 install.ps1 脚本

[英]How to debug install.ps1 script of NuGet package

So we can include an install/uninstall powershell scripts in a NuGet package.所以我们可以在 NuGet package 中包含安装/卸载 powershell 脚本。 I tried, but my install.ps1 does not work.我试过了,但我的 install.ps1 不起作用。 Is there any possibility to find out why?有没有可能找出原因? Debugging, logging, anything?调试,记录,什么?

Update更新

Please note that the script is executed as part of an installation process of Nuget package.请注意,该脚本是作为 Nuget package 安装过程的一部分执行的。 It may be very Nuget-specific.它可能是非常特定于 Nuget 的。

Perhaps I am late to the party but here is a solution for debugging NuGet specific scripts, the NuGet package NuGetDebugTools .也许我迟到了,但这里有一个调试 NuGet 特定脚本的解决方案,NuGet package NuGetDebugTools Its script Add-Debugger.ps1 adds a simple and yet effective debugger to the NuGet package manager console.它的脚本Add-Debugger.ps1为 NuGet package 管理控制台添加了一个简单而有效的调试器。

The sample scenario:示例场景:

  • start Visual Studio启动 Visual Studio
  • open NuGet console and type commands打开 NuGet 控制台并输入命令

    PM> Add-Debugger [-ReadHost] PM> Set-PSBreakpoint -Command init PM> Set-PSBreakpoint -Command install

(or set more specific breakpoints, see help Set-PSBreakpoint ) (或设置更具体的断点,请参阅help Set-PSBreakpoint

  • open a Visual Studio solution or invoke Install-Package XYZ for already opened打开 Visual Studio 解决方案或为已打开的调用 Install-Package XYZ
  • the debugger input dialog appears on any init.ps1 and install.ps1 invoked调试器输入对话框出现在任何init.ps1install.ps1调用
  • type?类型? as debugger input and see what you can do:作为调试器输入,看看你能做什么:

     s, StepInto Step to the next statement into functions, scripts, etc. v, StepOver Step to the next statement over functions, scripts, etc. o, StepOut Step out of the current function, script, etc. c, Continue Continue operation (also on empty input). q, Quit Stop operation and exit the debugger. ?, h Display this help message. r Display PowerShell command history. k Display call stack (Get-PSCallStack). <number> Show debug location in context of <number> lines. +<number> Set location context preference to <number> lines. <command> Invoke any PowerShell <command> and write its output.
  • type other debugger and PowerShell commands and watch the output in the NuGet console键入其他调试器和 PowerShell 命令并在 NuGet 控制台中观看 output


v1.4.0 - New switch ReadHost tells to use Read-Host for input instead of the default GUI input box. v1.4.0 - 新开关ReadHost告诉使用Read-Host进行输入,而不是默认的 GUI 输入框。

This is how I was able to step-through install.ps1 using PowerShell ISE:这就是我能够使用 PowerShell ISE 逐步完成 install.ps1 的方式:

To be able to step through execution of the install script using PowerShell ISE follow these steps: Enable execution of assemblies built with.Net 4为了能够使用 PowerShell ISE 逐步执行安装脚本,请执行以下步骤:启用使用 .Net 4 构建的程序集的执行

Either任何一个

C:\Windows\System32\WindowsPowerShell\v1.0 Or C:\Windows\System32\WindowsPowerShell\v1.0 或

C:\Windows\SysWOW64\WindowsPowerShell\v1.0 C:\Windows\SysWOW64\WindowsPowerShell\v1.0

Depending on which version of PS you're using If files are not there create them取决于您使用的 PS 版本 如果文件不存在,请创建它们

Either C:\Windows\System32\WindowsPowerShell\v1.0 Or C:\Windows\SysWOW64\WindowsPowerShell\v1.0 C:\Windows\System32\WindowsPowerShell\v1.0 或 C:\Windows\SysWOW64\WindowsPowerShell\v1.0

Depending on which version of PS you're using取决于您使用的 PS 版本

If config files are not there create them如果配置文件不存在,请创建它们

powershell.exe.config: powershell.exe.config:

<configuration>  
    <startup useLegacyV2RuntimeActivationPolicy="true">  
        <supportedRuntime version="v4.0.30319"/>  
        <supportedRuntime version="v2.0.50727"/>  
    </startup>  
</configuration>  

powershell_ise.exe.config: powershell_ise.exe.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup>
      <supportedRuntime version="v4.0.30319" />
    </startup>
</configuration>

To be able to run PowerShell scripts included with a NuGet package the execution policy will need to be changed:为了能够运行 NuGet package 中包含的 PowerShell 脚本,需要更改执行策略:

Set-ExecutionPolicy RemoteSigned -Scope Process Set-ExecutionPolicy RemoteSigned -Scope 进程

Copy install.ps1 that you want to debug and modify it's contents as follows:复制您要调试的 install.ps1 并修改其内容如下:

delete the parameters block删除参数块

param(
    [Parameter(Mandatory=$true)] [string]   $installPath,
    [Parameter(Mandatory=$true)] [string]   $toolsPath,
    [Parameter(Mandatory=$true)]            $package,
    [Parameter(Mandatory=$true)]            $project
)

import a module which allows to use nuget cmdlets outside of the VS host process导入一个允许在 VS 主机进程之外使用 nuget cmdlet 的模块

Download http://community.sharpdevelop.net/blogs/mattward/NuGet/NuGetOutsideVisualStudio.zip Extract contents of the bin folder to some place and then import the PackageManagement.Cmdlets.dll下载http://community.sharpdevelop.net/blogs/mattward/NuGet/NuGetOutsideVisualStudio.zip将 bin 文件夹的内容提取到某个位置,然后导入 PackageManagement.Cmdlets.Z0614162324AB248C

like so:像这样:

import-module "C:\dev\NuGetOutsideVisualStudio\bin\PackageManagement.Cmdlets.dll"

now you are able to set all the parameters manually like so:现在您可以像这样手动设置所有参数:

$toolsPath="C:\dev\demo-solution\packages\X1.Registration.DbUpdate.0.4\tools"
$installPath="C:\dev\demo-solution\packages\X1.Registration.DbUpdate.0.4"

set-project DemoSolution.Logic C:\dev\demo-solution\DemoSolution.sln

$project = Get-Project -name DemoSolution.Logic

That still leaves $package object unset but I found that script doesn't really refer to that parameter这仍然使 $package object 未设置,但我发现脚本并没有真正引用该参数

References: http://community.sharpdevelop.net/blogs/mattward/archive/2011/06/12/InstallingNuGetPackagesOutsideVisualStudio.aspx参考文献: http://community.sharpdevelop.net/blogs/mattward/archive/2011/06/12/InstallingNuGetPackagesOutsideVisualStudio.aspx

Use Set-PsDebug -trace 2 to see what is happening.使用Set-PsDebug -trace 2查看发生了什么。

Run your scripts through the Package Manager Console in VS (details on the console at https://docs.nuget.org/ndocs/tools/package-manager-console ) -- and anything that causes an error along the way will be written out in red.通过 VS 中的 Package 管理器控制台运行您的脚本(控制台上的详细信息https://docs.nuget.org/ndocs/tools/package会导致错误)红色。

Also, you can write diagnostic trace type info with Write-Host to the same console.此外,您可以使用 Write-Host 将诊断跟踪类型信息写入同一控制台。

You might call Start-Transcript at the beginning of install script and Stop-Transcript at the end.您可以在安装脚本的开头调用Start-Transcript ,在结尾调用Stop-Transcript You would probably wrap the install code like this:您可能会像这样包装安装代码:

try {
  $ErrorActionPreference = 'stop'  # stop on error
  Start-Transcript c:\a.txt
  ...
}
catch {
  write-host $_
}
finally {
  Stop-Transcript
}

Also $ErrorActionPreference = 'inquire' (instead of stop) could possibly work. $ErrorActionPreference = 'inquire' (而不是停止)也可能起作用。 However, no chance to try it now.但是,现在没有机会尝试。 See http://tasteofpowershell.blogspot.com/2008/07/handling-errors-in-powershell.htmlhttp://tasteofpowershell.blogspot.com/2008/07/handling-errors-in-powershell.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM