Class: Lapidist::Cli::Command
- Inherits:
-
Object
- Object
- Lapidist::Cli::Command
- Defined in:
- lib/lapidist/cli/command.rb
Constant Summary collapse
- DEVEL_GEMFILE =
'Gemfile.devel'
Instance Method Summary collapse
- #finish(options) ⇒ Object
-
#gem_repo_path_with_opts(gemname, org = "metanorma") ⇒ Object
FIXME: generalize!!.
- #git_branch_is(branch_name) ⇒ Object
- #log(message, options) ⇒ Object
- #rake(options) ⇒ Object
- #release(options) ⇒ Object
- #run_cmd(cmd, options) ⇒ Object
- #start(options) ⇒ Object
- #validate(action, options) ⇒ Object
- #yesno(message, options) ⇒ Object
Instance Method Details
#finish(options) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/lapidist/cli/command.rb', line 90 def finish() validate 'finish', branch_name = [:branch] path = [:gems_path] log("merge branch [#{branch_name}] to master", ) if yesno("please confirm that all PRs for [#{branch_name}] are 'green'", ) leaf_gems = Lapidist.gems(path) gem_deps = Lapidist.gem_deps(path) leaf_gems.each do |mn_gem| gem_project_path = File.join(path, mn_gem.to_s) gem_devel_gemfile = File.join(gem_project_path, DEVEL_GEMFILE) Dir.chdir(gem_project_path) do next unless git_branch_is(branch_name) File.delete(gem_devel_gemfile) unless [:dry_run] Bundler.with_clean_env do gem_deps[mn_gem].each do |d| run_cmd("bundle config --delete local.#{d} #{File.join(path, d)}", ) end run_cmd("bundle install", ) end run_cmd("git add -u #{DEVEL_GEMFILE} Gemfile.lock", ) run_cmd("git commit -m \"Feature #{branch_name}\" done", ) run_cmd("git push origin #{branch_name}", ) end end else puts "[!] please fix all issues before finish [#{branch_name}] branch" end end |
#gem_repo_path_with_opts(gemname, org = "metanorma") ⇒ Object
FIXME: generalize!!
9 10 11 |
# File 'lib/lapidist/cli/command.rb', line 9 def gem_repo_path_with_opts(gemname, org = "metanorma") ":github => '#{org}/#{gemname}'" end |
#git_branch_is(branch_name) ⇒ Object
180 181 182 |
# File 'lib/lapidist/cli/command.rb', line 180 def git_branch_is(branch_name) `git branch --show-current`.strip == branch_name end |
#log(message, options) ⇒ Object
176 177 178 |
# File 'lib/lapidist/cli/command.rb', line 176 def log(, ) puts "[v] #{}" if [:verbose] end |
#rake(options) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/lapidist/cli/command.rb', line 66 def rake() validate 'rake', path = [:gems_path] branch_name = [:branch] log("start tests for branch [#{branch_name}]", ) Lapidist.gems(path).each do |mn_gem| gem_project_path = File.(File.join(path, mn_gem.to_s)) Dir.chdir(gem_project_path) do if git_branch_is(branch_name) log("run tests for [#{mn_gem}] gem...", ) Bundler.with_clean_env do run_cmd("bundle exec rake", ) end else log("ignore tests for [#{mn_gem}] gem because branch isn't [#{branch_name}]", ) end end end end |
#release(options) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/lapidist/cli/command.rb', line 129 def release() validate 'release', leaf_gems = [:gems] version = [:version] path = [:gems_path] log("release #{leaf_gems} gems...", ) leaf_gems.each { |mn_gem| gem_project_path = File.(File.join(path, mn_gem.to_s)) Dir.chdir(gem_project_path) do if git_branch_is('master') log("do version bump for [#{mn_gem}] gem...", ) run_cmd("gem bump --tag --push --release --version #{version}", ) else raise "Stop release for [#{mn_gem}] gem because current branch isn't [master]" end end } end |
#run_cmd(cmd, options) ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/lapidist/cli/command.rb', line 154 def run_cmd(cmd, ) if [:dry_run] || [:verbose] puts "run system(#{cmd}) pwd:#{Dir.pwd}" end if ![:dry_run] system(cmd, chdir: Dir.pwd) end end |
#start(options) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/lapidist/cli/command.rb', line 13 def start() validate 'start', branch_name = [:branch] path = [:gems_path] leaf_gems = [:gems] || Lapidist.gems(path) gem_deps = Lapidist.gem_deps(path) log("start feature branch [#{branch_name}] for #{gems} gems", ) leaf_gems.each do |mn_gem| gem_project_path = File.join(path, mn_gem.to_s) log("create branch [#{branch_name}] for [#{mn_gem}] gem", ) Dir.chdir(gem_project_path) do run_cmd("git checkout -b #{branch_name}", ) end end leaf_gems.each do |mn_gem| gem_project_path = File.(File.join(path, mn_gem.to_s)) log("setup local dependencies for [#{mn_gem}] gem", ) Dir.chdir(gem_project_path) do File.open(DEVEL_GEMFILE, 'w') { |file| gem_deps[mn_gem.to_s].each do |d| file.write("gem '#{d}', #{gem_repo_path_with_opts(d)}, :branch => '#{branch_name}'\n") end } Bundler.with_clean_env do gem_deps[mn_gem.to_s].each do |d| run_cmd("bundle config --local local.#{d} #{File.join(path, d)}", ) end run_cmd("bundle install", ) end end end if yesno("push [#{branch_name}] branch to remote right now", ) leaf_gems.each do |mn_gem| gem_project_path = File.join(path, mn_gem.to_s) Dir.chdir(gem_project_path) do run_cmd("git push origin #{branch_name}", ) end end end end |
#validate(action, options) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/lapidist/cli/command.rb', line 164 def validate(action, ) case action when 'start', 'rake', 'finish' raise OptionParser::MissingArgument, "Missing -b/--branch [value]" if [:branch].nil? raise OptionParser::MissingArgument, "Missing -p/--gems-path [path]" if [:gems_path].nil? when 'release' raise OptionParser::MissingArgument, "Missing -p/--gems-path [path]" if [:gems_path].nil? raise OptionParser::MissingArgument, "Missing -g/--gems [value]" if [:gems].nil? raise OptionParser::MissingArgument, "Missing --version [value]" if [:version].nil? end end |
#yesno(message, options) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/lapidist/cli/command.rb', line 184 def yesno(, ) true if [:silent] printf "[?] #{} - press 'y' to continue: " prompt = STDIN.gets.chomp return prompt == 'y' end |