stitch-rb 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ require('models/user');
2
+
3
+ // Do some stuff
@@ -0,0 +1 @@
1
+ module.exports = "A ORM";
@@ -0,0 +1,4 @@
1
+ var ORM = require('models/orm');
2
+
3
+ var User = Dove.Class.sub();
4
+ User.extend(ORM);
data/examples/compile.rb CHANGED
@@ -2,4 +2,5 @@ $: << File.join(File.dirname(__FILE__), *%w[.. lib])
2
2
 
3
3
  require "stitch"
4
4
 
5
- puts Stitch::Package.new(:paths => ["app"], :dependencies => ["lib"]).compile
5
+ # puts Stitch::Package.new(:paths => ["app"], :dependencies => ["lib"]).compile
6
+ puts Stitch::Package.new(:files => ["./app/index.js"], :root => './app', :dependencies => ["lib"]).compile
data/lib/stitch.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "rubygems"
1
2
  require "json"
2
3
  require "stitch/version"
3
4
 
@@ -4,38 +4,55 @@ module Stitch
4
4
  def compilers
5
5
  @compilers ||= []
6
6
  end
7
-
7
+
8
8
  def inherited(child)
9
9
  Compiler.compilers.unshift(child)
10
10
  end
11
-
11
+
12
+ def all
13
+ Compiler.compilers.select {|c| c.enabled? }
14
+ end
15
+
16
+ def source_extensions
17
+ compilers = all.select {|c| c.source? }
18
+ compilers.map {|c| c.extensions }.flatten
19
+ end
20
+
12
21
  def for_extension(extension)
13
22
  extension.gsub!(/^\./, "")
14
- Compiler.compilers.select(&:enabled?).find do |item|
15
- item.extensions.include?(extension)
23
+ all.find do |item|
24
+ item.extensions.include?(extension)
16
25
  end
17
26
  end
18
-
27
+
19
28
  # Child methods
20
-
29
+
21
30
  def extensions(*exts)
22
31
  @extensions ||= []
23
32
  @extensions |= exts.map(&:to_s)
24
33
  end
25
-
34
+
26
35
  def enabled(value)
27
36
  @enabled = value
28
37
  end
29
-
38
+
30
39
  def enabled?
31
40
  @enabled != false
32
41
  end
33
-
42
+
43
+ def source(value)
44
+ @source = value
45
+ end
46
+
47
+ def source?
48
+ @source || false
49
+ end
50
+
34
51
  def compile(*args)
35
52
  self.new.compile(*args)
36
53
  end
37
54
  end
38
-
55
+
39
56
  def compile(filename)
40
57
  raise "Re-implement"
41
58
  end
@@ -44,4 +61,6 @@ end
44
61
 
45
62
  # Require default compilers
46
63
  require "stitch/compilers/javascript"
47
- require "stitch/compilers/coffeescript"
64
+ require "stitch/compilers/coffeescript"
65
+ require "stitch/compilers/tmpl"
66
+ require "stitch/compilers/mustache"
@@ -1,14 +1,16 @@
1
1
  module Stitch
2
2
  class CoffeeScriptCompiler < Compiler
3
3
  extensions :cs, :coffee
4
-
4
+
5
5
  enabled begin
6
6
  require "coffee-script"
7
7
  true
8
8
  rescue LoadError
9
9
  false
10
10
  end
11
-
11
+
12
+ source true
13
+
12
14
  def compile(path)
13
15
  source = File.read(path)
14
16
  CoffeeScript.compile(source, :filename => path.to_s)
@@ -1,7 +1,9 @@
1
1
  module Stitch
2
2
  class JavaScriptCompiler < Compiler
3
3
  extensions :js
4
-
4
+
5
+ source true
6
+
5
7
  def compile(path)
6
8
  File.read(path)
7
9
  end
@@ -0,0 +1,11 @@
1
+ module Stitch
2
+ class MustacheCompiler < Compiler
3
+ extensions :mustache
4
+
5
+ def compile(path)
6
+ content = File.read(path)
7
+ %{var template = #{content.to_json};
8
+ module.exports = (function(view){ return Mustache.to_html(template, view); });}
9
+ end
10
+ end
11
+ end
@@ -2,23 +2,26 @@ module Stitch
2
2
  class Package
3
3
  DEFAULTS = {
4
4
  :identifier => "require",
5
- :paths => ["lib"],
5
+ :paths => [],
6
+ :files => [],
6
7
  :dependencies => []
7
8
  }
8
-
9
+
9
10
  def initialize(options = {})
10
11
  options = DEFAULTS.merge(options)
11
-
12
+
12
13
  @identifier = options[:identifier]
13
- @paths = options[:paths]
14
- @dependencies = options[:dependencies]
14
+ @paths = Array(options[:paths])
15
+ @files = Array(options[:files])
16
+ @root = options[:root]
17
+ @dependencies = Array(options[:dependencies])
15
18
  end
16
-
19
+
17
20
  def compile
18
21
  [compile_dependencies, compile_sources].join("\n")
19
22
  end
20
-
21
- protected
23
+
24
+ protected
22
25
  def compile_dependencies
23
26
  @dependencies.map {|path|
24
27
  Source.from_path(path)
@@ -26,12 +29,22 @@ module Stitch
26
29
  dep.compile
27
30
  }.join("\n")
28
31
  end
29
-
32
+
30
33
  def compile_sources
31
- sources = @paths.map {|path|
32
- Source.from_path(path)
34
+ sources = @paths.map {|path|
35
+ Source.from_path(path)
33
36
  }.flatten
34
-
37
+
38
+ sources |= @files.map {|file|
39
+ Source.from_file(@root, file)
40
+ }.flatten
41
+
42
+ if sources.any?
43
+ stitch(sources)
44
+ end
45
+ end
46
+
47
+ def stitch(sources)
35
48
  result = <<-EOF
36
49
  (function(/*! Stitch !*/) {
37
50
  if (!this.#{@identifier}) {
@@ -84,11 +97,10 @@ module Stitch
84
97
  return this.#{@identifier}.define;
85
98
  }).call(this)({
86
99
  EOF
87
-
88
100
  sources.each_with_index do |source, i|
89
101
  result += i == 0 ? "" : ", "
90
102
  result += source.name.to_json
91
- result += ": function(exports, require, module) {#{source.compile}}"
103
+ result += ": function(exports, require, module) {\n#{source.compile}\n}"
92
104
  end
93
105
 
94
106
  result += "});\n"
data/lib/stitch/source.rb CHANGED
@@ -6,9 +6,9 @@ module Stitch
6
6
  def from_path(root, path = nil, result = [])
7
7
  path ||= root
8
8
  path = Pathname.new(path)
9
-
9
+
10
10
  if path.directory?
11
- path.children.each do |child|
11
+ path.children.each do |child|
12
12
  from_path(root, child, result)
13
13
  end
14
14
  else
@@ -17,36 +17,79 @@ module Stitch
17
17
  end
18
18
  result
19
19
  end
20
+
21
+ def from_file(root, path = nil, result = [])
22
+ unless path
23
+ path = root
24
+ root = root.dirname
25
+ end
26
+
27
+ source = self.new(root, path)
28
+ source.requires.each do |child|
29
+ from_file(root, child, result)
30
+ end
31
+
32
+ raise "Recursive" if result.include?(source)
33
+ result << source
34
+ end
35
+
36
+ def resolve(path, relative_to)
37
+ path = Pathname.new(path)
38
+ relative_to = Pathname.new(relative_to)
39
+
40
+ unless path.absolute?
41
+ path = path.expand_path(relative_to)
42
+ end
43
+
44
+ return path if path.exist?
45
+
46
+ Compiler.source_extensions.each do |ext|
47
+ candidate = Pathname.new(path.to_s + "." + ext)
48
+ return candidate if candidate.exist?
49
+ end
50
+
51
+ raise "#{path} not found"
52
+ end
20
53
  end
21
-
54
+
22
55
  attr_reader :root, :path
23
-
56
+
24
57
  def initialize(root, path)
25
- @root = Pathname.new(root)
26
- @path = Pathname.new(path)
58
+ @root = Pathname.new(root).expand_path
59
+ @path = Pathname.new(path).expand_path
27
60
  end
28
-
61
+
29
62
  def name
30
63
  name = path.relative_path_from(root)
31
64
  name = name.dirname + name.basename(".*")
32
65
  name.to_s
33
66
  end
34
-
67
+
35
68
  def ext
36
69
  path.extname
37
70
  end
38
-
71
+
39
72
  def compile
40
73
  compiler.compile(path)
41
74
  end
42
-
75
+
43
76
  def valid?
44
77
  !!compiler
45
78
  end
46
-
47
- protected
79
+
80
+ def requires
81
+ return [] unless source?
82
+ requires = path.read.scan(/require\(("|')(.+)\1\)/)
83
+ requires.map {|(_, pn)| self.class.resolve(pn, root) }
84
+ end
85
+
86
+ protected
87
+ def source?
88
+ valid? && compiler.source?
89
+ end
90
+
48
91
  def compiler
49
- Compiler.for_extension(ext)
92
+ @compiler ||= Compiler.for_extension(ext)
50
93
  end
51
94
  end
52
95
  end
@@ -1,3 +1,3 @@
1
1
  module Stitch
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stitch-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-19 00:00:00.000000000Z
12
+ date: 2011-12-03 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A JavaScript package manager
15
15
  email:
@@ -24,7 +24,10 @@ files:
24
24
  - README.md
25
25
  - Rakefile
26
26
  - examples/.DS_Store
27
- - examples/app/controllers/controller.coffee
27
+ - examples/app/controllers/users.coffee
28
+ - examples/app/index.js
29
+ - examples/app/models/orm.js
30
+ - examples/app/models/user.js
28
31
  - examples/compile.rb
29
32
  - examples/lib/jquery.js
30
33
  - examples/lib/lib.coffee
@@ -33,6 +36,7 @@ files:
33
36
  - lib/stitch/compiler.rb
34
37
  - lib/stitch/compilers/coffeescript.rb
35
38
  - lib/stitch/compilers/javascript.rb
39
+ - lib/stitch/compilers/mustache.rb
36
40
  - lib/stitch/compilers/tmpl.rb
37
41
  - lib/stitch/package.rb
38
42
  - lib/stitch/server.rb
@@ -59,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
63
  version: '0'
60
64
  requirements: []
61
65
  rubyforge_project:
62
- rubygems_version: 1.8.6
66
+ rubygems_version: 1.8.10
63
67
  signing_key:
64
68
  specification_version: 3
65
69
  summary: Stitch ported to Ruby