serve 1.1.1 → 1.5.0.pre

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,8 @@
1
1
  = Change Log
2
2
 
3
+ == edge
4
+ * Added an export command which allows you to convert a Serve site to static HTML. Closes #10. [jlong]
5
+
3
6
  == 1.1.1 (June 7, 2011)
4
7
  * Fix: Allow create command to work without Git. Closes #24. [jlong]
5
8
  * Add javascript and stylesheet include helpers [zaeleus]
data/Gemfile CHANGED
@@ -2,6 +2,7 @@ source "http://rubygems.org"
2
2
 
3
3
  gem 'rake', '~> 0.9.0'
4
4
  gem 'rack', '~> 1.3.0'
5
+ gem 'rack-test', '~> 0.6.0'
5
6
  gem 'tilt', '~> 1.3.1'
6
7
  gem 'activesupport', '~> 3.0.7'
7
8
  gem 'tzinfo', '~> 0.3.27'
data/Gemfile.lock CHANGED
@@ -26,6 +26,8 @@ GEM
26
26
  mutter (0.5.3)
27
27
  polyglot (0.3.1)
28
28
  rack (1.3.0)
29
+ rack-test (0.6.0)
30
+ rack (>= 1.0)
29
31
  radius (0.6.1)
30
32
  rake (0.9.0)
31
33
  rdiscount (1.6.8)
@@ -62,6 +64,7 @@ DEPENDENCIES
62
64
  jeweler (~> 1.4.0)
63
65
  less (~> 1.2.21)
64
66
  rack (~> 1.3.0)
67
+ rack-test (~> 0.6.0)
65
68
  radius (~> 0.6.1)
66
69
  rake (~> 0.9.0)
67
70
  rdiscount (~> 1.6.8)
data/README.rdoc CHANGED
@@ -229,8 +229,8 @@ module and define your helper methods there:
229
229
  Helpers have full access to the request and response objects so you can easily
230
230
  read and manipulate headers or do other fancy tricks.
231
231
 
232
- Serve provides a number of stock helpers to make it easier for you to prototype
233
- Rails applications:
232
+ Serve provides a number of stock helpers to make it easier for you to
233
+ prototype Rails applications:
234
234
 
235
235
  # Escape Helpers
236
236
  html_escape(string) # escape HTML in string
@@ -285,6 +285,24 @@ email :: Evaluates the document as if it is an e-mail message; the format is ide
285
285
  redirect :: Redirects to the URL contained in the document
286
286
 
287
287
 
288
+ == Exporting A Serve Project
289
+
290
+ The edge version of Serve now has limited support for exporting your Serve
291
+ project to HTML. To get started with the prerelease version:
292
+
293
+ gem install --pre serve
294
+
295
+ To export your project, use the new "export" command:
296
+
297
+ serve export <project_dir>:<output_dir>
298
+
299
+ Where <project_dir> is the path to the project and <output_dir> is the path to
300
+ the directory where you would like your HTML and CSS generated.
301
+
302
+ Please note! This feature is in beta. If you have issues with this approach,
303
+ please post them to the GitHub issue tracker.
304
+
305
+
288
306
  == More Information
289
307
 
290
308
  You can find more information about Serve, including a detailed Screencast, on
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.1
1
+ 1.5.0.pre
@@ -20,10 +20,13 @@ module Serve
20
20
  case
21
21
  when options[:create]
22
22
  require 'serve/project'
23
- Serve::Project.new(options[:create]).create
23
+ Serve::Project.create(options[:create])
24
24
  when options[:convert]
25
25
  require 'serve/project'
26
- Serve::Project.new(options[:convert]).convert
26
+ Serve::Project.convert(options[:convert])
27
+ when options[:export]
28
+ require 'serve/export'
29
+ Serve.export(options[:export])
27
30
  when options[:version]
28
31
  puts version
29
32
  when options[:help]
@@ -49,8 +52,9 @@ module Serve
49
52
  args = normalize_args(args)
50
53
  options[:help] = extract_boolean(args, '-h', '--help')
51
54
  options[:version] = extract_boolean(args, '-v', '--version')
52
- options[:create] = extract_creation(args)
53
- options[:convert] = extract_conversion(args)
55
+ options[:create] = extract_create(args)
56
+ options[:convert] = extract_convert(args)
57
+ options[:export] = extract_export(args)
54
58
  options[:environment] = extract_environment(args)
55
59
  options[:root] = extract_root(args)
56
60
  options[:address] = extract_address(args)
@@ -78,11 +82,12 @@ module Serve
78
82
  " #{program} command [arguments] [options]",
79
83
  " ",
80
84
  "Examples:",
81
- " #{program} # start Serve in current directory",
82
- " #{program} 2100 # start Serve on port 2100",
83
- " #{program} /path/to/project # start Serve for a specific directory",
84
- " #{program} create mockups # create a Serve project in mockups directory",
85
- " #{program} convert mockups # convert a Compass project in mockups",
85
+ " #{program} # launch server in current directory",
86
+ " #{program} 2100 # launch server on port 2100",
87
+ " #{program} mockups # launch server for mockups directory",
88
+ " #{program} create mockups # create a new project in mockups dir",
89
+ " #{program} convert mockups # convert a Compass project in mockups",
90
+ " #{program} export mockups:output # export mockups to output dir",
86
91
  " ",
87
92
  "Description:",
88
93
  " Starts a web server on the specified address and port with its document root ",
@@ -174,7 +179,7 @@ module Serve
174
179
  framework
175
180
  end
176
181
 
177
- def extract_creation(args)
182
+ def extract_create(args)
178
183
  if args.delete('create')
179
184
  framework = extract_javascript_framework(args, '-j', '--javascript')
180
185
  args.reverse!
@@ -186,7 +191,7 @@ module Serve
186
191
  end
187
192
  end
188
193
 
189
- def extract_conversion(args)
194
+ def extract_convert(args)
190
195
  if args.delete('convert')
191
196
  framework = extract_javascript_framework(args, '-j', '--javascript')
192
197
  {
@@ -196,6 +201,19 @@ module Serve
196
201
  end
197
202
  end
198
203
 
204
+ def extract_export(args)
205
+ if args.delete('export')
206
+ input, output = (args.shift || '').split(':')
207
+ output = args.shift if output.nil?
208
+ input, output = Dir.pwd, input if output.nil?
209
+ output = 'output' if output.nil?
210
+ {
211
+ :input => input,
212
+ :output => output
213
+ }
214
+ end
215
+ end
216
+
199
217
  def rails_script_server
200
218
  @rails_server_script ||= options[:root] + '/script/server'
201
219
  end
@@ -0,0 +1,161 @@
1
+ require 'active_support/all'
2
+ require 'serve/out'
3
+ require 'serve/path'
4
+ require 'serve/rack'
5
+ require 'fileutils'
6
+ require 'rack/test'
7
+
8
+ module Serve
9
+ class Exporter
10
+
11
+ def initialize(options={})
12
+ @input = normalize_path(options[:input])
13
+ @output = normalize_path(options[:output])
14
+ end
15
+
16
+ def process
17
+ compile_compass_sass
18
+ collect_files
19
+ compile_views
20
+ compile_redirects
21
+ copy_remaining
22
+ end
23
+
24
+ private
25
+
26
+ include Serve::Out
27
+ include Serve::Path
28
+
29
+ def collect_files
30
+ if rackified?
31
+ @root = "#{@input}/views"
32
+ @views = files_from_path("#{@input}/views")
33
+ @redirects, @views = @views.partition { |fn| fn =~ %r{\.redirect$} }
34
+ @views.reject! { |fn| fn =~ /view_helpers.rb$/} # remove view_helpers.rb
35
+ @public = files_from_path("#{@input}/public")
36
+ else
37
+ @root = @input
38
+ files = files_from_path(@input)
39
+ extensions = Serve::DynamicHandler.extensions
40
+ @views, files = files.partition { |fn| fn =~ %r{#{extensions.join('|')}$} }
41
+ files.reject! { |fn| fn =~ /view_helpers.rb$/} # remove view_helpers.rb
42
+ @redirects, @public = files.partition { |fn| fn =~ %r{\.redirect$} }
43
+ end
44
+ @views.reject! { |v| v =~ %r{_[^\/]+$} } # remove partials
45
+ end
46
+
47
+ def compile_compass_sass
48
+ if rackified?
49
+ `compass compile -c '#{@input}/compass.config' '#{@input}'`
50
+ log_action 'compiled', 'sass files'
51
+ end
52
+ end
53
+
54
+ def compile_views
55
+ @views.each { |v| compile_view(v) }
56
+ end
57
+
58
+ def compile_redirects
59
+ @redirects.each { |r| compile_redirect(r) }
60
+ end
61
+
62
+ def copy_remaining
63
+ @public.each { |fn| copy_file(fn) }
64
+ end
65
+
66
+ def files_from_path(path)
67
+ result = nil
68
+ FileUtils.cd(path) do
69
+ result = Dir["**/*"]
70
+ result.reject! { |fn| File.directory?(fn) }
71
+ end
72
+ result
73
+ end
74
+
75
+ def compile_view(filename)
76
+ from_path = rackified? ? "#{@input}/views/#{filename}" : "#{@input}/#{filename}"
77
+
78
+ # request the path
79
+ browser = Rack::Test::Session.new(Rack::MockSession.new(Serve::RackAdapter.new(@root)))
80
+ browser.get filename
81
+ response = browser.last_response
82
+
83
+ # body and extension
84
+ contents = response.body
85
+ ext = extract_ext(response.content_type)
86
+
87
+ # write contents
88
+ to_path = "#{@output}/#{remove_ext(filename)}#{ext}"
89
+ ensure_path to_path
90
+ File.open(to_path, 'w+') { |f| f.puts contents }
91
+
92
+ log_action "compiled", to_path
93
+ rescue => e
94
+ log_error "failed", "#{from_path}\n#{e.message}\n#{e.backtrace.join("\n")}"
95
+ end
96
+
97
+ def compile_redirect(filename)
98
+ from_path = rackified? ? "#{@input}/views/#{filename}" : "#{@input}/#{filename}"
99
+ to_path = "#{@output}/#{remove_ext(filename)}.html"
100
+
101
+ ensure_path to_path
102
+
103
+ url = IO.read(from_path).strip
104
+ contents = %{<html><head><meta http-equiv="refresh" content="0;#{url}" /></head></html>}
105
+
106
+ File.open(to_path, 'w+') { |f| f.puts contents }
107
+
108
+ log_action "compiled", to_path
109
+ rescue => e
110
+ log_error "failed", "#{from_path}\n#{e.message}\n#{e.backtrace.join("\n")}"
111
+ end
112
+
113
+ def copy_file(filename)
114
+ from_path = rackified? ? "#{@input}/public/#{filename}" : "#{@input}/#{filename}"
115
+ to_path = "#{@output}/#{filename}"
116
+
117
+ ensure_path to_path
118
+
119
+ FileUtils.cp from_path, to_path
120
+
121
+ log_action "copied", to_path
122
+ end
123
+
124
+ def ensure_path(path)
125
+ dirname = normalize_path(File.dirname(path))
126
+ unless File.directory?(dirname)
127
+ FileUtils.mkdir_p(dirname)
128
+ log_action "created", dirname
129
+ end
130
+ end
131
+
132
+ def remove_ext(path)
133
+ if path =~ /^(.*?)\.[A-Za-z]+[A-Za-z.]*$/
134
+ $1
135
+ else
136
+ path
137
+ end
138
+ end
139
+
140
+ def extract_ext(content_type)
141
+ case content_type
142
+ when %r{text/html}
143
+ ".html"
144
+ when %r{text/css}
145
+ ".css"
146
+ when %r{text/javascript}
147
+ ".js"
148
+ else
149
+ raise content_type.inspect
150
+ end
151
+ end
152
+
153
+ def rackified?
154
+ @rackified ||= File.directory?("#{@input}/views") && File.directory?("#{@input}/public")
155
+ end
156
+ end
157
+
158
+ def self.export(options={})
159
+ Exporter.new(options).process
160
+ end
161
+ end
data/lib/serve/out.rb CHANGED
@@ -1,8 +1,12 @@
1
+ require 'pathname'
2
+
1
3
  module Serve #:nodoc:
2
4
 
3
5
  # Utility methods for handling output to the terminal
4
6
  module Out #:nodoc:
5
7
 
8
+ COLUMN_WIDTH = 12
9
+
6
10
  COLORS = {
7
11
  :clear => 0,
8
12
  :red => 31,
@@ -31,12 +35,19 @@ module Serve #:nodoc:
31
35
  end
32
36
 
33
37
  def print(*args)
34
- stderr.print(color_msg(:green, *args))
38
+ stderr.print(*args)
35
39
  end
36
40
 
37
41
  def log_action(name, message)
38
- print " " * (12 - name.length)
39
- print name
42
+ print " " * (COLUMN_WIDTH - name.length)
43
+ print color_msg(:green, name)
44
+ print " "
45
+ puts message
46
+ end
47
+
48
+ def log_error(name, message)
49
+ print " " * (COLUMN_WIDTH - name.length)
50
+ print color_msg(:red, name)
40
51
  print " "
41
52
  puts message
42
53
  end
@@ -52,5 +63,7 @@ module Serve #:nodoc:
52
63
  def color(pigment)
53
64
  "\e[#{COLORS[pigment.to_sym]}m"
54
65
  end
66
+
55
67
  end
68
+
56
69
  end
data/lib/serve/path.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'pathname'
2
+
3
+ module Serve #:nodoc:
4
+ module Path #:nodoc:
5
+
6
+ # Normalize a path relative to the current working directory
7
+ def normalize_path(*paths)
8
+ path = File.join(*paths)
9
+ Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(Dir.pwd)).to_s
10
+ end
11
+
12
+ end
13
+ end
data/lib/serve/project.rb CHANGED
@@ -1,5 +1,6 @@
1
- require 'pathname'
1
+ require 'fileutils'
2
2
  require 'serve/out'
3
+ require 'serve/path'
3
4
  require 'serve/javascripts'
4
5
 
5
6
  module Serve #:nodoc:
@@ -34,6 +35,10 @@ module Serve #:nodoc:
34
35
  install_javascript_framework @framework
35
36
  end
36
37
 
38
+ def self.create(options={})
39
+ new(options).create
40
+ end
41
+
37
42
  # Convert an existing Compass project to a Serve project
38
43
  def convert
39
44
  setup_base
@@ -49,10 +54,14 @@ module Serve #:nodoc:
49
54
  note_old_compass_config
50
55
  end
51
56
 
57
+ def self.convert(options={})
58
+ new(options).convert
59
+ end
60
+
52
61
  private
53
62
 
54
63
  include Serve::Out
55
-
64
+ include Serve::Path
56
65
  include Serve::JavaScripts
57
66
 
58
67
  # Files required for both a new server project and for an existing compass project.
@@ -109,6 +118,14 @@ module Serve #:nodoc:
109
118
  instance_eval "%{#{contents}}"
110
119
  end
111
120
 
121
+ # Grab data by key from the git config file if it exists
122
+ def git_config(key)
123
+ value = `git config #{key}`.chomp
124
+ value.empty? ? nil : value
125
+ rescue
126
+ nil
127
+ end
128
+
112
129
  # Create a file with contents
113
130
  def create_file(file, contents)
114
131
  path = normalize_path(@location, file)
@@ -148,19 +165,6 @@ module Serve #:nodoc:
148
165
  end
149
166
  end
150
167
 
151
- # Convert dashes and spaces to underscores
152
- def underscore(string)
153
- string.gsub(/-|\s+/, '_')
154
- end
155
-
156
- # Grab data by key from the git config file if it exists
157
- def git_config(key)
158
- value = `git config #{key}`.chomp
159
- value.empty? ? nil : value
160
- rescue
161
- nil
162
- end
163
-
164
168
  # Normalize the path of the target directory
165
169
  def normalize_location(path, name = nil)
166
170
  path = File.join(path, underscore(name)) if name
@@ -168,10 +172,9 @@ module Serve #:nodoc:
168
172
  path
169
173
  end
170
174
 
171
- # Normalize a path relative to the current working directory
172
- def normalize_path(*paths)
173
- path = File.join(*paths)
174
- Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(Dir.pwd)).to_s
175
+ # Convert dashes and spaces to underscores
176
+ def underscore(string)
177
+ string.gsub(/-|\s+/, '_')
175
178
  end
176
179
 
177
180
  end
data/spec/project_spec.rb CHANGED
@@ -18,7 +18,7 @@ describe Serve::Project do
18
18
  :framework => 'jquery'
19
19
  }
20
20
 
21
- @mockup = Serve::Project.new(@options)
21
+ @mockup = Serve::Project.new(@options)
22
22
  @mockup.stdout = SilentOut.new
23
23
  @mockup.stderr = SilentOut.new
24
24
 
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serve
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
5
- prerelease: false
4
+ hash: 961916028
5
+ prerelease: true
6
6
  segments:
7
7
  - 1
8
- - 1
9
- - 1
10
- version: 1.1.1
8
+ - 5
9
+ - 0
10
+ - pre
11
+ version: 1.5.0.pre
11
12
  platform: ruby
12
13
  authors:
13
14
  - John W. Long
@@ -37,9 +38,25 @@ dependencies:
37
38
  type: :runtime
38
39
  version_requirements: *id001
39
40
  - !ruby/object:Gem::Dependency
40
- name: tilt
41
+ name: rack-test
41
42
  prerelease: false
42
43
  requirement: &id002 !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ hash: 7
49
+ segments:
50
+ - 0
51
+ - 6
52
+ - 0
53
+ version: 0.6.0
54
+ type: :runtime
55
+ version_requirements: *id002
56
+ - !ruby/object:Gem::Dependency
57
+ name: tilt
58
+ prerelease: false
59
+ requirement: &id003 !ruby/object:Gem::Requirement
43
60
  none: false
44
61
  requirements:
45
62
  - - ~>
@@ -51,11 +68,11 @@ dependencies:
51
68
  - 1
52
69
  version: 1.3.1
53
70
  type: :runtime
54
- version_requirements: *id002
71
+ version_requirements: *id003
55
72
  - !ruby/object:Gem::Dependency
56
73
  name: activesupport
57
74
  prerelease: false
58
- requirement: &id003 !ruby/object:Gem::Requirement
75
+ requirement: &id004 !ruby/object:Gem::Requirement
59
76
  none: false
60
77
  requirements:
61
78
  - - ~>
@@ -67,11 +84,11 @@ dependencies:
67
84
  - 7
68
85
  version: 3.0.7
69
86
  type: :runtime
70
- version_requirements: *id003
87
+ version_requirements: *id004
71
88
  - !ruby/object:Gem::Dependency
72
89
  name: tzinfo
73
90
  prerelease: false
74
- requirement: &id004 !ruby/object:Gem::Requirement
91
+ requirement: &id005 !ruby/object:Gem::Requirement
75
92
  none: false
76
93
  requirements:
77
94
  - - ~>
@@ -83,11 +100,11 @@ dependencies:
83
100
  - 27
84
101
  version: 0.3.27
85
102
  type: :runtime
86
- version_requirements: *id004
103
+ version_requirements: *id005
87
104
  - !ruby/object:Gem::Dependency
88
105
  name: i18n
89
106
  prerelease: false
90
- requirement: &id005 !ruby/object:Gem::Requirement
107
+ requirement: &id006 !ruby/object:Gem::Requirement
91
108
  none: false
92
109
  requirements:
93
110
  - - ~>
@@ -99,11 +116,11 @@ dependencies:
99
116
  - 0
100
117
  version: 0.6.0
101
118
  type: :runtime
102
- version_requirements: *id005
119
+ version_requirements: *id006
103
120
  - !ruby/object:Gem::Dependency
104
121
  name: rspec
105
122
  prerelease: false
106
- requirement: &id006 !ruby/object:Gem::Requirement
123
+ requirement: &id007 !ruby/object:Gem::Requirement
107
124
  none: false
108
125
  requirements:
109
126
  - - ~>
@@ -115,7 +132,7 @@ dependencies:
115
132
  - 0
116
133
  version: 2.6.0
117
134
  type: :development
118
- version_requirements: *id006
135
+ version_requirements: *id007
119
136
  description: Serve is a small Rack-based web server that makes it easy to serve ERB or HAML from any directory. Serve is an ideal tool for building HTML prototypes of Rails applications. Serve can also handle SASS, Textile, and Markdown if the appropriate gems are installed.
120
137
  email: me@johnwlong.com
121
138
  executables:
@@ -137,6 +154,7 @@ files:
137
154
  - bin/serve
138
155
  - lib/serve.rb
139
156
  - lib/serve/application.rb
157
+ - lib/serve/export.rb
140
158
  - lib/serve/handlers/dynamic_handler.rb
141
159
  - lib/serve/handlers/email_handler.rb
142
160
  - lib/serve/handlers/file_type_handler.rb
@@ -145,6 +163,7 @@ files:
145
163
  - lib/serve/handlers/sass_handler.rb
146
164
  - lib/serve/javascripts.rb
147
165
  - lib/serve/out.rb
166
+ - lib/serve/path.rb
148
167
  - lib/serve/project.rb
149
168
  - lib/serve/rack.rb
150
169
  - lib/serve/router.rb
@@ -189,12 +208,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
189
208
  required_rubygems_version: !ruby/object:Gem::Requirement
190
209
  none: false
191
210
  requirements:
192
- - - ">="
211
+ - - ">"
193
212
  - !ruby/object:Gem::Version
194
- hash: 3
213
+ hash: 25
195
214
  segments:
196
- - 0
197
- version: "0"
215
+ - 1
216
+ - 3
217
+ - 1
218
+ version: 1.3.1
198
219
  requirements: []
199
220
 
200
221
  rubyforge_project: