scharfie-bones 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -12,7 +12,7 @@ What it does:
12
12
  Requirements:
13
13
 
14
14
  - ActiveSupport
15
- - rack
15
+ - rack (0.3.0 only at this time)
16
16
 
17
17
  Starting it up:
18
18
 
@@ -32,7 +32,7 @@ Starting it up:
32
32
  Flatten your views into .html files:
33
33
 
34
34
  - Cache
35
- rake cache:simple # without versioning
35
+ rake cache # without versioning
36
36
  rake cache:versioned # with versioning
37
37
 
38
38
  Then upload the entire public/ directory to your server.
data/bones.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "bones"
3
- s.version = "0.1.1"
4
- s.date = "2008-08-22"
3
+ s.version = "0.1.2"
4
+ s.date = "2009-02-02"
5
5
  s.authors = ["Chris Scharf", "Ryan Heath"]
6
6
  s.email = "scharfie@gmail.com"
7
7
 
@@ -34,6 +34,6 @@ Gem::Specification.new do |s|
34
34
  ]
35
35
 
36
36
  # Dependencies
37
- s.add_dependency("rack", [">= 0.3.0"])
37
+ s.add_dependency("rack", ["= 0.3.0"])
38
38
  s.add_dependency("activesupport", [">= 2.1.0"])
39
39
  end
data/lib/bones.rb CHANGED
@@ -2,6 +2,14 @@
2
2
  # which BonesProxy loads and calls upon to
3
3
  # do the dirty work.
4
4
  class Bones
5
+ class << self
6
+ attr_accessor :base
7
+
8
+ def base
9
+ @base || ''
10
+ end
11
+ end
12
+
5
13
  # Process incoming request (for real this time!)
6
14
  def call(env)
7
15
  # Grab the request
@@ -60,6 +68,12 @@ class Bones
60
68
  @path = path.gsub(/\.html|\.html\.erb/, '')
61
69
  @layout = layout == -1 ? 'application' : layout
62
70
  @options = options
71
+
72
+ self.class.include_helpers
73
+ end
74
+
75
+ def inspect
76
+ '#<Bones::Template @path="%s" @layout="%s">' % [path, layout]
63
77
  end
64
78
 
65
79
  # Full path to template file
@@ -130,66 +144,9 @@ class Bones
130
144
  path = name.to_s.split('/')
131
145
  path[-1] = '_' + path.last unless path.last.starts_with?('_')
132
146
  name = path.join('/')
133
- Template.compile(name, false, options)
134
- end
135
- end
136
-
137
- # Class used to encapsulate the logic needed to
138
- # maintain mockup versions
139
- class Versioned
140
- # Pre-fix used for versioned directories
141
- DIR_PREFIX = 'v'
142
-
143
- # Start with the original destination
144
- def initialize(original_destination)
145
- @original_destination = original_destination
146
- @versioned_destination = nil
147
- end
148
-
149
- # Returns the new destination, which depends on
150
- # the existing amount of 'versions'
151
- def destination
152
- @versioned_destination ||= get_versioned_destination
153
- end
154
-
155
- # Copies all public directories to the new 'versioned'
156
- # directory, so each version can contain it's own mockup
157
- # (note: only copies asset directories - ignores "v1", "v2", etc)
158
- def copy_public_directories
159
- public_directories.each do |src|
160
- FileUtils.copy_entry ROOT / 'public' / src, destination / src
161
- end
162
- end
163
-
164
- # Returns the versioned directories within the 'public' folder
165
- # $> Bones::Versioned.directories
166
- # $> => ["/v1", "/v2", ... "/vN"]
167
- def self.directories
168
- Dir.glob(ROOT / 'public' / "#{DIR_PREFIX}**").inject([]) do |dirs, dir|
169
- dirs << '/' + dir.gsub(/^\/.+\//, '')
170
- end
171
- end
172
-
173
- # Public accessor of version
174
- def version
175
- next_version
176
- end
177
-
178
- # Returns directory name of versioned path
179
- # For example, 'v1'
180
- def versioned_directory_name
181
- DIR_PREFIX + next_version
182
- end
183
-
184
- private
185
- # increments to the next version based on existing versions
186
- def next_version
187
- (self.class.directories.size + 1).to_s
188
- end
189
-
190
- # constructs the next version path
191
- def get_versioned_destination
192
- @original_destination /= versioned_directory_name
147
+ template = Template.new(name, false, options)
148
+ template.request = request
149
+ template.compile
193
150
  end
194
151
  end
195
152
  end
data/lib/boot.rb CHANGED
@@ -15,6 +15,7 @@ require 'activesupport'
15
15
  require 'extensions'
16
16
  require 'erb'
17
17
  require 'bones'
18
+ require 'bones/release'
18
19
 
19
20
  def directories(base)
20
21
  Dir.chdir(base) do
@@ -30,7 +31,7 @@ def page_directories
30
31
  end
31
32
 
32
33
  def versioned_directories
33
- Bones::Versioned.directories
34
+ Bones::VersionedRelease.directories
34
35
  end
35
36
 
36
37
  def public_directories
data/lib/cache.rb CHANGED
@@ -1,105 +1,161 @@
1
- #!/usr/bin/ruby
1
+ #!/usr/bin/env ruby
2
2
  require File.join(File.dirname(__FILE__), 'boot')
3
3
  require 'fileutils'
4
- require 'bones'
5
4
  require 'optparse'
6
5
  require 'ostruct'
7
6
 
8
- # Argument processor
9
- class CacheOptions < OpenStruct
10
- # Default options
11
- def self.default_options
12
- returning new do |o|
13
- o.destination = ROOT / 'public' # Set original destination
14
- o.versioned = false # No versions by default
15
- o.base = '' # Base URL is empty
16
- end
17
- end
7
+ class Bones
8
+ class Cache
9
+ class Options
10
+ attr_accessor :base, :versioned, :release, :destination
11
+
12
+ def initialize
13
+ super
14
+ self.base = '' # Base URL is empty
15
+ self.release = nil
16
+ self.destination = ROOT / 'public' # Set original destination
17
+ end
18
+
19
+ def merge(options={})
20
+ options.each do |k, v|
21
+ method = "#{k}=".to_sym
22
+ send(method, v) if respond_to?(method)
23
+ end
24
+ end
18
25
 
19
- # Process arguments
20
- def self.process(args)
21
- options = default_options
26
+ # Process arguments
27
+ def self.process(args)
28
+ options = new
29
+
30
+ OptionParser.new do |o|
31
+ o.on('--destination PATH', '-d PATH', "Change the destination directory") do |path|
32
+ options.destination = path
33
+ end
34
+
35
+ o.on('--versioned', '--versions', "Enable versioning") do
36
+ options.versioned = true
37
+ end
38
+
39
+ o.on('--base PATH', "Change the base URL path") do |path|
40
+ options.base = path # Bones.base = path
41
+ end
42
+
43
+ o.on_tail("-h", "--help", "Show this message") do
44
+ puts o; exit
45
+ end
46
+ end.parse!(args)
22
47
 
23
- OptionParser.new do |o|
24
- o.on('--versioned', '--versions', "Enable versioning") do
25
- options.versioned = Bones::Versioned.new(options.destination)
48
+ options
26
49
  end
27
50
 
28
- o.on('--base PATH', "Change the base URL path") do |path|
29
- options.base = path
51
+ def base=(path)
52
+ @base = Bones.base = path
30
53
  end
31
-
32
- o.on_tail("-h", "--help", "Show this message") do
33
- puts o
34
- exit
54
+
55
+ def versioned=(*args)
56
+ @release = Bones::VersionedRelease.new(@destination)
57
+ @destination = release.destination
58
+ end
59
+
60
+ def destination=(path)
61
+ @release = Bones::Release.new(@destination, path)
62
+ @destination = release.destination
35
63
  end
36
- end.parse!(args)
37
-
38
- options
39
- end
40
64
 
41
- # Returns true if the versions enabled
42
- def versioned?
43
- versioned != false
44
- end
65
+ # Returns true if the versions enabled
66
+ def versioned?
67
+ Bones::VersionedRelease === release
68
+ end
45
69
 
46
- # Returns destination
47
- def destination
48
- versioned? ? versioned.destination : super
49
- end
50
- end
51
-
52
- # Process arguments
53
- options = CacheOptions.process(ARGV)
54
-
55
- # Fixes the given URL path to begin at given base.
56
- # In addition, the .html extension will be added if
57
- # the path is a page.
58
- #
59
- # For example, if the base is /some_folder,
60
- # normalize_url('/page_path', '/some_folder')
61
- # # => /some_folder/page_path.html
62
- def normalize_url(path, base='')
63
- @known_pairs ||= {}
64
- @public_directories_regex ||= Regexp.new(public_directories.join('|'))
70
+ def release?
71
+ !release.nil?
72
+ end
65
73
 
66
- if v = @known_pairs[path]
67
- return v
68
- else
69
- value = case
70
- when path =~ @public_directories_regex
71
- path
72
- when File.directory?('pages' / path)
73
- path
74
- else
75
- path + '.html'
74
+ # Returns destination
75
+ def destination
76
+ release? ? release.destination : super
77
+ end
76
78
  end
77
79
 
78
- @known_pairs[path] = base / value
79
- end
80
- end
80
+ attr_accessor :options
81
+
82
+ def initialize(options=nil)
83
+ self.options = Options === options ? options : Options.process(options)
84
+ end
85
+
86
+ def self.run(options=nil)
87
+ new(options).run
88
+ end
81
89
 
82
- version = options.versioned? ? options.versioned.versioned_directory_name : nil
90
+ def run
91
+ version = options.versioned? ? options.release.versioned_directory_name : nil
92
+
93
+ # Process each page
94
+ Dir.chdir(ROOT) do
95
+ puts "** Writing to: #{options.destination}"
96
+ puts "** Using base: #{options.base}" unless options.base.blank?
97
+
98
+ Bones.pages.each do |page|
99
+ puts "** Generating #{[version, page].compact.join('/')}.html"
100
+ template = Bones::Template.new(page)
101
+ template.request = generate_mock_request(:path_info => page)
102
+ result = template.compile
103
+ result.gsub!(/(href|src|action|url)(="|\()([-A-Za-z0-9_\.\/]+)([^:]*?)("|\))/) do |match|
104
+ property, url, params = $1, normalize_url(original_url = $3, options.base), $4
105
+ property =~ /url/ ? 'url(%s%s)' % [url, params] : '%s="%s%s"' % [property, url, params]
106
+ end
107
+ path = options.destination / page + '.html'
108
+ FileUtils.mkdir_p(File.dirname(path))
109
+ File.open(path, 'w') { |f| f.write(result) }
110
+ end
83
111
 
84
- # Process each page
85
- Dir.chdir(ROOT) do
86
- Bones.pages.each do |page|
87
- puts "** Generating #{[version, page].compact.join('/')}.html"
88
- result = Bones::Template.compile(page)
89
- result.gsub!(/(href|src|action)="([-A-Za-z0-9_\.\/]+)(.*?)"/) do |match|
90
- property, url, params = $1, normalize_url(original_url = $2, options.base), $3
91
- '%s="%s%s"' % [property, url, params]
112
+ if options.release?
113
+ puts "** Copying public files"
114
+ options.release.copy_public_directories
115
+ end
116
+
117
+ puts "** Cached to: #{options.destination}"
118
+ puts "** Using base: #{options.base}" unless options.base.blank?
119
+ end
120
+
121
+ puts "** Done."
92
122
  end
123
+
124
+ # Fixes the given URL path to begin at given base.
125
+ # In addition, the .html extension will be added if
126
+ # the path is a page.
127
+ #
128
+ # For example, if the base is /some_folder,
129
+ # normalize_url('/page_path', '/some_folder')
130
+ # # => /some_folder/page_path.html
131
+ def normalize_url(path, base='')
132
+ @known_pairs ||= {}
133
+ @public_directories_regex ||= Regexp.new(public_directories.join('|'))
93
134
 
94
- path = options.destination / page + '.html'
95
- FileUtils.mkdir_p(File.dirname(path))
96
- File.open(path, 'w') { |f| f.write(result) }
97
- end
135
+ if v = @known_pairs[path]
136
+ return v
137
+ else
138
+ value = case
139
+ when path =~ /^(\w{3,}:\/\/|mailto)/
140
+ return path
141
+ when path =~ @public_directories_regex
142
+ path
143
+ when File.directory?('pages' / path)
144
+ path
145
+ else
146
+ path + '.html'
147
+ end
148
+
149
+ @known_pairs[path] = base / value
150
+ end
151
+ end
98
152
 
99
- if options.versioned?
100
- puts "** Copying public files"
101
- options.versioned.copy_public_directories
102
- end
153
+ def generate_mock_request(options={})
154
+ OpenStruct.new(options)
155
+ end
156
+ end
103
157
  end
104
158
 
105
- puts "** Done."
159
+ if __FILE__ == $0
160
+ Bones::Cache.run(ARGV)
161
+ end
@@ -137,4 +137,9 @@ module CoreHelper
137
137
  return @content_for_title if value == -1
138
138
  @content_for_title = value
139
139
  end
140
+
141
+ # Returns base URL
142
+ def base
143
+ Bones.base
144
+ end
140
145
  end
data/lib/tasks/bones.rb CHANGED
@@ -1,3 +1,7 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'cache.rb')
2
+
3
+ task :default => :server
4
+
1
5
  desc "Start bones server"
2
6
  task :server do
3
7
  ARGV.shift
@@ -5,18 +9,27 @@ task :server do
5
9
  BonesServer.run
6
10
  end
7
11
 
12
+ desc "Cache page templates for redistribution (non-versioned)"
13
+ task :cache => 'cache:simple'
14
+
8
15
  namespace :cache do
9
- desc "Cache page templates for redistribution (non-versioned)"
16
+ def generate_options_from_environment(extra={})
17
+ returning Bones::Cache::Options.new do |options|
18
+ options.base = ENV['BASE'] if ENV['BASE']
19
+ options.destination = ENV['DESTINATION'] if ENV['DESTINATION']
20
+ options.merge extra
21
+ end
22
+ end
23
+
10
24
  task :simple do
11
- ARGV.shift
12
- load File.join(File.dirname(__FILE__), '..', 'cache.rb')
25
+ options = generate_options_from_environment
26
+ Bones::Cache.run(options)
13
27
  end
14
28
 
15
29
  desc "Cache page templatets for redistribution (versioned)"
16
30
  task :versioned do
17
- ARGV.shift
18
- file = File.join(File.dirname(__FILE__), '..', 'cache.rb')
19
- system "ruby #{file} '--versioned'"
31
+ options = generate_options_from_environment(:versioned => true)
32
+ Bones::Cache.run(options)
20
33
  end
21
34
  end
22
35
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scharfie-bones
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Scharf
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2008-08-22 00:00:00 -07:00
13
+ date: 2009-02-02 00:00:00 -08:00
14
14
  default_executable: bones
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -18,7 +18,7 @@ dependencies:
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
- - - ">="
21
+ - - "="
22
22
  - !ruby/object:Gem::Version
23
23
  version: 0.3.0
24
24
  version: