Skip to content

Commit f27e3be

Browse files
committed
Rearrange npm release process again
Make prep_release idempotent, including the npm bump.
1 parent 7bcfe84 commit f27e3be

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

tasks/release.rb

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66

77
directory "pkg"
88

9+
# This "npm-ifies" the current version number
10+
# With npm, versions such as "5.0.0.rc1" or "5.0.0.beta1.1" are not compliant with its
11+
# versioning system, so they must be transformed to "5.0.0-rc1" and "5.0.0-beta1-1" respectively.
12+
13+
# "5.0.1" --> "5.0.1"
14+
# "5.0.1.1" --> "5.0.1-1" *
15+
# "5.0.0.rc1" --> "5.0.0-rc1"
16+
#
17+
# * This makes it a prerelease. That's bad, but we haven't come up with
18+
# a better solution at the moment.
19+
npm_version = version.gsub(/\./).with_index { |s, i| i >= 2 ? "-" : s }
20+
921
(FRAMEWORKS + ['rails']).each do |framework|
1022
namespace framework do
1123
gem = "pkg/#{framework}-#{version}.gem"
@@ -43,6 +55,17 @@
4355
raise "Could not insert PRE in #{file}" unless $1
4456

4557
File.open(file, 'w') { |f| f.write ruby }
58+
59+
require "json"
60+
if File.exist?("#{framework}/package.json") && JSON.parse(File.read("#{framework}/package.json"))["version"] != npm_version
61+
Dir.chdir("#{framework}") do
62+
if sh "which npm"
63+
sh "npm version #{npm_version} --no-git-tag-version"
64+
else
65+
raise "You must have npm installed to release Rails."
66+
end
67+
end
68+
end
4669
end
4770

4871
task gem => %w(update_versions pkg) do
@@ -61,38 +84,10 @@
6184
task :push => :build do
6285
sh "gem push #{gem}"
6386

64-
# When running the release task we usually run build first to check that the gem works properly.
65-
# NPM will refuse to publish or rebuild the gem if the version is changed when the Rails gem
66-
# versions are changed. This then causes the gem push to fail. Because of this we need to update
67-
# the version and publish at the same time.
6887
if File.exist?("#{framework}/package.json")
6988
Dir.chdir("#{framework}") do
70-
# This "npm-ifies" the current version
71-
# With npm, versions such as "5.0.0.rc1" or "5.0.0.beta1.1" are not compliant with its
72-
# versioning system, so they must be transformed to "5.0.0-rc1" and "5.0.0-beta1-1" respectively.
73-
74-
# In essence, the code below runs through all "."s that appear in the version,
75-
# and checks to see if their index in the version string is greater than or equal to 2,
76-
# and if so, it will change the "." to a "-".
77-
78-
# Sample version transformations:
79-
# irb(main):001:0> version = "5.0.1.1"
80-
# => "5.0.1.1"
81-
# irb(main):002:0> version.gsub(/\./).with_index { |s, i| i >= 2 ? '-' : s }
82-
# => "5.0.1-1"
83-
# irb(main):003:0> version = "5.0.0.rc1"
84-
# => "5.0.0.rc1"
85-
# irb(main):004:0> version.gsub(/\./).with_index { |s, i| i >= 2 ? '-' : s }
86-
# => "5.0.0-rc1"
87-
version = version.gsub(/\./).with_index { |s, i| i >= 2 ? '-' : s }
88-
89-
# Check if npm is installed, and raise an error if not
90-
if sh 'which npm'
91-
sh "npm version #{version} --no-git-tag-version"
92-
sh "npm publish"
93-
else
94-
raise 'You must have npm installed to release Rails.'
95-
end
89+
npm_tag = version =~ /[a-z]/ ? "pre" : "latest"
90+
sh "npm publish --tag #{npm_tag}"
9691
end
9792
end
9893
end
@@ -104,9 +99,11 @@
10499
(FRAMEWORKS + ['guides']).each do |fw|
105100
require 'date'
106101
fname = File.join fw, 'CHANGELOG.md'
102+
current_contents = File.read(fname)
107103

108-
header = "## Rails #{version} (#{Date.today.strftime('%B %d, %Y')}) ##\n\n* No changes.\n\n\n"
109-
contents = header + File.read(fname)
104+
header = "## Rails #{version} (#{Date.today.strftime('%B %d, %Y')}) ##\n\n"
105+
header << "* No changes.\n\n\n" if current_contents =~ /\A##/
106+
contents = header + current_contents
110107
File.open(fname, 'wb') { |f| f.write contents }
111108
end
112109
end
@@ -143,7 +140,7 @@
143140
task :push => FRAMEWORKS.map { |f| "#{f}:push" } + ['rails:push']
144141

145142
task :ensure_clean_state do
146-
unless `git status -s | grep -v 'RAILS_VERSION\\|CHANGELOG\\|Gemfile.lock'`.strip.empty?
143+
unless `git status -s | grep -v 'RAILS_VERSION\\|CHANGELOG\\|Gemfile.lock\\|package.json\\|version.rb'`.strip.empty?
147144
abort "[ABORTING] `git status` reports a dirty tree. Make sure all changes are committed"
148145
end
149146

@@ -158,22 +155,24 @@
158155
end
159156

160157
task :commit do
161-
File.open('pkg/commit_message.txt', 'w') do |f|
162-
f.puts "# Preparing for #{version} release\n"
163-
f.puts
164-
f.puts "# UNCOMMENT THE LINE ABOVE TO APPROVE THIS COMMIT"
165-
end
158+
unless `git status -s`.strip.empty?
159+
File.open('pkg/commit_message.txt', 'w') do |f|
160+
f.puts "# Preparing for #{version} release\n"
161+
f.puts
162+
f.puts "# UNCOMMENT THE LINE ABOVE TO APPROVE THIS COMMIT"
163+
end
166164

167-
sh "git add . && git commit --verbose --template=pkg/commit_message.txt"
168-
rm_f "pkg/commit_message.txt"
165+
sh "git add . && git commit --verbose --template=pkg/commit_message.txt"
166+
rm_f "pkg/commit_message.txt"
167+
end
169168
end
170169

171170
task :tag do
172171
sh "git tag -s -m '#{tag} release' #{tag}"
173172
sh "git push --tags"
174173
end
175174

176-
task :prep_release => %w(ensure_clean_state build)
175+
task :prep_release => %w(ensure_clean_state build bundle commit)
177176

178-
task :release => %w(ensure_clean_state build bundle commit tag push)
177+
task :release => %w(prep_release tag push)
179178
end

0 commit comments

Comments
 (0)