Class: Lapidist::Cli::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/lapidist/cli/command.rb

Constant Summary collapse

DEVEL_GEMFILE =
'Gemfile.devel'

Instance Method Summary collapse

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(options)
  validate 'finish', options

  branch_name = options[:branch]
  path = options[:gems_path]

  log("merge branch [#{branch_name}] to master", options)

  if yesno("please confirm that all PRs for [#{branch_name}] are 'green'", options)
    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 options[: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)}", options)
          end

          run_cmd("bundle install", options)
        end

        run_cmd("git add -u #{DEVEL_GEMFILE} Gemfile.lock", options)
        run_cmd("git commit -m \"Feature #{branch_name}\" done", options)
        run_cmd("git push origin #{branch_name}", options)
      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(message, options)
  puts "[v] #{message}" if options[: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(options)
  validate 'rake', options

  path = options[:gems_path]
  branch_name = options[:branch]

  log("start tests for branch [#{branch_name}]", options)

  Lapidist.gems(path).each do |mn_gem|
    gem_project_path = File.expand_path(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...", options)
        Bundler.with_clean_env do
          run_cmd("bundle exec rake", options)
        end
      else
        log("ignore tests for [#{mn_gem}] gem because branch isn't [#{branch_name}]", options)
      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(options)
  validate 'release', options

  leaf_gems = options[:gems]
  version = options[:version]
  path = options[:gems_path]

  log("release #{leaf_gems} gems...", options)

  leaf_gems.each { |mn_gem|
    gem_project_path = File.expand_path(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...", options)
        run_cmd("gem bump --tag --push --release --version #{version}", options)
      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, options)
  if options[:dry_run] || options[:verbose]
    puts "run system(#{cmd}) pwd:#{Dir.pwd}"
  end

  if !options[: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(options)
  validate 'start', options

  branch_name = options[:branch]
  path = options[:gems_path]

  leaf_gems = options[:gems] || Lapidist.gems(path)
  gem_deps = Lapidist.gem_deps(path)

  log("start feature branch [#{branch_name}] for #{gems} gems", options)

  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", options)

    Dir.chdir(gem_project_path) do
      run_cmd("git checkout -b #{branch_name}", options)
    end
  end

  leaf_gems.each do |mn_gem|
    gem_project_path = File.expand_path(File.join(path, mn_gem.to_s))

    log("setup local dependencies for [#{mn_gem}] gem", options)

    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)}", options)
        end

        run_cmd("bundle install", options)
      end
    end
  end

  if yesno("push [#{branch_name}] branch to remote right now", options)
    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}", options)
      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, options)
  case action
  when 'start', 'rake', 'finish'
    raise OptionParser::MissingArgument, "Missing -b/--branch [value]" if options[:branch].nil?
    raise OptionParser::MissingArgument, "Missing -p/--gems-path [path]" if options[:gems_path].nil?
  when 'release'
    raise OptionParser::MissingArgument, "Missing -p/--gems-path [path]" if options[:gems_path].nil?
    raise OptionParser::MissingArgument, "Missing -g/--gems [value]" if options[:gems].nil?
    raise OptionParser::MissingArgument, "Missing --version [value]" if options[:version].nil?
  end
end

#yesno(message, options) ⇒ Object



184
185
186
187
188
189
# File 'lib/lapidist/cli/command.rb', line 184

def yesno(message, options)
  true if options[:silent]
  printf "[?] #{message} - press 'y' to continue: "
  prompt = STDIN.gets.chomp
  return prompt == 'y'
end