Repository navigation
Expand file tree
/
Copy pathruby_gem.rb
More file actions
127 lines (104 loc) · 4.75 KB
/
Copy pathruby_gem.rb
File metadata and controls
127 lines (104 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
# frozen_string_literal: true
require_relative 'lifecycle'
require_relative '../package/ruby_gem'
require_relative '../destination/ruby_gems'
require_relative '../source_control/git'
require_relative '../hosting/github_release'
require_relative '../version/interface'
module RubyGemRelease
module Workflow
class RubyGem
include Lifecycle
NEXT_VERSIONS = %i[major minor patch].freeze
attr_reader :version, :changelog, :package, :destination, :git, :hosting, :provider
def initialize(version:, changelog:, provider: ExecutionProvider.current,
package: Package::RubyGem.new(gemspec: provider.fetch('GEMSPEC_PATH', provider.fetch('INPUT_GEMSPEC_PATH', '')) || '', directory: provider.directory),
destination: Destination::RubyGems.new,
git: SourceControl::Git.new(directory: provider.directory), hosting: Hosting::GitHubRelease.new,
validate: nil, success: nil, failure: nil, next_version: :patch)
configure_version!(version, next_version)
@changelog = changelog
@package = package
@destination = destination
@git = git
@hosting = hosting
@provider = provider
@validate = validate
@success = success
@failure = failure
end
def environment
Environment.new(provider: @provider)
end
private
def configure_version!(version, next_version)
@version = Version::Interface.check!(version)
part = next_version.to_sym if next_version.is_a?(String) || next_version.is_a?(Symbol)
raise ArgumentError, 'next_version must be :major, :minor, or :patch' unless NEXT_VERSIONS.include?(part)
raise ArgumentError, "version object's increment! must accept part: to use next_version: #{part.inspect}" unless part == :patch || increment_accepts_part?
@next_version = part
end
def increment_accepts_part?
@version.method(:increment!).parameters.any? { |type, name| %i[keyrest rest].include?(type) || (%i[key keyreq].include?(type) && name == :part) }
end
def increment_options
@next_version == :patch ? {} : { part: @next_version }
end
def install_callbacks(tasks)
tasks.validate { |ctx| validate_with_hooks(ctx) }
tasks.prepare { |ctx| prepare_release(ctx) }
tasks.publish { |ctx| publish(ctx) }
tasks.bump { |ctx| prepare_next(ctx) }
tasks.success(&@success) if @success
tasks.failure(&@failure) if @failure
end
def validate_release(ctx)
return @validate.call(ctx) if @validate
return if @changelog.nil? || @changelog.released?(ctx.release.version) || @changelog.pending?
raise ArgumentError, 'Nothing to release: no pending changelog notes'
end
def prepare_release(ctx)
@git.stage_changes! do
run_stage(:prepare_release, ctx) { prepare_release_files(ctx.release) }
end
end
def prepare_release_files(release)
@version.set!(release.version) unless @provider.fetch('INPUT_VERSION', '').empty?
@changelog&.release!(release.with_version(@version.read))
end
def prepare_publication(ctx)
run_stage(:commit_release, ctx) { @git.commit!(ctx.release) }
run_stage(:build_package, ctx) { @package.build!(ctx.release) }
run_stage(:tag_release, ctx) { @git.tag!(ctx.release) }
end
def publish(ctx)
hosted = hosted_release(ctx.release) if @hosting
prepare_publication(ctx)
run_stage(:publish_registry, ctx) { @destination.publish!(ctx.release, package: @package) }
run_stage(:push_release, ctx) { @git.push!(ctx.release) }
run_stage(:publish_hosted_release, ctx) { @hosting.publish!(hosted, package: @package) } if @hosting
end
# Reads changelog notes before any publication, so a missing section fails before the gem is pushed.
def hosted_release(release)
return release if release.notes || !@changelog.respond_to?(:notes)
release.with_notes(@changelog.notes(release.version))
end
def prepare_next(ctx)
next_ctx = stage_next!(ctx)
run_stage(:commit_next, next_ctx) { @git.commit_next!(next_ctx.release) }
run_stage(:push_next, next_ctx) { @git.push! }
ctx.record_next_version!(next_ctx.release.next_version)
end
def stage_next!(ctx)
next_ctx = ctx
@git.stage_changes! do
run_stage(:prepare_next, ctx, after_context: -> { next_ctx }) do
next_ctx = ctx.with(release: ctx.release.with_next_version(@version.increment!(from: ctx.release.version, **increment_options)))
@changelog&.prepare_next!(next_ctx.release)
end
end
next_ctx
end
end
end
end