public
Rubygem
Description: Johnson wraps JavaScript in a loving Ruby embrace.
Homepage: http://github.com/jbarnette/johnson/wikis
Clone URL: git://github.com/jbarnette/johnson.git
commit  5c40882c3aba3ce1f26bb07fee4593cc1199888b
tree    04c0ca283c1593021acf23d46099262cc7ad98aa
parent  57fe54d7dbd7dbb5dd3a23a2158db3c962fd885f
johnson / Rakefile
100644 162 lines (130 sloc) 4.387 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require "erb"
 
require "./lib/hoe.rb"
require "./lib/johnson/version.rb"
 
# what sort of extension are we building?
kind = Config::CONFIG["DLEXT"]
 
CROSS = ENV["CROSS"]
LIBJS = FileList["vendor/spidermonkey/#{CROSS || ''}*.OBJ/libjs.{#{kind},so}"].first || :libjs
 
GENERATED_NODE = "ext/spidermonkey/immutable_node.c"
 
HOE = Hoe.new("johnson", Johnson::VERSION) do |p|
  p.author = ["John Barnette", "Aaron Patterson", "Yehuda Katz", "Matthew Draper"]
  p.changes = p.paragraphs_of("CHANGELOG", 0..1).join("\n\n")
  p.email = "johnson-talk@googlegroups.com"
  p.rubyforge_name = "johnson"
  p.description = "Johnson wraps JavaScript in a loving Ruby embrace."
  p.summary = p.description
  p.url = "http://github.com/jbarnette/johnson/wikis"
 
  p.clean_globs = [
    "lib/johnson/spidermonkey.#{kind}",
    "ext/spidermonkey/Makefile",
    "ext/spidermonkey/*.{o,so,bundle,log}",
    GENERATED_NODE,
    "vendor/spidermonkey/**/*.OBJ"]
 
  p.test_globs = ["test/**/*_test.rb"]
  p.spec_extras = { :extensions => ["Rakefile"] }
  p.extra_deps = ["rake"]
end
 
namespace :gem do
  namespace :spec do
    task :generate do
      File.open("johnson.gemspec", "w") do |f|
        f.puts(HOE.spec.to_ruby)
      end
    end
  end
end
 
namespace :test do
  Rake::TestTask.new("todo") do |t|
    t.test_files = FileList["todo/**/*_test.rb"]
    t.verbose = true
  end
  
  task :jspec => :extensions do
    $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/lib")
    Johnson.send(:remove_const, :VERSION)
    require "johnson"
 
    Dir["test/jspec/**/*_spec.js"].each do |file|
      Johnson::Runtime.new.load(file)
    end
  end
end
 
# make sure the C bits are up-to-date when testing
Rake::Task[:test].prerequisites << :extensions
Rake::Task["test:todo"].prerequisites << :extensions
 
Rake::Task[:check_manifest].prerequisites << GENERATED_NODE
 
task :build => :extensions
task :extension => :build
 
task :extensions => ["lib/johnson/spidermonkey.#{kind}"]
 
namespace :extensions do
  task :clean do
    Dir.chdir("ext/spidermonkey") do
      sh "rm -f Makefile"
      sh "rm -f *.{o,so,bundle,log}"
    end
  end
end
 
build_sm = lambda do
  cmd = "make -f Makefile.ref"
  cmd << " OS_CONFIG=#{CROSS}" if CROSS
  Dir.chdir("vendor/spidermonkey") { sh cmd }
end
 
if Symbol === LIBJS
  task LIBJS, &build_sm
else
  file LIBJS, &build_sm
 
  task LIBJS => "vendor/spidermonkey/Makefile.ref"
  task LIBJS => Dir["vendor/spidermonkey/*.[ch]"]
  task LIBJS => Dir["vendor/spidermonkey/config/*.mk"]
end
 
task LIBJS => "vendor/spidermonkey/jsapi.h"
task LIBJS => "vendor/spidermonkey/config/#{CROSS}.mk" if CROSS
 
file "vendor/spidermonkey/config/MINGW32.mk" => "MINGW32.mk" do |t|
  cp t.prerequisites.first, t.name
end
 
file "ext/spidermonkey/spidermonkey.#{kind}" =>
  ["ext/spidermonkey/Makefile"] + FileList["ext/spidermonkey/*.{c,h}"].to_a do |t|
 
  old_time = File.mtime(t.name) rescue nil
  Dir.chdir("ext/spidermonkey") { sh "make" }
 
  # If make chose not to rebuild the file, we'll touch it, so we don't
  # bother to call make again next time.
  sh "touch #{t.name}" if old_time && File.mtime(t.name) <= old_time
end
 
# for testing, we toss the SpiderMonkey extension in lib/johnson
file "lib/johnson/spidermonkey.#{kind}" =>
  "ext/spidermonkey/spidermonkey.#{kind}" do |t|
 
  cp t.prerequisites.first, t.name
end
 
file "ext/spidermonkey/Makefile" =>
  [LIBJS, GENERATED_NODE, "ext/spidermonkey/extconf.rb"] do
  
  dirs = (CROSS ? [ENV["CROSSLIB"]] : []) + $:
  command = ["ruby"] + dirs.map{|dir| "-I#{File.expand_path dir}"} + ["extconf.rb"]
  Dir.chdir("ext/spidermonkey") { sh *command }
end
 
def jsops
  ops = []
  File.open("vendor/spidermonkey/jsopcode.tbl", "rb") { |f|
    f.each_line do |line|
      if line =~ /^OPDEF\((\w+),/
        ops << $1
      end
    end
  }
  ops
end
 
def tokens
  toks = []
  File.open("vendor/spidermonkey/jsscan.h", "rb") { |f|
    f.each_line do |line|
      line.scan(/TOK_\w+/).each do |token|
        next if token == "TOK_ERROR"
        toks << token
      end
    end
  }
  toks.uniq
end
 
file GENERATED_NODE => ["ext/spidermonkey/immutable_node.c.erb", "vendor/spidermonkey/jsopcode.tbl", "vendor/spidermonkey/jsscan.h"] do |t|
  template = ERB.new(File.open(t.prerequisites.first, "rb") { |x| x.read })
  File.open(GENERATED_NODE, "wb") { |f|
    f.write template.result(binding)
  }
end