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.


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


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

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

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

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


71
72
73
# File 'lib/decidim/gem_manager.rb', line 71

def capture(cmd, env: {})
  Open3.capture2e(env, cmd)
end

.install_all(out: $stdout) ⇒ Object


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

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

.pluginsObject


149
150
151
# File 'lib/decidim/gem_manager.rb', line 149

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

.replace_file(name, regexp, replacement) ⇒ Object


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

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

  File.open(name, "w") { |f| f.write(new_content) }
end

.replace_versionsObject


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/decidim/gem_manager.rb', line 87

def replace_versions
  replace_file(
    "package.json",
    /^  "version": "[^"]*"/,
    "  \"version\": \"#{semver_friendly_version}\""
  )

  all_dirs do |dir|
    new(dir).replace_version
  end
end

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


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

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

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


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

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["FAIL_FAST"] == "false"
  end
end

.test_componentObject


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

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

.test_participatory_spaceObject


79
80
81
# File 'lib/decidim/gem_manager.rb', line 79

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

.uninstall_all(out: $stdout) ⇒ Object


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

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


132
133
134
# File 'lib/decidim/gem_manager.rb', line 132

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

Instance Method Details

#capture(command) ⇒ Object


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

def capture(command)
  interpolated_in_folder(command) do |cmd|
    self.class.capture(cmd)
  end
end

#replace_versionObject


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

def replace_version
  Dir.chdir(@dir) do
    self.class.replace_file(
      "lib/#{name.tr("-", "/")}/version.rb",
      /def self\.version(\s*)"[^"]*"/,
      "def self.version\\1\"#{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: out)
  end
end

#short_nameObject


66
67
68
# File 'lib/decidim/gem_manager.rb', line 66

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