Skip to content

Commit 92297f9

Browse files
committed
Improve scripts
1 parent e9222b1 commit 92297f9

3 files changed

Lines changed: 62 additions & 66 deletions

File tree

scripts/bump.rb

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
require 'tempfile'
55
require_relative 'scripty'
6+
include Scripty # rubocop:disable Style/MixinUsage
67

78
# Abort if the working tree is dirty
89
abort('Working tree dirty!') unless `git status --porcelain`.empty?
@@ -21,34 +22,6 @@
2122
new_fragments = new_version.split('.')
2223
abort('Incomplete semantic version!') if new_fragments.size < 3
2324

24-
# Abort if there is no gem change
25-
abort("No gem changes since version #{old_version}!") \
26-
if `git diff --name-only --relative=gem "#{old_version}"..HEAD`.empty?
27-
28-
# Bump the version in files
29-
%w[retype.yml
30-
.github/ISSUE_TEMPLATE/Code.yml
31-
.github/latest_release_body.md
32-
gem/apps/calendar.ru
33-
gem/apps/demo.ru
34-
gem/apps/rails.ru
35-
gem/apps/repro.ru
36-
gem/bin/pagy
37-
gem/config/pagy.rb
38-
gem/lib/pagy.rb
39-
gem/pagy.gemspec
40-
src/pagy.ts].each do |path|
41-
Scripty.file_sub(path, old_version, new_version)
42-
end
43-
44-
# Bumps docs example
45-
Scripty.file_sub('quick-start.md',
46-
old_version.split('.')[0, 2].join('.'),
47-
new_version.split('.')[0, 2].join('.'))
48-
49-
# Build javascript files
50-
system(Scripty::ROOT.join('src/build').to_s)
51-
5225
# Create a tempfile with the formatted changes from the gem-filtered gitlog
5326
gitlog = Tempfile.new
5427
commits = `git rev-list "#{old_version}"..HEAD`.split("\n")
@@ -66,38 +39,65 @@
6639
body = lines.map { |line| " #{line}" }.join("\n")
6740
gitlog.puts body
6841
end
42+
# Abort if there is no gem change
43+
abort("No gem changes since version #{old_version}!") if gitlog.empty?
44+
6945
gitlog.close
7046

7147
# Edit the gitlog?
72-
Scripty.file_edit?('Gitlog', gitlog.path)
48+
edit_file?(gitlog.path, 'Gitlog')
7349

7450
# Prepare the .github/latest_release_body.md file
7551
# Used by .github/workflows/create_release.yml which is triggered by the :rubygem_release task (push tag)
7652
release_body_path = '.github/latest_release_body.md'
77-
# Insert whats_new from the README into latest_release_body file
78-
whats_new_content = Scripty.tagged_extract('README.md', 'whats_new')
79-
Scripty.tagged_file_sub(release_body_path, 'whats_new', whats_new_content)
53+
# Copy the whats_new from the README to the latest_release_body file
54+
whats_new_content = extract_section_from_file('README.md', 'whats_new')
55+
replace_section_in_file(release_body_path, 'whats_new', whats_new_content)
8056

81-
# Insert the changes into latest_release_body file
57+
# Insert the changes into the latest_release_body file
8258
changes = File.read(gitlog.path)
83-
Scripty.tagged_file_sub(release_body_path, 'changes', changes)
59+
replace_section_in_file(release_body_path, 'changes', changes)
8460

85-
# Edit the Rlease Body?
86-
Scripty.file_edit?('Release Body', release_body_path)
61+
# Edit the Release Body?
62+
edit_file?(release_body_path, 'Release Body')
8763

8864
# Update the CHANGELOG
89-
Scripty.file_sub('CHANGELOG.md', /<hr>\n/, "<hr>\n\n## Version #{new_version}\n\n#{changes}")
65+
replace_string_in_file('CHANGELOG.md', /<hr>\n/, "<hr>\n\n## Version #{new_version}\n\n#{changes}")
66+
67+
# Bump the version in files
68+
%w[retype.yml
69+
.github/ISSUE_TEMPLATE/Code.yml
70+
.github/latest_release_body.md
71+
gem/apps/calendar.ru
72+
gem/apps/demo.ru
73+
gem/apps/rails.ru
74+
gem/apps/repro.ru
75+
gem/bin/pagy
76+
gem/config/pagy.rb
77+
gem/lib/pagy.rb
78+
gem/pagy.gemspec
79+
src/pagy.ts].each do |path|
80+
replace_string_in_file(path, old_version, new_version)
81+
end
82+
83+
# Bumps docs example
84+
replace_string_in_file('quick-start.md',
85+
old_version.split('.')[0, 2].join('.'),
86+
new_version.split('.')[0, 2].join('.'))
87+
88+
# Build javascript files
89+
system(Scripty::ROOT.join('src/build').to_s)
9090

9191
# Run the test to check the consistency of versioning across files
9292
system('bundle exec rake test_version')
9393

9494
# Optional update of top 100
95-
Scripty.ask_and_do('Do you want to update the "Top 100 contributors"? (y/n)> ') do
95+
confirm_to('update the "Top 100 contributors"') do
9696
require_relative 'update_top100'
9797
end
9898

9999
# Optional commit
100-
Scripty.ask_and_do('Do you want to commit the changes? (y/n)> ') do
100+
confirm_to('commit the changes') do
101101
system('git add -A')
102102
system("git commit -m 'Version #{new_version}'")
103103
end

scripts/scripty.rb

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,41 @@ module Scripty
77
ROOT = Pathname.new(`git rev-parse --show-toplevel`.chomp)
88

99
# Ask for confirmation and do
10-
def ask_and_do(question)
11-
print question
10+
def confirm_to(action)
11+
print %(Do you want to #{action}? (y/n)> )
1212
yield if gets.chomp.start_with?(/y/i)
1313
end
14-
module_function :ask_and_do
1514

1615
# Optional edit
17-
def file_edit?(name, filepath)
18-
filepath = ROOT.join(filepath).to_s
19-
puts "\n#{name}:"
20-
puts File.read(filepath)
21-
print "Do you want to edit #{name.inspect}? (y/n)> "
16+
def edit_file?(filepath, caption = filepath)
17+
filepath = ROOT.join(filepath)
18+
puts "\n#{caption}:"
19+
puts filepath.read
20+
print "Do you want to edit #{caption.inspect}? (y/n)> "
2221
system("nano #{filepath}") if gets.chomp.start_with?(/y/i)
2322
end
24-
module_function :file_edit?
2523

2624
# Substitute a string in filepth
27-
def file_sub(filepath, search, replace)
28-
filepath = ROOT.join(filepath).to_s
29-
content = File.read(filepath)
25+
def replace_string_in_file(filepath, search, replace)
26+
filepath = ROOT.join(filepath)
27+
content = filepath.read
3028
content.sub!(search, replace)
31-
File.write(filepath, content)
29+
filepath.write(content)
3230
end
33-
module_function :file_sub
3431

3532
# Extract a tagged string in filepth
36-
def tagged_extract(filepath, tag)
37-
filepath = ROOT.join(filepath).to_s
38-
content = File.read(filepath)
39-
content[/<!-- #{tag}_start -->\n(.*)<!-- #{tag}_end -->/m, 1]
33+
def extract_section_from_file(filepath, section)
34+
filepath = ROOT.join(filepath)
35+
content = filepath.read
36+
content[/<!-- #{section}_start -->\n(.*)<!-- #{section}_end -->/m, 1]
4037
end
41-
module_function :tagged_extract
4238

4339
# Substitute a tagged string in filepth
44-
def tagged_file_sub(filepath, tag, new_content)
45-
filepath = ROOT.join(filepath).to_s
46-
content = File.read(filepath)
47-
content.sub!(/<!-- #{tag}_start -->\n.*<!-- #{tag}_end -->/m,
48-
"<!-- #{tag}_start -->\n#{new_content}<!-- #{tag}_end -->")
49-
File.write(filepath, content)
40+
def replace_section_in_file(filepath, section, new_content)
41+
filepath = ROOT.join(filepath)
42+
content = filepath.read
43+
content.sub!(/<!-- #{section}_start -->\n.*<!-- #{section}_end -->/m,
44+
"<!-- #{section}_start -->\n#{new_content}<!-- #{section}_end -->")
45+
filepath.write(content)
5046
end
51-
module_function :tagged_file_sub
5247
end

scripts/update_top100.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'json'
55
require 'net/http'
66
require_relative 'scripty'
7+
include Scripty # rubocop:disable Style/MixinUsage
78

89
USERS_URL_FMT = 'https://api.github.com/repos/ddnexus/pagy/contributors?page=%s'
910
COMMITS_URL_FMT = 'https://github.com/ddnexus/pagy/commits?author=%s'
@@ -27,6 +28,6 @@
2728
end
2829
top100 << "\n"
2930

30-
Scripty.tagged_file_sub('README.md', 'top100', top100)
31+
replace_section_in_file('README.md', 'top100', top100)
3132

3233
puts %("Top 100 Contributors" README section updated! (#{count}/#{MAX_COUNT}))

0 commit comments

Comments
 (0)