Commandbox: Possible to inject coldbox settings in a task runner?

// task.cfc
component {
	property name="buildSetting"  inject="coldbox:setting:build";

       function run() {
	}
}

command task run will produce the following error:

The DSLNamespace: [coldbox] cannot be used as it requires a ColdBox Context

I could not get getSetting() working as well.

Any insights will be appreciated!

The issue is your DSL. If you are in a pure CommandBox task runner environment, ColdBox is not there. So you need to use the box: namespace. box:setting:build

What Luis says is correct, but I’m a little confused that you’re expecting ColdBox bits to exist within CommandBox. Can you describe what you’re doing?

Sorry, I didn’t provide enough information.

I use /task.cfc (running commands such as cfcompile) to build the project.

Commandbox:pdm-server> task run

I set up some variables in /config/coldbox.cfc:

// /config/coldbox.cfc
function configure() {
  coldbox = {...},
  settings = {
    build = {
      clientI18nFolder = "/Users/myleslee/websites/pdm-console/src/i18n",
      syncI18nFiles    = true,
      ...
    }
  }
}

/task.cfc needs to access settings.build which is also used by others, i.e. /models/SchemaService.cfc.

// models/SchemaService.cfc
component singleton {
  property name="buildSetting"  inject="coldbox:setting:build";
  ... 
}

I tried Luis’ suggestion but box:setting:build didn’t work. It must point to somewhere else.

CommandBox DSL cannot find dependency using definition: {ref={null}, required={true}, argName={}, javaCast={null}, dsl={box:setting:build}, name={buildSetting}, type={any}, scope={variables}, value={null}}

The config setting requested: build does not exist in the loaded settings. Loaded settings are modules,server

So I went a verbose way to get the job done.

//  /task.cfc
function run() {
  var buildSetting = createObject("component", "config.coldbox").getBuildSetting()
  ...
}

// /config/coldbox.cfc
function getBuildSetting() {
  return {
      clientI18nFolder = "/Users/myleslee/websites/pdm-console/src/i18n",
      syncI18nFiles    = true,
      ...
  }
} 

function configure() {
  coldbox = {...},
  settings = {
    build = getBuildSetting()
    ...
  }
}

I know this might be a bad practice, so I would love to hear about how it is usually done from experienced ones.

Right, so CommandBox CLI and ColdBox MVC are two separate things. ColdBox doesn’t know about, care about, or use anything of CommandBox directly and CommandBox doesn’t know about, care about, or use anything from ColdBox. So, you have a task.cfc and you want CommandBox to run it-- the task run command doesn’t pay any attention to the fact that the task just so happens to be in a folder that also contains a ColdBox app. None of the MVC files are touched at all by CommandBox, unless you specifically write some sort of code to do so.

So while Luis was correct that

  • CommandBox, like ColdBox, has a concept of modules
  • CommandBox also has a global setting concept (config settings) similar to ColdBox’s framework settings
  • CommandBox uses WireBox, and has a similar injection DSL namespace for injecting these settings
    but that doesn’t mean CommandBox is going to see or be aware of settings that happen to be part of a ColdBox app which happens to be in the folder as your task.cfc.

My recommendation-- is to try and put some of these settings into a .env file which CommandBox will load into any servers you start and CommandBox will also load in when you run any commands. You can access those environment vars in both the ColdBox app and the CommandBox CLI.

Alternatively, create some sort of of stand alone configuration file or CFC that you load in both places. Creating the ColdBox config file directly may work in your case, but use caution as some ColdBox configs may rely on variables or methods that only exist when the CFC is created in the context of ColdBox.

Wow, you are awesome! I was under the impression that ColdBox and Commandbox were interchangeable in nature. Now I have a clear picture of what to do in the future.
Thanks a lot!