re1ro / grunt-bg-shell

Better shell commands for grunt
MIT License
50 stars 8 forks source link

Prevent inadvertent template compilation #16

Closed ekhaled closed 9 years ago

ekhaled commented 9 years ago

This call is just to fetch the _defaults object, and we do not need parsed templates here. Because of this, we cannot have template strings on other targets.

For example, if I have config something like this:

bgShell:{
  install:{
    cmd: 'npm install',
    bg: true
  },
  compile:{
    cmd: 'mv binary folder/<%=buildEnv.folder%>'
  }
}

In the config above buildEnv may not be available when we run the install target, it might be injected later from another task using grunt.option.set() before we run the compile target. In this case, the install target does not run, because grunt.option.get tries to compile the template string, and fails.

re1ro commented 9 years ago

Hey @ekhaled ! Thank you for a pull request! Unfortunately I can't replace grunt.config.get with grunt.config.getRaw, because there might me a lot of users relying on that standard grunt functionality.

In your case I would just define the command as a function returning command string.

bgShell:{
  install:{
    cmd: 'npm install',
    bg: true
  },
  compile:{
    cmd: function(){ return 'mv binary folder/' + buildEnv.folder }
  }
}

assuming that you will define buildEnv object in your grunt file.

ekhaled commented 9 years ago

I see, thanks for pointing out that alternate syntax.

ekhaled commented 9 years ago

For future reference of people encountering this, I ended up doing it like this:

bgShell:{
  install:{
    cmd: 'npm install',
    bg: true
  },
  compile:{
    cmd: function(){
      return grunt.config.process('mv binary folder/<%=buildEnv.folder%>');
    }
  }
}

Because buildEnv here wasn't a global variable, it was an object in grunt.initConfig populated by another task, very similar what grunt-gitinfo does.