Skip to content

Developing Ensemble Core

Melanie Dickinson edited this page Nov 15, 2019 · 12 revisions

Information for people interested in developing Ensemble "core", the standalone ensemble.js library that is included in any Ensemble project.

The standalone version of ensemble.js (which can be found on the Releases page) is compiled by merging all of the Javascript files listed in ensemble-config.js.

The component files include libraries like jQuery and Underscore, but, most importantly, the core Ensemble files in the js/ensemble subdirectory. You will likely be editing files in that directory if you're working on Ensemble core.

—Though you could also edit or improve the build process (via the Gruntfile.js and package.json), or remove or update dependencies (by editing both ensemble code and ensemble-config.js).

Browse the open ensemble core issues to find something to work on here.

After you make any edits to js/ensemble files, test that you didn't break any unit tests (see instructions below). If you're ready to distribute it as a standalone file, compile it with grunt deploy (see first time setup instructions below). Note, we've explicitly prevented committing the compiled ensemble.js via .gitignore. It should instead be distributed on the Releases page.


Running the Ensemble library unit tests

To run the unit tests, start an HTTP server at the root of the repository and navigate to localhost:YOUR_PORT/tests.html in your web browser. For example, if you ran the following command to start a server:

python -m SimpleHTTPServer 8080

...then you could navigate to localhost:8080/tests.html to run the tests.

If all is well, you should be presented with a screen full of green passing tests! If a test does not pass, a message explaining what the test was looking for (and the value it actually saw) will be printed in red. If the unit tests crashed before completion, a red message will appear at the bottom of the page informing you that a crash has occurred.


Writing unit tests

Open the appropriate file in the js/tests directory based on the module you are writing a test for. Find the function runTests() within it to see the types of things already being tested for. If you want to add an additional test to one of these existing areas, navigate to that function. Otherwise, make a new function (giving it an appropriately descriptive name for what it is testing), and call that new function within the runRules() function.

The basic structure will consist of these three function calls:

  • test.start("MODULENAME", "PARTOFSYSTEMYOUARETESTING"); -- An example MODULENAME might be RuleLibrary, and an example part of the system might be "testTimeOrderedRules."

  • test.assert(ARG1, ARG2, "MESSAGE"); -- Asserts that ARG1 and ARG2 are equal to each other. If they are, then the unit test passes. If they are not, then the MESSAGE will appear, colored red, when you run the unit tests. This is the heart of the unit tests, and you can have as many assert statements as you want and need between the 'start' and 'finish' function calls.

  • test.finish(); -- Signifies that this unit test is over and that you are ready to move on to the following one.


Building ensemble.js from source

We use the RequireJS plugin for Grunt to compile the standalone library. Grunt is a Javascript task runner for automating routine tasks. (We also use it to build the MacOS version of the tool.)

If you're interested in modifying the build process, read the Getting Started pages of Grunt and NW.js and the Ensemble Tool's Gruntfile.js. Be sure to update this document if any instructions change.

1. Install Node.js (first time machine setup only)

Note, NPM is installed when you install Node.js. Test if you already have them with: node -v and npm -v on a command line.

If not, download and use the installer programs (or package manager alternatives) on the Node.js website.

You may also want to ensure your NPM is up-to-date with npm update -g npm (some systems may require sudo).

2. Install Grunt (first time machine and new directory setup only)

See also: the Grunt Getting Started page

If you're using Grunt for the first time on this machine, open a command shell and install the Grunt command line interface (CLI) by running:

npm install -g grunt-cli

You might have to preface this with sudo (on OSX, *nix, BSD, etc) or run your command shell as Administrator (for Windows).

This will install the Grunt CLI globally, putting the grunt command in your system path, allowing it to be run from any directory, as long as you also have a local grunt install in that directory.

Now, if you aren't already there, move to this directory of the Ensemble project (where the js, jslib etc. folders live), and install Grunt locally by running:

npm install grunt

This should create a node_modules folder in this directory. You'll need to do this for every new directory that uses Grunt (e.g., if you re-clone Ensemble).

Then, install the project dependencies listed in package.json with:

npm install

See a list of your newly installed packages with npm list --depth=0.

3. Build

And finally, run the following to build the standalone ensemble.js file!

grunt deploy

This will generate a directory called bin with a standalone version of ensemble.js inside. You'll be able run grunt deploy from now on in this directory, without having to repeat any of the above steps.