Skip to content

Commit 6a21a19

Browse files
committed
Refactor and tidy up
1 parent 4d7583d commit 6a21a19

9 files changed

+189
-108
lines changed

bin/build

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
require "fileutils"
44
require "yaml"
5-
require_relative "./lib/config"
6-
require_relative "./lib/builder"
5+
require_relative "lib/build_config"
6+
require_relative "lib/builder"
7+
require_relative "lib/terminal"
78

89
# Usage:
910
#
@@ -19,13 +20,23 @@ require_relative "./lib/builder"
1920
# $ ./build default # build using the default config
2021
# $ ./build basic # build using the basic CI config
2122
# $ ./build react # build using the React CI config
23+
#
24+
# You can capture the output to a file using the `tee` command:
25+
#
26+
# $ ./build all_variants | tee build.log
27+
#
28+
# If you want to capture the output to a file and keep the color output, you can
29+
# use the `unbuffer` command:
30+
#
31+
# $ brew install expect # or your linux distro equivalent
32+
# $ unbuffer ./build all_variants | tee build.log
2233

2334
class Main
2435
class << self
2536
def main
26-
configs = Config.resolve_to_configs(ARGV.first)
37+
configs = BuildConfig.resolve_to_configs(ARGV.first)
2738

28-
puts configs_summary(configs)
39+
Terminal.puts_header(configs_summary(configs))
2940

3041
configs.each do |config|
3142
Builder.new(config:).build

bin/compare

-61
This file was deleted.

bin/create_comarison_repo_prs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env ruby
2+
3+
require_relative "lib/comparison_config"
4+
require_relative "lib/terminal"
5+
6+
class Main
7+
class << self
8+
def main(dry_run:)
9+
Dir.chdir(ComparisonConfig.comparison_repo_root_path) do
10+
ComparisonConfig.comparison_builds.each do |build_name|
11+
Terminal.puts_header("Creating PR for #{build_name}")
12+
cmd = "gh pr create --title 'Compare #{build_name} against vanilla Rails' --body 'Compare #{build_name} against vanilla Rails' --base main --head #{build_name} #{dry_run ? "--dry-run" : ""}"
13+
system(cmd)
14+
end
15+
end
16+
end
17+
end
18+
end
19+
20+
Main.main(dry_run: ARGV.include?("--dry-run"))

bin/create_comparison_repo

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env ruby
2+
3+
require "fileutils"
4+
require_relative "lib/comparison_config"
5+
require_relative "lib/terminal"
6+
7+
class Main
8+
class << self
9+
def main
10+
reset_comparison_repo
11+
use_vanilla_rails_as_main_branch
12+
13+
ComparisonConfig.comparison_builds.each do |build_name|
14+
populate_new_branch_for_build(build_name:)
15+
checkout_main
16+
end
17+
end
18+
19+
private
20+
21+
def reset_comparison_repo
22+
repo_path = ComparisonConfig.comparison_repo_root_path
23+
24+
FileUtils.rm_rf(repo_path)
25+
FileUtils.mkdir_p(repo_path)
26+
27+
Dir.chdir(repo_path) do
28+
system("git init")
29+
system("git remote add origin git@github.com:ackama/rails-template-variants-comparison.git")
30+
end
31+
end
32+
33+
def use_vanilla_rails_as_main_branch
34+
Terminal.puts_header("Adding vanilla Rails build to main branch")
35+
copy_build(build_path: ComparisonConfig.vanilla_build_path)
36+
commit_changes(commit_message: "Add #{ComparisonConfig.vanilla_build_name}")
37+
end
38+
39+
def populate_new_branch_for_build(build_name:)
40+
Terminal.puts_header("Creating branch for #{build_name}")
41+
42+
rm_rf_except_git_dir
43+
create_new_branch(branch_name: build_name)
44+
45+
build_path = File.join(ComparisonConfig.build_path, build_name)
46+
copy_build(build_path:)
47+
48+
commit_changes(commit_message: "Add #{build_name}")
49+
end
50+
51+
def checkout_main
52+
Dir.chdir(ComparisonConfig.comparison_repo_root_path) do
53+
system("git checkout main")
54+
end
55+
end
56+
57+
# clean out all the files from the previous build (except .git)
58+
def rm_rf_except_git_dir
59+
Dir.chdir(ComparisonConfig.comparison_repo_root_path) do
60+
keepers = [".git", "."]
61+
deletable_files = Dir.glob("{*,.*}").reject { |f| keepers.include?(f) }
62+
63+
FileUtils.rm_rf(deletable_files)
64+
end
65+
end
66+
67+
def create_new_branch(branch_name:)
68+
Dir.chdir(ComparisonConfig.comparison_repo_root_path) do
69+
system("git checkout -b #{branch_name}")
70+
end
71+
end
72+
73+
def commit_changes(commit_message:)
74+
Dir.chdir(ComparisonConfig.comparison_repo_root_path) do
75+
system("git add .")
76+
# skip pre-commit hooks because they're not relevant here
77+
system("git commit --quiet --no-verify -m '#{commit_message}'")
78+
end
79+
end
80+
81+
def copy_build(build_path:)
82+
system("rsync -qav --exclude='.git' --exclude='node_modules' --exclude='tmp' #{build_path}/ #{ComparisonConfig.comparison_repo_root_path}/")
83+
end
84+
end
85+
end
86+
87+
Main.main

bin/create_prs

-20
This file was deleted.

bin/lib/config.rb renamed to bin/lib/build_config.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Config
1+
class BuildConfig
22
PROJECT_ROOT_PATH = File.absolute_path(File.join(__dir__, "../.."))
33
CI_CONFIGS_PATH = File.join(PROJECT_ROOT_PATH, "ci/configs")
44
BUILD_PATH = File.join(PROJECT_ROOT_PATH, "tmp/builds")

bin/lib/builder.rb

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
##
2-
# Invokes the template as a user would, not as CI does via the build-and-test script.
3-
# Automatically removes old DB before generating the app if required
4-
#
1+
require "rubygems"
2+
53
class Builder
64
def initialize(config:)
75
@config = config
6+
@rails_cmd_version = build_rails_cmd_version(target_rails_major_minor: config.target_rails_major_minor)
87
end
98

109
def build
11-
verify_rails_cmd_is_expected_version!
12-
1310
Dir.chdir(@config.build_path) do
1411
delete_any_previous_build
1512
drop_dbs_from_previous_build
1613

17-
# TODO: how to handle errors? raise? or log and continue? or rename the app dir with a .failed suffix?
18-
# TODO: how to handle output? redirect to a file? or sep files for stdout and stderr?
19-
2014
if @config.vanilla?
15+
Terminal.puts_header("Building vanilla Rails app")
2116
system(build_vanilla_cmd_env, build_vanilla_cmd)
2217
else
18+
Terminal.puts_header("Building Rails app")
2319
system(build_cmd_env, build_cmd)
2420
end
2521
end
@@ -29,20 +25,33 @@ def build
2925

3026
def base_cmd_parts
3127
[
32-
"rails new #{@config.app_name}",
28+
"rails _#{@rails_cmd_version}_ new #{@config.app_name}",
3329
"-d postgresql",
3430
"--skip-javascript",
3531
"--skip-kamal",
3632
"--skip-solid"
3733
]
3834
end
3935

40-
def verify_rails_cmd_is_expected_version!
41-
actual_rails_version = `rails -v`.strip.sub("Rails ", "")
36+
def build_rails_cmd_version(target_rails_major_minor:)
37+
specs = Gem::Specification.find_all_by_name("rails")
38+
39+
raise "No versions of gem '#{gem_name}' are installed" if specs.empty?
4240

43-
return if actual_rails_version.start_with?(@config.target_rails_major_minor)
41+
version = specs
42+
.map { _1.version.to_s }
43+
.sort
44+
.reverse
45+
.find { |v| v.start_with?(target_rails_major_minor) }
46+
test_cmd = "rails _#{version}_ -v"
47+
expected_test_output = "Rails #{version}"
48+
actual_test_output = `#{test_cmd}`.strip
4449

45-
raise "Expected Rails version #{@config.target_rails_major_minor}, but got #{actual_rails_version}"
50+
raise "Command failed: #{test_cmd}. Actual: #{actual_test_output}" unless expected_test_output == actual_test_output
51+
52+
Terminal.puts_header("Using Rails version #{version}")
53+
54+
version
4655
end
4756

4857
def delete_any_previous_build
@@ -84,17 +93,10 @@ def build_vanilla_cmd
8493
base_cmd_parts.join(" ")
8594
end
8695

87-
def print_separator
88-
puts ""
89-
puts "*" * 80
90-
puts ""
91-
end
92-
9396
def drop_dbs_from_previous_build
94-
print_separator
97+
Terminal.puts_header("Dropping databases from previous build")
9598
system "psql -c 'DROP DATABASE IF EXISTS #{@config.app_name}_test;'"
9699
system "psql -c 'DROP DATABASE IF EXISTS #{@config.app_name}_development;'"
97100
system "psql -c 'DROP DATABASE IF EXISTS #{@config.app_name}_test;'"
98-
print_separator
99101
end
100102
end

bin/lib/comparison_config.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class ComparisonConfig
2+
PROJECT_ROOT_PATH = File.absolute_path(File.join(__dir__, "../.."))
3+
BUILD_PATH = File.join(PROJECT_ROOT_PATH, "tmp/builds")
4+
5+
class << self
6+
def build_path
7+
BUILD_PATH
8+
end
9+
10+
def comparison_repo_root_path
11+
File.join(PROJECT_ROOT_PATH, "tmp/rails-template-variants-comparison")
12+
end
13+
14+
def vanilla_build_name
15+
all_builds.find { |build_name| build_name.start_with?("vanilla_") }
16+
end
17+
18+
def vanilla_build_path
19+
File.join(BUILD_PATH, vanilla_build_name)
20+
end
21+
22+
def comparison_builds
23+
all_builds - [vanilla_build_name]
24+
end
25+
26+
def all_builds
27+
Dir.children(BUILD_PATH)
28+
end
29+
end
30+
end

bin/lib/terminal.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Terminal
2+
class << self
3+
def puts_header(title)
4+
puts <<~EO_HEADER
5+
6+
#{title}
7+
#{"=" * title.length}
8+
9+
EO_HEADER
10+
end
11+
end
12+
end

0 commit comments

Comments
 (0)