2015-03-13 11:01:18 +01:00
|
|
|
#!/usr/bin/env coffee
|
2015-03-13 11:44:12 +01:00
|
|
|
chalk = require 'chalk'
|
2015-03-13 12:37:53 +01:00
|
|
|
cli = require 'commander'
|
|
|
|
fs = require 'fs'
|
2015-03-13 11:01:18 +01:00
|
|
|
|
2015-03-13 12:37:53 +01:00
|
|
|
log = (x...) -> if cli.verbose then console.log chalk.bold("Log:"), x...
|
2015-03-13 11:44:12 +01:00
|
|
|
err = (x...) -> console.log chalk.bold (chalk.red "Error:"), x...
|
2015-03-13 11:01:18 +01:00
|
|
|
|
2015-03-13 11:44:12 +01:00
|
|
|
# Returns the highest number in an array of numbers
|
2015-03-13 11:01:18 +01:00
|
|
|
maxa = (l) -> return Math.max.apply null, l
|
|
|
|
|
|
|
|
# Find the activity with given id
|
2015-03-13 11:44:12 +01:00
|
|
|
toActivity = (id,list) ->
|
|
|
|
if !list? then err "list is",list
|
2015-03-13 11:01:18 +01:00
|
|
|
item = {}
|
|
|
|
list.forEach (x) -> if x.id is id then item = x
|
|
|
|
return item
|
|
|
|
|
|
|
|
# Find the item
|
2015-03-13 11:44:12 +01:00
|
|
|
calculateEndDay = (item,list) ->
|
2015-03-13 11:01:18 +01:00
|
|
|
if !item.startDay?
|
2015-03-13 11:44:12 +01:00
|
|
|
log "calculating start day of",item.id
|
|
|
|
item.startDay = calculateStartDay item, list
|
|
|
|
log "start day of",item.id,"is",item.startDay
|
2015-03-13 11:01:18 +01:00
|
|
|
item.endDay = item.startDay + item.duration
|
2015-03-13 11:44:12 +01:00
|
|
|
log "end day of",item.id,"is",item.endDay
|
2015-03-13 11:01:18 +01:00
|
|
|
return item.endDay
|
|
|
|
|
|
|
|
# Find out which day the activity starts
|
2015-03-13 11:44:12 +01:00
|
|
|
calculateStartDay = (item,list) ->
|
2015-03-13 12:37:53 +01:00
|
|
|
if !item.depends? or item.depends.length is 0 then return item.startDay = 0
|
|
|
|
item.startDay = maxa item.depends.map((x) -> toActivity x,list).map((x) -> calculateEndDay x, list)
|
|
|
|
log "start day of",item.id,"is",item.startDay
|
2015-03-13 11:01:18 +01:00
|
|
|
# write max delay time to each depend
|
|
|
|
item.depends.forEach (x) ->
|
2015-03-13 12:37:53 +01:00
|
|
|
log "checking permittedDelay to dependency", x, "of", item
|
2015-03-13 11:44:12 +01:00
|
|
|
i = toActivity x, list
|
2015-03-13 12:37:53 +01:00
|
|
|
if !i.permittedDelay?
|
|
|
|
i.permittedDelay = item.startDay - calculateEndDay i, list
|
|
|
|
log "written permittedDelay to dependency", x, "of", item, "as", i.permittedDelay
|
|
|
|
else log "aborting permittedDelay: already calculated"
|
2015-03-13 11:44:12 +01:00
|
|
|
log "permitted delay of",x,"is",i.permittedDelay
|
2015-03-13 12:37:53 +01:00
|
|
|
return item.startDay
|
2015-03-13 11:01:18 +01:00
|
|
|
|
|
|
|
# Find out which activity has the highest id
|
2015-03-13 11:44:12 +01:00
|
|
|
highestID = (list) -> return maxa(list.map (x) -> x.id)
|
2015-03-13 11:01:18 +01:00
|
|
|
|
2015-03-13 11:44:12 +01:00
|
|
|
calculate = (list) ->
|
|
|
|
calculateEndDay (toActivity highestID(list), list), list
|
2015-03-13 11:01:18 +01:00
|
|
|
return list
|
|
|
|
|
2015-03-13 11:44:12 +01:00
|
|
|
ex = [{id: 0, depends: [], duration: 2}, { id: 1, depends: [0], duration: 3},{id: 2, depends: [0,1], duration: 4}]
|
2015-03-13 12:37:53 +01:00
|
|
|
|
|
|
|
cli
|
|
|
|
.version '0.1'
|
|
|
|
.usage 'loads activity data from JSON and computes the possible activity delays'
|
|
|
|
.option '--verbose', 'be verbose (for debugging)'
|
|
|
|
|
|
|
|
didSomething = no
|
|
|
|
|
|
|
|
cli
|
|
|
|
.command 'example'
|
|
|
|
.description 'show an example of the JSON data format'
|
|
|
|
.action ->
|
|
|
|
didSomething = yes
|
|
|
|
console.log chalk.bold.green('Before:'), ex
|
|
|
|
console.log chalk.bold.green('After calculations:'), calculate ex
|
|
|
|
console.log chalk.green 'Tip:',chalk.bold 'optional fields can be freely included in the input data'
|
|
|
|
|
|
|
|
cli
|
|
|
|
.command 'calculate <file>'
|
|
|
|
.description 'calculate data on given JSON document'
|
|
|
|
.alias 'c'
|
|
|
|
.option '-j, --json', 'output json data'
|
|
|
|
.action (file,options) ->
|
|
|
|
didSomething = yes
|
|
|
|
fs.readFile file, (error,content) ->
|
|
|
|
if error then err error
|
|
|
|
else
|
|
|
|
if options.json then console.log JSON.stringify (calculate JSON.parse(content))
|
|
|
|
else console.log calculate JSON.parse(content)
|
|
|
|
|
|
|
|
cli.parse process.argv
|
|
|
|
|
|
|
|
if !didSomething then console.log chalk.green('Tip:'), 'see', chalk.bold(cli.name()+' --help')
|