Class: Decidim::Generators::ComponentGenerator

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/decidim/generators/component_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#component_descriptionObject (readonly)

Returns the value of attribute component_description.



16
17
18
# File 'lib/decidim/generators/component_generator.rb', line 16

def component_description
  @component_description
end

#component_folderObject (readonly)

Returns the value of attribute component_folder.



16
17
18
# File 'lib/decidim/generators/component_generator.rb', line 16

def component_folder
  @component_folder
end

#component_module_nameObject (readonly)

Returns the value of attribute component_module_name.



16
17
18
# File 'lib/decidim/generators/component_generator.rb', line 16

def component_module_name
  @component_module_name
end

#component_nameObject (readonly)

Returns the value of attribute component_name.



16
17
18
# File 'lib/decidim/generators/component_generator.rb', line 16

def component_name
  @component_name
end

#component_resource_nameObject (readonly)

Returns the value of attribute component_resource_name.



16
17
18
# File 'lib/decidim/generators/component_generator.rb', line 16

def component_resource_name
  @component_resource_name
end

#core_versionObject (readonly)

Returns the value of attribute core_version.



16
17
18
# File 'lib/decidim/generators/component_generator.rb', line 16

def core_version
  @core_version
end

#edge_git_branchObject (readonly)

Returns the value of attribute edge_git_branch.



16
17
18
# File 'lib/decidim/generators/component_generator.rb', line 16

def edge_git_branch
  @edge_git_branch
end

#npm_package_versionObject (readonly)

Returns the value of attribute npm_package_version.



16
17
18
# File 'lib/decidim/generators/component_generator.rb', line 16

def npm_package_version
  @npm_package_version
end

#required_ruby_versionObject (readonly)

Returns the value of attribute required_ruby_version.



16
17
18
# File 'lib/decidim/generators/component_generator.rb', line 16

def required_ruby_version
  @required_ruby_version
end

#security_emailObject (readonly)

Returns the value of attribute security_email.



16
17
18
# File 'lib/decidim/generators/component_generator.rb', line 16

def security_email
  @security_email
end

Instance Method Details

#component(component_name) ⇒ Object



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
# File 'lib/decidim/generators/component_generator.rb', line 33

def component(component_name)
  @component_name = component_name
  @component_module_name = component_name.camelize
  @component_folder = options[:destination_folder] || "decidim-module-#{component_name}"
  @core_version = Decidim::Core.version
  @npm_package_version = "^#{semver_friendly_version}"
  @edge_git_branch = Decidim::Generators.edge_git_branch
  @component_description = ask "Write a description for the new component:"
  @required_ruby_version = RUBY_VERSION.length == 5 ? RUBY_VERSION[0..2] : RUBY_VERSION
  @security_email = ask "Provide a public email in case of security concern:"
  format_email!

  template "decidim-component.gemspec.erb", "#{component_folder}/decidim-#{component_name}.gemspec"
  template "Gemfile.erb", "#{component_folder}/Gemfile" if options[:external]
  template "Rakefile", "#{component_folder}/Rakefile"
  template "LICENSE-AGPLv3.txt", "#{component_folder}/LICENSE-AGPLv3.txt"
  template "README.md.erb", "#{component_folder}/README.md"
  template "gitignore", "#{component_folder}/.gitignore"
  template "github/ci.yml.erb", "#{component_folder}/.github/workflows/ci_#{component_name}.yml"
  template "package.json.erb", "#{component_folder}/package.json"
  copy_file ".ruby-version", "#{component_folder}/.ruby-version"
  copy_file ".node-version", "#{component_folder}/.node-version"
  copy_file ".rubocop.yml", "#{component_folder}/.rubocop.yml"
  copy_file ".eslintrc.json", "#{component_folder}/.eslintrc.json"
  copy_file ".stylelintrc.json", "#{component_folder}/.stylelintrc.json"

  app_folder = "#{component_folder}/app"
  template "app/packs/js/entrypoint.js", "#{app_folder}/packs/entrypoints/decidim_#{component_name}.js"
  template "app/packs/images/decidim/component/icon.svg", "#{app_folder}/packs/images/decidim/#{component_name}/icon.svg"
  template "app/packs/stylesheets/decidim/default.scss", "#{app_folder}/packs/stylesheets/decidim/#{component_name}/#{component_name}.scss"
  template "app/controllers/decidim/component/application_controller.rb.erb", "#{app_folder}/controllers/decidim/#{component_name}/application_controller.rb"
  template "app/controllers/decidim/component/admin/application_controller.rb.erb", "#{app_folder}/controllers/decidim/#{component_name}/admin/application_controller.rb"
  template "app/helpers/decidim/component/application_helper.rb.erb", "#{app_folder}/helpers/decidim/#{component_name}/application_helper.rb"
  template "app/models/decidim/component/application_record.rb.erb", "#{app_folder}/models/decidim/#{component_name}/application_record.rb"
  template "app/permissions/decidim/component/admin/permissions.rb.erb", "#{app_folder}/permissions/decidim/#{component_name}/admin/permissions.rb"

  bin_folder = "#{component_folder}/bin"
  template "bin/rails.erb", "#{bin_folder}/rails"
  chmod "#{bin_folder}/rails", "+x"

  config_folder = "#{component_folder}/config"
  template "config/assets.rb.erb", "#{config_folder}/assets.rb"
  template "config/locales/en.yml.erb", "#{config_folder}/locales/en.yml"
  template "config/i18n-tasks.yml.erb", "#{config_folder}/i18n-tasks.yml"

  lib_folder = "#{component_folder}/lib"
  template "lib/decidim/component.rb.erb", "#{lib_folder}/decidim/#{component_name}.rb"
  template "lib/decidim/component/engine.rb.erb", "#{lib_folder}/decidim/#{component_name}/engine.rb"
  template "lib/decidim/component/admin.rb.erb", "#{lib_folder}/decidim/#{component_name}/admin.rb"
  template "lib/decidim/component/admin_engine.rb.erb", "#{lib_folder}/decidim/#{component_name}/admin_engine.rb"
  template "lib/decidim/component/component.rb.erb", "#{lib_folder}/decidim/#{component_name}/component.rb"
  template "lib/decidim/component/version.rb.erb", "#{lib_folder}/decidim/#{component_name}/version.rb"
  template "lib/decidim/component/test/factories.rb.erb", "#{lib_folder}/decidim/#{component_name}/test/factories.rb"

  spec_folder = "#{component_folder}/spec"
  template "spec/spec_helper.rb.erb", "#{spec_folder}/spec_helper.rb"
  template "spec/factories.rb.erb", "#{spec_folder}/factories.rb"
  template "spec/permissions/admin/permissions_spec.rb.erb", "#{spec_folder}/permissions/admin/permissions_spec.rb"
  template "spec/lib/version_spec.rb.erb", "#{spec_folder}/lib/decidim/#{component_name}/version_spec.rb"

  if options[:external]
    inside(component_folder) do
      Bundler.with_original_env { run "bundle install" }
      Bundler.with_original_env { run "bundle lock --add-platform x86_64-linux" }
      Bundler.with_original_env { run "bundle lock --add-platform ruby" }
    end
  end
end