Skip to content

Commit

Permalink
Use custom destination (#44)
Browse files Browse the repository at this point in the history
* Use configuration option for destiation path

* Use given destination on install/initialize generator
  • Loading branch information
eelcoj authored Dec 20, 2024
1 parent 8e76d5d commit 111ef82
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
14 changes: 13 additions & 1 deletion lib/generators/rails_icons/initializer_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ module RailsIcons
class InitializerGenerator < Rails::Generators::Base
source_root File.expand_path("templates", __dir__)

desc "Create the Rails Icons initializer."

class_option :libraries, type: :array, default: [], desc: "Choose libraries (#{RailsIcons.libraries.keys.join("/")})"
class_option :destination, type: :string, default: "app/assets/svg/icons/", desc: "Specify destination folder for icons"
class_option :destination, type: :string, default: RailsIcons.configuration.destination_path, desc: "Specify destination folder for icons"
class_option :custom, type: :string, desc: "Name of the custom library"

def copy_initializer
Expand All @@ -28,6 +30,16 @@ def insert_default_configuration
end
end

def insert_custom_destination_path
return if options[:destination] && options[:destination] == RailsIcons.configuration.destination_path

insert_into_file INITIALIZER, <<~RB.indent(2), after: "RailsIcons.configure do |config|\n"
# Default destination path
config.destination_path = "#{options[:destination]}"
RB
end

def insert_libraries_configuration
insert_into_file INITIALIZER, "\n#{library_configuration}", before: "end"
end
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
Expand Up @@ -4,8 +4,10 @@ module RailsIcons
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path("templates", __dir__)

desc "Install Rails Icons with the choosen libraries. This creates the configuration initializer and will sync the icons."

class_option :libraries, type: :array, default: [], desc: "Choose libraries (#{RailsIcons.libraries.keys.join("/")})"
class_option :destination, type: :string, default: "app/assets/svg/icons/", desc: "Specify destination folder for icons"
class_option :destination, type: :string, default: RailsIcons.configuration.destination_path, desc: "Specify destination folder for icons"
class_option :skip_sync, type: :boolean, default: false

def initializer_generator
Expand Down
8 changes: 4 additions & 4 deletions lib/generators/rails_icons/sync_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

module RailsIcons
class SyncGenerator < Rails::Generators::Base
class_option :libraries, type: :array, default: [], desc: "Choose libraries (#{RailsIcons.libraries.keys.join("/")})"
class_option :destination, type: :string, default: "app/assets/svg/icons/", desc: "Specify destination folder for icons"

desc "Sync an icon library(s) from their respective git repos."
source_root File.expand_path("templates", __dir__)

desc "Sync the choosen 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 options[:libraries].empty?

Expand Down
1 change: 1 addition & 0 deletions lib/rails_icons/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def respond_to_missing?(method_name)

def set_default_config
@config.default_library = nil
@config.destination_path = "app/assets/svg/icons"
end

def set_libraries_config
Expand Down
7 changes: 2 additions & 5 deletions lib/rails_icons/icon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ def file_path
return Rails.root.join(custom_library.dig(:path), "#{@name}.svg") if custom_library?

parts = [
"app",
"assets",
"svg",
"icons",
RailsIcons.configuration.destination_path,
@library,
@variant,
"#{@name}.svg"
Expand Down Expand Up @@ -79,7 +76,7 @@ def custom_library
&.dig("custom")
&.dig(@library.to_sym)&.with_defaults(
{
path: "app/assets/svg/icons/#{@library}"
path: [RailsIcons.configuration.destination_path, @library].join("/")
}
) || {}
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 @@ -51,7 +51,7 @@ def remove_non_svg_files
end

def move_library
destination = File.join("app/assets/svg/icons/", @name)
destination = File.join(RailsIcons.configuration.destination_path, @name)

FileUtils.mkdir_p(destination)
FileUtils.mv(Dir.glob("#{@temp_directory}/*"), destination, force: true)
Expand Down

0 comments on commit 111ef82

Please sign in to comment.