@@ -6,8 +6,6 @@ require 'fileutils'
66
77# rubocop:disable Metrics/BlockLength
88namespace :version do
9- PROJECT_ROOT = File . expand_path ( FileUtils . pwd ) . freeze
10- PROJECT_NAME = ENV [ 'PROJECT_NAME' ] || File . basename ( PROJECT_ROOT )
119
1210 desc 'Write changes to the CHANGELOG'
1311 task :changes do
@@ -23,7 +21,7 @@ namespace :version do
2321
2422 desc 'Increment the patch version and write changes to the changelog'
2523 task :bump_patch do
26- exit unless check_branch_and_warn
24+ exit unless check_branch_and_warn?
2725 major , minor , patch = read_version
2826 patch = patch . to_i + 1
2927 write_version_file ( [ major , minor , patch ] )
@@ -36,7 +34,7 @@ namespace :version do
3634
3735 desc 'Increment the minor version and write changes to the changelog'
3836 task :bump_minor do
39- exit unless check_branch_and_warn
37+ exit unless check_branch_and_warn?
4038 major , minor , _patch = read_version
4139 minor = minor . to_i + 1
4240 patch = 0
@@ -47,7 +45,7 @@ namespace :version do
4745
4846 desc 'Increment the major version and write changes to the changelog'
4947 task :bump_major do
50- exit unless check_branch_and_warn
48+ exit unless check_branch_and_warn?
5149 major , _minor , _patch = read_version
5250 major = major . to_i + 1
5351 minor = 0
@@ -59,27 +57,35 @@ namespace :version do
5957
6058 private
6159
60+ def project_root
61+ @project_root ||= File . expand_path ( FileUtils . pwd ) . freeze
62+ end
63+
64+ def project_name
65+ @project_name ||= ENV [ 'PROJECT_NAME' ] || File . basename ( project_root )
66+ end
67+
6268 def version_file_path
63- split = PROJECT_NAME . split ( '-' )
64- "#{ PROJECT_ROOT } /lib/#{ split . join ( '/' ) } /version.rb"
69+ split = project_name . split ( '-' )
70+ "#{ project_root } /lib/#{ split . join ( '/' ) } /version.rb"
6571 end
6672
6773 def module_name
68- case PROJECT_NAME
74+ case project_name
6975 when /-/
70- PROJECT_NAME . split ( '-' ) . map ( &:capitalize ) . join ( '::' )
76+ project_name . split ( '-' ) . map ( &:capitalize ) . join ( '::' )
7177 when /_/
72- PROJECT_NAME . split ( '_' ) . map ( &:capitalize ) . join
78+ project_name . split ( '_' ) . map ( &:capitalize ) . join
7379 else
74- PROJECT_NAME . capitalize
80+ project_name . capitalize
7581 end
7682 end
7783
7884 def read_version
7985 silence_warnings do
8086 load version_file_path
8187 end
82- text = eval ( " #{ module_name } ::VERSION" )
88+ text = module_name . split ( '::' ) . inject ( Object ) { | mod , name | mod . const_get ( name ) } ::VERSION
8389 text . split ( '.' )
8490 end
8591
@@ -104,15 +110,13 @@ namespace :version do
104110 regex = /^\* \* Version: [0-9.]+\* \* $/i
105111 return nil unless readme =~ regex
106112
107- File . open ( 'README.md' , 'w' ) do |f |
108- f . write ( readme . gsub ( regex , "**Version: #{ version_string } **" ) )
109- end
113+ File . write ( 'README.md' , readme . gsub ( regex , "**Version: #{ version_string } **" ) )
110114 end
111115
112116 def changelog
113117 return @changelog_path if @changelog_path
114118
115- @changelog_path = File . join ( PROJECT_ROOT , 'CHANGELOG' )
119+ @changelog_path = File . join ( project_root , 'CHANGELOG' )
116120 FileUtils . touch ( @changelog_path )
117121 @changelog_path
118122 end
@@ -159,16 +163,15 @@ namespace :version do
159163 STRING
160164 end
161165
162- def check_branch_and_warn
166+ def check_branch_and_warn?
163167 return true unless current_branch == 'master'
164168
165169 puts ( branch_warning_message )
166- while ( line = $stdin. gets . chomp )
167- return true if line =~ /[yY]/
170+ line = $stdin. gets . chomp
171+ return true if line =~ /[yY]/
168172
169- puts 'Aborting version bump.'
170- return false
171- end
173+ puts 'Aborting version bump.'
174+ false
172175 end
173176
174177 def launch_editor ( file )
0 commit comments