Skip to content
Browse files

stable 1

  • Loading branch information...
1 parent 028b818 commit 85cfec45fd976db1c5869442cc60ad0a9a5459b4 @dalssoft dalssoft committed Feb 14, 2013
View
16 example/project1/Cakefile
@@ -0,0 +1,16 @@
+{exec, spawn} = require 'child_process'
+Rehab = require '../../src/rehab'
+
+build = ->
+ console.log "Building project from src/*.coffee to lib/app.js"
+ join_to_single_file = "--join lib/app.js"
+
+ rehab = new Rehab()
+ files = rehab.process './src'
+
+ console.log "FILES:", files
+ exec "coffee #{join_to_single_file} --compile --output lib/", (err, stdout, stderr) ->
+ throw err if err
+
+task 'build', 'Build coffee2js using Rehab', sbuild = ->
+ build()
View
3 example/project1/src/model/model1.coffee
@@ -0,0 +1,3 @@
+#_require ../repository/repo1.coffee
+
+class Model1
View
1 example/project1/src/repository/repo1.coffee
@@ -0,0 +1 @@
+class Repo1
View
3 example/project1/src/view/view1.coffee
@@ -0,0 +1,3 @@
+#_require ../model/model1.coffee
+
+class View1
View
65 src/rehab.coffee
@@ -3,29 +3,63 @@
wrench = require('wrench')
fs = require('fs')
tsort = require('./tsort')
+path = require('path')
module.exports = class Rehab
String::beginsWith = (str) -> if @match(new RegExp "^#{str}") then true else false
String::endsWith = (str) -> if @match(new RegExp "#{str}$") then true else false
REQ_TOKEN: "#_require"
- REQ_MAIN_NODE: "*MAIN*"
+ REQ_MAIN_NODE: "__MAIN__"
process: (folder) ->
- depInfo = @processDependencyInfo(folder)
- console.log depInfo
- @processDependencyList depInfo
+ # create a graph from a folder name:
+ # C <- A -> B.coffee -> C
+ depGraph = @processDependencyGraph(folder)
+ console.log "1: processDependencyGraph", depGraph
- processDependencyInfo: (folder) ->
- depInfo = []
+ # normalize filenames:
+ # C.coffee <- A.coffee -> B.coffee -> C.coffee
+ depGraph = @normalizeFilename(folder, depGraph)
+ console.log "2: normalizeFilename", depGraph
+
+ # create a graph from a folder name:
+ # A.coffee -> B.coffee -> C.coffee
+ depList = @processDependencyList depGraph
+ console.log "3: processDependencyList", depList
+ depList
+
+ processDependencyGraph: (folder) ->
+ depGraph = []
for f in (@getSourceFiles folder)
- @parseRequiredFile folder, f, depInfo
- depInfo
+ @parseRequiredFile folder, f, depGraph
+ depGraph
+
+ normalizeFilename: (folder, depGraph) ->
+ # [[./fileA, fileB]] => [[fileA, fileB]]
+ for edge in depGraph
+ file = edge[1]
+ continue if file == @REQ_MAIN_NODE
+
+ fileDep = @normalizeCoffeeFilename(edge[0])
+ fileDep = path.normalize fileDep
+
+ file = @normalizeCoffeeFilename(file)
+
+ fullPath = path.resolve path.dirname(fileDep), file
+ file = path.join(folder, path.relative(folder, fullPath))
+ edge[0..1] = [fileDep, file]
+ depGraph
+
+ normalizeCoffeeFilename: (file) ->
+ file = "#{file}.coffee" unless file.endsWith ".coffee"
+ file
+
- processDependencyList: (depInfo) ->
- depList = tsort(depInfo)
- depList.filter (i) -> i isnt @REQ_MAIN_NODE
+ processDependencyList: (depGraph) ->
+ depList = tsort(depGraph)
+ depList.filter (i) => not i.beginsWith @REQ_MAIN_NODE
getSourceFiles: (folder) ->
files = wrench.readdirSyncRecursive folder
@@ -34,10 +68,11 @@ module.exports = class Rehab
parseRequiredLine: (line) ->
line.replace "#{@REQ_TOKEN} ", ""
- parseRequiredFile: (folder, file, depInfo) ->
- depInfo.push [file, @REQ_MAIN_NODE] #every file depends on MAIN
+ parseRequiredFile: (folder, file, depGraph) ->
+ fileName = path.join(folder, file)
+ depGraph.push [fileName, @REQ_MAIN_NODE] #every file depends on MAIN (a fake file)
- content = fs.readFileSync(folder + file, 'utf8')
+ content = fs.readFileSync(fileName, 'utf8')
lines = content.split '\n'
for line in lines
- depInfo.push [file, @parseRequiredLine(line)] if line.beginsWith @REQ_TOKEN
+ depGraph.push [fileName, @parseRequiredLine(line)] if line.beginsWith @REQ_TOKEN
View
2 test.coffee
@@ -1,4 +1,4 @@
Rehab = require './src/rehab'
rehab = new Rehab()
-console.log rehab.process './src/'
+console.log rehab.process './example/project1/'
View
63 test/rehab_test.coffee
@@ -2,27 +2,54 @@ require 'source-map-support'
Rehab = require '../src/rehab'
describe "rehab", ->
+ describe "normalizeFilePath", ->
+ describe "should return a graph", ->
+ it "desnormalized file path", ->
+ #given
+ files = [["fileA", "fileB"], ["./fileB", "fileC"]]
+
+ #when
+ rehab = new Rehab()
+ sorted = rehab.normalizeFilename '.', files
+
+ #then
+ sorted.should.eql files = [["fileA.coffee", "fileB.coffee"], ["fileB.coffee", "fileC.coffee"]]
+
+ it "desnormalized relative file path", ->
+ #given
+ files = [["src/model/mode1", "../repo/repo1"], ["src/view/view1", "../model/model1"]]
+
+ #when
+ rehab = new Rehab()
+ sorted = rehab.normalizeFilename '.', files
+
+ #then
+ sorted.should.eql files = [["src/model/mode1.coffee", "src/repo/repo1.coffee"], ["src/view/view1.coffee", "src/model/model1.coffee"]]
+
describe "processDependencyList", ->
- it "should return a ordered list", ->
- #given
- files = [["fileA", "fileB"], ["fileB", "fileC"], ["fileC", "fileD"]]
+ describe "should return a ordered list when", ->
+ it "sequence dependence", ->
+ #given
+ files = [["fileA", "fileB"], ["fileB", "fileC"], ["fileC", "fileD"]]
+
+ #when
+ rehab = new Rehab()
+ sorted = rehab.processDependencyList files
+
+ #then
+ sorted.should.eql [ 'fileA', 'fileB', 'fileC', 'fileD' ]
- #when
- rehab = new Rehab()
- sorted = rehab.processDependencyList files
+ it "only one dependence informed", ->
+ REQ_MAIN_NODE = "__MAIN__"
- #then
- sorted.should.eql [ 'fileA', 'fileB', 'fileC', 'fileD' ]
+ #given
+ files = [[REQ_MAIN_NODE, "fileB"], ["fileB", "fileC"], [REQ_MAIN_NODE, "fileC"]]
- it "with MAIN info, should return a ordered list", ->
- REQ_MAIN_NODE = "*MAIN*"
-
- #given
- files = [[REQ_MAIN_NODE, "fileB"], ["fileB", "fileC"], [REQ_MAIN_NODE, "fileB"]]
+ #when
+ rehab = new Rehab()
+ sorted = rehab.processDependencyList files
- #when
- rehab = new Rehab()
- sorted = rehab.processDependencyList files
+ #then
+ sorted.should.eql [ 'fileB', 'fileC' ]
- #then
- sorted.should.eql [ REQ_MAIN_NODE, 'fileB', 'fileC' ]
+

0 comments on commit 85cfec4

Please sign in to comment.
Something went wrong with that request. Please try again.