Question:
I am using gulp to build and deploy our application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
var msbuild = require('gulp-msbuild'); gulp.task('build', ['clean'], function () { return gulp.src('../../*.sln') .pipe(msbuild({ toolsVersion: 14.0, targets: ['Rebuild'], errorOnFail: true, properties: { DeployOnBuild: true, DeployTarget: 'Package', PublishProfile: 'Development' }, maxBuffer: 2048 * 1024, stderr: true, stdout: true, fileLoggerParameters: 'LogFile=Build.log;Append;Verbosity=detailed', })); }); |
However after build I have to call a PowerShell script file “publish.ps1”, how can I call it in gulp?
Answer:
I haven’t tested this but if you combine the two it would look something like this. just run the default task, which uses run-sequence to manage the dependency order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
var gulp = require('gulp'), runSequence = require('run-sequence'), msbuild = require('gulp-msbuild'), spawn = require("child_process").spawn, child; gulp.task('default', function(){ runSequence('clean', 'build', 'powershell'); }); gulp.task('build', ['clean'], function () { return gulp.src('../../*.sln') .pipe(msbuild({ toolsVersion: 14.0, targets: ['Rebuild'], errorOnFail: true, properties: { DeployOnBuild: true, DeployTarget: 'Package', PublishProfile: 'Development' }, maxBuffer: 2048 * 1024, stderr: true, stdout: true, fileLoggerParameters: 'LogFile=Build.log;Append;Verbosity=detailed', })); }); gulp.task('powershell', function(callback){ child = spawn("powershell.exe",["c:\\temp\\helloworld.ps1"]); child.stdout.on("data",function(data){ console.log("Powershell Data: " + data); }); child.stderr.on("data",function(data){ console.log("Powershell Errors: " + data); }); child.on("exit",function(){ console.log("Powershell Script finished"); }); child.stdin.end(); //end input callback(); }); |
EDIT
Call a powershell file with parameters
1 2 3 4 5 6 7 8 9 10 11 12 |
var exec = require("child_process").exec; gulp.task("powershell", function(callback) { exec( "Powershell.exe -executionpolicy remotesigned -File file.ps1", function(err, stdout, stderr) { console.log(stdout); callback(err); } ); }); |
Powershell file.ps1 in the root of your solution
Write-Host 'hello'
EDIT 2
OK, one more try. Can you put the params/arguments in file.ps1?
1 2 3 4 5 6 |
function Write-Stuff($arg1, $arg2){ Write-Output $arg1; Write-Output $arg2; } Write-Stuff -arg1 "hello" -arg2 "See Ya" |
EDIT 3
Pass the params from the gulp task::
1 2 3 4 5 6 7 |
gulp.task('powershell', function (callback) { exec("Powershell.exe -executionpolicy remotesigned . .\\file.ps1; Write-Stuff -arg1 'My first param' -arg2 'second one here'" , function(err, stdout, stderr){ console.log(stdout); callback(err) }); }); |
Update file.ps1 to remove
1 2 3 4 5 |
function Write-Stuff([string]$arg1, [string]$arg2){ Write-Output $arg1; Write-Output $arg2; } |