简体   繁体   中英

How to combine filenames in gulp to use as arguments for a command line tool?

How would I create a Gulpfile.js that was able to (fi) pass the paths of all cpp files in a certain directory to g++?

The generated shell command should look something like this:

g++ -o myprog.exe file1.cpp file2.cpp

and I am thinking of a gulp solution that might look like this:

gulp.task('compile-cpps',function (){
    gulp.src("src/*.cpp")
       .pipe(listify-filenames())
       .pipe(shell_cmd("g++ -o myprog.exe <$filenames$>"))
})

If you aren't actually using gulp streams, just bypass them:

var glob = require('glob'),
    exec = require('child_process').exec;

gulp task('compile-cpps', function(gulpCallback) {
    glob('src/*.cpp', function(files) {
        files = files.map(function(f) { return '"'+f+'"'; }).join(' ');
        exec('g++ -o my-prog.exe '+files, function(error, stdout, stderr) {
            gulpCallback(error);
        });
    });
});

This lists all files that match your glob , wraps each filename in quotes (to allow for spaces), then spawns a new process for g++ with the filename appended. On completion, the async callback for gulp is handled .

You can either log or output the stdout and stderr from your process. If you'd rather handle the process in a streaming manner, replace exec with spawn and attach to the events :

var glob = require('glob'),
    spawn = require('child_process').spawn;

gulp task('compile-cpps', function(gulpCallback) {
    glob('src/*.cpp', function(files) {
        spawn('g++', ['-o', 'my-prog.exe'].concat(files))
            .on('close', function(code) {
                gulpCallback(code);
            });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM