resin 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@
4
4
  Resin
5
5
  = embed_amber
6
6
  %body
7
- - if Resin.development?
7
+ - if development?
8
8
  %div{:style => 'float: right; clear: all'}
9
9
  %button{:onclick => "smalltalk.Browser._open();"}
10
10
  Run IDE
@@ -0,0 +1,93 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra/resin/helpers'
3
+ require 'haml'
4
+
5
+ module Sinatra
6
+ module Resin
7
+ AMBER_PATH = File.expand_path('../../../amber', __FILE__)
8
+
9
+ def env
10
+ ENV['RACK_ENV']
11
+ end
12
+ module_function :env
13
+
14
+ def development?
15
+ !(env == 'production')
16
+ end
17
+ module_function :development?
18
+
19
+ def self.registered(app)
20
+ app.helpers Sinatra::Resin::Helpers
21
+ app.set :dump_errors, true
22
+ app.set :port, ENV['PORT'] || 4567
23
+ app.set :views, [File.join(Dir.pwd, 'views'), File.expand_path('../../resin/views', __FILE__)]
24
+
25
+ app.before do
26
+ # Make sure to call Sinatra::Resin::Helpers#drops to load all our drops
27
+ drops
28
+ end
29
+
30
+ app.get '/' do
31
+ haml :index
32
+ end
33
+
34
+ app.get '/:view/?' do |view|
35
+ begin
36
+ haml view.to_sym
37
+ rescue ::Errno::ENOENT
38
+ halt 404, 'Not found'
39
+ end
40
+ end
41
+
42
+ ['js', 'css', 'images'].each do |path|
43
+ app.get "/#{path}/*" do |filename|
44
+ content_type_for_ext filename
45
+ data = load_resource(path, filename)
46
+ if data.nil? and path == 'js'
47
+ data = load_drop_resource(filename)
48
+ end
49
+
50
+ if data.nil?
51
+ halt 404, 'Not found'
52
+ end
53
+
54
+ data
55
+ end
56
+ end
57
+
58
+ if development?
59
+ app.set :logging, true
60
+ app.disable :protection
61
+
62
+ # Only enable the saving mechanism in test/development
63
+ app.put '*' do
64
+ unless request.body.nil?
65
+ filename = request.path.split('/')[-1]
66
+ directory = '/st/'
67
+ if filename.end_with? '.js'
68
+ directory = '/js/'
69
+ end
70
+
71
+ root_dir = Dir.pwd
72
+
73
+ if drops_filemap[filename]
74
+ drop_name = drops_filemap[filename]
75
+ root_dir = drops_map[drop_name]
76
+ end
77
+
78
+ path = File.join(root_dir, directory, filename)
79
+ puts ">> Commiting changes to #{path}"
80
+ File.open(path, 'w') do |fd|
81
+ request.body.each do |line|
82
+ fd.write(line)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ register Resin
92
+
93
+ end
@@ -0,0 +1,171 @@
1
+ require 'json'
2
+
3
+ module Sinatra
4
+ module Resin
5
+ module Helpers
6
+ def self.append_js_file(filename, to_array)
7
+ filename = File.basename(filename)
8
+
9
+ if Sinatra::Resin.development?
10
+ unless filename.include? 'deploy'
11
+ to_array << filename
12
+ end
13
+ return
14
+ end
15
+
16
+ if (filename.include? 'deploy') && !(filename.include? '-Tests')
17
+ to_array << filename
18
+ end
19
+ end
20
+
21
+ def development?
22
+ Sinatra::Resin.development?
23
+ end
24
+
25
+ def javascript_files
26
+ files = []
27
+
28
+ # First our project's files take precedence
29
+ Dir.glob("#{Dir.pwd}/js/*.js") do |filename|
30
+ Sinatra::Resin::Helpers.append_js_file(filename, files)
31
+ end
32
+
33
+ # Then our drops get loaded
34
+ drops.each do |drop|
35
+ unless drop[:meta]
36
+ next
37
+ end
38
+
39
+ if drop[:js]
40
+ drop[:js].each do |filename|
41
+ Sinatra::Resin::Helpers.append_js_file(filename, files)
42
+ end
43
+ end
44
+ end
45
+
46
+ files
47
+ end
48
+
49
+ def find_template(views, name, engine, &block)
50
+ Array(views).each do |view|
51
+ super(view, name, engine, &block)
52
+ end
53
+ end
54
+
55
+ def embed_amber(options={})
56
+ deploy_line = ''
57
+ unless development?
58
+ deploy_line = "deploy: true,"
59
+ end
60
+
61
+ on_ready_function = options[:on_ready] || ''
62
+
63
+ return <<-END
64
+ <script type="text/javascript" src="/js/amber.js"></script>
65
+ <script type="text/javascript">
66
+ loadAmber({
67
+ #{deploy_line}
68
+ files : #{JSON.dump(javascript_files)},
69
+ prefix : 'js',
70
+ ready : function() { #{on_ready_function} }
71
+ });
72
+ </script>
73
+ END
74
+ end
75
+
76
+ def load_resource(prefix, filename)
77
+ # A file in our working directory will take precedence over the
78
+ # Amber-bundled files. This should allow custom Kernel-Objects.js files
79
+ # for example.
80
+ local_file = File.join(Dir.pwd, "#{prefix}/", filename)
81
+ amber_file = File.join(Sinatra::Resin::AMBER_PATH, "/#{prefix}/", filename)
82
+
83
+ if File.exists? local_file
84
+ File.open(local_file, 'r').read
85
+ elsif File.exists? amber_file
86
+ File.open(amber_file, 'r').read
87
+ else
88
+ nil
89
+ end
90
+ end
91
+
92
+ def load_drop_resource(filename)
93
+ drops.each do |drop|
94
+ if drop.nil? or drop.empty? or drop[:js].empty?
95
+ next
96
+ end
97
+
98
+ drop[:js].each do |drop_filepath|
99
+ drop_filename = File.basename(drop_filepath)
100
+ if filename == drop_filename
101
+ return File.open(drop_filepath).read
102
+ end
103
+ end
104
+ end
105
+ nil
106
+ end
107
+
108
+ def content_type_for_ext(filename)
109
+ if File.extname(filename) == '.js'
110
+ content_type 'application/javascript'
111
+ elsif File.extname(filename) == '.css'
112
+ content_type 'text/css'
113
+ else
114
+ content_type 'text/plain'
115
+ end
116
+ end
117
+
118
+ def load_drop_file(filepath)
119
+ YAML::load(File.open(filepath).read)
120
+ end
121
+
122
+ def self.flush_drops
123
+ @@drops = nil
124
+ @@drops_map = {}
125
+ @@drops_filemap = {}
126
+ end
127
+
128
+ def drops_map
129
+ @@drops_map
130
+ end
131
+
132
+ def drops_filemap
133
+ @@drops_filemap
134
+ end
135
+
136
+ def drops
137
+ @@drops ||= begin
138
+ @@drops_map ||= {}
139
+ @@drops_filemap ||= {}
140
+ drops_path = File.join(Dir.pwd, 'drops')
141
+ loaded = []
142
+ Dir.glob("#{drops_path}/*/drop.yml") do |filename|
143
+ drop = load_drop_file(filename)
144
+ if drop.nil? or drop.empty?
145
+ next
146
+ end
147
+ drop_dir = File.dirname(filename)
148
+ name = drop['drop']['name']
149
+ puts ">>> Loading Resin Drop: #{name}"
150
+
151
+ data = {:meta => drop['drop'],
152
+ :js => Dir[File.join(drop_dir, 'js', '*.js')],
153
+ :st => Dir[File.join(drop_dir, 'st', '*.st')]
154
+ }
155
+ loaded << data
156
+
157
+ @@drops_map[name] = drop_dir
158
+
159
+ data[:js].each do |js|
160
+ @@drops_filemap[File.basename(js)] = name
161
+ end
162
+ data[:st].each do |st|
163
+ @@drops_filemap[File.basename(st)] = name
164
+ end
165
+ end
166
+ loaded
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-24 00:00:00.000000000 Z
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &18263980 !ruby/object:Gem::Requirement
16
+ requirement: &5027820 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.0.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *18263980
24
+ version_requirements: *5027820
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &18263200 !ruby/object:Gem::Requirement
27
+ requirement: &5027080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *18263200
35
+ version_requirements: *5027080
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: shotgun
38
- requirement: &18262440 !ruby/object:Gem::Requirement
38
+ requirement: &5026260 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *18262440
46
+ version_requirements: *5026260
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &18261660 !ruby/object:Gem::Requirement
49
+ requirement: &5025480 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *18261660
57
+ version_requirements: *5025480
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rack-test
60
- requirement: &18260540 !ruby/object:Gem::Requirement
60
+ requirement: &5024440 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *18260540
68
+ version_requirements: *5024440
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: haml
71
- requirement: &18259400 !ruby/object:Gem::Requirement
71
+ requirement: &5023260 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *18259400
79
+ version_requirements: *5023260
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: sinatra
82
- requirement: &18274480 !ruby/object:Gem::Requirement
82
+ requirement: &5038320 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *18274480
90
+ version_requirements: *5038320
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: json
93
- requirement: &18272920 !ruby/object:Gem::Requirement
93
+ requirement: &5036780 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *18272920
101
+ version_requirements: *5036780
102
102
  description:
103
103
  email: tyler@linux.com
104
104
  executables:
@@ -110,8 +110,9 @@ extra_rdoc_files:
110
110
  files:
111
111
  - bin/runresin
112
112
  - bin/resin-compile
113
+ - lib/sinatra/resin/helpers.rb
114
+ - lib/sinatra/resin.rb
113
115
  - lib/resin/helpers.rb
114
- - lib/resin/app.rb
115
116
  - lib/resin/compiler.rb
116
117
  - lib/resin.rb
117
118
  - lib/resin/views/index.haml
@@ -279,7 +280,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
279
280
  version: '0'
280
281
  segments:
281
282
  - 0
282
- hash: -1080527507548658967
283
+ hash: -3056217880712448120
283
284
  required_rubygems_version: !ruby/object:Gem::Requirement
284
285
  none: false
285
286
  requirements:
@@ -288,7 +289,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
288
289
  version: '0'
289
290
  segments:
290
291
  - 0
291
- hash: -1080527507548658967
292
+ hash: -3056217880712448120
292
293
  requirements: []
293
294
  rubyforge_project:
294
295
  rubygems_version: 1.8.10
@@ -1,93 +0,0 @@
1
- require 'rubygems'
2
- require 'sinatra/base'
3
- require 'haml'
4
-
5
- require File.expand_path(File.dirname(__FILE__) + '/helpers.rb')
6
-
7
- AMBER_PATH = File.expand_path('../../../amber', __FILE__)
8
-
9
- module Resin
10
- def env
11
- ENV['RACK_ENV']
12
- end
13
- module_function :env
14
-
15
- def development?
16
- !(env == 'production')
17
- end
18
- module_function :development?
19
-
20
- class Server < Sinatra::Base
21
- set :dump_errors, true
22
- set :port, ENV['PORT'] || 4567
23
- set :views, [File.join(Dir.pwd, 'views'), File.expand_path('../views', __FILE__)]
24
-
25
- helpers do
26
- include Resin::Helpers
27
- end
28
-
29
- before do
30
- # Make sure drops are loaded
31
- drops
32
- end
33
-
34
- get '/' do
35
- haml :index
36
- end
37
-
38
- get '/:view/?' do |view|
39
- begin
40
- haml view.to_sym
41
- rescue ::Errno::ENOENT
42
- halt 404, 'Not found'
43
- end
44
- end
45
-
46
- ['js', 'css', 'images'].each do |path|
47
- get "/#{path}/*" do |filename|
48
- content_type_for_ext filename
49
- data = load_resource(path, filename)
50
- if data.nil? and path == 'js'
51
- data = load_drop_resource(filename)
52
- end
53
-
54
- if data.nil?
55
- halt 404, 'Not found'
56
- end
57
-
58
- data
59
- end
60
- end
61
-
62
- if Resin.development?
63
- set :logging, true
64
- disable :protection
65
-
66
- # Only enable the saving mechanism in test/development
67
- put '*' do
68
- unless request.body.nil?
69
- filename = request.path.split('/')[-1]
70
- directory = '/st/'
71
- if filename.end_with? '.js'
72
- directory = '/js/'
73
- end
74
-
75
- root_dir = Dir.pwd
76
-
77
- if drops_filemap[filename]
78
- drop_name = drops_filemap[filename]
79
- root_dir = drops_map[drop_name]
80
- end
81
-
82
- path = File.join(root_dir, directory, filename)
83
- puts ">> Commiting changes to #{path}"
84
- File.open(path, 'w') do |fd|
85
- request.body.each do |line|
86
- fd.write(line)
87
- end
88
- end
89
- end
90
- end
91
- end
92
- end
93
- end