Class: Decidim::GemManager

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/gem_manager.rb

Overview

Handles a decidim components.

Allows to perform custom operations on all modules in a given folder, or on specific module folders.

Potential operations are:

  • Running custom commands, via the ‘run` method, such as releasing it, or installing it locally.

  • Updating version files from the main ‘.decidim-version` file in the root of the repository.

Constant Summary collapse

PARTICIPATORY_SPACES =
%w(
  participatory_processes
  assemblies
  conferences
).freeze
COMPONENTS =
%w(
  accountability
  budgets
  debates
  meetings
  pages
  proposals
  surveys
  sortitions
  blogs
).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ GemManager

Returns a new instance of GemManager.



40
41
42
# File 'lib/decidim/gem_manager.rb', line 40

def initialize(dir)
  @dir = File.expand_path(dir)
end

Class Method Details

.all_dirs(include_root: true) ⇒ Object



173
174
175
176
177
178
# File 'lib/decidim/gem_manager.rb', line 173

def all_dirs(include_root: true)
  dirs = plugins
  dirs << "./" if include_root

  dirs.each { |dir| yield(dir) }
end

.capture(cmd, env: {}, with_stderr: true) ⇒ Object



81
82
83
84
85
# File 'lib/decidim/gem_manager.rb', line 81

def capture(cmd, env: {}, with_stderr: true)
  return Open3.capture2e(env, cmd) if with_stderr

  Open3.capture2(env, cmd)
end

.fail_fast?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/decidim/gem_manager.rb', line 194

def fail_fast?
  ENV.fetch("FAIL_FAST", nil) != "false"
end

.install_all(out: $stdout) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/decidim/gem_manager.rb', line 111

def install_all(out: $stdout)
  run_all(
    "gem build %name && mv %name-%version.gem ..",
    include_root: false,
    out:
  )

  new(root).run(
    "gem build %name && gem install *.gem",
    out:
  )
end

.package_dirsObject



180
181
182
183
184
# File 'lib/decidim/gem_manager.rb', line 180

def package_dirs
  dirs = Dir.glob("#{root}/packages/*")

  dirs.each { |dir| yield(dir) }
end

.patch_component_gemfile_generatorObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/decidim/gem_manager.rb', line 202

def patch_component_gemfile_generator
  content = %(# frozen_string_literal: true
<%# This file is automatically generated. Do not edit this file, it will be overwritten
If you want to modify it you can do it in the Decidim::GemManager.patch_component_gemfile_generator method %>
source "https://rubygems.org"

ruby RUBY_VERSION

gem "decidim", "~> #{version}"
gem "decidim-<%= component_name %>", path: "."

gem "puma", "#{fetch_gemfile_version("puma")}"
gem "bootsnap", "#{fetch_gemfile_version("bootsnap")}"

group :development, :test do
  gem "byebug", "#{fetch_gemfile_version("byebug")}", platform: :mri

  gem "decidim-dev", "~> #{version}"
end

group :development do
  gem "faker", "#{Gem.loaded_specs["decidim-dev"].dependencies.select { |a| a.name == "faker" }.first.requirement}"
  gem "letter_opener_web", "#{fetch_gemfile_version("letter_opener_web")}"
  gem "listen", "#{fetch_gemfile_version("listen")}"
  gem "web-console", "#{fetch_gemfile_version("web-console")}"
end
)

  file_path = File.join(root, "decidim-generators/lib/decidim/generators/component_templates/Gemfile.erb")
  File.write(file_path, content)
end

.pluginsObject



186
187
188
# File 'lib/decidim/gem_manager.rb', line 186

def plugins
  Dir.glob("#{root}/decidim-*/")
end

.replace_file(name, regexp, replacement) ⇒ Object



167
168
169
170
171
# File 'lib/decidim/gem_manager.rb', line 167

def replace_file(name, regexp, replacement)
  new_content = File.read(name).gsub(regexp, replacement)

  File.write(name, new_content)
end

.replace_versionsObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/decidim/gem_manager.rb', line 99

def replace_versions
  all_dirs do |dir|
    new(dir).replace_gem_version
  end

  package_dirs do |dir|
    new(dir).replace_package_version
  end

  replace_antora_version
end

.retry_timesObject



198
199
200
# File 'lib/decidim/gem_manager.rb', line 198

def retry_times
  ENV.fetch("RETRY_TIMES", 10).to_i
end

.run(cmd, out: $stdout) ⇒ Object



87
88
89
# File 'lib/decidim/gem_manager.rb', line 87

def run(cmd, out: $stdout)
  system(cmd, out:)
end

.run_all(command, out: $stdout, include_root: true) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/decidim/gem_manager.rb', line 136

def run_all(command, out: $stdout, include_root: true)
  all_dirs(include_root:) do |dir|
    status = run_at(dir, command, out:)

    break if !status && fail_fast?
  end
end

.run_at(dir, command, out: $stdout) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/decidim/gem_manager.rb', line 152

def run_at(dir, command, out: $stdout)
  attempts = 0
  until (status = new(dir).run(command, out:))
    attempts += 1

    break if attempts > Decidim::GemManager.retry_times
  end

  status
end

.run_packages(command, out: $stdout) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/decidim/gem_manager.rb', line 144

def run_packages(command, out: $stdout)
  package_dirs do |dir|
    status = run_at(dir, command, out:)

    break if !status && fail_fast?
  end
end

.semver_friendly_version(a_version) ⇒ Object



190
191
192
# File 'lib/decidim/gem_manager.rb', line 190

def semver_friendly_version(a_version)
  a_version.gsub(/\.pre/, "-pre").gsub(/\.dev/, "-dev").gsub(/.rc(\d*)/, "-rc\\1")
end

.test_componentObject



95
96
97
# File 'lib/decidim/gem_manager.rb', line 95

def test_component
  new("decidim-#{COMPONENTS.sample}").run("rake")
end

.test_participatory_spaceObject



91
92
93
# File 'lib/decidim/gem_manager.rb', line 91

def test_participatory_space
  new("decidim-#{PARTICIPATORY_SPACES.sample}").run("rake")
end

.uninstall_all(out: $stdout) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/decidim/gem_manager.rb', line 124

def uninstall_all(out: $stdout)
  run_all(
    "gem uninstall %name -v %version --executables --force",
    out:
  )

  new(root).run(
    "rm decidim-*.gem",
    out:
  )
end

.versionObject



163
164
165
# File 'lib/decidim/gem_manager.rb', line 163

def version
  @version ||= File.read(version_file).strip
end

Instance Method Details

#capture(command, env: {}, with_stderr: true) ⇒ Object



50
51
52
53
54
# File 'lib/decidim/gem_manager.rb', line 50

def capture(command, env: {}, with_stderr: true)
  interpolated_in_folder(command) do |cmd|
    self.class.capture(cmd, env:, with_stderr:)
  end
end

#replace_gem_versionObject



56
57
58
59
60
61
62
63
64
# File 'lib/decidim/gem_manager.rb', line 56

def replace_gem_version
  Dir.chdir(@dir) do
    replace_file(
      "lib/#{name.tr("-", "/")}/version.rb",
      /def self\.version(\s*)"[^"]*"/,
      "def self.version\\1\"#{version}\""
    )
  end
end

#replace_package_versionObject



66
67
68
69
70
71
72
73
74
# File 'lib/decidim/gem_manager.rb', line 66

def replace_package_version
  Dir.chdir(@dir) do
    replace_file(
      "package.json",
      /^  "version": "[^"]*"/,
      "  \"version\": \"#{semver_friendly_version(version)}\""
    )
  end
end

#run(command, out: $stdout) ⇒ Object



44
45
46
47
48
# File 'lib/decidim/gem_manager.rb', line 44

def run(command, out: $stdout)
  interpolated_in_folder(command) do |cmd|
    self.class.run(cmd, out:)
  end
end

#short_nameObject



76
77
78
# File 'lib/decidim/gem_manager.rb', line 76

def short_name
  name.gsub(/decidim-/, "")
end