执行PowerShell脚本作为预构建事件失败

时间:2022-08-05 23:07:37

I have a pretty big JavaScript project I am working on, embedded into an ASP.NET MVC project. I have separated the JS code into several (12-ish) files, to have an easier handling of the code. The problem is, I have lost IntelliSense with this, and also, I need to link several of the files on the pages. There came the idea: let's make a PS script, to concatenate the files into one file. It works nicely, I give reference to that file only, so I have IntelliSense, and also, I only need to link that file into the page. However, I need to run the script every build manually. So the next idea was to set a pre-build event to run the script. And here comes the problem, the script doesn't run successfully.

我正在开发一个非常大的JavaScript项目,嵌入到ASP.NET MVC项目中。我已将JS代码分成几个(12-ish)文件,以便更轻松地处理代码。问题是,我已经失去了IntelliSense,而且,我需要链接页面上的几个文件。出现了这个想法:让我们制作一个PS脚本,将文件连接成一个文件。它工作得很好,我只提供该文件的引用,所以我有IntelliSense,而且,我只需要将该文件链接到页面。但是,我需要手动运行每个构建脚本。所以下一个想法是设置一个预构建事件来运行脚本。问题来了,脚本无法成功运行。

Here is the script:

这是脚本:

if(Test-Path myprefix-concatenated.js){
Remove-Item myprefix-concatenated.js
}
cat myprefix-*.js > concatenated.js
Rename-Item -path concatenated.js -newname myprefix-concatenated.js

Here is the pre-build event:

这是预构建事件:

powershell.exe -file "$(ProjectDir)\Scripts\my-scripts\concat.ps1"

And here is the output of the build:

以下是构建的输出:

3>  Get-Content : An object at the specified path myprefix-* does not exist, or has been
3>  filtered by the -Include or -Exclude parameter.
3>  At ...\Scripts\my-scripts\concat.ps1:4 char:4
3>  + cat <<<<  myprefix-* > concatenated.js
3>      + CategoryInfo    : ObjectNotFound: (System.String[]:String[]) [Get-
3>     Content], Exception
3>      + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetCo
3>     ntentCommand

After this, the build succeeds, but the concatenation does not happen. It works, if I run the script manually. Do you have any idea what causes this problem, and how to fix it? Thanks in advance!

在此之后,构建成功,但串联不会发生。如果我手动运行脚本,它可以工作。你知道是什么导致了这个问题,以及如何解决它?提前致谢!

1 个解决方案

#1


1  

I would not assume the current directory is set like you're expecting. You're using relative paths and that probably isn't a good idea. You can either pass in a path as a parameter to your script e.g.

我不认为当前目录的设置与您期望的一样。你正在使用相对路径,这可能不是一个好主意。您可以将路径作为参数传递给脚本,例如

powershell.exe -file "$(ProjectDir)\Scripts\my-scripts\concat.ps1" "$(ProjectDir)"

-- concat.ps1 --
param($projectDir)

if (Test-Path $projectDir\Scripts\my-scripts\myprefix-concatenated.js) {
    Remove-Item $projectDir\Scripts\my-scripts\myprefix-concatenated.js
}
cat $projectDir\Scripts\my-scripts\myprefix-*.js > $projectDir\Scripts\my-scripts\concatenated.js
Rename-Item $projectDir\Scripts\my-scripts\concatenated.js -newname $projectDir\Scripts\my-scripts\myprefix-concatenated.js

If you are on PowerShell v3, you can reference files relative to where your concat.ps1 script resides with the $PSScriptRoot automatic variable e.g.:

如果您使用的是PowerShell v3,则可以使用$ PSScriptRoot自动变量引用相对于concat.ps1脚本所在位置的文件,例如:

if (Test-Path $PSScriptRoot\myprefix-concatenated.js) {
    Remove-Item $PSScriptRoot\myprefix-concatenated.js
}
cat $PSScriptRoot\myprefix-*.js > $PSScriptRoot\concatenated.js
Rename-Item $PSScriptRoot\concatenated.js -newname $PSScriptRoot\myprefix-concatenated.js

#1


1  

I would not assume the current directory is set like you're expecting. You're using relative paths and that probably isn't a good idea. You can either pass in a path as a parameter to your script e.g.

我不认为当前目录的设置与您期望的一样。你正在使用相对路径,这可能不是一个好主意。您可以将路径作为参数传递给脚本,例如

powershell.exe -file "$(ProjectDir)\Scripts\my-scripts\concat.ps1" "$(ProjectDir)"

-- concat.ps1 --
param($projectDir)

if (Test-Path $projectDir\Scripts\my-scripts\myprefix-concatenated.js) {
    Remove-Item $projectDir\Scripts\my-scripts\myprefix-concatenated.js
}
cat $projectDir\Scripts\my-scripts\myprefix-*.js > $projectDir\Scripts\my-scripts\concatenated.js
Rename-Item $projectDir\Scripts\my-scripts\concatenated.js -newname $projectDir\Scripts\my-scripts\myprefix-concatenated.js

If you are on PowerShell v3, you can reference files relative to where your concat.ps1 script resides with the $PSScriptRoot automatic variable e.g.:

如果您使用的是PowerShell v3,则可以使用$ PSScriptRoot自动变量引用相对于concat.ps1脚本所在位置的文件,例如:

if (Test-Path $PSScriptRoot\myprefix-concatenated.js) {
    Remove-Item $PSScriptRoot\myprefix-concatenated.js
}
cat $PSScriptRoot\myprefix-*.js > $PSScriptRoot\concatenated.js
Rename-Item $PSScriptRoot\concatenated.js -newname $PSScriptRoot\myprefix-concatenated.js