forked from mhs/tidbits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.thor
188 lines (163 loc) · 4.84 KB
/
tasks.thor
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
179
180
181
182
183
184
185
186
187
class ::String
def format
md = self.match(/^\s+/)
offset = md.offset(0)[1]
gsub(/^.{#{offset}}/, '')
end
end
module ProjectTemplateMethods
include FileUtils
# Makes a directory unless it already exists
def directory(dir, &block)
if File.directory?(dir)
log dir, "(already exists)"
else
log dir, "(creating directory)"
mkdir_p(dir)
end
Dir.chdir(dir, &block) if block_given?
end
# Makes a file with the following content unless the file already exists
def file(file, contents)
if File.exists?(file)
log file, "(already exists)"
else
log file, "(creating file)"
touch file
File.open(file, "w"){ |f| f.write contents } if contents
end
end
def log(path, message)
path = path += '/' if File.directory?(path) && path !~ /\/$/
puts "#{relativize(path)} #{message}"
end
def relativize(path)
File.expand_path(path).gsub(@project_dir, '')
end
def project(dir, &block)
@project_dir = File.expand_path(dir) + '/'
yield
@project_dir = nil
end
end
class Gen < Thor
include ProjectTemplateMethods
desc "cucumber", "Make cucumber directory structure in current directory"
def cucumber
project Dir.pwd do
file 'cucumber.yml', <<-EOS.format
default: -r features/support -r features/step_definitions
EOS
directory 'features' do
directory 'step_definitions'
directory 'support' do
file 'env.rb', <<-EOF.format
require 'cucumber'
require 'cucumber/formatter/unicode'
require 'spec'
#require 'webrat'
EOF
end
end
end
end
desc "rakefile", "Make cookie-cutter Rakefile in current directory"
def rakefile
project Dir.pwd do
file "Rakefile", <<-EOT.format
require 'rake/rdoctask'
require 'spec'
require 'spec/rake/spectask'
desc "Run all specs in spec directory"
Spec::Rake::SpecTask.new do |t|
t.spec_opts = ['--options', "\#{File.dirname(__FILE__)}/spec/spec.opts"]
t.spec_files = FileList['spec/**/*_spec.rb']
end
desc "Run all specs in spec directory with RCov"
Spec::Rake::SpecTask.new(:rcov) do |t|
t.spec_opts = ['--options', "\#{File.dirname(__FILE__)}/spec/spec.opts"]
t.spec_files = FileList['spec/**/*_spec.rb']
t.rcov = true
t.rcov_opts = lambda do
IO.readlines(File.dirname(__FILE__) + "/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
end
end
desc "Generate RDoc"
task :docs => :clobber_docs do
system "hanna --title '#{File.basename(Dir.pwd)} API Documentation'"
end
desc "Run specs using jruby"
task "spec:jruby" do
result = system "jruby -S rake spec"
raise "JRuby tests failed" unless result
end
desc "Run each spec in isolation to test for dependency issues"
task :spec_deps do
Dir["spec/**/*_spec.rb"].each do |test|
if !system("spec \#{test} &> /dev/null")
puts "Dependency Issues: \#{test}"
end
end
end
task :default => :spec
EOT
end
end
desc "rspec", "Make rspec directory structure in current directory"
def rspec
project Dir.pwd do
directory "spec" do
file "rcov.opts", <<-RCOVOPTS.format
-x gems,spec
RCOVOPTS
file "spec.opts", <<-SPECOPTS.format
--diff
--color
SPECOPTS
file "spec_helper.rb", <<-EOF.format
require 'spec'
# gem install redgreen for colored test output
begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end
Spec::Runner.configure do |config|
# spec config goes here
end
EOF
end
end
end
desc "databaseyml", "Make Rails database.yml file"
def databaseyml
dbname = File.basename(Dir.pwd).gsub(/\W+/, '_')
project Dir.pwd do
directory "config" do
file "database.yml", <<-CONFIG.gsub(/^\s*\|/, '')
|development: &DEVELOPMENT
| adapter: mysql
| encoding: utf8
| reconnect: false
| database: #{dbname}_development
| pool: 5
| username: root
| password:
| host: localhost
|
|test: &TEST
| <<: *DEVELOPMENT
| database: #{dbname}_test
|
|selenium:
| <<: *TEST
| database: #{dbname}_test
|
|cucumber:
| <<: *TEST
| database: #{dbname}_test
|
|production:
| <<: *DEVELOPMENT
| database: #{dbname}_production
CONFIG
end
end
end
end