opal 0.3.2 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (203) hide show
  1. data/README.md +85 -127
  2. data/bin/opal +3 -3
  3. data/lib/core.rb +114 -0
  4. data/{gems/core/lib → lib}/core/array.rb +68 -187
  5. data/{gems/core/lib → lib}/core/basic_object.rb +22 -5
  6. data/lib/core/class.rb +38 -0
  7. data/{gems/core/lib → lib}/core/dir.rb +1 -1
  8. data/lib/core/enumerable.rb +33 -0
  9. data/{gems/core/lib → lib}/core/error.rb +1 -4
  10. data/{gems/core/lib → lib}/core/false_class.rb +0 -0
  11. data/{gems/core/lib → lib}/core/file.rb +11 -3
  12. data/{gems/core/lib → lib}/core/hash.rb +12 -0
  13. data/{gems/core/lib → lib}/core/kernel.rb +114 -86
  14. data/lib/core/match_data.rb +33 -0
  15. data/lib/core/module.rb +97 -0
  16. data/{gems/core/lib → lib}/core/nil_class.rb +0 -0
  17. data/{gems/core/lib → lib}/core/numeric.rb +5 -0
  18. data/{gems/core/lib → lib}/core/object.rb +0 -0
  19. data/{gems/core/lib → lib}/core/proc.rb +13 -3
  20. data/{gems/core/lib → lib}/core/range.rb +10 -0
  21. data/{gems/core/lib → lib}/core/regexp.rb +33 -1
  22. data/{gems/core/lib → lib}/core/string.rb +25 -10
  23. data/{gems/core/lib → lib}/core/symbol.rb +3 -3
  24. data/{gems/core/lib → lib}/core/top_self.rb +0 -0
  25. data/{gems/core/lib → lib}/core/true_class.rb +0 -0
  26. data/lib/dev.rb +169 -0
  27. data/{gems/ospec/lib → lib}/ospec.rb +1 -0
  28. data/lib/ospec/autorun.rb +8 -0
  29. data/{gems/ospec/lib → lib}/ospec/dsl.rb +2 -2
  30. data/{gems/ospec/lib → lib}/ospec/example.rb +0 -0
  31. data/{gems/ospec/lib → lib}/ospec/example/before_and_after_hooks.rb +0 -0
  32. data/{gems/ospec/lib → lib}/ospec/example/errors.rb +0 -0
  33. data/{gems/ospec/lib → lib}/ospec/example/example_group.rb +0 -0
  34. data/{gems/ospec/lib → lib}/ospec/example/example_group_factory.rb +0 -3
  35. data/{gems/ospec/lib → lib}/ospec/example/example_group_hierarchy.rb +4 -3
  36. data/{gems/ospec/lib → lib}/ospec/example/example_group_methods.rb +1 -1
  37. data/{gems/ospec/lib → lib}/ospec/example/example_group_proxy.rb +3 -2
  38. data/{gems/ospec/lib → lib}/ospec/example/example_methods.rb +1 -1
  39. data/{gems/ospec/lib → lib}/ospec/example/example_proxy.rb +7 -7
  40. data/{gems/ospec/lib → lib}/ospec/expectations.rb +0 -0
  41. data/{gems/ospec/lib → lib}/ospec/expectations/errors.rb +0 -0
  42. data/{gems/ospec/lib → lib}/ospec/expectations/fail_with.rb +4 -3
  43. data/{gems/ospec/lib → lib}/ospec/expectations/handler.rb +6 -0
  44. data/lib/ospec/helpers/scratch.rb +18 -0
  45. data/{gems/ospec/lib → lib}/ospec/matchers.rb +2 -2
  46. data/{gems/ospec/lib → lib}/ospec/matchers/be.rb +0 -0
  47. data/{gems/ospec/lib → lib}/ospec/matchers/generated_descriptions.rb +0 -0
  48. data/{gems/ospec/lib → lib}/ospec/matchers/operator_matcher.rb +2 -0
  49. data/lib/ospec/matchers/raise_error.rb +38 -0
  50. data/lib/ospec/runner.rb +90 -0
  51. data/{gems/ospec/lib → lib}/ospec/runner/example_group_runner.rb +10 -13
  52. data/lib/ospec/runner/formatter/html_formatter.rb +139 -0
  53. data/{gems/ospec/lib → lib}/ospec/runner/formatter/terminal_formatter.rb +0 -0
  54. data/{gems/ospec/lib → lib}/ospec/runner/options.rb +1 -3
  55. data/{gems/ospec/lib → lib}/ospec/runner/reporter.rb +0 -9
  56. data/lib/racc/parser.rb +165 -0
  57. data/lib/strscan.rb +52 -0
  58. data/{lib → opal_lib}/opal.rb +2 -0
  59. data/opal_lib/opal/build_methods.rb +51 -0
  60. data/opal_lib/opal/builder.rb +164 -0
  61. data/opal_lib/opal/bundle.rb +70 -0
  62. data/opal_lib/opal/command.rb +68 -0
  63. data/{lib → opal_lib}/opal/context.rb +21 -9
  64. data/{lib → opal_lib}/opal/context/console.rb +0 -2
  65. data/opal_lib/opal/context/file_system.rb +34 -0
  66. data/{lib → opal_lib}/opal/context/loader.rb +26 -8
  67. data/opal_lib/opal/gem.rb +84 -0
  68. data/{lib → opal_lib}/opal/rake/builder_task.rb +2 -2
  69. data/opal_lib/opal/rake/spec_task.rb +32 -0
  70. data/{lib → opal_lib}/opal/ruby/nodes.rb +730 -109
  71. data/{lib → opal_lib}/opal/ruby/parser.rb +90 -23
  72. data/opal_lib/opal/ruby/ruby_parser.rb +4862 -0
  73. data/opal_lib/opal/ruby/ruby_parser.y +1454 -0
  74. data/opal_lib/opal/version.rb +4 -0
  75. data/runtime/class.js +359 -0
  76. data/runtime/debug.js +84 -0
  77. data/runtime/fs.js +199 -0
  78. data/runtime/init.js +558 -0
  79. data/runtime/loader.js +351 -0
  80. data/runtime/module.js +109 -0
  81. data/runtime/post.js +10 -0
  82. data/runtime/pre.js +7 -0
  83. data/runtime/runtime.js +351 -0
  84. metadata +88 -175
  85. data/.gitignore +0 -7
  86. data/Changelog +0 -31
  87. data/LICENSE +0 -75
  88. data/Rakefile +0 -86
  89. data/gems/core/README.md +0 -14
  90. data/gems/core/Rakefile +0 -8
  91. data/gems/core/core.gemspec +0 -13
  92. data/gems/core/lib/core.rb +0 -34
  93. data/gems/core/lib/core/class.rb +0 -31
  94. data/gems/core/lib/core/module.rb +0 -100
  95. data/gems/core/lib/core/vm.rb +0 -16
  96. data/gems/core/spec/core/array/append_spec.rb +0 -30
  97. data/gems/core/spec/core/array/assoc_spec.rb +0 -29
  98. data/gems/core/spec/core/array/at_spec.rb +0 -37
  99. data/gems/core/spec/core/array/clear_spec.rb +0 -22
  100. data/gems/core/spec/core/array/collect_bang_spec.rb +0 -27
  101. data/gems/core/spec/core/array/collect_spec.rb +0 -27
  102. data/gems/core/spec/core/array/compact_spec.rb +0 -41
  103. data/gems/core/spec/core/array/concat_spec.rb +0 -15
  104. data/gems/core/spec/core/array/constructor_spec.rb +0 -14
  105. data/gems/core/spec/core/array/each_spec.rb +0 -9
  106. data/gems/core/spec/core/array/element_reference_spec.rb +0 -4
  107. data/gems/core/spec/core/array/first_spec.rb +0 -35
  108. data/gems/core/spec/core/array/include_spec.rb +0 -9
  109. data/gems/core/spec/core/array/join_spec.rb +0 -6
  110. data/gems/core/spec/core/array/last_spec.rb +0 -51
  111. data/gems/core/spec/core/array/length_spec.rb +0 -6
  112. data/gems/core/spec/core/array/map_spec.rb +0 -33
  113. data/gems/core/spec/core/array/reverse_spec.rb +0 -6
  114. data/gems/core/spec/core/builtin_constants/builtin_constants_spec.rb +0 -7
  115. data/gems/core/spec/core/false/and_spec.rb +0 -10
  116. data/gems/core/spec/core/false/inspect_spec.rb +0 -6
  117. data/gems/core/spec/core/false/or_spec.rb +0 -10
  118. data/gems/core/spec/core/false/to_s_spec.rb +0 -6
  119. data/gems/core/spec/core/false/xor_spec.rb +0 -10
  120. data/gems/core/spec/core/file/join_spec.rb +0 -19
  121. data/gems/core/spec/core/hash/assoc_spec.rb +0 -32
  122. data/gems/core/spec/core/kernel/instance_eval_spec.rb +0 -0
  123. data/gems/core/spec/core/kernel/loop_spec.rb +0 -24
  124. data/gems/core/spec/core/kernel/raise_spec.rb +0 -0
  125. data/gems/core/spec/core/module/attr_accessor_spec.rb +0 -28
  126. data/gems/core/spec/core/number/lt_spec.rb +0 -12
  127. data/gems/core/spec/core/string/sub_spec.rb +0 -24
  128. data/gems/core/spec/core/true/and_spec.rb +0 -10
  129. data/gems/core/spec/core/true/inspect_spec.rb +0 -6
  130. data/gems/core/spec/core/true/or_spec.rb +0 -10
  131. data/gems/core/spec/core/true/to_s_spec.rb +0 -6
  132. data/gems/core/spec/core/true/xor_spec.rb +0 -10
  133. data/gems/core/spec/language/and_spec.rb +0 -61
  134. data/gems/core/spec/language/array_spec.rb +0 -68
  135. data/gems/core/spec/language/block_spec.rb +0 -38
  136. data/gems/core/spec/language/break_spec.rb +0 -36
  137. data/gems/core/spec/language/case_spec.rb +0 -103
  138. data/gems/core/spec/language/def_spec.rb +0 -21
  139. data/gems/core/spec/language/eigenclass_spec.rb +0 -60
  140. data/gems/core/spec/language/file_spec.rb +0 -13
  141. data/gems/core/spec/language/fixtures/block.rb +0 -21
  142. data/gems/core/spec/language/fixtures/super.rb +0 -293
  143. data/gems/core/spec/language/hash_spec.rb +0 -29
  144. data/gems/core/spec/language/if_spec.rb +0 -54
  145. data/gems/core/spec/language/loop_spec.rb +0 -11
  146. data/gems/core/spec/language/metaclass_spec.rb +0 -21
  147. data/gems/core/spec/language/method_spec.rb +0 -124
  148. data/gems/core/spec/language/next_spec.rb +0 -25
  149. data/gems/core/spec/language/or_spec.rb +0 -34
  150. data/gems/core/spec/language/redo_spec.rb +0 -24
  151. data/gems/core/spec/language/regexp_spec.rb +0 -26
  152. data/gems/core/spec/language/rescue_spec.rb +0 -20
  153. data/gems/core/spec/language/return_spec.rb +0 -47
  154. data/gems/core/spec/language/string_spec.rb +0 -25
  155. data/gems/core/spec/language/super_spec.rb +0 -32
  156. data/gems/core/spec/language/until_spec.rb +0 -157
  157. data/gems/core/spec/language/variables_spec.rb +0 -155
  158. data/gems/core/spec/language/while_spec.rb +0 -163
  159. data/gems/core/spec/spec_helper.rb +0 -5
  160. data/gems/core_fs/README.md +0 -19
  161. data/gems/dev/Rakefile +0 -5
  162. data/gems/dev/lib/dev.js +0 -99
  163. data/gems/dev/lib/dev/generator.js +0 -1264
  164. data/gems/dev/lib/dev/parser.js +0 -979
  165. data/gems/dev/lib/dev/ruby_parser.js +0 -1088
  166. data/gems/dev/lib/dev/ruby_parser.y +0 -1267
  167. data/gems/dev/lib/dev/string_scanner.js +0 -38
  168. data/gems/dev/tools/racc2js/README.md +0 -39
  169. data/gems/dev/tools/racc2js/math_parser.js +0 -222
  170. data/gems/dev/tools/racc2js/math_parser.rb +0 -133
  171. data/gems/dev/tools/racc2js/math_parser.y +0 -28
  172. data/gems/dev/tools/racc2js/parser.js +0 -218
  173. data/gems/dev/tools/racc2js/racc2js.rb +0 -153
  174. data/gems/json/README.md +0 -4
  175. data/gems/json/json.gemspec +0 -14
  176. data/gems/json/lib/json.rb +0 -64
  177. data/gems/json/lib/json/ext.rb +0 -51
  178. data/gems/json/lib/json/json2.js +0 -481
  179. data/gems/ospec/README.md +0 -0
  180. data/gems/ospec/lib/ospec/autorun.rb +0 -3
  181. data/gems/ospec/lib/ospec/runner.rb +0 -40
  182. data/gems/ospec/lib/ospec/runner/formatter/html_formatter.rb +0 -91
  183. data/gems/ospec/ospec.gemspec +0 -0
  184. data/gems/rquery/README.md +0 -9
  185. data/gems/rquery/lib/rquery.rb +0 -10
  186. data/gems/rquery/lib/rquery/ajax.rb +0 -4
  187. data/gems/rquery/lib/rquery/css.rb +0 -96
  188. data/gems/rquery/lib/rquery/document.rb +0 -25
  189. data/gems/rquery/lib/rquery/element.rb +0 -292
  190. data/gems/rquery/lib/rquery/event.rb +0 -108
  191. data/gems/rquery/lib/rquery/jquery.js +0 -8177
  192. data/gems/rquery/lib/rquery/request.rb +0 -138
  193. data/gems/rquery/lib/rquery/response.rb +0 -49
  194. data/gems/rquery/rquery.gemspec +0 -16
  195. data/lib/opal.js +0 -1597
  196. data/lib/opal/builder.rb +0 -117
  197. data/lib/opal/bundle.rb +0 -131
  198. data/lib/opal/command.rb +0 -11
  199. data/lib/opal/context/file_system.rb +0 -19
  200. data/lib/opal/gem.rb +0 -153
  201. data/lib/opal/ruby/ruby_parser.rb +0 -5170
  202. data/lib/opal/ruby/ruby_parser.y +0 -1298
  203. data/opal.gemspec +0 -15
@@ -0,0 +1,70 @@
1
+ require 'opal/build_methods'
2
+
3
+ module Opal
4
+
5
+ class Bundle
6
+ include BuildMethods
7
+
8
+ attr_reader :gem
9
+
10
+ def initialize(gem)
11
+ @gem = gem
12
+ end
13
+
14
+ def wrap_source(full_path, relative_path = nil)
15
+ relative_path ||= full_path
16
+ content = compile_source full_path
17
+ "'#{relative_path}': #{content}\n"
18
+ end
19
+
20
+ # Actually build the bundle. Returns the result, for now, as a string.
21
+ #
22
+ # @return {String} bundled packages
23
+ def build(options = {})
24
+ gem = @gem
25
+ if options[:test]
26
+ result = build_test_files
27
+ else
28
+ result = build_lib_files
29
+ end
30
+
31
+ if options[:out]
32
+ FileUtils.mkdir_p File.dirname(options[:out])
33
+ File.open(options[:out], 'w+') { |o| o.write result }
34
+ else
35
+ result
36
+ end
37
+ end
38
+
39
+ def build_lib_files
40
+ files = @gem.lib_files
41
+
42
+ bundle = []
43
+ bundle << %[opal.register("#{gem.name}", {]
44
+ bundle << %[ "name": #{gem.name.inspect},]
45
+ bundle << %[ "version": #{gem.version.inspect},]
46
+ bundle << %[ "require_paths": #{gem.require_paths.inspect},]
47
+ bundle << %[ "files": {]
48
+ bundle << %[ #{files.map { |f| wrap_source f }.join(",\n ")}]
49
+ bundle << %[ }]
50
+ bundle << %[});]
51
+
52
+ result = bundle.join ''
53
+ end
54
+
55
+ def build_test_files
56
+ files = @gem.test_files
57
+
58
+ bundle = []
59
+ bundle << %[opal.register("#{gem.name}", {]
60
+ bundle << %[ "name": #{gem.name.inspect},]
61
+ bundle << %[ "files": {]
62
+ bundle << %[ #{files.map { |f| wrap_source f }.join(",\n ")}]
63
+ bundle << %[ }]
64
+ bundle << %[});]
65
+
66
+ result = bundle.join ''
67
+ end
68
+ end
69
+ end
70
+
@@ -0,0 +1,68 @@
1
+ module Opal
2
+ class Command < Thor
3
+
4
+ desc "irb", "Opens interactive opal/ruby repl"
5
+ def irb
6
+ require 'opal/context'
7
+ ctx = Opal::Context.new
8
+ ctx.start_repl
9
+ end
10
+
11
+ desc "spec [FILENAME]", "ospec runner"
12
+ long_desc %[
13
+ This will run the ospec gem in the current working directory
14
+ to load the given specs. If no input files are given then
15
+ ospec will run all specs it can find in the spec/ dir.
16
+ ]
17
+ def spec(*specs)
18
+ require 'opal/context'
19
+
20
+ argv = specs
21
+
22
+ ctx = Opal::Context.new
23
+ ctx.argv = argv
24
+ ctx.require_file 'ospec/autorun'
25
+ end
26
+
27
+ desc "compile [FILE]", "Compile and puts compiled code to stdout"
28
+ long_desc %[
29
+ Basically compile the given file, and output it to the
30
+ stdout. This is useful for testing the compiler to see
31
+ what the generated code will be. If the str name is a
32
+ file that exists, it will be read, otherwise the
33
+ content will be run as a string.
34
+ ]
35
+ method_options :out => :string, :watch => :boolean, :main => :string
36
+ def compile(*path)
37
+ opts = options
38
+ builder = Builder.new
39
+ builder.build :files => path,
40
+ :out => opts["out"], :watch => opts["watch"], :main => opts["main"]
41
+ end
42
+
43
+ desc "bundle", "Bundle the gem in the given directory ready for browser"
44
+ method_options :out => :string
45
+ def bundle
46
+ opts = options
47
+ bundle = Opal::Bundle.new(Opal::Gem.new(Dir.getwd))
48
+ bundle.build opts
49
+ end
50
+
51
+ desc "exec [FILENAME]", "Run the given ruby file"
52
+ def exec(file)
53
+ puts "need to run #{file.inspect}"
54
+ require 'opal/context'
55
+ ctx = Opal::Context.new
56
+ ctx.require_file file
57
+ end
58
+
59
+ def method_missing(task, *)
60
+ if File.exist? task.to_s
61
+ exec task.to_s
62
+ else
63
+ super
64
+ end
65
+ end
66
+ end
67
+ end
68
+
@@ -15,8 +15,11 @@ require 'opal/context/file_system'
15
15
  module Opal
16
16
  class Context < V8::Context
17
17
 
18
+ RUNTIME_PATH = File.expand_path File.join('..', '..', '..', 'runtime.js'), __FILE__
19
+
18
20
  def initialize(opts = {})
19
21
  super opts
22
+ @builder = Opal::Builder.new
20
23
  setup_context
21
24
  end
22
25
 
@@ -25,19 +28,28 @@ module Opal
25
28
  # default "browser" loader cannot access files from disk.
26
29
  def setup_context
27
30
  self['console'] = Console.new
28
- load opal_js_path
31
+ eval "OPAL_DEBUG = true;", "(opal)"
32
+ eval @builder.build_runtime, "(opal)"
29
33
 
30
34
  opal = self['opal']
31
35
  opal['loader'] = Loader.new opal, self
32
- # opal['fs'] = FileSystem.new opal, self
36
+ opal['fs'] = FileSystem.new opal, self
37
+ opal['platform']['engine'] = 'opal-gem'
38
+
39
+ # eval "opal.require('core');", "(opal)"
40
+ require_file 'core'
41
+ end
33
42
 
34
- eval "opal.require('core');", "(opal)"
43
+ # Require the given id as if it was required in the context. This simply
44
+ # passes the require through to the underlying context.
45
+ def require_file(path)
46
+ eval "opal.require('#{path}');", "(opal)"
35
47
  end
36
48
 
37
- # Returns the path to our main opal.js file. This makes it easier as
38
- # we will likely require this file more than once (once per context).
39
- def opal_js_path
40
- File.expand_path File.join(File.dirname(__FILE__), '..', '..', 'lib', 'opal.js')
49
+ # Set ARGV for the context
50
+ def argv=(args)
51
+ puts "setting argv to #{args.inspect}"
52
+ eval "opal.runtime.cs(opal.runtime.Object, 'ARGV', #{args.inspect});"
41
53
  end
42
54
 
43
55
  # Start normal js repl
@@ -53,11 +65,11 @@ module Opal
53
65
  def eval_ruby(content, line = "")
54
66
  begin
55
67
  code = Opal::RubyParser.new(content).parse!.generate_top
56
- code = "var VM = opal.vm, self = VM.top, __FILE__ = '(opal)';" + code
68
+ code = "opal.run(function() {var $rb = opal.runtime, self = $rb.top, __FILE__ = '(opal)';" + code + "});"
57
69
  # puts code
58
70
  self['$opal_irb_result'] = eval code, line
59
71
  # self['$code'].to_s + "ww"
60
- eval "$opal_irb_result.$m.inspect($opal_irb_result);"
72
+ eval "!$opal_irb_result.$m.inspect.$rbMM ? $opal_irb_result.$m.inspect($opal_irb_result) : '(Object doesnt support #inspect)'"
61
73
  rescue => e
62
74
  puts e
63
75
  puts("\t" + e.backtrace.join("\n\t"))
@@ -1,6 +1,4 @@
1
-
2
1
  module Opal
3
-
4
2
  class Console
5
3
 
6
4
  def log(*str)
@@ -0,0 +1,34 @@
1
+ module Opal
2
+ class FileSystem
3
+
4
+ def initialize(opal, context)
5
+ @opal = opal
6
+ @ctx = context
7
+ end
8
+
9
+ def cwd
10
+ Dir.getwd
11
+ end
12
+
13
+ def glob(*arr)
14
+ Dir.glob(arr)
15
+ end
16
+
17
+ def exist_p(path)
18
+ File.exist? path
19
+ end
20
+
21
+ def expand_path(filename, dir_string = nil)
22
+ File.expand_path filename, dir_string
23
+ end
24
+
25
+ def dirname(file)
26
+ File.dirname file
27
+ end
28
+
29
+ def join(*parts)
30
+ File.join *parts
31
+ end
32
+ end
33
+ end
34
+
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Opal
3
- # Custom loader class, which an instance is set on the vienna.loader property
3
+ # Custom loader class, which an instance is set on the opal.loader property
4
4
  # to allow for disk based loading and access to modules/packages/gems.
5
5
  #
6
6
  # For now, this simply adds hard-coded paths to the load path. In future,
@@ -22,6 +22,10 @@ module Opal
22
22
  @vienna = vienna
23
23
  @ctx = context
24
24
  @paths = []
25
+
26
+ # core lib/std lib
27
+ @paths << File.expand_path(File.join('..', '..', '..', '..', 'lib'), __FILE__)
28
+
25
29
  hardcode_gems
26
30
  end
27
31
 
@@ -29,6 +33,7 @@ module Opal
29
33
  # proper package manager. This needs to be replaced to be more dynamic in
30
34
  # lookup.
31
35
  def hardcode_gems
36
+ return nil
32
37
  core = File.expand_path(File.join(__FILE__, '..', '..', '..', '..', 'gems'))
33
38
 
34
39
  Dir.open(core).each do |entry|
@@ -58,14 +63,14 @@ module Opal
58
63
  end
59
64
 
60
65
  # Exposed as replacement method.
61
- def resolve_module(id, parent)
62
- resolved = find_module id, @paths
63
- raise "Cannot find module '#{id}'" unless resolved
66
+ def resolve_lib(id)
67
+ resolved = find_lib id, @paths
68
+ raise "Cannot find lib '#{id}'" unless resolved
64
69
  resolved
65
70
  end
66
71
 
67
72
  # Resolve the id requested with the given valid paths
68
- def find_module(id, paths)
73
+ def find_lib(id, paths)
69
74
  extensions = valid_extensions
70
75
 
71
76
  @paths.each do |path|
@@ -76,6 +81,19 @@ module Opal
76
81
  # if file exists, return it!
77
82
  return candidate if File.exists? candidate
78
83
  end
84
+
85
+ # if has extension already
86
+ candidate = File.join(path, id)
87
+ return candidate if File.exists? candidate
88
+ end
89
+
90
+ # alternatively, if id is a full path, just load it
91
+ if File.exist? id
92
+ return File.expand_path id
93
+ end
94
+
95
+ if File.exist? id + '.rb'
96
+ return File.expand_path(id + '.rb')
79
97
  end
80
98
 
81
99
  # if we cannot find it, just return nil
@@ -88,7 +106,7 @@ module Opal
88
106
  #
89
107
  # @param {String} filename The filename to load. This is a full filename,
90
108
  # not just a module id.
91
- def module_contents(filename)
109
+ def file_contents(filename)
92
110
  File.read filename
93
111
  end
94
112
 
@@ -97,7 +115,7 @@ module Opal
97
115
  # to vienna so that it makes it easier to compile and run ruby from the
98
116
  # v8 context. The default implementation of this throws an error to say
99
117
  # that in browser ruby code cannot be run.
100
- def ruby_module_contents(filename)
118
+ def ruby_file_contents(filename)
101
119
  parser = Opal::RubyParser.new File.read(filename)
102
120
  result = parser.parse!.generate_top
103
121
  result
@@ -108,7 +126,7 @@ module Opal
108
126
  # require etc, and returns the function ready for calling. If an
109
127
  # error occures (likely a parse error) it is just thrown as normal
110
128
  def wrap(content, filename)
111
- code = "(function(VM, self, __FILE__) { #{content} });"
129
+ code = "(function($rb, self, __FILE__) { #{content} });"
112
130
  # puts code
113
131
  @ctx.eval code, filename
114
132
  end
@@ -0,0 +1,84 @@
1
+ module Opal
2
+
3
+ # The gem class wraps around RubyGem's Gem::Specification class to basically add
4
+ # support for building a gem ready for the browser. The actual gem object can be
5
+ # accessed through {#spec}.
6
+ class Gem
7
+
8
+ # The real Gem::Specification object, which is used for accessing gem info
9
+ attr_reader :spec
10
+
11
+ # Root directory for the package
12
+ attr_reader :root_dir
13
+
14
+ def initialize(rootdir = Dir.getwd)
15
+ require 'rubygems'
16
+ @root_dir = rootdir
17
+ path = File.expand_path File.join(@root_dir, File.basename(@root_dir) + '.gemspec')
18
+
19
+ # we MUST chdir here to make sure files globs etc in the gemspec use the basedir
20
+ # as the working directory, otherwise opal, or the main gem working dir will
21
+ # accidentally be the cwd.
22
+ Dir.chdir(File.dirname(path)) do
23
+ @spec = ::Gem::Specification.load path
24
+ end
25
+
26
+ raise "Bad/missing gemspec in #{rootdir}" unless @spec
27
+ end
28
+
29
+ def bundle(opts = {})
30
+ Opal::Bundle.new(self).build opts
31
+ end
32
+
33
+ def name
34
+ @spec.name
35
+ end
36
+
37
+ def version
38
+ @spec.version.to_s
39
+ end
40
+
41
+ def require_paths
42
+ @spec.require_paths
43
+ end
44
+
45
+ def files
46
+ @spec.files
47
+ end
48
+
49
+ def test_files
50
+ @spec.test_files
51
+ end
52
+
53
+ def to_s
54
+ "#<Gem '#{name}'>"
55
+ end
56
+
57
+ # Returns an array of dependencies for this package. Version requirements
58
+ # are ignored for now. This simply returns the array of package names that
59
+ # are needed. If the package.json does not declare any, then an empty
60
+ # array is returned.
61
+ #
62
+ # @return {Array<String>}
63
+ def dependencies
64
+ dependencies = @spec.dependencies
65
+
66
+ dependencies.map { |d| d.name }
67
+ end
68
+
69
+ # Returns the files from the .files property of the gem that are only
70
+ # available from the require_paths array. Basically, these are the files
71
+ # that are our 'lib' files. They are returned as relative to the root.
72
+ #
73
+ # @return [Array<String>]
74
+ def lib_files
75
+ paths = require_paths
76
+ ext = %w[.rb .js]
77
+
78
+ files.select do |f|
79
+ f.start_with?(*paths) && ext.include?(File.extname(f))
80
+ end
81
+ end
82
+ end
83
+ end
84
+
@@ -34,8 +34,8 @@ module Opal
34
34
  options[:main] = @main if @main
35
35
  options[:watch] = @watch
36
36
 
37
- builder = Opal::Builder.new options
38
- builder.build
37
+ builder = Opal::Builder.new
38
+ builder.build options
39
39
  end
40
40
  end
41
41
  end
@@ -0,0 +1,32 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+
4
+ module Opal
5
+
6
+ module Rake
7
+
8
+ class SpecTask < ::Rake::TaskLib
9
+
10
+ def initialize(name = :spec)
11
+ @name = name
12
+ yield self if block_given?
13
+ define
14
+ end
15
+
16
+ def define
17
+ desc "Build ospec files ready for browser"
18
+ task(@name) do
19
+ base = File.basename Dir.getwd
20
+ gem = Opal::Gem.new Dir.getwd
21
+ content = gem.bundle :dependencies => 'ospec',
22
+ :main => 'ospec/autorun', :test_files => true
23
+
24
+ File.open("tmp/#{base}.spec.js", 'w+') do |out|
25
+ out.write content
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+