gem_comet 0.3.0 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,6 +26,10 @@ release:
26
26
  version_file_path: { Path of the version file. e.g. lib/gem_comet/version.rb }
27
27
  ```
28
28
 
29
+ ## Supported versions
30
+
31
+ * Ruby 2.7, 3.0
32
+
29
33
  ## Usage
30
34
 
31
35
  ### Display changelogs
@@ -22,7 +22,9 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ['lib']
24
24
 
25
- spec.add_runtime_dependency 'pr_comet', '~> 0.3.0'
25
+ spec.required_ruby_version = '>= 2.7.0'
26
+
27
+ spec.add_runtime_dependency 'pr_comet', '~> 0.4.0'
26
28
  spec.add_runtime_dependency 'thor'
27
29
  spec.add_runtime_dependency 'type_struct'
28
30
 
@@ -34,6 +36,6 @@ Gem::Specification.new do |spec|
34
36
  spec.add_development_dependency 'rubocop'
35
37
  spec.add_development_dependency 'rubocop-performance'
36
38
  spec.add_development_dependency 'rubocop-rspec'
37
- spec.add_development_dependency 'simplecov'
39
+ spec.add_development_dependency 'simplecov', '0.17.1'
38
40
  spec.add_development_dependency 'yard'
39
41
  end
@@ -8,11 +8,18 @@ require 'yaml'
8
8
  require 'gem_comet/version'
9
9
  require 'gem_comet/service_abstract'
10
10
  require 'gem_comet/config'
11
+ require 'gem_comet/git_command'
12
+ require 'gem_comet/bundle_updater'
11
13
  require 'gem_comet/version_editor'
14
+ require 'gem_comet/version_history'
12
15
  require 'gem_comet/changelog'
16
+ require 'gem_comet/changelog/initializer'
13
17
  require 'gem_comet/changelog/editor'
14
18
  require 'gem_comet/changelog/generator'
19
+ require 'gem_comet/open_github_pulls_page'
20
+ require 'gem_comet/repository_url'
15
21
  require 'gem_comet/release'
16
- require 'gem_comet/release/update_pr'
17
- require 'gem_comet/release/release_pr'
22
+ require 'gem_comet/release/create_update_pr'
23
+ require 'gem_comet/release/create_release_pr'
24
+ require 'gem_comet/verify_git_condition'
18
25
  require 'gem_comet/cli'
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GemComet
4
+ # Executes `$ bundle update` command
5
+ class BundleUpdater < ServiceAbstract
6
+ def initialize; end
7
+
8
+ private
9
+
10
+ def call
11
+ `bundle update`
12
+ end
13
+ end
14
+ end
@@ -3,29 +3,19 @@
3
3
  module GemComet
4
4
  # Generates changelog from git log
5
5
  class Changelog < ServiceAbstract
6
- # @param from_version [String]
7
- # The beginning of version number to create a changelog.
8
- # Default is specified current version.
9
- # @param to_version [String]
10
- # The end of version number to create a changelog.
11
- # Default is specified `HEAD`.
12
- # @param append [Boolean]
13
- # Appends execution result to CHANGELOG.md.
14
- # @param prepend [Boolean]
15
- # Prepends execution result to CHANGELOG.md.
16
- def initialize(from_version: nil, to_version: nil, append: false, prepend: false)
17
- @version_editor = VersionEditor.new
6
+ # @param version [String] The version number to create a changelog.
7
+ # @param append [Boolean] Appends execution result to CHANGELOG.md.
8
+ # @param prepend [Boolean] Prepends execution result to CHANGELOG.md.
9
+ def initialize(version:, append: false, prepend: false)
18
10
  @changelog_editor = Changelog::Editor.new
19
- @from_version = from_version || version_editor.current_version
20
- @to_version = to_version
11
+ @version = version
21
12
  @append = append
22
13
  @prepend = prepend
23
14
  end
24
15
 
25
16
  private
26
17
 
27
- attr_reader :version_editor, :changelog_editor,
28
- :from_version, :to_version, :append, :prepend
18
+ attr_reader :changelog_editor, :version, :append, :prepend
29
19
 
30
20
  # Displays changelogs. If specified a `append` or `prepend` option, updates
31
21
  # CHANGELOG.md with generated changelog.
@@ -44,7 +34,7 @@ module GemComet
44
34
  #
45
35
  # @return [String] Ganalated changelog
46
36
  def changelog
47
- @changelog ||= Generator.call(from_version: from_version, to_version: to_version)
37
+ @changelog ||= Generator.call(version: version)
48
38
  end
49
39
  end
50
40
  end
@@ -4,17 +4,14 @@ module GemComet
4
4
  class Changelog
5
5
  # Generates changelog from git log
6
6
  class Generator < ServiceAbstract
7
- attr_reader :from, :to, :new_version
8
-
9
- # @param from_version [String] The beginning of version number to create a changelog
10
- # @param to_version [String]
11
- # The end of version number to create a changelog. If ommit this option,
12
- # it's specified `HEAD`.
13
- # @param new_version [String] Next version of your gem
14
- def initialize(from_version:, to_version: nil, new_version: nil)
15
- @from = "v#{from_version}"
16
- @to = to_version.nil? ? 'HEAD' : "v#{to_version}"
17
- @new_version = new_version || to
7
+ attr_reader :version, :title, :origin_url
8
+
9
+ # @param version [String] The end of version number to create a changelog.
10
+ # @param title [String] Next version of your gem
11
+ def initialize(version:, title: nil)
12
+ @version = version
13
+ @title = title || version
14
+ @origin_url = RepositoryUrl.call
18
15
  end
19
16
 
20
17
  private
@@ -24,10 +21,11 @@ module GemComet
24
21
  # Returns changelogs as markdown format from current version to HEAD commit.
25
22
  #
26
23
  # @return [String] Changelogs as markdown format
24
+ # @raise [RuntimeError] The specified version cannot be found
27
25
  def call
28
26
  <<~MARKDOWN
29
27
 
30
- ## #{new_version} (#{Date.today.strftime('%b %d, %Y')})
28
+ ## #{title} (#{versioning_date})
31
29
 
32
30
  ### Feature
33
31
  ### Bugfix
@@ -38,6 +36,13 @@ module GemComet
38
36
  MARKDOWN
39
37
  end
40
38
 
39
+ # Returns the versioning date
40
+ #
41
+ # @return [String] The versioning date. e.g. "Oct 26, 2019"
42
+ def versioning_date
43
+ VersionHistory.new.versioning_date_of(version).strftime('%b %d, %Y')
44
+ end
45
+
41
46
  # Returns array of changelogs as markdown format.
42
47
  #
43
48
  # @return [Array<String>] Array of changelogs as markdown format
@@ -45,14 +50,29 @@ module GemComet
45
50
  merge_commits.map do |merge_commit|
46
51
  next unless merge_commit.match?(MERGE_COMMIT_TITLE)
47
52
 
48
- pull_request_number = extract_pull_request_number(merge_commit)
49
- pull_request_url = get_pull_request_url(pull_request_number)
50
- description = extract_description(merge_commit) || extract_branch_name(merge_commit)
53
+ number = extract_pull_request_number(merge_commit)
54
+ title = extract_description(merge_commit) || extract_branch_name(merge_commit)
55
+ author = extract_author_name(merge_commit)
51
56
 
52
- "* #{description} ([##{pull_request_number}](#{pull_request_url}))"
57
+ generate_changelog(number, title, author)
53
58
  end.compact
54
59
  end
55
60
 
61
+ # Generates changelog message as markdown list
62
+ #
63
+ # @param number [Integer] The pull request number
64
+ # @param title [String] The pull request title
65
+ # @param author [String] The author of the PR
66
+ # @return [String] Changelog message as markdown list
67
+ def generate_changelog(number, title, author)
68
+ [
69
+ '*',
70
+ "[##{number}](#{get_pull_request_url(number)})",
71
+ title,
72
+ "([@#{author}](https://github.com/#{author}))"
73
+ ].join(' ')
74
+ end
75
+
56
76
  # Extracts PR number from merge commit string.
57
77
  #
58
78
  # @param merge_commit [String] The target string
@@ -87,6 +107,14 @@ module GemComet
87
107
  MERGE_COMMIT_TITLE.match(merge_commit).captures.last.chomp.strip
88
108
  end
89
109
 
110
+ # Extracts the author of the PR from merge commit string.
111
+ #
112
+ # @param merge_commit [String] The target string
113
+ # @return [String] Author name
114
+ def extract_author_name(merge_commit)
115
+ extract_branch_name(merge_commit).split('/').first
116
+ end
117
+
90
118
  # Parces the merge commit log, which to separate by each commit.
91
119
  #
92
120
  # @return [Array<String>] Array of git commit log
@@ -98,14 +126,14 @@ module GemComet
98
126
  #
99
127
  # @return [String] Get only merge commit logs
100
128
  def merge_commit_log
101
- @merge_commit_log ||= `git log --merges #{from}..#{to}`
129
+ @merge_commit_log ||= `git log --merges #{prev_version}..#{version}`
102
130
  end
103
131
 
104
- # Returns the git origin URL via git command.
132
+ # Get a previous version number from the specified version number
105
133
  #
106
- # @return [String] The origin URL
107
- def origin_url
108
- @origin_url ||= `git remote get-url --push origin`.sub('.git', '').chomp
134
+ # @raise [RuntimeError] The specified version cannot be found
135
+ def prev_version
136
+ @prev_version ||= VersionHistory.new.previous_version_from(version)
109
137
  end
110
138
  end
111
139
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GemComet
4
+ class Changelog
5
+ # Initializes the CHANGELOG.md to append past version change-logs
6
+ class Initializer < ServiceAbstract
7
+ def initialize
8
+ @changelog_editor = Changelog::Editor.new
9
+ @version_history = VersionHistory.new
10
+ end
11
+
12
+ private
13
+
14
+ attr_reader :changelog_editor, :version_history
15
+
16
+ def call
17
+ version_history.versions.each do |version|
18
+ changelog = Changelog::Generator.call(version: version)
19
+ changelog_editor.prepend!(content: changelog)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -14,24 +14,26 @@ module GemComet
14
14
  template '../../template/.gem_comet.yml.erb', '.gem_comet.yml',
15
15
  version: GemComet::Config::CURRENT_VERSION
16
16
  template '../../template/CHANGELOG.md.erb', 'CHANGELOG.md'
17
+ Changelog::Initializer.call
18
+ rescue StandardError => e
19
+ puts e.message
17
20
  end
18
21
 
19
22
  desc 'release VERSION', 'Creates update PR and release PR'
20
23
  def release(version)
21
24
  Release.call(version: version)
25
+ rescue StandardError => e
26
+ puts e.message
22
27
  end
23
28
 
24
29
  desc 'changelog', 'Displays changelogs'
25
- option :from,
30
+ option :version,
26
31
  type: :string,
27
- aliases: :f,
28
- desc: 'The beginning of version number to create a changelog. ' \
29
- 'Default is specified current version.'
30
- option :to,
31
- type: :string,
32
- aliases: :t,
33
- desc: 'The end of version number to create a changelog. ' \
34
- 'Default is specified `HEAD`.'
32
+ aliases: :v,
33
+ default: 'HEAD',
34
+ desc: 'The version number to create a changelog. ' \
35
+ 'Default is specified `HEAD`.',
36
+ banner: 'v1.2.3'
35
37
  option :append,
36
38
  type: :boolean,
37
39
  aliases: :a,
@@ -44,16 +46,19 @@ module GemComet
44
46
  desc: 'Prepends execution result to CHANGELOG.md.'
45
47
  def changelog
46
48
  puts Changelog.call(
47
- from_version: options[:from],
48
- to_version: options[:to],
49
+ version: options[:version],
49
50
  append: options[:append],
50
51
  prepend: options[:prepend]
51
52
  )
53
+ rescue StandardError => e
54
+ puts e.message
52
55
  end
53
56
 
54
57
  desc 'versions', 'Displays version numbers of your gem.'
55
58
  def versions
56
- puts `git tag`
59
+ puts VersionHistory.new.versions
60
+ rescue StandardError => e
61
+ puts e.message
57
62
  end
58
63
 
59
64
  desc 'version', 'Shows current version'
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GemComet
4
+ # Executes git command
5
+ class GitCommand
6
+ def initialize; end
7
+
8
+ # Get uncommitted files
9
+ #
10
+ # @return [Array<String>] Uncommitted files
11
+ def uncommitted_files
12
+ git_status_short.lines.map(&:chomp)
13
+ end
14
+
15
+ # Get current git branch name
16
+ #
17
+ # @return [String] The current branch name
18
+ def current_branch
19
+ run 'rev-parse', '--abbrev-ref', 'HEAD'
20
+ end
21
+
22
+ def checkout(branch)
23
+ run 'checkout', branch
24
+ end
25
+
26
+ # Executes `$ git pull`
27
+ def pull
28
+ run 'pull'
29
+ end
30
+
31
+ private
32
+
33
+ def execute(command)
34
+ `#{command}`.chomp
35
+ end
36
+
37
+ def run(*subcommands)
38
+ command = "git #{subcommands.join(' ')}"
39
+ execute(command)
40
+ end
41
+
42
+ def git_status_short
43
+ run 'status', '--short'
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GemComet
4
+ # Creates pull requests for gem release and that preparation
5
+ class OpenGithubPullsPage < ServiceAbstract
6
+ attr_reader :origin_url
7
+
8
+ def initialize
9
+ @origin_url = RepositoryUrl.call
10
+ end
11
+
12
+ private
13
+
14
+ def call
15
+ `open #{origin_url}/pulls`
16
+ end
17
+ end
18
+ end
@@ -15,8 +15,10 @@ module GemComet
15
15
  attr_reader :version, :config
16
16
 
17
17
  def call
18
- UpdatePR.call(update_pr_args)
19
- ReleasePR.call(release_pr_args)
18
+ VerifyGitCondition.call
19
+ CreateUpdatePR.call(**update_pr_args)
20
+ CreateReleasePR.call(**release_pr_args)
21
+ OpenGithubPullsPage.call
20
22
  end
21
23
 
22
24
  def update_pr_args
@@ -3,7 +3,7 @@
3
3
  module GemComet
4
4
  class Release
5
5
  # Creates a pull request for your gem release
6
- class ReleasePR < ServiceAbstract
6
+ class CreateReleasePR < ServiceAbstract
7
7
  private
8
8
 
9
9
  attr_reader :version, :pr_comet
@@ -18,7 +18,20 @@ module GemComet
18
18
  end
19
19
 
20
20
  def create_pull_request
21
- pr_comet.create!(title: "Release v#{version}", body: '', validate: false)
21
+ pr_comet.create!(
22
+ title: "Release v#{version}",
23
+ body: pull_request_body,
24
+ validate: false
25
+ )
26
+ end
27
+
28
+ def pull_request_body
29
+ template = File.read(template_file_path)
30
+ ERB.new(template, trim_mode: '-').result(binding)
31
+ end
32
+
33
+ def template_file_path
34
+ File.expand_path('../../../template/release_pr.md.erb', __dir__)
22
35
  end
23
36
  end
24
37
  end
@@ -3,17 +3,18 @@
3
3
  module GemComet
4
4
  class Release
5
5
  # Creates a pull request for release preparation
6
- class UpdatePR < ServiceAbstract
6
+ class CreateUpdatePR < ServiceAbstract
7
7
  def initialize(version:, base_branch:)
8
8
  @version = version
9
9
  @pr_comet = PrComet.new(base: base_branch, branch: "update/v#{version}")
10
10
  @version_editor = VersionEditor.new
11
+ @prev_version = version_editor.current_version
11
12
  @changelog_editor = Changelog::Editor.new
12
13
  end
13
14
 
14
15
  private
15
16
 
16
- attr_reader :version, :pr_comet, :version_editor, :changelog_editor
17
+ attr_reader :version, :prev_version, :pr_comet, :version_editor, :changelog_editor
17
18
 
18
19
  def call
19
20
  update_changelog
@@ -24,10 +25,7 @@ module GemComet
24
25
 
25
26
  def update_changelog
26
27
  pr_comet.commit ':comet: Update CHANGELOG.md' do
27
- changelog = Changelog::Generator.call(
28
- from_version: version_editor.current_version,
29
- new_version: version
30
- )
28
+ changelog = Changelog::Generator.call(version: 'HEAD', title: "v#{version}")
31
29
  changelog_editor.prepend!(content: changelog)
32
30
  end
33
31
  end
@@ -39,11 +37,20 @@ module GemComet
39
37
  end
40
38
 
41
39
  def bundle_update
42
- pr_comet.commit(':comet: $ bundle update') { `bundle update` }
40
+ pr_comet.commit(':comet: $ bundle update') { BundleUpdater.call }
43
41
  end
44
42
 
45
43
  def create_pull_request
46
- pr_comet.create!(title: "Update v#{version}", body: '')
44
+ pr_comet.create!(title: "Update v#{version}", body: pull_request_body)
45
+ end
46
+
47
+ def pull_request_body
48
+ template = File.read(template_file_path)
49
+ ERB.new(template, trim_mode: '-').result(binding)
50
+ end
51
+
52
+ def template_file_path
53
+ File.expand_path('../../../template/update_pr.md.erb', __dir__)
47
54
  end
48
55
  end
49
56
  end