middleman-core 3.0.0 → 3.0.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/.cane +2 -0
- data/.cane-exclusions.yml +5 -0
- data/Rakefile +1 -1
- data/bin/middleman +9 -55
- data/features/cli_extension.feature +1 -0
- data/features/frontmatter_page_settings.feature +11 -1
- data/features/instance_vars.feature +9 -0
- data/features/step_definitions/page_layout_steps.rb +3 -3
- data/features/strip_url.feature +40 -0
- data/features/support/env.rb +1 -1
- data/fixtures/automatic-directory-matcher-app/config.rb +1 -1
- data/fixtures/basic-data-app/config.rb +1 -1
- data/fixtures/build-with-errors-app/config.rb +1 -1
- data/fixtures/clean-app/config-complications.rb +1 -1
- data/fixtures/clean-app/config.rb +1 -1
- data/fixtures/current-page-app/config.rb +1 -1
- data/fixtures/custom-layout-app/config.rb +1 -1
- data/fixtures/data-app/config.rb +1 -1
- data/fixtures/external-helpers/config.rb +1 -1
- data/fixtures/external-helpers/helpers/derp.rb +1 -1
- data/fixtures/external-helpers/helpers/four_helpers.rb +1 -1
- data/fixtures/external-helpers/helpers/one_helper.rb +1 -1
- data/fixtures/external-helpers/helpers/yet_another_thingy.rb +1 -1
- data/fixtures/external-helpers/lib/hello_helper.rb +1 -1
- data/fixtures/feature-params-app/config.rb +1 -1
- data/fixtures/frontmatter-settings-app/config.rb +3 -1
- data/fixtures/frontmatter-settings-app/source/layouts/override.erb +2 -0
- data/fixtures/frontmatter-settings-app/source/override_layout.html.erb +4 -0
- data/fixtures/frontmatter-settings-app/source/page_mentioned.html.erb +4 -0
- data/fixtures/generator-test/config.rb +10 -10
- data/fixtures/glob-app/config.rb +1 -1
- data/fixtures/instance-vars-app/config.rb +7 -0
- data/fixtures/instance-vars-app/source/content.html.erb +2 -0
- data/fixtures/instance-vars-app/source/layout.erb +1 -0
- data/fixtures/large-build-app/config.rb +1 -1
- data/fixtures/large-build-app/source/images/Child folder/regular_file(example).txt +1 -1
- data/fixtures/large-build-app/source/images/Read me (example).txt +1 -1
- data/fixtures/manual-layout-missing/config.rb +1 -1
- data/fixtures/manual-layout-override/config.rb +1 -1
- data/fixtures/manual-layout/config.rb +1 -1
- data/fixtures/page-helper-layout-block-app/config.rb +1 -1
- data/fixtures/sinatra-app/config.rb +1 -1
- data/fixtures/strip-url-app/config.rb +0 -0
- data/fixtures/strip-url-app/source/index.html.erb +1 -0
- data/fixtures/strip-url-app/source/other.html.erb +1 -0
- data/fixtures/strip-url-app/source/subdir/index.html.erb +1 -0
- data/fixtures/traversal-app/config.rb +1 -1
- data/fixtures/wildcard-app/config.rb +1 -1
- data/lib/middleman-core.rb +3 -3
- data/lib/middleman-core/application.rb +12 -11
- data/lib/middleman-core/cli.rb +23 -10
- data/lib/middleman-core/cli/build.rb +65 -54
- data/lib/middleman-core/cli/bundler.rb +7 -7
- data/lib/middleman-core/cli/extension.rb +25 -12
- data/lib/middleman-core/cli/init.rb +19 -19
- data/lib/middleman-core/cli/server.rb +25 -16
- data/lib/middleman-core/core_extensions/builder.rb +3 -3
- data/lib/middleman-core/core_extensions/data.rb +28 -28
- data/lib/middleman-core/core_extensions/extensions.rb +24 -25
- data/lib/middleman-core/core_extensions/external_helpers.rb +7 -7
- data/lib/middleman-core/core_extensions/file_watcher.rb +41 -38
- data/lib/middleman-core/core_extensions/front_matter.rb +36 -33
- data/lib/middleman-core/core_extensions/rendering.rb +49 -46
- data/lib/middleman-core/core_extensions/request.rb +34 -33
- data/lib/middleman-core/core_extensions/routing.rb +17 -17
- data/lib/middleman-core/core_extensions/ruby_encoding.rb +1 -1
- data/lib/middleman-core/core_extensions/show_exceptions.rb +5 -5
- data/lib/middleman-core/extensions.rb +4 -4
- data/lib/middleman-core/load_paths.rb +56 -0
- data/lib/middleman-core/logger.rb +29 -0
- data/lib/middleman-core/preview_server.rb +35 -34
- data/lib/middleman-core/profiling.rb +58 -0
- data/lib/middleman-core/renderers/coffee_script.rb +8 -6
- data/lib/middleman-core/renderers/erb.rb +5 -5
- data/lib/middleman-core/renderers/haml.rb +5 -5
- data/lib/middleman-core/renderers/less.rb +14 -10
- data/lib/middleman-core/renderers/liquid.rb +7 -7
- data/lib/middleman-core/renderers/markdown.rb +13 -14
- data/lib/middleman-core/renderers/redcarpet.rb +31 -11
- data/lib/middleman-core/renderers/sass.rb +38 -22
- data/lib/middleman-core/renderers/slim.rb +15 -7
- data/lib/middleman-core/sitemap.rb +25 -28
- data/lib/middleman-core/sitemap/extensions/ignores.rb +13 -13
- data/lib/middleman-core/sitemap/extensions/on_disk.rb +12 -12
- data/lib/middleman-core/sitemap/extensions/proxies.rb +26 -20
- data/lib/middleman-core/sitemap/extensions/traversal.rb +10 -10
- data/lib/middleman-core/sitemap/resource.rb +47 -50
- data/lib/middleman-core/sitemap/store.rb +38 -36
- data/lib/middleman-core/step_definitions.rb +1 -1
- data/lib/middleman-core/step_definitions/builder_steps.rb +1 -1
- data/lib/middleman-core/step_definitions/middleman_steps.rb +3 -3
- data/lib/middleman-core/step_definitions/server_steps.rb +5 -5
- data/lib/middleman-core/templates.rb +12 -12
- data/lib/middleman-core/templates/default.rb +11 -11
- data/lib/middleman-core/templates/extension/Gemfile +14 -1
- data/lib/middleman-core/templates/extension/Rakefile +3 -1
- data/lib/middleman-core/templates/extension/features/support/env.rb +1 -1
- data/lib/middleman-core/templates/extension/gitignore +2 -0
- data/lib/middleman-core/templates/extension/lib/lib.rb +11 -11
- data/lib/middleman-core/templates/extension/lib/middleman_extension.rb +1 -1
- data/lib/middleman-core/templates/html5.rb +10 -10
- data/lib/middleman-core/templates/html5/source/humans.txt +0 -0
- data/lib/middleman-core/templates/html5/source/robots.txt +0 -0
- data/lib/middleman-core/templates/local.rb +4 -4
- data/lib/middleman-core/templates/mobile.rb +3 -3
- data/lib/middleman-core/templates/mobile/source/humans.txt +0 -0
- data/lib/middleman-core/templates/mobile/source/robots.txt +0 -0
- data/lib/middleman-core/templates/shared/config.ru +1 -1
- data/lib/middleman-core/templates/shared/config.tt +10 -10
- data/lib/middleman-core/util.rb +30 -6
- data/lib/middleman-core/vendor/hooks-0.2.0/lib/hooks.rb +19 -19
- data/lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb +2 -2
- data/lib/middleman-core/vendor/hooks-0.2.0/test/hooks_test.rb +29 -29
- data/lib/middleman-core/vendor/hooks-0.2.0/test/inheritable_attribute_test.rb +11 -11
- data/lib/middleman-core/version.rb +1 -1
- data/lib/middleman/rack.rb +4 -0
- data/middleman-core-x86-mingw32.gemspec +38 -0
- data/middleman-core.gemspec +3 -2
- metadata +72 -11
@@ -2,22 +2,22 @@
|
|
2
2
|
module Middleman
|
3
3
|
module CoreExtensions
|
4
4
|
module Routing
|
5
|
-
|
5
|
+
|
6
6
|
# Setup extension
|
7
7
|
class << self
|
8
|
-
|
8
|
+
|
9
9
|
# Once registered
|
10
10
|
def registered(app)
|
11
11
|
# Include methods
|
12
12
|
app.send :include, InstanceMethods
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
alias :included :registered
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
# Routing instance methods
|
19
19
|
module InstanceMethods
|
20
|
-
|
20
|
+
|
21
21
|
# Takes a block which allows many pages to have the same layout
|
22
22
|
#
|
23
23
|
# with_layout :admin do
|
@@ -29,13 +29,13 @@ module Middleman
|
|
29
29
|
# @return [void]
|
30
30
|
def with_layout(layout_name, &block)
|
31
31
|
old_layout = layout
|
32
|
-
|
32
|
+
|
33
33
|
set :layout, layout_name
|
34
34
|
instance_exec(&block) if block_given?
|
35
35
|
ensure
|
36
36
|
set :layout, old_layout
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
# The page method allows the layout to be set on a specific path
|
40
40
|
#
|
41
41
|
# page "/about.html", :layout => false
|
@@ -45,36 +45,36 @@ module Middleman
|
|
45
45
|
# @param [Hash] opts
|
46
46
|
# @return [void]
|
47
47
|
def page(url, opts={}, &block)
|
48
|
-
|
48
|
+
|
49
49
|
blocks = []
|
50
50
|
blocks << block if block_given?
|
51
|
-
|
51
|
+
|
52
52
|
# Default layout
|
53
53
|
opts[:layout] = layout if opts[:layout].nil?
|
54
54
|
|
55
55
|
# If the url is a regexp
|
56
56
|
if url.is_a?(Regexp) || url.include?("*")
|
57
|
-
|
57
|
+
|
58
58
|
# Use the metadata loop for matching against paths at runtime
|
59
59
|
sitemap.provides_metadata_for_path url do |url|
|
60
60
|
{ :options => opts, :blocks => blocks }
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
return
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
# Normalized path
|
67
67
|
url = full_path(url)
|
68
68
|
|
69
69
|
# Setup proxy
|
70
70
|
if opts.has_key?(:proxy)
|
71
71
|
proxy(url, opts[:proxy])
|
72
|
-
|
72
|
+
|
73
73
|
if opts.has_key?(:ignore) && opts[:ignore]
|
74
74
|
ignore(opts[:proxy])
|
75
75
|
opts.delete(:ignore)
|
76
|
-
end
|
77
|
-
|
76
|
+
end
|
77
|
+
|
78
78
|
opts.delete(:proxy)
|
79
79
|
else
|
80
80
|
if opts.has_key?(:ignore) && opts[:ignore]
|
@@ -82,7 +82,7 @@ module Middleman
|
|
82
82
|
opts.delete(:ignore)
|
83
83
|
end
|
84
84
|
end
|
85
|
-
|
85
|
+
|
86
86
|
# Setup a metadata matcher for rendering those options
|
87
87
|
sitemap.provides_metadata_for_path url do |url|
|
88
88
|
{ :options => opts, :blocks => blocks }
|
@@ -91,4 +91,4 @@ module Middleman
|
|
91
91
|
end
|
92
92
|
end
|
93
93
|
end
|
94
|
-
end
|
94
|
+
end
|
@@ -5,10 +5,10 @@ require 'rack/showexceptions'
|
|
5
5
|
module Middleman
|
6
6
|
module CoreExtensions
|
7
7
|
module ShowExceptions
|
8
|
-
|
8
|
+
|
9
9
|
# Setup extension
|
10
10
|
class << self
|
11
|
-
|
11
|
+
|
12
12
|
# Once registered
|
13
13
|
def registered(app)
|
14
14
|
# When in dev
|
@@ -20,12 +20,12 @@ module Middleman
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
# Custom exception class
|
25
25
|
# TODO: Style this ourselves
|
26
26
|
class Middleware < ::Rack::ShowExceptions
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
end
|
30
30
|
end
|
31
|
-
end
|
31
|
+
end
|
@@ -59,16 +59,16 @@ module Middleman
|
|
59
59
|
# @private
|
60
60
|
def load_extensions_in_path
|
61
61
|
require "rubygems"
|
62
|
-
|
62
|
+
|
63
63
|
begin
|
64
64
|
require "middleman-more"
|
65
65
|
rescue LoadError
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
extensions = rubygems_latest_specs.select do |spec|
|
69
69
|
spec_has_file?(spec, EXTENSION_FILE)
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
extensions.each do |spec|
|
73
73
|
require spec.name
|
74
74
|
end
|
@@ -100,4 +100,4 @@ module Middleman
|
|
100
100
|
File.exists?(full_path)
|
101
101
|
end
|
102
102
|
end
|
103
|
-
end
|
103
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# Core Pathname library used for traversal
|
2
|
+
require "pathname"
|
3
|
+
|
4
|
+
module Middleman
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def setup_load_paths
|
8
|
+
@_is_setup ||= begin
|
9
|
+
|
10
|
+
# Only look for config.rb if MM_ROOT isn't set
|
11
|
+
if !ENV["MM_ROOT"] && found_path = locate_root
|
12
|
+
ENV["MM_ROOT"] = found_path
|
13
|
+
end
|
14
|
+
|
15
|
+
is_bundler_setup = false
|
16
|
+
|
17
|
+
# If we've found the root, try to setup Bundler
|
18
|
+
if ENV["MM_ROOT"]
|
19
|
+
|
20
|
+
root_gemfile = File.expand_path('Gemfile', ENV["MM_ROOT"])
|
21
|
+
ENV['BUNDLE_GEMFILE'] ||= root_gemfile
|
22
|
+
|
23
|
+
if !File.exists?(ENV['BUNDLE_GEMFILE'])
|
24
|
+
git_gemfile = Pathname.new(__FILE__).expand_path.parent.parent.parent + "Gemfile"
|
25
|
+
ENV['BUNDLE_GEMFILE'] = git_gemfile.to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
if File.exists?(ENV['BUNDLE_GEMFILE'])
|
29
|
+
is_bundler_setup = true
|
30
|
+
require 'bundler/setup'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Automatically discover extensions in RubyGems
|
35
|
+
require "middleman-core/extensions"
|
36
|
+
|
37
|
+
if is_bundler_setup
|
38
|
+
Bundler.require
|
39
|
+
else
|
40
|
+
::Middleman.load_extensions_in_path
|
41
|
+
end
|
42
|
+
|
43
|
+
true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Recursive method to find config.rb
|
48
|
+
def locate_root(cwd = Pathname.new(Dir.pwd))
|
49
|
+
return cwd.to_s if (cwd + 'config.rb').exist?
|
50
|
+
return false if cwd.root?
|
51
|
+
locate_root(cwd.parent)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Use the Ruby/Rails logger
|
2
|
+
require 'active_support/core_ext/logger'
|
3
|
+
require "securerandom"
|
4
|
+
|
5
|
+
module Middleman
|
6
|
+
|
7
|
+
# The Middleman Logger
|
8
|
+
class Logger < ::Logger
|
9
|
+
|
10
|
+
# Force output to STDOUT
|
11
|
+
def initialize(log_level=1, is_instrumenting=false, target=STDOUT)
|
12
|
+
super(STDOUT)
|
13
|
+
|
14
|
+
self.level = log_level
|
15
|
+
@instrumenting = is_instrumenting
|
16
|
+
|
17
|
+
if @instrumenting != false
|
18
|
+
::ActiveSupport::Notifications.subscribe(/\.middleman$/, self)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def call(message, *args)
|
23
|
+
return if @instrumenting.is_a?(String) && @instrumenting != "instrument" && !message.include?(@instrumenting)
|
24
|
+
|
25
|
+
evt = ActiveSupport::Notifications::Event.new(message, *args)
|
26
|
+
self.info "== Instrument (#{evt.name.sub(/.middleman$/, '')}): #{evt.duration}ms"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -1,52 +1,53 @@
|
|
1
1
|
require "webrick"
|
2
2
|
|
3
3
|
module Middleman
|
4
|
-
|
4
|
+
|
5
5
|
WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i) unless const_defined?(:WINDOWS)
|
6
6
|
|
7
7
|
module PreviewServer
|
8
|
-
|
8
|
+
|
9
9
|
DEFAULT_PORT = 4567
|
10
|
-
|
10
|
+
|
11
11
|
class << self
|
12
|
-
|
12
|
+
attr_reader :app
|
13
|
+
delegate :logger, :to => :app
|
14
|
+
|
13
15
|
# Start an instance of Middleman::Application
|
14
16
|
# @return [void]
|
15
17
|
def start(options={})
|
16
|
-
app = ::Middleman::Application.server.inst do
|
18
|
+
@app = ::Middleman::Application.server.inst do
|
17
19
|
if options[:environment]
|
18
20
|
set :environment, options[:environment].to_sym
|
19
21
|
end
|
20
|
-
|
21
|
-
|
22
|
-
set :logging, true
|
23
|
-
end
|
22
|
+
|
23
|
+
logger(options[:debug] ? 0 : 1, options[:instrumenting] || false)
|
24
24
|
end
|
25
25
|
|
26
26
|
port = options[:port] || DEFAULT_PORT
|
27
|
-
|
28
|
-
|
27
|
+
|
28
|
+
logger.info "== The Middleman is standing watch on port #{port}"
|
29
29
|
|
30
30
|
@webrick ||= setup_webrick(
|
31
31
|
options[:host] || "0.0.0.0",
|
32
32
|
port,
|
33
33
|
options[:debug] || false
|
34
34
|
)
|
35
|
-
|
35
|
+
|
36
36
|
mount_instance(app)
|
37
37
|
|
38
38
|
start_file_watcher unless options[:"disable-watcher"]
|
39
|
-
|
39
|
+
|
40
40
|
@initialized ||= false
|
41
41
|
unless @initialized
|
42
42
|
@initialized = true
|
43
|
-
|
43
|
+
|
44
44
|
register_signal_handlers unless ::Middleman::WINDOWS
|
45
|
-
|
45
|
+
|
46
46
|
# Save the last-used options so it may be re-used when
|
47
47
|
# reloading later on.
|
48
48
|
@last_options = options
|
49
|
-
|
49
|
+
::Middleman::Profiling.report("server_start")
|
50
|
+
|
50
51
|
@webrick.start
|
51
52
|
end
|
52
53
|
end
|
@@ -54,14 +55,14 @@ module Middleman
|
|
54
55
|
# Detach the current Middleman::Application instance
|
55
56
|
# @return [void]
|
56
57
|
def stop
|
57
|
-
|
58
|
+
logger.info "== The Middleman is shutting down"
|
58
59
|
if @listener
|
59
60
|
@listener.stop
|
60
61
|
@listener = nil
|
61
62
|
end
|
62
63
|
unmount_instance
|
63
64
|
end
|
64
|
-
|
65
|
+
|
65
66
|
# Simply stop, then start the server
|
66
67
|
# @return [void]
|
67
68
|
def reload
|
@@ -75,17 +76,17 @@ module Middleman
|
|
75
76
|
stop
|
76
77
|
@webrick.shutdown
|
77
78
|
end
|
78
|
-
|
79
|
+
|
79
80
|
private
|
80
|
-
|
81
|
+
|
81
82
|
def start_file_watcher
|
82
83
|
# Watcher Library
|
83
84
|
require "listen"
|
84
|
-
|
85
|
+
|
85
86
|
return if @listener
|
86
87
|
|
87
88
|
@listener = Listen.to(Dir.pwd, :relative_paths => true)
|
88
|
-
|
89
|
+
|
89
90
|
@listener.change do |modified, added, removed|
|
90
91
|
added_and_modified = (modified + added)
|
91
92
|
|
@@ -98,10 +99,10 @@ module Middleman
|
|
98
99
|
|
99
100
|
# Otherwise forward to Middleman
|
100
101
|
added_and_modified.each do |path|
|
101
|
-
@app.files.did_change(
|
102
|
+
@app.files.did_change(path)
|
102
103
|
end
|
103
104
|
end
|
104
|
-
|
105
|
+
|
105
106
|
unless removed.empty?
|
106
107
|
# See if the changed file is config.rb or lib/*.rb
|
107
108
|
if needs_to_reload?(removed)
|
@@ -111,15 +112,15 @@ module Middleman
|
|
111
112
|
|
112
113
|
# Otherwise forward to Middleman
|
113
114
|
removed.each do |path|
|
114
|
-
@app.files.did_delete(
|
115
|
+
@app.files.did_delete(path)
|
115
116
|
end
|
116
117
|
end
|
117
118
|
end
|
118
|
-
|
119
|
+
|
119
120
|
# Don't block this thread
|
120
121
|
@listener.start(false)
|
121
122
|
end
|
122
|
-
|
123
|
+
|
123
124
|
# Trap the interupt signal and shut down smoothly
|
124
125
|
# @return [void]
|
125
126
|
def register_signal_handlers
|
@@ -127,28 +128,28 @@ module Middleman
|
|
127
128
|
trap("TERM") { shutdown }
|
128
129
|
trap("QUIT") { shutdown }
|
129
130
|
end
|
130
|
-
|
131
|
-
# Initialize webrick
|
131
|
+
|
132
|
+
# Initialize webrick
|
132
133
|
# @return [void]
|
133
134
|
def setup_webrick(host, port, is_logging)
|
134
135
|
@host = host
|
135
136
|
@port = port
|
136
|
-
|
137
|
+
|
137
138
|
http_opts = {
|
138
139
|
:BindAddress => @host,
|
139
140
|
:Port => @port,
|
140
141
|
:AccessLog => []
|
141
142
|
}
|
142
|
-
|
143
|
+
|
143
144
|
if is_logging
|
144
145
|
http_opts[:Logger] = FilteredWebrickLog.new
|
145
146
|
else
|
146
147
|
http_opts[:Logger] = ::WEBrick::Log.new(nil, 0)
|
147
148
|
end
|
148
|
-
|
149
|
+
|
149
150
|
::WEBrick::HTTPServer.new(http_opts)
|
150
151
|
end
|
151
|
-
|
152
|
+
|
152
153
|
# Attach a new Middleman::Application instance
|
153
154
|
# @param [Middleman::Application] app
|
154
155
|
# @return [void]
|
@@ -156,7 +157,7 @@ module Middleman
|
|
156
157
|
@app = app
|
157
158
|
@webrick.mount "/", ::Rack::Handler::WEBrick, @app.class.to_rack_app
|
158
159
|
end
|
159
|
-
|
160
|
+
|
160
161
|
# Detach the current Middleman::Application instance
|
161
162
|
# @return [void]
|
162
163
|
def unmount_instance
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Middleman
|
2
|
+
module Profiling
|
3
|
+
|
4
|
+
# The profiler instance. There can only be one!
|
5
|
+
def self.profiler=(prof)
|
6
|
+
@profiler = prof
|
7
|
+
end
|
8
|
+
def self.profiler
|
9
|
+
@profiler ||= NullProfiler.new
|
10
|
+
end
|
11
|
+
|
12
|
+
# Start the profiler
|
13
|
+
def self.start
|
14
|
+
profiler.start
|
15
|
+
end
|
16
|
+
|
17
|
+
# Stop the profiler and generate a report. Make sure to call start first
|
18
|
+
def self.report(report_name)
|
19
|
+
profiler.report(report_name)
|
20
|
+
end
|
21
|
+
|
22
|
+
# A profiler that does nothing. The default.
|
23
|
+
class NullProfiler
|
24
|
+
def start
|
25
|
+
end
|
26
|
+
|
27
|
+
def report(report_name)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# A profiler that uses ruby-prof
|
32
|
+
class RubyProfProfiler
|
33
|
+
def initialize
|
34
|
+
begin
|
35
|
+
require 'ruby-prof'
|
36
|
+
rescue LoadError
|
37
|
+
raise "To use the --profile option, you must 'gem install ruby-prof' (and include it in your Gemfile if running under bundle exec)"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def start
|
42
|
+
RubyProf.start
|
43
|
+
end
|
44
|
+
|
45
|
+
def report(report_name)
|
46
|
+
result = RubyProf.stop
|
47
|
+
|
48
|
+
printer = RubyProf::GraphHtmlPrinter.new(result)
|
49
|
+
outfile = File.join("profile", report_name)
|
50
|
+
outfile = (outfile + '.html') unless outfile.end_with? '.html'
|
51
|
+
FileUtils.mkdir_p(File.dirname(outfile))
|
52
|
+
File.open(outfile, 'w') do |f|
|
53
|
+
printer.print(f, :min_percent=> 1)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|