我还没有测试过这个,但如果你将两者结合起来,它会看起来像这样。只需运行默认任务,该任务使用运行序列来管理依赖顺序。
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
带参数调用powershell文件
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 位于解决方案的根目录中
Write-Host 'hello'
EDIT 2
好的,再试一次。你能把参数/参数放在file.ps1中吗?
function Write-Stuff($arg1, $arg2){
Write-Output $arg1;
Write-Output $arg2;
}
Write-Stuff -arg1 "hello" -arg2 "See Ya"
EDIT 3
从 gulp 任务传递参数::
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)
});
});
更新 file.ps1 以删除
function Write-Stuff([string]$arg1, [string]$arg2){
Write-Output $arg1;
Write-Output $arg2;
}