在 gruntfile 中执行 shell 脚本并将结果分配给变量 [英] Execute shell script in gruntfile and assign result to variable

查看:33
本文介绍了在 gruntfile 中执行 shell 脚本并将结果分配给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 grunt 来管理一套 mocha-run 测试.mocha 测试套件中要求的一件事是设置某些环境变量,以便根据运行测试的开发人员的环境正确执行测试.其中一个环境变量在每个开发人员的机器上都有不同的值,因此我们执行一个 bash 脚本来为我们正在设置的环境变量返回该值.

I am using grunt to manage a suite of mocha-run tests. One of the things required in the mocha test suite is that certain environment variables be set so that the tests are executed properly based on the environment of the developer running the tests. One of these environment variables will have a different value on every developer's machine, so we execute a bash script to return that value for the environment variable we are setting.

我正在使用 grunt.util.spawn 运行脚本并将其结果分配给我的 gruntfile 中定义的变量,然后 grunt-env 使用该值设置环境变量.下面是我的 gruntfile 示例(在 coffeescript 中):

I am using grunt.util.spawn to run the script and assign its result to a variable defined in my gruntfile, and then grunt-env to set the environment variable with that value. Below is an example of my gruntfile (in coffeescript):

module.exports = (grunt) ->
  envvar = ''

  grunt.initConfig
    pkg: grunt.file.readJSON('package.json')

    env:
      dev:
        ENV_VAR: envvar

    simplemocha:
      options:
        timeout: 30000
        reporter: 'spec'
        compilers: 'coffee:coffee-script'
      all:
        src: ['Tests/**/*.coffee']

  grunt.registerTask 'init', ->
    done = this.async
    command =
      cmd: './bin/get_envvar.sh'
    grunt.util.spawn command, (error, result, code) ->
      envvar = result
      console.log 'envvar: ' + envvar
      done

  grunt.registerTask 'test', ['init', 'env', 'simplemocha']

要执行此操作,我调用...

To execute this, I call...

/path/to/grunt test

不幸的是,虽然 init 运行了,但其中的回调似乎没有被执行,所以 envvar 永远不会被设置.奇怪的是,如果我在我的测试中禁用日志记录,回调会被调用,但只有在我的 env 和 simplemocha 任务被启动之后.我对 grunt 任务的理解是它们是阻塞的,所以我希望 init 任务必须完成(即使其中有异步函数)才能继续执行下一个任务.

Unfortunately, although init runs, the callback therein doesn't seem to get executed, so envvar never gets set. Oddly, if I disable logging in my tests, the callback DOES get called, but only after my env and simplemocha tasks have been kicked off. My understanding of grunt tasks is that they are blocking, so I would expect the init task to have to completed (even with the async function therein) before moving on to the next task.

有什么想法吗?

推荐答案

虽然我还不清楚为什么上面的方法不起作用,欢迎反馈,经过一番研究,我发现 shelljs,我可以用它来解决我的问题.由于 shelljs 可以同步执行 shell 命令,所以当我真的想要阻止时,我不必使用回调:

Although I'm still unclear on why the method above is not working, and welcome any feedback, after a bit of research, I found shelljs, which I was able to use to solve my problem. Since shelljs can exec shell commands synchronously, I don't have to work with callbacks when I really want things to block:

module.exports = (grunt) ->
  shell = require 'shelljs'
  envvar = shell.exec('./bin/get_envvar.sh', {'silent':true}).output

  grunt.initConfig
    pkg: grunt.file.readJSON('package.json')

    env:
      dev:
        ENV_VAR: envvar

    simplemocha:
      options:
        timeout: 30000
        reporter: 'spec'
        compilers: 'coffee:coffee-script'
      all:
        src: ['Tests/**/*.coffee']

  grunt.registerTask 'test', ['env', 'simplemocha']

也干净多了!

参考资料:

这篇关于在 gruntfile 中执行 shell 脚本并将结果分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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