在gruntfile执行shell脚本并分配结果变量 [英] Execute shell script in gruntfile and assign result to variable
问题描述
我使用咕噜管理一套摩卡运行测试。一个在摩卡测试套件所需的事情之一是,某些环境变量,使得测试被执行正常的基础上运行测试的显影剂的环境来设置。这些环境变量中都会有每个开发人员的计算机上不同的值,所以我们执行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定义的变量,然后咕噜-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
不幸的是,尽管初始化运行,回调在其中似乎并没有得到执行,所以ENVVAR永远不会被设置。奇怪的是,如果我禁用我的测试记录,回调确实被调用,但只有在我的ENV和simplemocha任务已经拉开序幕。我的呼噜声任务的理解是,他们被封锁,所以我会移动到下一个任务之前期望有初始化任务完成(甚至与异步功能在其中)。
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可以Exec的外壳命令同步,我没有与回调在工作的时候我真的希望事情块:
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']
很多清洁呢!
参考文献:
- How can I perform an asynchronous operation before grunt.initConfig()?
- http://jaketrent.com/post/impressions-of-grunt/
这篇关于在gruntfile执行shell脚本并分配结果变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!