Class: QuicksilverUI::Generators::ComponentGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/quicksilver_ui/component/component_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



12
13
14
# File 'lib/generators/quicksilver_ui/component/component_generator.rb', line 12

def self.banner
  "rails generate quicksilver_ui:component NAME [options]"
end

.desc(description = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/generators/quicksilver_ui/component/component_generator.rb', line 22

def self.desc(description = nil)
  return super if description

  components = Dir.glob(File.join(QuicksilverUI.ui_path, "*.rb"))
    .map { |f| File.basename(f, ".rb") }
    .reject { |n| n == "base" }
    .sort
    .map { |c| "  #{c}" }
    .join("\n")

  "#{super}\n#{components}"
end

Instance Method Details

#add_gemsObject



50
51
52
53
54
55
56
57
# File 'lib/generators/quicksilver_ui/component/component_generator.rb', line 50

def add_gems
  all_gems.each do |gem_name|
    unless gem_installed?(gem_name)
      say "Adding #{gem_name} to Gemfile...", :yellow
      run "bundle add #{gem_name}"
    end
  end
end

#copy_component_filesObject



59
60
61
62
63
64
65
66
67
# File 'lib/generators/quicksilver_ui/component/component_generator.rb', line 59

def copy_component_files
  all_components.each do |name|
    paths = file_paths_for(name)
    paths.each do |file_path|
      relative = Pathname.new(file_path).relative_path_from(self.class.source_root)
      copy_file file_path, Rails.root.join("app/views/ui", relative), force: options["force"]
    end
  end
end

#copy_controllersObject



80
81
82
83
84
85
86
87
# File 'lib/generators/quicksilver_ui/component/component_generator.rb', line 80

def copy_controllers
  all_controllers.each do |name|
    source = File.join(QuicksilverUI.javascript_controllers_path, "#{name}_controller.js")
    next unless File.exist?(source)

    copy_file source, Rails.root.join("app/javascript/controllers", "#{name}_controller.js"), force: options["force"]
  end
end

#copy_mixinsObject



89
90
91
92
93
94
95
96
# File 'lib/generators/quicksilver_ui/component/component_generator.rb', line 89

def copy_mixins
  all_mixins.each do |name|
    source = File.join(QuicksilverUI.javascript_mixins_path, "#{name}.js")
    next unless File.exist?(source)

    copy_file source, Rails.root.join("app/javascript/mixins", "#{name}.js"), force: options["force"]
  end
end

#copy_stylesheetsObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/generators/quicksilver_ui/component/component_generator.rb', line 69

def copy_stylesheets
  all_stylesheets.each do |name|
    source = File.join(QuicksilverUI.stylesheets_path, "#{name}.css")
    next unless File.exist?(source)

    dest = Rails.root.join("app/assets/tailwind", "#{name}.css")
    copy_file source, dest, force: options["force"]
    add_css_import(name)
  end
end

#doneObject



98
99
100
101
102
103
104
105
106
# File 'lib/generators/quicksilver_ui/component/component_generator.rb', line 98

def done
  say ""
  say "#{component_name} component generated!", :green

  deps = all_components - [component_folder_name]
  if deps.any?
    say "  Dependencies copied: #{deps.join(", ")}", :cyan
  end
end

#generate_componentObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/generators/quicksilver_ui/component/component_generator.rb', line 38

def generate_component
  if component_not_found?
    say "Component not found: #{component_name}", :red
    say ""
    say "Available components:", :green
    available_components.each { |c| say "  - #{c}" }
    exit 1
  end

  say "Generating #{component_name} component..."
end