1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/Rakefile

176 lines
4.9 KiB
Text
Raw Normal View History

require 'rubygems'
2008-08-31 03:49:22 -04:00
require 'rake/clean'
require 'fileutils'
2007-11-21 17:56:42 -05:00
task :default => :test
2008-08-31 03:49:22 -04:00
# SPECS ===============================================================
desc 'Run specs with story style output'
task :spec do
I knew I shoulda taken that left turn at Hoboken This is a fairly large reworking of Sinatra's innards. Although most of the internal implementation has been modified, it provides the same basic feature set and is meant to be compatible with Sinatra 0.3.2. * The Event and EventContext classes have been removed. Sinatra applications are now defined within the class context of a Sinatra::Base subclass; each request is processed within a new instance. * Sinatra::Base can be used as a base class for multiple Rack applications within a single process and can be used as Rack middleware. * The routing and result type processing implementation has been simplified and enhanced a bit. There's a new route conditions system for things like :agent/:host matching and a request level #pass method has been added to allow an event handler to exit immediately, passing control to the next matching route. * Regular expressions may now be used in route patterns. Captures are available as an array from "params[:captures]". * The #body helper method now takes a block. The block is not evaluated until an attempt is made to read the body. * Options are now dynamically generated class attributes on the Sinatra::Base subclass (instead of OpenStruct); options are inherited by subclasses and may be overridden up the inheritance hierarchy. The Base.set manages all option related stuff. * The application file (app_file) detection heuristics are bit more sane now. This fixes some bugs with reloading and public/views directory detection. All thin / passenger issues of these type should be better now. * Error mappings are now split into to distinct layers: exception mappings and custom error pages. Exception mappings are registered with 'error(Exception)' and are run only when the app raises an exception. Custom error pages are registered with error(status_code) and are run any time the response has the status code specified. It's also possible to register an error page for a range of status codes: 'error(500..599)'. * The spec and unit testing extensions have been modified to take advantage of the ability to have multiple Sinatra applications. The Sinatra::Test module must be included within the TestCase in order to take advantage of these methods (unless the 'sinatra/compat' library has been required). * Rebuilt specs from scratch for better coverage and organization. Sinatra 3.2 unit tests have been retained under ./compat to ensure a baseline level of compatibility with previous versions; use the 'rake compat' task to run these. A large number of existing Sinatra idioms have been deprecated but continue to be supported through the 'sinatra/compat' library. * The "set_option" and "set_options" methods have been deprecated due to redundancy; use "set". * The "env" option (Sinatra::Base.env) has been renamed to "environment" and deprecated because it's too easy to confuse with the request-level Rack environment Hash (Sinatra::Base#env). * The request level "stop" method has been renamed "halt" and deprecated. This is for consistency with `throw :halt`. * The request level "entity_tag" method has been renamed "etag" and deprecated. Both versions were previously supported. * The request level "headers" method has been deprecated. Use response['Header-Name'] to access and modify response headers. * Sinatra.application is deprecated. Use Sinatra::Application instead. * Setting Sinatra.application = nil to reset an application is deprecated. You shouldn't have to reset objects anymore. * The Sinatra.default_options Hash is deprecated. Modifying this object now results in "set(key, value)" invocations on the Sinatra::Base subclass. * The "body.to_result" convention has been deprecated. * The ServerError exception has been deprecated. Any Exception is now considered a ServerError.
2008-12-13 16:06:02 -05:00
pattern = ENV['TEST'] || '.*'
sh "specrb --testcase '#{pattern}' --specdox -Ilib:test test/*_test.rb"
2008-08-31 03:49:22 -04:00
end
desc 'Run specs with unit test style output'
I knew I shoulda taken that left turn at Hoboken This is a fairly large reworking of Sinatra's innards. Although most of the internal implementation has been modified, it provides the same basic feature set and is meant to be compatible with Sinatra 0.3.2. * The Event and EventContext classes have been removed. Sinatra applications are now defined within the class context of a Sinatra::Base subclass; each request is processed within a new instance. * Sinatra::Base can be used as a base class for multiple Rack applications within a single process and can be used as Rack middleware. * The routing and result type processing implementation has been simplified and enhanced a bit. There's a new route conditions system for things like :agent/:host matching and a request level #pass method has been added to allow an event handler to exit immediately, passing control to the next matching route. * Regular expressions may now be used in route patterns. Captures are available as an array from "params[:captures]". * The #body helper method now takes a block. The block is not evaluated until an attempt is made to read the body. * Options are now dynamically generated class attributes on the Sinatra::Base subclass (instead of OpenStruct); options are inherited by subclasses and may be overridden up the inheritance hierarchy. The Base.set manages all option related stuff. * The application file (app_file) detection heuristics are bit more sane now. This fixes some bugs with reloading and public/views directory detection. All thin / passenger issues of these type should be better now. * Error mappings are now split into to distinct layers: exception mappings and custom error pages. Exception mappings are registered with 'error(Exception)' and are run only when the app raises an exception. Custom error pages are registered with error(status_code) and are run any time the response has the status code specified. It's also possible to register an error page for a range of status codes: 'error(500..599)'. * The spec and unit testing extensions have been modified to take advantage of the ability to have multiple Sinatra applications. The Sinatra::Test module must be included within the TestCase in order to take advantage of these methods (unless the 'sinatra/compat' library has been required). * Rebuilt specs from scratch for better coverage and organization. Sinatra 3.2 unit tests have been retained under ./compat to ensure a baseline level of compatibility with previous versions; use the 'rake compat' task to run these. A large number of existing Sinatra idioms have been deprecated but continue to be supported through the 'sinatra/compat' library. * The "set_option" and "set_options" methods have been deprecated due to redundancy; use "set". * The "env" option (Sinatra::Base.env) has been renamed to "environment" and deprecated because it's too easy to confuse with the request-level Rack environment Hash (Sinatra::Base#env). * The request level "stop" method has been renamed "halt" and deprecated. This is for consistency with `throw :halt`. * The request level "entity_tag" method has been renamed "etag" and deprecated. Both versions were previously supported. * The request level "headers" method has been deprecated. Use response['Header-Name'] to access and modify response headers. * Sinatra.application is deprecated. Use Sinatra::Application instead. * Setting Sinatra.application = nil to reset an application is deprecated. You shouldn't have to reset objects anymore. * The Sinatra.default_options Hash is deprecated. Modifying this object now results in "set(key, value)" invocations on the Sinatra::Base subclass. * The "body.to_result" convention has been deprecated. * The ServerError exception has been deprecated. Any Exception is now considered a ServerError.
2008-12-13 16:06:02 -05:00
task :test do |t|
sh "specrb -Ilib:test test/*_test.rb"
end
desc 'Run compatibility specs'
task :compat do |t|
pattern = ENV['TEST'] || '.*'
sh "specrb --testcase '#{pattern}' -Ilib:test compat/*_test.rb"
2008-08-31 03:49:22 -04:00
end
# PACKAGING ============================================================
# Load the gemspec using the same limitations as github
2008-08-31 03:49:22 -04:00
def spec
@spec ||=
begin
require 'rubygems/specification'
data = File.read('sinatra.gemspec')
spec = nil
Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
spec
end
2008-08-31 03:49:22 -04:00
end
def package(ext='')
"dist/sinatra-#{spec.version}" + ext
end
desc 'Build packages'
task :package => %w[.gem .tar.gz].map {|e| package(e)}
desc 'Build and install as local gem'
task :install => package('.gem') do
sh "gem install #{package('.gem')}"
end
directory 'dist/'
file package('.gem') => %w[dist/ sinatra.gemspec] + spec.files do |f|
sh "gem build sinatra.gemspec"
mv File.basename(f.name), f.name
end
file package('.tar.gz') => %w[dist/] + spec.files do |f|
sh "git archive --format=tar HEAD | gzip > #{f.name}"
2008-04-11 19:29:36 -04:00
end
# Rubyforge Release / Publish Tasks ==================================
2008-11-02 06:42:25 -05:00
desc 'Publish website to rubyforge'
task 'publish:doc' => 'doc/api/index.html' do
sh 'scp -rp doc/* rubyforge.org:/var/www/gforge-projects/sinatra/'
end
task 'publish:gem' => [package('.gem'), package('.tar.gz')] do |t|
sh <<-end
rubyforge add_release sinatra sinatra #{spec.version} #{package('.gem')} &&
rubyforge add_file sinatra sinatra #{spec.version} #{package('.tar.gz')}
end
end
2008-11-02 06:42:25 -05:00
# Website ============================================================
2008-11-02 07:51:09 -05:00
# Building docs requires HAML and the hanna gem:
# gem install mislav-hanna --source=http://gems.github.com
task 'doc' => ['doc:api','doc:site']
desc 'Generate Hanna RDoc under doc/api'
task 'doc:api' => ['doc/api/index.html']
file 'doc/api/index.html' => FileList['lib/**/*.rb','README.rdoc'] do |f|
rb_files = f.prerequisites
sh((<<-end).gsub(/\s+/, ' '))
hanna --charset utf8 \
--fmt html \
--inline-source \
--line-numbers \
--main README.rdoc \
--op doc/api \
--title 'Sinatra API Documentation' \
#{rb_files.join(' ')}
end
end
CLEAN.include 'doc/api'
2008-11-02 06:42:25 -05:00
def rdoc_to_html(file_name)
require 'rdoc/markup/to_html'
rdoc = RDoc::Markup::ToHtml.new
rdoc.convert(File.read(file_name))
end
def haml(locals={})
require 'haml'
template = File.read('doc/template.haml')
haml = Haml::Engine.new(template, :format => :html4, :attr_wrapper => '"')
haml.render(Object.new, locals)
end
2008-11-02 07:51:09 -05:00
desc 'Build website HTML and stuff'
task 'doc:site' => ['doc/index.html', 'doc/book.html']
2008-11-02 06:42:25 -05:00
2008-11-02 07:51:09 -05:00
file 'doc/index.html' => %w[README.rdoc doc/template.haml] do |file|
2008-11-02 06:42:25 -05:00
File.open(file.name, 'w') do |file|
file << haml(:title => 'Sinatra', :content => rdoc_to_html('README.rdoc'))
end
end
2008-11-02 07:51:09 -05:00
CLEAN.include 'doc/index.html'
2008-11-02 06:42:25 -05:00
2008-11-02 07:51:09 -05:00
file 'doc/book.html' => ['book/output/sinatra-book.html'] do |file|
2008-11-02 06:42:25 -05:00
File.open(file.name, 'w') do |file|
2008-11-02 07:51:09 -05:00
book_content = File.read('book/output/sinatra-book.html')
2008-11-02 06:42:25 -05:00
file << haml(:title => 'Sinatra Book', :content => book_content)
end
end
2008-11-02 07:51:09 -05:00
CLEAN.include 'doc/book.html'
2008-11-02 06:42:25 -05:00
2008-11-02 07:51:09 -05:00
file 'book/output/sinatra-book.html' => FileList['book/**'] do |f|
unless File.directory?('book')
sh 'git clone git://github.com/cschneid/sinatra-book.git book'
2008-11-02 06:42:25 -05:00
end
2008-11-02 07:51:09 -05:00
sh((<<-SH).strip.gsub(/\s+/, ' '))
cd book &&
git fetch origin &&
git rebase origin/master &&
thor book:build
SH
2008-11-02 06:42:25 -05:00
end
2008-11-02 07:51:09 -05:00
CLEAN.include 'book/output/sinatra-book.html'
desc 'Build the Sinatra book'
task 'doc:book' => ['book/output/sinatra-book.html']
2008-11-02 06:42:25 -05:00
# Gemspec Helpers ====================================================
2008-12-21 01:27:40 -05:00
def source_version
line = File.read('lib/sinatra/base.rb')[/^\s*VERSION = .*/]
line.match(/.*VERSION = '(.*)'/)[1]
end
file 'sinatra.gemspec' => FileList['{lib,test,images}/**','Rakefile'] do |f|
# read spec file and split out manifest section
spec = File.read(f.name)
2008-12-21 01:27:40 -05:00
head, manifest, tail = spec.split(" # = MANIFEST =\n")
# replace version and date
head.sub!(/\.version = '.*'/, ".version = '#{source_version}'")
head.sub!(/\.date = '.*'/, ".date = '#{Date.today.to_s}'")
# determine file list from git ls-files
files = `git ls-files`.
split("\n").
sort.
reject{ |file| file =~ /^\./ }.
2008-11-02 06:42:25 -05:00
reject { |file| file =~ /^doc/ }.
map{ |file| " #{file}" }.
join("\n")
# piece file back together and write...
2008-12-21 01:27:40 -05:00
manifest = " s.files = %w[\n#{files}\n ]\n"
spec = [head,manifest,tail].join(" # = MANIFEST =\n")
File.open(f.name, 'w') { |io| io.write(spec) }
puts "updated #{f.name}"
end