mirror of
https://github.com/fazo96/pbs.git
synced 2025-01-29 14:54:18 +01:00
separated lib from cli, --verbose broken
This commit is contained in:
parent
ecfaf08b46
commit
d27c9b9cb3
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
test/
|
test/
|
||||||
|
lib/
|
||||||
bin/
|
bin/
|
||||||
|
4
build.sh
4
build.sh
@ -1,4 +0,0 @@
|
|||||||
mkdir -p bin
|
|
||||||
echo '#!/usr/bin/env node' > bin/pert
|
|
||||||
coffee -b -c --no-header -p src/pert.coffee >> bin/pert
|
|
||||||
chmod +x bin/pert
|
|
@ -2,13 +2,14 @@
|
|||||||
"name": "pert",
|
"name": "pert",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"description": "pert diagram calculator",
|
"description": "pert diagram calculator",
|
||||||
"main": "pert.coffee",
|
"main": "lib/pert.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
"pert": "./bin/pert"
|
"pert": "./bin/pert"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepublish": "./build.sh",
|
"prepublish": "scripts/build.sh",
|
||||||
"build": "./build.sh"
|
"build": "scripts/build.sh",
|
||||||
|
"clean": "scripts/clean.sh"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
5
scripts/build.sh
Executable file
5
scripts/build.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
mkdir -p bin lib
|
||||||
|
echo '#!/usr/bin/env node' > bin/pert
|
||||||
|
coffee -b -c --no-header -p src/pert-cli.coffee >> bin/pert
|
||||||
|
chmod +x bin/pert
|
||||||
|
coffee -b -c --no-header -p src/pert.coffee > lib/pert.js
|
2
scripts/clean.sh
Executable file
2
scripts/clean.sh
Executable file
@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
rm -rf bin lib
|
40
src/pert-cli.coffee
Executable file
40
src/pert-cli.coffee
Executable file
@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env coffee
|
||||||
|
chalk = require 'chalk'
|
||||||
|
cli = require 'commander'
|
||||||
|
fs = require 'fs'
|
||||||
|
pert = require '../lib/pert.js'
|
||||||
|
|
||||||
|
ex = [{id: 0, depends: [], duration: 2}, { id: 1, depends: [0], duration: 3},{id: 2, depends: [0,1], duration: 4}]
|
||||||
|
|
||||||
|
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:'), pert.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 (pert.calculate JSON.parse(content))
|
||||||
|
else console.log pert.calculate JSON.parse(content)
|
||||||
|
|
||||||
|
cli.parse process.argv
|
||||||
|
|
||||||
|
if !didSomething then console.log chalk.green('Tip:'), 'see', chalk.bold(cli.name()+' --help')
|
117
src/pert.coffee
117
src/pert.coffee
@ -1,85 +1,52 @@
|
|||||||
#!/usr/bin/env coffee
|
|
||||||
chalk = require 'chalk'
|
chalk = require 'chalk'
|
||||||
cli = require 'commander'
|
|
||||||
fs = require 'fs'
|
fs = require 'fs'
|
||||||
|
|
||||||
log = (x...) -> if cli.verbose then console.log chalk.bold("Log:"), x...
|
pert =
|
||||||
err = (x...) -> console.log chalk.bold (chalk.red "Error:"), x...
|
log: (x...) -> if @verbose then console.log chalk.bold("Pert:"), x...
|
||||||
|
err: (x...) -> console.log chalk.bold (chalk.red "Pert:"), x...
|
||||||
|
|
||||||
# Returns the highest number in an array of numbers
|
# Returns the highest number in an array of numbers
|
||||||
maxa = (l) -> return Math.max.apply null, l
|
maxa: (l) -> return Math.max.apply null, l
|
||||||
|
|
||||||
# Find the activity with given id
|
# Find the activity with given id
|
||||||
toActivity = (id,list) ->
|
toActivity: (id,list) ->
|
||||||
if !list? then err "list is",list
|
if !list? then pert.err "list is",list
|
||||||
item = {}
|
item = {}
|
||||||
list.forEach (x) -> if x.id is id then item = x
|
list.forEach (x) -> if x.id is id then item = x
|
||||||
return item
|
return item
|
||||||
|
|
||||||
# Find the item
|
# Find the item
|
||||||
calculateEndDay = (item,list) ->
|
calculateEndDay: (item,list) ->
|
||||||
if !item.startDay?
|
if !item.startDay?
|
||||||
log "calculating start day of",item.id
|
pert.log "calculating start day of",item.id
|
||||||
item.startDay = calculateStartDay item, list
|
item.startDay = pert.calculateStartDay item, list
|
||||||
log "start day of",item.id,"is",item.startDay
|
pert.log "start day of",item.id,"is",item.startDay
|
||||||
item.endDay = item.startDay + item.duration
|
item.endDay = item.startDay + item.duration
|
||||||
log "end day of",item.id,"is",item.endDay
|
pert.log "end day of",item.id,"is",item.endDay
|
||||||
return item.endDay
|
return item.endDay
|
||||||
|
|
||||||
# Find out which day the activity starts
|
# Find out which day the activity starts
|
||||||
calculateStartDay = (item,list) ->
|
calculateStartDay: (item,list) ->
|
||||||
if !item.depends? or item.depends.length is 0 then return item.startDay = 0
|
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)
|
item.startDay = pert.maxa item.depends.map((x) -> pert.toActivity x,list).map((x) -> pert.calculateEndDay x, list)
|
||||||
log "start day of",item.id,"is",item.startDay
|
pert.log "start day of",item.id,"is",item.startDay
|
||||||
# write max delay time to each depend
|
# write max delay time to each depend
|
||||||
item.depends.forEach (x) ->
|
item.depends.forEach (x) ->
|
||||||
log "checking permittedDelay to dependency", x, "of", item
|
pert.log "checking permittedDelay to dependency", x, "of", item
|
||||||
i = toActivity x, list
|
i = pert.toActivity x, list
|
||||||
if !i.permittedDelay?
|
if !i.permittedDelay?
|
||||||
i.permittedDelay = item.startDay - calculateEndDay i, list
|
i.permittedDelay = item.startDay - pert.calculateEndDay i, list
|
||||||
log "written permittedDelay to dependency", x, "of", item, "as", i.permittedDelay
|
pert.log "written permittedDelay to dependency", x, "of", item, "as", i.permittedDelay
|
||||||
else log "aborting permittedDelay: already calculated"
|
else pert.log "aborting permittedDelay: already calculated"
|
||||||
log "permitted delay of",x,"is",i.permittedDelay
|
pert.log "permitted delay of",x,"is",i.permittedDelay
|
||||||
return item.startDay
|
return item.startDay
|
||||||
|
|
||||||
# Find out which activity has the highest id
|
# Find out which activity has the highest id
|
||||||
highestID = (list) -> return maxa(list.map (x) -> x.id)
|
highestID: (list) -> return pert.maxa(list.map (x) -> x.id)
|
||||||
|
|
||||||
calculate = (list) ->
|
calculate: (list,verbose) ->
|
||||||
calculateEndDay (toActivity highestID(list), list), list
|
pert.verbose = verbose
|
||||||
return list
|
pert.calculateEndDay (pert.toActivity pert.highestID(list), list), list
|
||||||
|
return list
|
||||||
|
|
||||||
ex = [{id: 0, depends: [], duration: 2}, { id: 1, depends: [0], duration: 3},{id: 2, depends: [0,1], duration: 4}]
|
module.exports = pert
|
||||||
|
|
||||||
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')
|
|
||||||
|
Loading…
Reference in New Issue
Block a user