使用gulp创建tar存档 [英] creating tar archives using gulp

查看:48
本文介绍了使用gulp创建tar存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gulp-tar创建tar文件...我如何添加一个顶级文件夹,以便当用户运行 tar -xzf myArchive.tar 时,它会提取到一个特定的文件夹中文件夹.

I'm using gulp-tar to create a tar file... how do I add a top level folder so that when the user runs tar -xzf myArchive.tar it extracts into a specific folder.

这是我的代码:

gulp.task('prod', ['min', 'gittag'], function() {

  //copy all files under /server into a zip file
    gulp.src('../server/**/*')
    .pipe(tar('xoserver' + '-'+ gittag +'.tar'))
    .pipe(gzip())
    .pipe(gulp.dest('../prod'));
});

上面的代码很好地创建了一个 tar.zip 文件,但是我必须小心在提取时添加 -C< folder> ,否则文件将被获取.解压到当前文件夹.

The above creates a tar.zip file all right, but I have to be careful to add a -C <folder> while extracting, else the files get extracted to the current folder.

[已编辑]

我在这里想要做的是生成一个格式为 xoserver-alpha-d414ddf.tar.gz 的tarball,将其与 tar xvf 一起提取时创建一个文件夹 xoserver-alpha-d414ddf 并将其下的所有文件解压缩.本质上,我试图在打包文件上方添加新的文件夹名称.如果添加 base 选项,则提取到的文件夹就是 server

What I'm trying to do here is generate a tarball of the format xoserver-alpha-d414ddf.tar.gz which, when extracted with a tar xvf will create a folder xoserver-alpha-d414ddf and unpack all the files under it. Essentially I am trying to add new folder name above my packed files. If I add a base option, the folder extracted to is just server

[答案]

感谢ddprrt提供了一个很好的答案.如果有人希望使用类似的策略将git标签嵌入tarball的名称中进行分发/测试,我将重现最终代码.

Thanks to ddprrt for a good answer. I am reproducing the final code in case someone else wants to use a similar strategy of embedding the git tag into the name of the tarball for distribution/testing.

gulp.task('gittag', function(cb) {   // generate the git tag 
    git.exec({args : 'branch -v'}, function (err, stdout) {
      var lines = stdout.split('\n');
      for (var l in lines) {
        if (lines[l][0] == '*') {
          var words = lines[l].split(/\s+/);
          gittag = words[1]+ '-' + words[2];
          console.log('Gittag is %s', gittag);
          break;
        }
      }
      cb();
    });
});

gulp.task('min', ['runbmin', 'template', 'vendor']); // generate min files

gulp.task('prod', ['min', 'gittag'], function() { // create tarball
  //copy all files under /server into a zip file
    return gulp.src('../server/**/*')
    .pipe(rename(function(path) {
        path.dirname = 'server-' + gittag + '/' + path.dirname;
    }))
    .pipe(tar('xoserver-'+gittag+'.tar'))
    .pipe(gzip())
    .pipe(gulp.dest('../prod'));
});

推荐答案

这是 base 选项的作用.

gulp.task('prod', ['min', 'gittag'], function() {
    return gulp.src('../server/**/*', { base: '../server/' })
        .pipe(tar('xoserver' + '-'+ gittag +'.tar'))
        .pipe(gzip())
        .pipe(gulp.dest('../prod'));
});

使用它可以告诉gulp处理收到的glob时要包括的路径.

With it you can tell gulp which paths to include when dealing with the globs you receive.

顺便说一句.不要忘记返回流或在任务中调用完成的回调.帮助gulp协调您的构建管道

Btw. Don't forget to return streams or call the done callback in your task. Helps gulp orchestrating your build pipeline

对于第二个问题,您可以使用 gulp-rename 任务来更改虚拟文件所在的目录.就像

As for the second question, you can use gulp-rename task to change the directory where your virtual files are located. Would be something like

.pipe(rename(function(path) {
     path.dirname = 'whatever/' + path.dirname
}));

这篇关于使用gulp创建tar存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆