Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/generators/rails_icons/initializer_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

require "rails_icons/base_generator"

module RailsIcons
class InitializerGenerator < Rails::Generators::Base
class InitializerGenerator < RailsIcons::BaseGenerator
source_root File.expand_path("templates", __dir__)

desc "Create the Rails Icons initializer."
Expand Down
4 changes: 3 additions & 1 deletion lib/generators/rails_icons/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

require "rails_icons/base_generator"

module RailsIcons
class InstallGenerator < Rails::Generators::Base
class InstallGenerator < RailsIcons::BaseGenerator
source_root File.expand_path("templates", __dir__)

desc "Install Rails Icons with the chosen libraries. This creates the configuration initializer and will sync the icons."
Expand Down
9 changes: 3 additions & 6 deletions lib/generators/rails_icons/sync_generator.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
# frozen_string_literal: true

require "rails_icons/base_generator"
require "rails_icons/sync/engine"

module RailsIcons
class SyncGenerator < Rails::Generators::Base
class SyncGenerator < RailsIcons::BaseGenerator
source_root File.expand_path("templates", __dir__)

desc "Sync the chosen icon libraries from their respective git repos."

class_option :libraries, type: :array, default: [], desc: "Choose libraries (#{RailsIcons.libraries.keys.join("/")})"

def sync_icons
raise "[Rails Icons] Not a valid library" if libraries.empty?

libraries.each { Sync::Engine.new(_1).sync }
end
def sync_icons = libraries.each { Sync::Engine.new(_1).sync }

private

Expand Down
20 changes: 20 additions & 0 deletions lib/rails_icons/base_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module RailsIcons
class BaseGenerator < Rails::Generators::Base
def initialize(*arguments)
super(*arguments)

validate!
end

private

def validate!
raise RailsIcons::LibraryNotFound.new("") if options.libraries.empty?
raise RailsIcons::LibraryNotFound.new(invalid_libraries.join(", ")) if invalid_libraries.any?
end

def invalid_libraries = options.libraries.map(&:to_sym).map(&:downcase).reject { RailsIcons.libraries.key?(_1) }
end
end
16 changes: 15 additions & 1 deletion lib/rails_icons/errors.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# frozen_string_literal: true

module RailsIcons
class NotFound < StandardError; end
class IconNotFound < StandardError
def initialize(icon_name)
super("The icon `#{icon_name}` is not available. Please check the icon name and try again.")
end
end

class LibraryNotFound < StandardError
def initialize(library_name)
if library_name.empty?
super("No libraries were specified. Please choose from: #{RailsIcons.libraries.keys.to_sentence(last_word_connector: " or ")}")
else
super("The library `#{library_name}` is not available. Please check the library name and try again.")
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rails_icons/icon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(name:, library:, arguments:, variant: nil)
end

def svg
raise RailsIcons::NotFound, error_message unless File.exist?(file_path)
raise RailsIcons::IconNotFound, error_message unless File.exist?(file_path)

Nokogiri::HTML::DocumentFragment.parse(File.read(file_path))
.at_css("svg")
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_icons/icon/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def call

icon_path = icons_path_in_app || icons_path_in_engines

raise RailsIcons::NotFound if icon_path.nil?
raise RailsIcons::IconNotFound if icon_path.nil?

icon_path
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_icons/sync/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def sync
def clone_repository
raise "[Rails Icons] Failed to clone repository" unless system("git clone '#{@library[:url]}' '#{@temp_directory}'")

say "'#{@name}' repository cloned successfully."
say "[Rails Icons] '#{@name}' repository cloned successfully."
end

def process_variants = Sync::ProcessVariants.new(@temp_directory, @name, @library).process
Expand Down
31 changes: 24 additions & 7 deletions test/generators/initializer_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ class InitializerGeneratorTest < Rails::Generators::TestCase

setup :prepare_destination

test "generator creates the initializer with default library" do
run_generator
test "generator creates the initializer with Boxicons library" do
run_generator %w[--libraries=boxicons]

assert_file "config/initializers/rails_icons.rb" do |file|
refute_match "heroicons", file
refute_match "tabler", file
assert_match "# Override Boxicons defaults", file
refute_match "Heroicons", file
end
end

test "generator creates the initializer with Feather library" do
run_generator %w[--libraries=feather]

assert_file "config/initializers/rails_icons.rb" do |file|
assert_match "# Override Feather defaults", file
refute_match "Tabler", file
end
end

Expand All @@ -28,7 +37,7 @@ class InitializerGeneratorTest < Rails::Generators::TestCase
end
end

test "generator creates the initializer with lucide library" do
test "generator creates the initializer with Lucide library" do
run_generator %w[--libraries=lucide]

assert_file "config/initializers/rails_icons.rb" do |file|
Expand All @@ -37,7 +46,7 @@ class InitializerGeneratorTest < Rails::Generators::TestCase
end
end

test "generator creates the initializer with phosphor library" do
test "generator creates the initializer with Phosphor library" do
run_generator %w[--libraries=phosphor]

assert_file "config/initializers/rails_icons.rb" do |file|
Expand All @@ -46,7 +55,7 @@ class InitializerGeneratorTest < Rails::Generators::TestCase
end
end

test "generator creates the initializer with tabler library" do
test "generator creates the initializer with Tabler library" do
run_generator %w[--libraries=tabler]

assert_file "config/initializers/rails_icons.rb" do |file|
Expand All @@ -64,4 +73,12 @@ class InitializerGeneratorTest < Rails::Generators::TestCase
refute_match "heroicons", file
end
end

test "generator raise RailsIcons::LibraryNotFound when no library is specified" do
assert_raises(RailsIcons::LibraryNotFound) do
run_generator
end

assert_no_file "config/initializers/rails_icons.rb"
end
end
Loading