Reputation: 1708
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.
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!
Upvotes: 1
Views: 2425
Reputation: 201592
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.:
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
Upvotes: 2