Skip to content

Commit c6c3a61

Browse files
committed
Add katapult update script
1 parent d1cb704 commit c6c3a61

File tree

9 files changed

+130
-46
lines changed

9 files changed

+130
-46
lines changed

Gemfile

+9
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@ source 'https://rubygems.org'
22

33
# Specify your gem's dependencies in katapult.gemspec
44
gemspec
5+
6+
gem 'bundler', '~> 1.5'
7+
gem 'rake'
8+
gem 'pry'
9+
10+
# Test
11+
gem 'aruba', '~> 0.10'
12+
gem 'guard-cucumber'
13+
gem 'rspec', '~> 3.4'

README.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,17 @@ account. Create a dedicated account on your local PostgreSQL server:
207207
postgres=# CREATE ROLE katapult WITH createdb LOGIN;
208208

209209
### Continuing development
210-
When you continue development on `katapult`, remove its `tmp/` directory first.
211-
It contains cached data that might lead to confusion.
210+
When you continue development on `katapult`, you first need to update a couple
211+
of things. `script/update` will guide you through the process.
212212

213213
### Debugging
214214
Add the `@announce-output` tag to `katapult` features in order to have any output
215215
logged to your terminal.
216216

217-
To precisely debug errors occurring _inside_ the generated application, you may
218-
cd to `tmp/aruba/katapult_test_app`. Run the failing command manually.
219-
220-
Note that after running a katapult feature, you need to call
221-
`cd ../../aruba/katapult_test_app` inside the generated app terminal. This is
222-
required because the `tmp/aruba` directory is being wiped before each scenario.
217+
To precisely debug errors occurring _inside_ the generated application, use
218+
`script/kta`. You could also just cd to the test app directory, but since it is
219+
destroyed between test runs, you'd need to `cd ../../aruba/katapult_test_app`
220+
after each test.
223221

224222
### Typical errors
225223
- Timeout error because of a script waiting for user input

bin/katapult

+15-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
# This script simplifies the usage of `katapult` by grouping relevant actions
44
# that the user else had to perform manually.
5-
# See bottom for USAGE.
5+
6+
usage = <<-USAGE
7+
Usage: katapult [new APP_NAME | fire [path/to/model] ]
8+
Suppress database credentials prompt with `--non-interactive`
9+
USAGE
610

711
require_relative '../lib/katapult/binary_util'
812
util = Katapult::BinaryUtil
@@ -14,35 +18,35 @@ when 'new'
1418
basics_command = 'bundle exec rails generate katapult:basics'
1519

1620
if interactive
17-
util.puts 'Please enter your database user: '
21+
util.pink 'Please enter your database user: '
1822
basics_command << ' --db-user ' << gets.chomp
1923

20-
util.puts 'Please enter your database password: '
24+
util.pink 'Please enter your database password: '
2125
basics_command << ' --db-password ' << gets.chomp
2226
end
2327

24-
util.puts 'Creating new Rails application ...'
28+
util.pink 'Creating new Rails application ...'
2529
util.create_rails_app app_name
2630

2731
Dir.chdir app_name
2832

29-
util.puts 'Initializing git repository ...'
33+
util.pink 'Initializing git repository ...'
3034
util.run 'git init --quiet'
3135
util.git_commit "rails new #{ app_name }", '--quiet'
3236

33-
util.puts 'Installing katapult ...'
37+
util.pink 'Installing katapult ...'
3438
File.open('Gemfile', 'a') do |file|
3539
file.puts "gem 'katapult'#{ ENV['KATAPULT_GEMFILE_OPTIONS'] }, group: :development"
3640
end
3741
util.run 'bundle install --quiet'
3842
util.run 'bundle exec rails generate katapult:install'
3943
util.git_commit 'rails generate katapult:install', '--quiet'
4044

41-
util.puts 'Generating katapult basics ...'
45+
util.pink 'Generating katapult basics ...'
4246
util.run basics_command
4347
util.git_commit 'rails generate katapult:basics', '--quiet'
4448

45-
util.puts <<-INSTRUCTIONS
49+
util.pink <<-INSTRUCTIONS
4650
Application initialization done.
4751
4852
Next step: Model your application in lib/katapult/application_model.rb and
@@ -53,20 +57,17 @@ when 'fire'
5357
app_model_path = ARGV.shift || 'lib/katapult/application_model.rb'
5458
transform_command = 'bin/rails generate katapult:transform ' + app_model_path
5559

56-
util.puts 'Loading katapult ...'
60+
util.pink 'Loading katapult ...'
5761
util.run transform_command
5862
util.git_commit transform_command
5963

60-
util.puts <<-INSTRUCTIONS
64+
util.pink <<-INSTRUCTIONS
6165
Model transformation done.
6266
6367
Now boot up your development server (e.g. with `rails server`) and try your
6468
kickstarted application in the browser!
6569
INSTRUCTIONS
6670

6771
else
68-
puts <<-USAGE
69-
Usage: katapult [new APP_NAME | fire [path/to/model] ]
70-
Suppress database credentials prompt with `--non-interactive`
71-
USAGE
72+
puts usage
7273
end

katapult.gemspec

+2-12
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,8 @@ Gem::Specification.new do |spec|
1818
spec.test_files = spec.files.grep(%r{^(spec|features)/})
1919
spec.require_paths = ['lib']
2020

21-
spec.required_ruby_version = '~> 2.0'
21+
# spec.required_ruby_version = Katapult::RUBY_VERSION
2222

23-
spec.add_runtime_dependency 'rails'
23+
spec.add_runtime_dependency 'rails', Katapult::RAILS_VERSION
2424
spec.add_runtime_dependency 'spring' # speed-up
25-
26-
# Development
27-
spec.add_development_dependency 'bundler', '~> 1.5'
28-
spec.add_development_dependency 'rake'
29-
spec.add_development_dependency 'pry'
30-
31-
# Testing
32-
spec.add_development_dependency 'aruba', '~> 0.10.2'
33-
spec.add_development_dependency 'guard-cucumber'
34-
spec.add_development_dependency 'rspec', '~> 3.4.0'
3525
end

lib/katapult.rb

-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
require 'active_support'
22
require 'active_support/core_ext/hash/slice'
3-
4-
module Katapult
5-
RAILS_VERSION = '4.2.7.1'
6-
RUBY_VERSION = '2.3.0'
7-
end

lib/katapult/binary_util.rb

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Utility methods.
22

3-
# The Katapult::Util module is used inside the `katapult` script. It should not
4-
# require any gems in order to prevent version conflicts.
5-
require_relative '../katapult'
3+
# This module is used inside the `katapult` binary and thus should not
4+
# require any gems in order to prevent version conflicts
5+
require_relative '../katapult/version'
66
require 'bundler'
77

88
module Katapult
@@ -18,20 +18,33 @@ def create_rails_app(name)
1818
run "rails _#{ Katapult::RAILS_VERSION }_ new #{ name } --skip-test-unit --skip-bundle --database postgresql"
1919
end
2020

21-
def puts(*args)
21+
def pink(*args)
2222
message = "\n> #{ args.join ' ' }"
2323
Kernel.puts "\e[35m#{ message }\e[0m" # pink
2424
end
2525

2626
# With clean Bundler env
2727
def run(command)
2828
success = Bundler.with_clean_env { system command }
29+
success or fail 'Something went wrong'
30+
end
31+
32+
def job(do_something, done = 'Done.', &job)
33+
pink "About to #{do_something}. [C]ontinue, [s]kip or [e]xit?"
2934

30-
if !success
31-
puts 'x Something went wrong'
32-
exit(1)
35+
case $stdin.getch
36+
when 's' then puts('Skipped.')
37+
when 'e' then fail('Cancelled.')
38+
else
39+
job.call
40+
puts done
3341
end
3442
end
3543

44+
def fail(message)
45+
puts "x #{message}"
46+
exit(1)
47+
end
48+
3649
end
3750
end

lib/katapult/version.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module Katapult
22
VERSION = '0.2.0'
3+
RUBY_VERSION = File.read('.ruby-version').strip
4+
RAILS_VERSION = '5.1.2'
35
end

script/kta

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env sh
2+
3+
# kta = katapult_test_app
4+
# This script performs any given command in the katapult test app directory
5+
6+
cd tmp/aruba/katapult_test_app
7+
eval $@

script/update

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env ruby
2+
3+
require_relative '../lib/katapult/version'
4+
require_relative '../lib/katapult/binary_util'
5+
6+
u = Katapult::BinaryUtil
7+
version_path = 'lib/katapult/version.rb'
8+
9+
10+
# Intro ########################################################################
11+
12+
at_project_root = File.exists?('lib/katapult.rb')
13+
at_project_root or fail 'Run this script from the Katapult project root.'
14+
15+
puts <<INTRO
16+
Katapult #{Katapult::VERSION}
17+
18+
This script will help you continue development on Katapult: updating the
19+
versions of Ruby and Rails, wiping tmp/ and more.
20+
21+
It will always ask before doing anything.
22+
23+
The script is intended as a one-time updater, but it wont' do harm if run
24+
repeatedly.
25+
26+
INTRO
27+
28+
u.pink "The Ruby version is #{`ruby -v`.chomp}."
29+
puts 'Should katapult use that version? [yN]'
30+
case $stdin.getch
31+
when 'y' then puts 'Ok.'
32+
else
33+
u.pink 'Please configure the Ruby version in .ruby-version, then re-run this script.'
34+
exit
35+
end
36+
37+
# Jobs #########################################################################
38+
39+
u.job 'update Bundler' do
40+
system 'gem install bundler'
41+
end
42+
43+
u.job 'update Rails version', "#{version_path} updated." do
44+
print "Please enter the desired Rails version (current: #{Katapult::RAILS_VERSION}): "
45+
rails_v = gets.strip
46+
47+
version_rb = File.read version_path
48+
version_rb.gsub! /^( RAILS_VERSION =).*$/, "\\1 '#{rails_v}'"
49+
File.open version_path, 'w' do |f|
50+
f.write version_rb
51+
end
52+
end
53+
54+
u.job 'update installed gems' do
55+
system 'bundle update'
56+
end
57+
58+
u.job 'wipe tmp directory' do
59+
FileUtils.rm_rf 'tmp'
60+
end
61+
62+
puts <<-DEBUG
63+
64+
All things automatic have been done. Now to the manual part: You need to fix
65+
whatever was broken by the updates.
66+
67+
The suggested way is to run `cucumber features/basics.feature` and fix issues
68+
until it is green.
69+
DEBUG

0 commit comments

Comments
 (0)