0.1.0 • Published 3 years ago

grunt-zh-environment-builder v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Zh Environment Builder

Zh Environment Builder is a Grunt Plugin that generates configuration files from templates.

Configure your package.json

Add the plugin to devDependencies:

"grunt-zh-environment-builder": "git+http://code.zeedhi.com/zeedhi/grunt-zh-environment-builder.git"

Go to your project's mobile/frontend folder and run the command:

npm install/update

Configure your GruntFile.js

The plugin provides a task called buildEnvironment that will create the configuration files.

Add to your GruntFile the configuration for the buildEnvironment task.

buildEnvironment uses the parameters:

  • configFile: string containing the relative path to the JSON file which has the environment variables.
  • srcFolders: array of strings that contains the folders which have template files.
  • options: object with the following properties:
    • force boolean: If true, environmentBuilder will generate the configuration files without replacing the placeholders not found and will not exit with an error code.

You can define multiple tasks and set only the specific parameters for each task. The default parameters are defined outside any task.

The task development is the default task to run, it doesn't need to be declared if all parameters are defined as default.

Example:

buildEnvironment: {
    "development": {
        "configFile": "../config/environment.json"
    },
    "test": {
        "configFile": "../config/environmentTest.json"
    },
    "deploy" {
        "configFile": "../config/environmentDeploy.json",
        "options": {
            "force": true
        }
    },
    "srcFolders": ["../mobile/config/", "../backend/config/"]
}

After initConfig add:

grunt.loadNpmTasks('grunt-zh-environment-builder');

Configure your project

Add the development configuration file to .gitignore, that way the programmer doesn't need to configure the project every time a PM Task is started.

All files that will use variables from the configuration file must be added to .gitignore and be replaced by a template file.

Creating a template

Templates are identified by the sulfix .template.

Template parameters are defined by "<>PARAM<>", "PARAM" being the attribute key on the configuration file.

The name of the generated file will be the template file name without the sulfix .template.

The generated file name must be added to .gitignore.

Example: environment.xml.template

<container>
   <parameter key="connection_params" type="collection">
        <parameter key="driver">oci8</parameter>
        <parameter key="user"><>DB_USER<></parameter>
        <parameter key="password"><>DB_PASSWORD<></parameter>
        <parameter key="host"><>DB_HOST<></parameter>
        <parameter key="port"><>DB_PORT<></parameter>
        <parameter key="dbname">xe</parameter>
        <parameter key="service">false</parameter>
        <parameter key="charset">AL32UTF8</parameter>
    </parameter>
</container>

Creating configuration files

Must be a JSON object with keys being the name of the parameter on the template file and value being the value the placeholder will be replaced by.

You can create multiple tasks and change the configuration file accordingly.

Example:

environment.json

{
    "serviceUrl": "http://localhost/project/backend/service/index.php"
}

environmentDeploy.json

{
    "serviceUrl": "http://project.teknisa.com/backend/index.php"
}

Run

To generate the configured files, you need to run grunt buildEnvironment from your project's mobile/frontend folder.

The command accepts one parameter that will define which task to use, being development the default task. To define the task run grunt buildEnvironment:taskName.

buildEnvironment accepts key-value pairs options to override the configuration file properties. To override a property add "--property=value" after the command.

  • property is the name of the project property to override.
  • value is the value that will be used. If value is not provided an exception will be thrown.