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
  consultations
  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.


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

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

Class Method Details

.all_dirs(include_root: true) ⇒ Object


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

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


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

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

  Open3.capture2(env, cmd)
end

.install_all(out: $stdout) ⇒ Object


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

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

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

.package_dirsObject


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

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

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

.pluginsObject


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

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

.replace_file(name, regexp, replacement) ⇒ Object


155
156
157
158
159
# File 'lib/decidim/gem_manager.rb', line 155

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

  File.write(name, new_content)
end

.replace_versionsObject


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

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

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

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


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

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

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


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

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

    break unless status || ENV.fetch("FAIL_FAST", nil) == "false"
  end
end

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


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

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

    break unless status || ENV.fetch("FAIL_FAST", nil) == "false"
  end
end

.semver_friendly_version(a_version) ⇒ Object


178
179
180
# File 'lib/decidim/gem_manager.rb', line 178

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

.test_componentObject


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

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

.test_participatory_spaceObject


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

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

.uninstall_all(out: $stdout) ⇒ Object


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

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

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

.versionObject


151
152
153
# File 'lib/decidim/gem_manager.rb', line 151

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

Instance Method Details

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


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

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

#replace_gem_versionObject


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

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


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

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


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

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

#short_nameObject


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

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