Class: Reissue::Task

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Gem
Defined in:
lib/reissue/rake.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :reissue) ⇒ Task

Returns a new instance of Task.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/reissue/rake.rb', line 63

def initialize(name = :reissue)
  @name = name
  @description = "Prepare the code for work on a new version."
  @version_file = nil
  @updated_paths = []
  @changelog_file = "CHANGELOG.md"
  @commit = true
  @commit_finalize = true
  @push_finalize = false
  @version_limit = 2
  @version_redo_proc = nil
  @push_reissue = :branch
end

Instance Attribute Details

#changelog_fileObject

The path to the changelog file.



32
33
34
# File 'lib/reissue/rake.rb', line 32

def changelog_file
  @changelog_file
end

#commitObject

Whether to commit the changes. Default: true.



42
43
44
# File 'lib/reissue/rake.rb', line 42

def commit
  @commit
end

#commit_finalizeObject

Whether to commit the finalize change to the changelog. Default: true.



45
46
47
# File 'lib/reissue/rake.rb', line 45

def commit_finalize
  @commit_finalize
end

#descriptionObject

A description of the main task.



20
21
22
# File 'lib/reissue/rake.rb', line 20

def description
  @description
end

#nameObject

The name of the main task and the namespace for the other tasks.



17
18
19
# File 'lib/reissue/rake.rb', line 17

def name
  @name
end

#push_finalizeObject

Whether to branch and push the changes. Default: :branch. Requires commit_finialize to be true.

Set this to false to disable pushing. Set this to true to push to the current branch. Set this to :branch to push to a new branch.



53
54
55
# File 'lib/reissue/rake.rb', line 53

def push_finalize
  @push_finalize
end

#push_reissueObject

Whether to push the changes when a new version is created. Default: :branch. Requires commit to be true.

Set this to false to disable pushing. Set this to true to push to the current branch. Set this to :branch to push to a new branch.



61
62
63
# File 'lib/reissue/rake.rb', line 61

def push_reissue
  @push_reissue
end

#updated_pathsObject



37
38
39
# File 'lib/reissue/rake.rb', line 37

def updated_paths
  Array(@updated_paths)
end

#version_fileObject

The path to the version file. Required.



23
24
25
# File 'lib/reissue/rake.rb', line 23

def version_file
  @version_file
end

#version_limitObject

The number of versions to retain in the changelog file. Defaults to 2.



26
27
28
# File 'lib/reissue/rake.rb', line 26

def version_limit
  @version_limit
end

#version_redo_procObject

A proc that can be used to create the new version string.



29
30
31
# File 'lib/reissue/rake.rb', line 29

def version_redo_proc
  @version_redo_proc
end

Class Method Details

.create(name = :reissue, &block) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/reissue/rake.rb', line 8

def self.create name = :reissue, &block
  task = new name
  task.instance_eval(&block) if block
  raise "No Reissue task.version_file specified" unless task.version_file
  task.define
  task
end

Instance Method Details

#defineObject



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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/reissue/rake.rb', line 93

def define
  desc description
  task name, [:segment] do |task, args|
    segment = args[:segment] || "patch"
    new_version = Reissue.call(segment:, version_file:, version_limit:, version_redo_proc:)
    if defined?(Bundler)
      Bundler.with_unbundled_env do
        system("bundle install")
      end
    end

    system("git add -u")
    if updated_paths.any?
      system("git add #{updated_paths.join(" ")}")
    end

    bump_message = "Bump version to #{new_version}"
    if commit
      if reissue_version_with_branch?
        Rake::Task["#{name}:branch"].invoke("reissue/#{new_version}")
      end
      system("git commit -m '#{bump_message}'")
      Rake::Task["#{name}:push"].invoke if push_reissue?
    else
      system("echo '#{bump_message}'")
    end

    new_version
  end

  desc "Reformat the changelog file to ensure it is correctly formatted."
  task "#{name}:reformat", [:version_limit] do |task, args|
    version_limit = if args[:version_limit].nil?
      self.version_limit
    else
      args[:version_limit].to_i
    end
    Reissue.reformat(changelog_file, version_limit:)
  end

  desc "Finalize the changelog for an unreleased version to set the release date."
  task "#{name}:finalize", [:date] do |task, args|
    date = args[:date] || Time.now.strftime("%Y-%m-%d")
    version, date = Reissue.finalize(date, changelog_file:)
    finalize_message = "Finalize the changelog for version #{version} on #{date}"
    if commit_finalize
      Rake::Task["#{name}:branch"].invoke("reissue/#{version}") if finalize_with_branch?
      system("git add -u")
      system("git commit -m '#{finalize_message}'")
      Rake::Task["#{name}:push"].invoke if push_finalize?
    else
      system("echo '#{finalize_message}'")
    end
  end

  desc "Create a new branch for the next version."
  task "#{name}:branch", [:branch] do |task, args|
    raise "No branch name specified" unless args[:branch]
    system("git checkout -b #{args[:branch]}")
  end

  desc "Push the current branch to the remote repository."
  task "#{name}:push" do
    system("git push origin HEAD")
  end
end

#finalize_with_branch?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/reissue/rake.rb', line 77

def finalize_with_branch?
  push_finalize == :branch
end

#push_finalize?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/reissue/rake.rb', line 81

def push_finalize?
  !!push_finalize
end

#push_reissue?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/reissue/rake.rb', line 89

def push_reissue?
  !!push_reissue
end

#reissue_version_with_branch?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/reissue/rake.rb', line 85

def reissue_version_with_branch?
  push_reissue == :branch
end