如何从gulp调用执行PowerShell脚本?

时间:2016-03-24 15:19:22

标签: node.js powershell msbuild visual-studio-2015 gulp

我正在使用gulp来构建和部署我们的应用程序。

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',
    }));
});

但是在构建之后我必须调用PowerShell脚本文件“publish.ps1”,如何在gulp中调用它?

1 个答案:

答案 0 :(得分:10)

我没有对此进行过测试,但如果将两者结合起来就会看起来像这样。只需运行默认任务,该任务使用run-sequence来管理依赖顺序。

    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();
    });

修改

使用参数

调用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'

编辑2

好的,再试一次。你能把params / arguments放在file.ps1中吗?

function Write-Stuff($arg1, $arg2){
    Write-Output $arg1;
    Write-Output $arg2;
}
Write-Stuff -arg1 "hello" -arg2 "See Ya"

编辑3

从gulp任务::

传递params
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)
    });
});

更新要删除的文件.ps1

function Write-Stuff([string]$arg1, [string]$arg2){
    Write-Output $arg1;
    Write-Output $arg2;
}