-
Notifications
You must be signed in to change notification settings - Fork 2
/
web_inspector.rb
178 lines (155 loc) · 4.88 KB
/
web_inspector.rb
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require "sinatra/base"
require "monkey-lib"
require "big_band/compass"
class BigBand < Sinatra::Base
# The WebInspector allowes you to inspect a running Sinatra app.
# Just browse http://localhost:4567/\_\_inspect\_\_
#
# Per default this will only be activated in development mode.
module WebInspector
attr_reader :middleware
class Middleware < Sinatra::Base
attr_accessor :sinatra_app
use_in_file_templates! __FILE__
register BasicExtensions
set :app_file, __FILE__
# If this is a git project, then it returns the path to the .git directory.
def git_directory
@git_directory ||= root_glob(".{,.,./..,./../..}/.git").first
end
# Whether or not this is a git project.
def git?
!!git_directory
end
# Figures out some URLs to public git hosts by parsing the remote urls from the git config.
# Currently detects: GitHub, Codaset and Gitorious.
def git_remotes
@git_remotes ||= (File.read(git_directory / :config).scan(/\s*url\s*=\s*(.*)\n/).flatten.collect do |url|
case url
when %r{(github.com)[:/](.+)/(.+)/?\.git$} then [$3, "GitHub", "http://#$1/#$2/#$3", "http://#$1"]
when %r{(codaset.com)[:/](.+)/(.+)?\.git$} then [$3, "Codaset", "http://#$1/#$2/#$3", "http://#$1"]
when %r{(gitorious.org)[:/](.+)/.+/?\.git$} then [$2, "Gitorious", "http://#$1/#$2", "http://#$1"]
end
end).compact
end
# Recent git log.
def git_log
@git_format ||= begin
line = ["<a href='mailto:%ae'>%an</a>", "%s", "<date>%ai</date>"].map { |e| "<td>#{e}</td>" }.join
"<tr>#{line}</tr>"
end
%x[git log -50 --pretty=format:"#{@git_format}"]
end
# webinspector stylesheet.
get "/__inspect__/screen.css" do
content_type 'text/css', :charset => 'utf-8'
sass :stylesheet, ::Compass.sass_engine_options
end
# Route for inspection. Currently we display all information on a single page. In case the amount of data
# increases, we might split this on multiple pages. Also, this would ease hooking own data into the front-end.
get "/__inspect__/?" do
ruby_env = %w[RUBY_VERSION RUBY_DESCRIPTION RUBY_PATCHLEVEL RUBY_PLATFORM RUBY_ENGINE RUBY_ENGINE_VERSION]
ruby_env.map! { |var| [var, eval("#{var}.inspect if defined? #{var}")] }
haml :inspect, {}, :ruby_env => ruby_env
end
end
def self.registered(klass)
klass.register BasicExtensions
klass.register AdvancedRoutes
klass.use(Middleware) { |m| m.sinatra_app = klass }
end
end
end
__END__
@@layout
!!!
%html
%head
%meta{:charset=>"utf-8"}/
%link{:rel => "stylesheet", :href => "/__inspect__/screen.css", :type => "text/css"}/
%title= "#{sinatra_app.name}.inspect"
%body
%header
%h1= "#{sinatra_app.name}.inspect"
Generated on
%date= Time.now
%nav
%a{:href => "/__inspect__/#routes" } Routes
%a{:href => "/__inspect__/#extensions" } Extensions
%a{:href => "/__inspect__/#middleware" } Middleware
%a{:href => "/__inspect__/#system" } System
- if git?
%a{:href => "/__inspect__/#git_log" } Git Log
%article
!=yield
%footer
powered by
%a{:href => "http://www.sinatrarb.com"} Sinatra
and
%a{:href => "http://github.com/rkh/big_band"} BigBand
@@stylesheet
@import big_band/layouts/inspector.sass
+layout_inspector
@@inspect
%a{ :name => "routes" }
%h2 Routes
%table
%tr
%th Verb
%th Pattern
%th File
%th Keys
%th Conditions
- sinatra_app.each_route do |route|
%tr
%td= route.verb
%td= route.pattern.inspect
%td
- if route.file?
= route.file
%i= "(line #{route.line})" if route.line
%td= route.keys.map { |e| e.inspect }.join ", "
%td= route.conditions.map { |e| e.inspect }.join ", "
%a{ :name => "extensions" }
%h2 Extensions
%table
%tr
%th Extension
%th Status
- sinatra_app.extensions.each do |extension|
%tr
%td= extension.name
%td= extension.status if extension.respond_to? :status
%a{ :name => "middleware" }
%h2 Middleware
%table
%tr
%th Middleware
%th Arguments
%th Block Given
- sinatra_app.middleware.each do |name, arguments, block|
%tr
%td= name
%td= arguments.map { |e| e.inspect }.join ", "
%td= block ? "yes" : "no"
%a{ :name => "system" }
%h2 System
%table
%tr
%th Variable
%th Value
- ruby_env.each do |key, value|
%tr
%td= key
%td= value
- if git?
%a{ :name => "git_log" }
%h2 Recent Git Log
- git_remotes.each do |name, service, url, service_url|
.note Visit <a href="#{url}">#{name}</a> on <a href="#{service_url}">#{service}</a>.
%table
%tr
%th Author
%th Subject
%th Date
!= git_log