简体   繁体   English

在gruntfile中执行shell脚本并将结果赋给变量

[英]Execute shell script in gruntfile and assign result to variable

I am using grunt to manage a suite of mocha-run tests. 我正在使用grunt来管理一套摩卡运行测试。 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. mocha测试套件中需要的一件事是设置某些环境变量,以便根据运行测试的开发人员的环境正确执行测试。 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. 其中一个环境变量在每个开发人员的机器上都有不同的值,因此我们执行一个bash脚本来为我们设置的环境变量返回该值。

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. 我正在使用grunt.util.spawn来运行脚本并将其结果分配给我的gruntfile中定义的变量,然后使用grunt-env来设置具有该值的环境变量。 Below is an example of my gruntfile (in coffeescript): 下面是我的gruntfile示例(在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

Unfortunately, although init runs, the callback therein doesn't seem to get executed, so envvar never gets set. 不幸的是,虽然init运行,但其中的回调似乎没有被执行,因此envvar永远不会被设置。 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. 奇怪的是,如果我在我的测试中禁用了日志记录,那么回调就会被调用,但只有在我的env和simplemocha任务被启动后才会被调用。 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. 我对grunt任务的理解是它们是阻塞的,所以我希望在继续下一个任务之前必须完成init任务(即使使用其中的异步函数)。

Any ideas? 有任何想法吗?

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. 虽然我仍然不清楚为什么上面的方法不起作用,并欢迎任何反馈,经过一些研究后,我找到了shelljs ,我能用它来解决我的问题。 Since shelljs can exec shell commands synchronously, I don't have to work with callbacks when I really want things to block: 由于shelljs可以同步执行shell命令,所以当我真的想要阻塞时,我不必使用回调:

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']

A lot cleaner, too! 也更干净!

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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