· 8 years ago · Mar 03, 2018, 09:36 PM
1#!/usr/bin/env ruby
2
3# = Synopsis
4#
5# The commandline tool to control ramaze
6#
7# = Usage
8# ramaze [switches] startfile [arguments]
9#
10# --create:: create a new application based on proto
11#
12# -m, --mongrel:: use mongrel to process requests
13# -w, --webrick:: the default
14# -g, --cgi:: CGI
15# -f, --fcgi:: FastCGI
16#
17# -b, --benchmark:: inform about :error, :info, :debug, :benchmark
18# -d, --debug:: inform about :error, info, :debug
19# -t, --stage:: inform about :info, :error
20# -l, --live:: inform about :error
21# -s, --silent:: inform about nothing
22#
23# -a, --autoreload:: Set the interval at which autoreload searches
24# for changes in your files
25#
26# -o, --host:: which host should ramaze listen for requests
27# default is 0.0.0.0
28# -p, --port:: what port should ramaze use, like 80 or 7000..7005
29# default is 7000
30#
31# -n, --no-errorpage:: don't use the default errorpage of ramaze
32# helpful if you want to make sure noone can
33# ever see your code by accident or for testing
34# --template-root:: set a custom template-root for the whole
35# application, please note that ~ is not
36# expanded to your home-directory
37# -a, --autoreload=N:: set how frequent ramaze should search for
38# updated files in your application
39# -r, --run-loose:: don't take control after startup.
40# this is useful for testcases.
41# -c, --console:: Start an IRB-like session.
42# --backtrace:: Show full backtrace at errors on start.
43# --cache:: use the simple caching based on the signature
44# of your requests (experimental)
45# --tidy:: run Tool::Tidy over text/html output
46#
47#
48# -h, --help:: print this help
49# -v, --version:: print the version
50# -c, --copyright:: print the copyright
51#
52# Please report bugs to <m.fellinger at gmail.com>
53#
54# = Copyright
55# Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
56# All files in this distribution are subject to the terms of the Ruby license.
57
58require 'ramaze'
59
60runner = File.expand_path(ARGV.find{|file| File.file?(file)} || 'main.rb')
61
62begin
63 require 'getoptlong'
64 require 'rdoc/ri/ri_paths'
65 require 'rdoc/usage'
66
67 # memo:
68 # REQUIRED_ARGUMENT, NO_ARGUMENT, OPTIONAL_ARGUMENT
69
70 opts = GetoptLong.new *[
71 [ '--create', GetoptLong::REQUIRED_ARGUMENT ],
72 [ '--mongrel', '-m', GetoptLong::NO_ARGUMENT ],
73 [ '--webrick', '-w', GetoptLong::NO_ARGUMENT ],
74 [ '--cgi', '-g', GetoptLong::NO_ARGUMENT ],
75 [ '--fcgi', '-f', GetoptLong::NO_ARGUMENT ],
76
77 [ '--debug', '-d', GetoptLong::NO_ARGUMENT ],
78 [ '--stage', '-t', GetoptLong::NO_ARGUMENT ],
79 [ '--live', '-l', GetoptLong::NO_ARGUMENT ],
80 [ '--silent', '-s', GetoptLong::NO_ARGUMENT ],
81
82 [ '--host', '-o', GetoptLong::REQUIRED_ARGUMENT ],
83 [ '--port', '-p', GetoptLong::REQUIRED_ARGUMENT ],
84
85 [ '--benchmark', '-b', GetoptLong::NO_ARGUMENT ],
86 [ '--no-errorpage', '-n', GetoptLong::NO_ARGUMENT ],
87 [ '--template-root', GetoptLong::REQUIRED_ARGUMENT ],
88 [ '--autoreload', '-a', GetoptLong::REQUIRED_ARGUMENT ],
89 [ '--run-loose', '-r', GetoptLong::NO_ARGUMENT ],
90
91 [ '--console', '-c', GetoptLong::NO_ARGUMENT ],
92 [ '--backtrace', GetoptLong::NO_ARGUMENT ],
93 [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
94 [ '--version', '-v', GetoptLong::NO_ARGUMENT ],
95 [ '--copyright', GetoptLong::NO_ARGUMENT ],
96 ]
97
98 options = {:origin => :console, :force => true}
99
100 create = lambda do |app|
101 require 'ramaze/tool/create'
102 Ramaze::Tool::Create.create(app)
103 exit
104 end
105
106 adapter = Dir[Ramaze::BASEDIR/'ramaze'/'adapter'/'*.rb']
107 adapter.map!{|f| File.basename(f, File.extname(f)).to_sym}
108
109 opts.each do |opt, arg|
110 sopt = opt[2..-1].gsub('-', '_').intern
111
112 options[:inform_tags] = Set.new([:debug, :info, :error])
113
114 case sopt
115 when :create
116 create[arg]
117 when :help
118 RDoc::ramazes_usage('Usage')
119 when :version
120 puts("ramaze #{Ramaze::VERSION}") or exit
121 when :copyright
122 RDoc::ramazes_usage('Copyright')
123 when *adapter
124 options[:adapter] = sopt
125
126 when :benchmark
127 options[:inform_tags] << :benchmark
128 when :debug
129 options
130 when :stage
131 options[:inform_tags].subtract [:debug]
132 when :live
133 options[:inform_tags].subtract [:info, :debug]
134 when :silent
135 options[:inform_tags].subtract options[:inform_tags]
136
137 when :host
138 options[:host] = arg
139 when :port
140 options[:port] = arg
141
142 when :no_errorpage
143 options[:error_page] = false
144 when :template_root
145 options[:template_root] = arg
146 when :autoreload
147 options[:autoreload] = arg.to_i
148 when :run_loose
149 options[:run_loose] = true
150 when :console
151 options[:console] = true
152 when :backtrace
153 options[:backtrace] = true
154 end
155 end
156
157 begin
158 puts "running #{runner}"
159 require runner
160 rescue LoadError => ex
161 puts ex
162 puts ex.backtrace if options[:backtrace]
163 puts "Maybe i cannot find `#{runner}'"
164 puts "You could provide a file to execute like:"
165 puts "$ ramaze myapp.rb"
166 exit
167 end
168
169 if options.delete(:console)
170 options.merge!(:run_loose => true)
171
172 ARGV.clear # Avoid passing args to IRB
173
174 Ramaze.start(options)
175
176 require 'irb'
177 require 'irb/completion'
178
179 def exit
180 exit!
181 end
182
183 ENV['IRBRC'] = ".irbrc" if File.exists? ".irbrc"
184 IRB.start
185 exit!
186 else
187 Ramaze.start(options)
188 end
189rescue LoadError => ex
190 puts ex
191end