Class: MonkeyButler::CLI
- Inherits:
-
Thor
- Object
- Thor
- MonkeyButler::CLI
show all
- Includes:
- Actions, Thor::Actions
- Defined in:
- lib/monkey_butler/cli.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Actions
#git, #git_add, #truncate_database
Class Method Details
.source_root ⇒ Object
Configures root path for resources (e.g. templates)
18
19
20
|
# File 'lib/monkey_butler/cli.rb', line 18
def self.source_root
File.dirname(__FILE__)
end
|
.start(given_args = ARGV, config = {}) ⇒ Object
Hook into the command execution for dynamic task configuration
314
315
316
317
318
319
320
|
# File 'lib/monkey_butler/cli.rb', line 314
def self.start(given_args = ARGV, config = {})
if File.exists?(Dir.pwd + '/.monkey_butler.yml')
project = MonkeyButler::Project.load
project.database_target_class.register_with_cli(self)
end
super
end
|
Instance Method Details
#config(key = nil, value = nil) ⇒ Object
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
# File 'lib/monkey_butler/cli.rb', line 297
def config(key = nil, value = nil)
if key && value
project.config[key] = value
project.save!(Dir.pwd)
elsif key
value = project.config[key]
if value
say "#{key}=#{value}"
else
say "No value for key '#{key}'"
end
else
project.config.each { |key, value| say "#{key}=#{value}" }
end
end
|
#drop ⇒ Object
101
102
103
104
|
# File 'lib/monkey_butler/cli.rb', line 101
def drop
@project = MonkeyButler::Project.load
invoke(project.database_target_class, :drop, [], options)
end
|
#dump ⇒ Object
89
90
91
92
|
# File 'lib/monkey_butler/cli.rb', line 89
def dump
@project = MonkeyButler::Project.load
invoke(project.database_target_class, :dump, [], options)
end
|
#generate ⇒ Object
230
231
232
233
234
235
236
237
238
|
# File 'lib/monkey_butler/cli.rb', line 230
def generate
project = MonkeyButler::Project.load
invoke(project.database_target_class, :generate, [], options)
target_names = options['targets'] || project.targets
MonkeyButler::Util.target_classes_named(target_names) do |target_class|
say "Invoking target '#{target_class.name}'..."
invoke(target_class, :generate, [], options)
end
end
|
#init(path) ⇒ Object
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
|
# File 'lib/monkey_butler/cli.rb', line 30
def init(path)
if File.exists?(path)
raise Error, "Cannot create repository: regular file exists at path '#{path}'" unless File.directory?(path)
raise Error, "Cannot create repository into non-empty path '#{path}'" if File.directory?(path) && Dir.entries(path) != %w{. ..}
end
self.destination_root = File.expand_path(path)
empty_directory('.')
inside(destination_root) { git init: '-q' }
project_name = options['name'] || File.basename(path)
sanitized_options = options.reject { |k,v| %w{bundler pretend database}.include?(k) }
sanitized_options[:name] = project_name
sanitized_options[:database_url] = options[:database] || "sqlite:#{project_name}.sqlite"
@project = MonkeyButler::Project.set(sanitized_options)
template('templates/gitignore.erb', ".gitignore")
git_add '.gitignore'
create_file '.monkey_butler.yml', YAML.dump(sanitized_options)
git_add '.monkey_butler.yml'
if options['bundler']
template('templates/Gemfile.erb', "Gemfile")
end
project = MonkeyButler::Project.set(sanitized_options)
target_options = options.merge('name' => project_name)
MonkeyButler::Util.target_classes_named(options[:targets]) do |target_class|
say "Initializing target '#{target_class.name}'..."
invoke(target_class, :init, [], target_options)
end
project.save!(destination_root) unless options['pretend']
git_add '.monkey_butler.yml'
if options['bundler']
git_add "Gemfile"
bundle
git_add "Gemfile.lock"
end
create_file(project.schema_path)
git_add project.schema_path
empty_directory('migrations')
inside do
invoke(project.database_target_class, :init, [], target_options)
end
end
|
#load ⇒ Object
95
96
97
98
|
# File 'lib/monkey_butler/cli.rb', line 95
def load
@project = MonkeyButler::Project.load
invoke(project.database_target_class, :load, [], options)
end
|
#migrate(version = nil) ⇒ Object
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
188
189
190
|
# File 'lib/monkey_butler/cli.rb', line 151
def migrate(version = nil)
project = MonkeyButler::Project.load
if migrations.up_to_date?
say "Database is up to date."
return
end
target_version = version || migrations.latest_version
if database.migrations_table?
say "Migrating from #{database.current_version} to #{target_version}"
else
say "Migrating new database to #{target_version}"
end
say
with_padding do
say "Migrating database..."
say
with_padding do
migrations.pending do |version, path|
say "applying migration: #{path}", :green
begin
database.execute_migration(File.read(path))
database.insert_version(version)
rescue project.database_class.exception_class => exception
fail Error, "Failed loading migration: #{exception}"
end
end
end
say
end
say "Migration to version #{target_version} complete."
if options['dump']
say
invoke :dump, [], options
end
end
|
#new(name) ⇒ Object
107
108
109
110
111
|
# File 'lib/monkey_butler/cli.rb', line 107
def new(name)
@project = MonkeyButler::Project.load
empty_directory('migrations')
invoke(project.database_target_class, :new, [name], options)
end
|
#package ⇒ Object
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
# File 'lib/monkey_butler/cli.rb', line 243
def package
validate
say
generate
say
git_add '.'
git :status unless options['quiet']
show_diff = options['diff'] != false && (options['diff'] || ask("Review package diff?", limited_to: %w{y n}) == 'y')
git diff: '--cached' if show_diff
commit = options['commit'] != false && (options['commit'] || ask("Commit package artifacts?", limited_to: %w{y n}) == 'y')
if commit
tag = unique_tag_for_version(migrations.latest_version)
git commit: "#{options['quiet'] && '-q '}-m 'Packaging release #{tag}' ."
git tag: "#{tag}"
else
say "Package artifacts were built but not committed. Re-run `mb package` when ready to complete build."
end
end
|
#push ⇒ Object
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
# File 'lib/monkey_butler/cli.rb', line 266
def push
git tag: "-l #{migrations.latest_version}"
unless $?.exitstatus.zero?
fail Error, "Could not find tag #{migrations.latest_version}. Did you forget to run `mb package`?"
end
push_options = []
push_options << '--force' if options['force']
branch_name = project.git_current_branch
run "git config branch.`git symbolic-ref --short HEAD`.merge", verbose: false
unless $?.exitstatus.zero?
say_status :git, "no merge branch detected: setting upstream during push", :yellow
push_options << "--set-upstream origin #{branch_name}"
end
push_options << "origin #{branch_name}"
push_options << "--tags"
git push: push_options.join(' ')
unless $?.exitstatus.zero?
fail Error, "git push failed."
end
target_names = options['targets'] || project.targets
MonkeyButler::Util.target_classes_named(target_names) do |target_class|
say "Invoking target '#{target_class.name}'..."
invoke(target_class, :push, [], options)
end
end
|
#status ⇒ Object
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
|
# File 'lib/monkey_butler/cli.rb', line 115
def status
project = MonkeyButler::Project.load
migrations = MonkeyButler::Migrations.new(project.migrations_path, database)
if database.migrations_table?
say "Current version: #{migrations.current_version}"
pending_count = migrations.pending.size
version = (pending_count == 1) ? "version" : "versions"
say "The database at '#{database}' is #{pending_count} #{version} behind #{migrations.latest_version}" unless migrations.up_to_date?
else
say "New database"
say "The database at '#{database}' does not have a 'schema_migrations' table."
end
if migrations.up_to_date?
say "Database is up to date."
return
end
say
say "Migrations to be applied"
with_padding do
say %q{(use "mb migrate" to apply)}
say
with_padding do
migrations.pending do |version, path|
say "pending migration: #{path}", :green
end
end
say
end
end
|
#validate ⇒ Object
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/monkey_butler/cli.rb', line 193
def validate
project = MonkeyButler::Project.load
say "Validating project configuration..."
say_status :git, "configuration", (project.git_url.empty? ? :red : :green)
if project.git_url.empty?
fail Error, "Invalid configuration: git does not have a remote named 'origin'."
end
say
invoke(project.database_target_class, :validate, [], options)
say "Validating schema loads..."
truncate_database
load
say
say "Validating migrations apply..."
truncate_database
migrate
say
say "Validating targets..."
target_names = options['targets'] || project.targets
MonkeyButler::Util.target_classes_named(target_names) do |target_class|
with_padding do
say_status :validate, target_class.name
invoke_with_padding(target_class, :validate, [], options)
end
end
say
say "Validation successful."
end
|