brewfile.coffee | |
---|---|
This module is used internally to parse a Brewfile at the root folder of a given project. It exports a single function :
| |
Some essential core modules are loaded, as well as coffee-script since a brewfile is written using this language. | _ = require 'underscore'
vm = require 'vm'
fs = require 'fs'
coffeescript = require 'coffee-script' |
Brewer classes (and utilities) are imported | {debug} = require './command'
brewer = require './index' |
Source directivesThis is the prototype of a Source configuration object.
The functions defined here are available in the
function body provided at the end of a | Source = |
The method used to specify options on a source configuration object | options: (opts) ->
_.extend @opts, opts
|
Package directivesThis is the prototype of a Package configuration object.
The functions defined here are available in the
function body provided at the end of a package statement
such as | Package = |
A method used to specify options on a package configuration object. | options: (opts) ->
_.extend @opts, opts
|
A method used to define bundles within a package | bundles: (bundles...) ->
@opts.bundles ?= []
@opts.bundles = _.union @opts.bundles, bundles
|
A method used to define a source in a package. The type of the source here is inferred by the default source type of the parent package | source: (path, opts, cb) ->
@_source.call this, @opts.type, path, opts, cb
|
A common method used to define any type of
source in a package.
A source configuration object is initialized
with the Source prototype above.
The options object is initialized then
put in the source object. We add the source
config object to parent package, then call the trailing
function, if present with | _source: (type, path, opts, cb) ->
src = Object.create Source
if _.isFunction opts
cb = opts
opts = {}
opts ?= {}
opts.type ?= type
opts.path = path
src.opts = opts
@srcs.push src.opts
cb.call src if _.isFunction cb
|
Iterate through all defined source types to add their type names to the Package object prototype, to add them as proxies to the Package._source method | _.each brewer.Source.types(), (key) ->
Package[key] = (path, options, cb) ->
Package._source.call this, brewer.Source[key].type, path, options, cb
|
The function used to define a package object in a Brewfile. | createPackage = (type, name, opts, cb) -> |
A package config object is initialized with the Package prototype above, then added to the list of packages for the Brewfile. | pkg = Object.create Package
@packages.push pkg
|
The package options are set using the provided values | if _.isFunction opts
cb = opts
opts = {}
opts.type ?= type
opts.name = name
pkg.opts = _.clone opts
pkg.srcs = []
|
Then the trailing function is called with | cb.call pkg if _.isFunction cb |
A utility function used to initialize a V8 Context to encapsulate the configuration included in the Brewfile and all the DSL functions. | newContext = () ->
ctx = {
project: prj = {
root: '.'
packages: []
reqs: {}
vendorDir: './vendor'
}
}
|
Iterate through all package types, using their
type name to proxy the | _.each brewer.Package.types(), (key) ->
ctx[key] = (name, opts, cb) ->
createPackage.call ctx.project, brewer.Package[key].type, name, opts, cb
|
Define DSL functions to specify properties of the project | ctx.root = (newRoot) -> prj.root = newRoot
ctx.vendor = (newVendorDir) -> prj.vendorDir = newVendorDir
|
Define the DSL | ctx.require = (reqs) ->
if _.isArray reqs
for req in reqs when req not of prj.reqs
prj.reqs[lib] = 'latest'
else if _.isString reqs
if reqs not in prj.reqs
prj.reqs[reqs] = 'latest'
else if _.isObject reqs
for key, value of reqs
prj.reqs[key] = value
|
Return a V8 context, using the container above as a seed. | vm.createContext ctx |
This is the exported function, which takes a path to a Brewfile
as argument and returns a configuration object containing all the
packages. It uses Coffee-script's | @configs = configs = (file) ->
coffeescript.eval fs.readFileSync(file, 'utf-8'),
sandbox: ctx = newContext()
filename: file
return ctx.project
|