From 7d2cdcbf9a44280e8ad6504848a62c9ae2a51ff0 Mon Sep 17 00:00:00 2001 From: Ali Hamdi Ali Fadel Date: Sun, 1 Sep 2024 10:25:01 +0000 Subject: [PATCH 1/7] Extract common generator helpers --- generators/helper.rb | 90 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 generators/helper.rb diff --git a/generators/helper.rb b/generators/helper.rb new file mode 100644 index 000000000..bfe7ad79a --- /dev/null +++ b/generators/helper.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +require 'erb' +require 'fileutils' +require 'phlexing' +require 'tqdm' + +ROBOCOP_DISABLE_WARNINGS = %w[ + Layout/LineLength + Metrics/AbcSize + Metrics/BlockLength + Metrics/ClassLength + Metrics/MethodLength +].join(',') + +def clone_repo(repo_url, repo_name) + print "⌛ Cloning '#{repo_name}' repo..." + + if Dir.exist?("generators/#{repo_name}") + puts "\r✅ '#{repo_name}' repo already exists" + + return + end + + system!("git clone #{repo_url} generators/#{repo_name}") + + puts "\r🎉 '#{repo_name}' repo cloned successfully!" +end + +def prepare_phlex_icons_pack_directory(icons_pack_path) + print "⌛ Preparing '#{icons_pack_path}' directory..." + + FileUtils.mkdir_p(icons_pack_path) + + Dir.glob("#{icons_pack_path}/*").each do |file| + next if ['base.rb', 'configuration.rb'].include?(File.basename(file)) + + File.delete(file) + end + + puts "\r🎉 '#{icons_pack_path}' directory prepared successfully!" +end + +def component_file_name(icon_file_name, replacements = nil) + replacements ||= {} + + icon_name = File.basename(icon_file_name, '.svg') + + replacements.each do |key, value| + icon_name = icon_name.gsub(key, value) if icon_name.start_with?(key) + end + + "#{icon_name.gsub('-', '_')}.rb" +end + +def component_class_name(icon_file_name, replacements = nil) + replacements ||= {} + + icon_name = File.basename(icon_file_name, '.svg') + + replacements.each do |key, value| + icon_name = icon_name.gsub(key, value) if icon_name.start_with?(key) + end + + icon_name.gsub('-', ' ').split.map(&:capitalize).join +end + +def run_rubocop(path) + print '⌛ Running RuboCop...' + + system("rubocop -A #{path}", out: File::NULL, err: File::NULL) + + Dir.glob("#{path}/*.rb").each do |file| + File.write(file, File.read(file).gsub('rubocop:enable ,', 'rubocop:enable ')) + end + + puts "\r🎉 RuboCop ran successfully!" +end + +def delete_repo(repo_name) + print "⌛ Deleting '#{repo_name}' repo..." + + FileUtils.rm_rf("generators/#{repo_name}") + + puts "\r🎉 '#{repo_name}' repo deleted successfully!" +end + +def system!(command) + system(command, out: File::NULL, err: File::NULL) || raise("❌ Command failed: '#{command}'") +end From b8997f08f8c00f89d91f8120f17c398ac62266bf Mon Sep 17 00:00:00 2001 From: Ali Hamdi Ali Fadel Date: Sun, 1 Sep 2024 10:25:27 +0000 Subject: [PATCH 2/7] Refactor heroicons generator --- generators/hero.rb | 71 +++++++++++++++++++++++++ generators/heroicons.rb | 115 ---------------------------------------- 2 files changed, 71 insertions(+), 115 deletions(-) create mode 100644 generators/hero.rb delete mode 100644 generators/heroicons.rb diff --git a/generators/hero.rb b/generators/hero.rb new file mode 100644 index 000000000..8c222fbeb --- /dev/null +++ b/generators/hero.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require_relative 'helper' + +REPO_URL = 'https://github.com/tailwindlabs/heroicons.git' +REPO_NAME = 'tailwindlabs-heroicons' +ICONS_PACK_PATH = 'lib/phlex/icons/hero' + +TEMPLATE = ERB.new <<~TEMPLATE + # frozen_string_literal: true + + # rubocop:disable #{ROBOCOP_DISABLE_WARNINGS} + module Phlex + module Icons + module Hero + class <%= icon_name %> < Base + def solid + <%= solid_icon %> + end + + def outline + <%= outline_icon %> + end + end + end + end + end + # rubocop:enable #{ROBOCOP_DISABLE_WARNINGS} +TEMPLATE + +def main + clone_repo(REPO_URL, REPO_NAME) + prepare_phlex_icons_pack_directory(ICONS_PACK_PATH) + + print '⌛ Creating icon components...' + icon_file_names.tqdm.each { create_icon_component(_1) } + puts "\r🎉 Icon components created successfully!" + + run_rubocop(ICONS_PACK_PATH) + delete_repo(REPO_NAME) +end + +def icon_file_names + Dir.glob("generators/#{REPO_NAME}/optimized/24/solid/*").map { |file| File.basename(file) } +end + +def create_icon_component(icon_file_name) + File.write( + File.join(ICONS_PACK_PATH, component_file_name(icon_file_name)), + TEMPLATE.result_with_hash( + icon_name: component_class_name(icon_file_name), + solid_icon: read_and_convert_icon(solid_icon_file_path(icon_file_name)), + outline_icon: read_and_convert_icon(outline_icon_file_path(icon_file_name)) + ) + ) +end + +def read_and_convert_icon(icon_file_path) + icon_file_content = File.read(icon_file_path) + Phlexing::Converter.convert(icon_file_content).sub('svg(', "svg(\nclass: classes,") +end + +def solid_icon_file_path(icon_file_name) + "generators/heroicons/optimized/24/solid/#{icon_file_name}" +end + +def outline_icon_file_path(icon_file_name) + "generators/heroicons/optimized/24/outline/#{icon_file_name}" +end + +main if __FILE__ == $PROGRAM_NAME diff --git a/generators/heroicons.rb b/generators/heroicons.rb deleted file mode 100644 index 5216e4095..000000000 --- a/generators/heroicons.rb +++ /dev/null @@ -1,115 +0,0 @@ -# frozen_string_literal: true - -require 'erb' -require 'fileutils' -require 'phlexing' -require 'tqdm' - -PHLEX_ICONS_HERO_PATH = 'lib/phlex/icons/hero' - -TEMPLATE = ERB.new <<~TEMPLATE - # frozen_string_literal: true - - # rubocop:disable Layout/LineLength - module Phlex - module Icons - module Hero - class <%= icon_name %> < Base - def solid - <%= solid_icon %> - end - - def outline - <%= outline_icon %> - end - end - end - end - end - # rubocop:enable Layout/LineLength -TEMPLATE - -def run - clone_heroicons_repo - prepare_phlex_icons_hero_directory - - puts 'Creating icon components...' - icon_file_names.tqdm.each { |icon_file_name| create_icon_component(icon_file_name) } - puts 'Icon components created' - - delete_heroicons_repo -end - -def clone_heroicons_repo - puts 'Cloning heroicons repo...' - - if Dir.exist?('generators/heroicons') - puts 'Heroicons repo already cloned' - - return - end - - system( - 'git clone https://github.com/tailwindlabs/heroicons.git generators/heroicons', - out: File::NULL, - err: File::NULL - ) - - puts 'Heroicons repo cloned' -end - -def prepare_phlex_icons_hero_directory - FileUtils.mkdir_p(PHLEX_ICONS_HERO_PATH) - - Dir.glob("#{PHLEX_ICONS_HERO_PATH}/*").each do |file| - next if ['base.rb', 'configuration.rb'].include?(File.basename(file)) - - File.delete(file) - end -end - -def icon_file_names - Dir.glob('generators/heroicons/optimized/24/solid/*').map { |file| File.basename(file) } -end - -def create_icon_component(icon_file_name) - component_file_path = "#{PHLEX_ICONS_HERO_PATH}/#{formatted_icon_name(icon_file_name)}.rb" - - File.write( - component_file_path, - TEMPLATE.result_with_hash( - icon_name: formatted_icon_class_name(icon_file_name), - solid_icon: read_and_convert_icon(solid_icon_file_path(icon_file_name)), - outline_icon: read_and_convert_icon(outline_icon_file_path(icon_file_name)) - ) - ) - - system("rubocop -A #{component_file_path}", out: File::NULL, err: File::NULL) -end - -def read_and_convert_icon(icon_file_path) - icon_file_content = File.read(icon_file_path) - Phlexing::Converter.convert(icon_file_content).sub('svg(', "svg(\nclass: classes,") -end - -def formatted_icon_name(icon_file_name) - File.basename(icon_file_name, '.svg').gsub('-', '_') -end - -def formatted_icon_class_name(icon_file_name) - File.basename(icon_file_name, '.svg').gsub('-', ' ').split.map(&:capitalize).join -end - -def solid_icon_file_path(icon_file_name) - "generators/heroicons/optimized/24/solid/#{icon_file_name}" -end - -def outline_icon_file_path(icon_file_name) - "generators/heroicons/optimized/24/outline/#{icon_file_name}" -end - -def delete_heroicons_repo - FileUtils.rm_rf('generators/heroicons') -end - -run if __FILE__ == $PROGRAM_NAME From 7fc2319783650da9a16cf72171b3ba70ac4198a3 Mon Sep 17 00:00:00 2001 From: Ali Hamdi Ali Fadel Date: Sun, 1 Sep 2024 10:25:58 +0000 Subject: [PATCH 3/7] Refactor lucide generator --- generators/lucide.rb | 62 +++++++++++++++++++++ generators/lucide_icons.rb | 108 ------------------------------------- 2 files changed, 62 insertions(+), 108 deletions(-) create mode 100644 generators/lucide.rb delete mode 100644 generators/lucide_icons.rb diff --git a/generators/lucide.rb b/generators/lucide.rb new file mode 100644 index 000000000..63f1b3ad7 --- /dev/null +++ b/generators/lucide.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require_relative 'helper' + +REPO_URL = 'https://github.com/lucide-icons/lucide.git' +REPO_NAME = 'lucide-icons-lucide' +ICONS_PACK_PATH = 'lib/phlex/icons/lucide' + +TEMPLATE = ERB.new <<~TEMPLATE + # frozen_string_literal: true + + # rubocop:disable #{ROBOCOP_DISABLE_WARNINGS} + module Phlex + module Icons + module Lucide + class <%= icon_name %> < Base + def view_template + <%= icon %> + end + end + end + end + end + # rubocop:enable #{ROBOCOP_DISABLE_WARNINGS} +TEMPLATE + +def main + clone_repo(REPO_URL, REPO_NAME) + prepare_phlex_icons_pack_directory(ICONS_PACK_PATH) + + print '⌛ Creating icon components...' + icon_file_paths.tqdm.each { create_icon_component(_1) } + puts "\r🎉 Icon components created successfully!" + + run_rubocop(ICONS_PACK_PATH) + delete_repo(REPO_NAME) +end + +def icon_file_paths + Dir.glob("generators/#{REPO_NAME}/icons/*.svg") +end + +def create_icon_component(icon_file_path) + File.write( + File.join(ICONS_PACK_PATH, component_file_name(icon_file_path)), + TEMPLATE.result_with_hash( + icon_name: component_class_name(icon_file_path), + icon: read_and_convert_icon(icon_file_path) + ) + ) +end + +def read_and_convert_icon(icon_file_path) + icon_file_content = File.read(icon_file_path) + .sub('width="24"', '') + .sub('height="24"', '') + + Phlexing::Converter.convert(icon_file_content) + .sub('svg(', "svg(\nclass: classes,") +end + +main if __FILE__ == $PROGRAM_NAME diff --git a/generators/lucide_icons.rb b/generators/lucide_icons.rb deleted file mode 100644 index 230e6e557..000000000 --- a/generators/lucide_icons.rb +++ /dev/null @@ -1,108 +0,0 @@ -# frozen_string_literal: true - -require 'erb' -require 'fileutils' -require 'phlexing' -require 'tqdm' - -PHLEX_ICONS_LUCIDE_PATH = 'lib/phlex/icons/lucide' - -TEMPLATE = ERB.new <<~TEMPLATE - # frozen_string_literal: true - - # rubocop:disable Layout/LineLength,Metrics/AbcSize,Metrics/BlockLength,Metrics/MethodLength - module Phlex - module Icons - module Lucide - class <%= icon_name %> < Base - def view_template - <%= icon %> - end - end - end - end - end - # rubocop:enable Layout/LineLength,Metrics/AbcSize,Metrics/BlockLength,Metrics/MethodLength -TEMPLATE - -def run - clone_lucide_icons_repo - prepare_phlex_icons_lucide_directory - - puts 'Creating icon components...' - icon_file_paths.tqdm.each { |icon_file_path| create_icon_component(icon_file_path) } - puts 'Icon components created' - - delete_lucide_icons_repo -end - -def clone_lucide_icons_repo - puts 'Cloning lucide icons repo...' - - if Dir.exist?('generators/lucide_icons') - puts 'Lucide icons repo already cloned' - - return - end - - system( - 'git clone https://github.com/lucide-icons/lucide.git generators/lucide_icons', - out: File::NULL, - err: File::NULL - ) - - puts 'Lucide icons repo cloned' -end - -def prepare_phlex_icons_lucide_directory - FileUtils.mkdir_p(PHLEX_ICONS_LUCIDE_PATH) - - Dir.glob("#{PHLEX_ICONS_LUCIDE_PATH}/*").each do |file| - next if ['base.rb'].include?(File.basename(file)) - - File.delete(file) - end -end - -def icon_file_paths - Dir.glob('generators/lucide_icons/icons/*.svg') -end - -def create_icon_component(icon_file_path) - component_file_path = "#{PHLEX_ICONS_LUCIDE_PATH}/#{formatted_icon_name(icon_file_path)}.rb" - - File.write( - component_file_path, - TEMPLATE.result_with_hash( - icon_name: formatted_icon_class_name(icon_file_path), - icon: read_and_convert_icon(icon_file_path) - ) - ) - - system("rubocop -A #{component_file_path}", out: File::NULL, err: File::NULL) - - File.write(component_file_path, File.read(component_file_path).gsub('rubocop:enable ,', 'rubocop:enable ')) -end - -def read_and_convert_icon(icon_file_path) - icon_file_content = File.read(icon_file_path) - .sub('width="24"', '') - .sub('height="24"', '') - - Phlexing::Converter.convert(icon_file_content) - .sub('svg(', "svg(\nclass: classes,") -end - -def formatted_icon_name(icon_file_path) - File.basename(icon_file_path, '.svg').gsub('-', '_') -end - -def formatted_icon_class_name(icon_file_path) - File.basename(icon_file_path, '.svg').gsub('-', ' ').split.map(&:capitalize).join -end - -def delete_lucide_icons_repo - FileUtils.rm_rf('generators/lucide_icons') -end - -run if __FILE__ == $PROGRAM_NAME From fe7df2260ae19bed914c696e3c37fece6e457c39 Mon Sep 17 00:00:00 2001 From: Ali Hamdi Ali Fadel Date: Sun, 1 Sep 2024 10:26:12 +0000 Subject: [PATCH 4/7] Refactor bootstrap generator --- generators/bootstrap.rb | 77 ++++++++++++++++++++ generators/bootstrap_icons.rb | 129 ---------------------------------- 2 files changed, 77 insertions(+), 129 deletions(-) create mode 100644 generators/bootstrap.rb delete mode 100644 generators/bootstrap_icons.rb diff --git a/generators/bootstrap.rb b/generators/bootstrap.rb new file mode 100644 index 000000000..77b3301d9 --- /dev/null +++ b/generators/bootstrap.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +require_relative 'helper' + +REPO_URL = 'https://github.com/twbs/icons.git' +REPO_NAME = 'twbs-icons' +ICONS_PACK_PATH = 'lib/phlex/icons/bootstrap' + +TEMPLATE = ERB.new <<~TEMPLATE + # frozen_string_literal: true + + # rubocop:disable #{ROBOCOP_DISABLE_WARNINGS} + module Phlex + module Icons + module Bootstrap + class <%= icon_name %> < Base + def view_template + <%= icon %> + end + end + end + end + end + # rubocop:enable #{ROBOCOP_DISABLE_WARNINGS} +TEMPLATE + +REPLACEMENTS = { + '0-' => 'zero-', + '1-' => 'one-', + '2-' => 'two-', + '3-' => 'three-', + '4-' => 'four-', + '5-' => 'five-', + '6-' => 'six-', + '7-' => 'seven-', + '8-' => 'eight-', + '9-' => 'nine-', + '123' => 'one-two-three' +}.freeze + +def main + clone_repo(REPO_URL, REPO_NAME) + prepare_phlex_icons_pack_directory(ICONS_PACK_PATH) + + print '⌛ Creating icon components...' + icon_file_paths.tqdm.each { create_icon_component(_1) } + puts "\r🎉 Icon components created successfully!" + + run_rubocop(ICONS_PACK_PATH) + delete_repo(REPO_NAME) +end + +def icon_file_paths + Dir.glob("generators/#{REPO_NAME}/icons/*") +end + +def create_icon_component(icon_file_path) + File.write( + File.join(ICONS_PACK_PATH, component_file_name(icon_file_path, REPLACEMENTS)), + TEMPLATE.result_with_hash( + icon_name: component_class_name(icon_file_path, REPLACEMENTS), + icon: read_and_convert_icon(icon_file_path) + ) + ) +end + +def read_and_convert_icon(icon_file_path) + icon_file_content = File.read(icon_file_path) + .sub(/class="bi [^"]*"/, '') + .sub('width="16"', '') + .sub('height="16"', '') + + Phlexing::Converter.convert(icon_file_content) + .sub('svg(', "svg(\nclass: classes,") +end + +main if __FILE__ == $PROGRAM_NAME diff --git a/generators/bootstrap_icons.rb b/generators/bootstrap_icons.rb deleted file mode 100644 index 36e36df4f..000000000 --- a/generators/bootstrap_icons.rb +++ /dev/null @@ -1,129 +0,0 @@ -# frozen_string_literal: true - -require 'erb' -require 'fileutils' -require 'phlexing' -require 'tqdm' - -PHLEX_ICONS_BOOTSTRAP_PATH = 'lib/phlex/icons/bootstrap' - -TEMPLATE = ERB.new <<~TEMPLATE - # frozen_string_literal: true - - # rubocop:disable Layout/LineLength - module Phlex - module Icons - module Bootstrap - class <%= icon_name %> < Base - def view_template - <%= icon %> - end - end - end - end - end - # rubocop:enable Layout/LineLength -TEMPLATE - -REPLACEMENTS = { - '0-' => 'zero-', - '1-' => 'one-', - '2-' => 'two-', - '3-' => 'three-', - '4-' => 'four-', - '5-' => 'five-', - '6-' => 'six-', - '7-' => 'seven-', - '8-' => 'eight-', - '9-' => 'nine-', - '123' => 'one-two-three' -}.freeze - -def run - clone_bootstrap_icons_repo - prepare_phlex_icons_bootstrap_directory - - puts 'Creating icon components...' - icon_file_paths.tqdm.each { |icon_file_path| create_icon_component(icon_file_path) } - puts 'Icon components created' - - delete_bootstrap_icons_repo -end - -def clone_bootstrap_icons_repo - puts 'Cloning bootstrap icons repo...' - - if Dir.exist?('generators/bootstrap_icons') - puts 'Bootstrap icons repo already cloned' - - return - end - - system('git clone https://github.com/twbs/icons.git generators/bootstrap_icons', out: File::NULL, err: File::NULL) - - puts 'Bootstrap icons repo cloned' -end - -def prepare_phlex_icons_bootstrap_directory - FileUtils.mkdir_p(PHLEX_ICONS_BOOTSTRAP_PATH) - - Dir.glob("#{PHLEX_ICONS_BOOTSTRAP_PATH}/*").each do |file| - next if ['base.rb'].include?(File.basename(file)) - - File.delete(file) - end -end - -def icon_file_paths - Dir.glob('generators/bootstrap_icons/icons/*') -end - -def create_icon_component(icon_file_path) - component_file_path = "#{PHLEX_ICONS_BOOTSTRAP_PATH}/#{formatted_icon_name(icon_file_path)}.rb" - - File.write( - component_file_path, - TEMPLATE.result_with_hash( - icon_name: formatted_icon_class_name(icon_file_path), - icon: read_and_convert_icon(icon_file_path) - ) - ) - - system("rubocop -A #{component_file_path}", out: File::NULL, err: File::NULL) -end - -def read_and_convert_icon(icon_file_path) - icon_file_content = File.read(icon_file_path) - .sub(/class="bi [^"]*"/, '') - .sub('width="16"', '') - .sub('height="16"', '') - - Phlexing::Converter.convert(icon_file_content) - .sub('svg(', "svg(\nclass: classes,") -end - -def formatted_icon_name(icon_file_path) - icon_name = File.basename(icon_file_path, '.svg') - - REPLACEMENTS.each do |key, value| - icon_name = icon_name.gsub(key, value) if icon_name.start_with?(key) - end - - icon_name.gsub('-', '_') -end - -def formatted_icon_class_name(icon_file_path) - icon_name = File.basename(icon_file_path, '.svg') - - REPLACEMENTS.each do |key, value| - icon_name = icon_name.gsub(key, value) if icon_name.start_with?(key) - end - - icon_name.gsub('-', ' ').split.map(&:capitalize).join -end - -def delete_bootstrap_icons_repo - FileUtils.rm_rf('generators/bootstrap_icons') -end - -run if __FILE__ == $PROGRAM_NAME From 6374090cd51dc3c0c285449f0bcc769c8ea6b80d Mon Sep 17 00:00:00 2001 From: Ali Hamdi Ali Fadel Date: Sun, 1 Sep 2024 10:26:26 +0000 Subject: [PATCH 5/7] Refactor flag generator --- generators/flag.rb | 76 ++++++++++++++++++++++++ generators/flag_icons.rb | 122 --------------------------------------- 2 files changed, 76 insertions(+), 122 deletions(-) create mode 100644 generators/flag.rb delete mode 100644 generators/flag_icons.rb diff --git a/generators/flag.rb b/generators/flag.rb new file mode 100644 index 000000000..0baa7316f --- /dev/null +++ b/generators/flag.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +require_relative 'helper' + +REPO_URL = 'https://github.com/lipis/flag-icons.git' +REPO_NAME = 'lipis-flag-icons' +ICONS_PACK_PATH = 'lib/phlex/icons/flag' + +TEMPLATE = ERB.new <<~TEMPLATE + # frozen_string_literal: true + + # rubocop:disable #{ROBOCOP_DISABLE_WARNINGS} + module Phlex + module Icons + module Flag + class <%= icon_name %> < Base + def square + <%= square_icon %> + end + + def rectangle + <%= rectangle_icon %> + end + end + end + end + end + # rubocop:enable #{ROBOCOP_DISABLE_WARNINGS} +TEMPLATE + +def main + clone_repo(REPO_URL, REPO_NAME) + prepare_phlex_icons_pack_directory(ICONS_PACK_PATH) + + print '⌛ Creating icon components...' + icon_file_names.tqdm.each { create_icon_component(_1) } + puts "\r🎉 Icon components created successfully!" + + run_rubocop(ICONS_PACK_PATH) + delete_repo(REPO_NAME) +end + +def icon_file_names + Dir.glob("generators/#{REPO_NAME}/flags/1x1/*").map { |file| File.basename(file) } +end + +def create_icon_component(icon_file_name) + File.write( + File.join(ICONS_PACK_PATH, component_file_name(icon_file_name)), + TEMPLATE.result_with_hash( + icon_name: component_class_name(icon_file_name), + square_icon: read_and_convert_icon(square_icon_file_path(icon_file_name)), + rectangle_icon: read_and_convert_icon(rectangle_icon_file_path(icon_file_name)) + ) + ) +end + +def read_and_convert_icon(icon_file_path) + icon_file_content = File.read(icon_file_path) + .sub(/id="flag-icons-[^"]*"/, '') + + Phlexing::Converter.convert(icon_file_content) + .sub('svg(', "svg(\nclass: classes,") + .sub('xmlns:xlink:', '"xmlns:xlink":') + .gsub('xlink:href:', '"xlink:href":') +end + +def square_icon_file_path(icon_file_name) + "generators/#{REPO_NAME}/flags/1x1/#{icon_file_name}" +end + +def rectangle_icon_file_path(icon_file_name) + "generators/#{REPO_NAME}/flags/4x3/#{icon_file_name}" +end + +main if __FILE__ == $PROGRAM_NAME diff --git a/generators/flag_icons.rb b/generators/flag_icons.rb deleted file mode 100644 index 3d8f6e8c6..000000000 --- a/generators/flag_icons.rb +++ /dev/null @@ -1,122 +0,0 @@ -# frozen_string_literal: true - -require 'erb' -require 'fileutils' -require 'phlexing' -require 'tqdm' - -PHLEX_ICONS_FLAG_PATH = 'lib/phlex/icons/flag' - -TEMPLATE = ERB.new <<~TEMPLATE - # frozen_string_literal: true - - # rubocop:disable Layout/LineLength,Metrics/AbcSize,Metrics/BlockLength,Metrics/ClassLength,Metrics/MethodLength - module Phlex - module Icons - module Flag - class <%= icon_name %> < Base - def square - <%= square_icon %> - end - - def rectangle - <%= rectangle_icon %> - end - end - end - end - end - # rubocop:enable Layout/LineLength,Metrics/AbcSize,Metrics/BlockLength,Metrics/ClassLength,Metrics/MethodLength -TEMPLATE - -def run - clone_flag_icons_repo - prepare_phlex_icons_flag_directory - - puts 'Creating icon components...' - icon_file_names.tqdm.each { |icon_file_name| create_icon_component(icon_file_name) } - puts 'Icon components created' - - delete_flag_icons_repo -end - -def clone_flag_icons_repo - puts 'Cloning flag icons repo...' - - if Dir.exist?('generators/flag_icons') - puts 'Flag icons repo already cloned' - - return - end - - system( - 'git clone https://github.com/lipis/flag-icons.git generators/flag_icons', - out: File::NULL, - err: File::NULL - ) - - puts 'Flag icons repo cloned' -end - -def prepare_phlex_icons_flag_directory - FileUtils.mkdir_p(PHLEX_ICONS_FLAG_PATH) - - Dir.glob("#{PHLEX_ICONS_FLAG_PATH}/*").each do |file| - next if ['base.rb', 'configuration.rb'].include?(File.basename(file)) - - File.delete(file) - end -end - -def icon_file_names - Dir.glob('generators/flag_icons/flags/1x1/*').map { |file| File.basename(file) } -end - -def create_icon_component(icon_file_name) - component_file_path = "#{PHLEX_ICONS_FLAG_PATH}/#{formatted_icon_name(icon_file_name)}.rb" - - File.write( - component_file_path, - TEMPLATE.result_with_hash( - icon_name: formatted_icon_class_name(icon_file_name), - square_icon: read_and_convert_icon(square_icon_file_path(icon_file_name)), - rectangle_icon: read_and_convert_icon(rectangle_icon_file_path(icon_file_name)) - ) - ) - - system("rubocop -A #{component_file_path}", out: File::NULL, err: File::NULL) - - File.write(component_file_path, File.read(component_file_path).gsub('rubocop:enable ,', 'rubocop:enable ')) -end - -def read_and_convert_icon(icon_file_path) - icon_file_content = File.read(icon_file_path) - .sub(/id="flag-icons-[^"]*"/, '') - - Phlexing::Converter.convert(icon_file_content) - .sub('svg(', "svg(\nclass: classes,") - .sub('xmlns:xlink:', '"xmlns:xlink":') - .gsub('xlink:href:', '"xlink:href":') -end - -def formatted_icon_name(icon_file_name) - File.basename(icon_file_name, '.svg').gsub('-', '_') -end - -def formatted_icon_class_name(icon_file_name) - File.basename(icon_file_name, '.svg').gsub('-', ' ').split.map(&:capitalize).join -end - -def square_icon_file_path(icon_file_name) - "generators/flag_icons/flags/1x1/#{icon_file_name}" -end - -def rectangle_icon_file_path(icon_file_name) - "generators/flag_icons/flags/4x3/#{icon_file_name}" -end - -def delete_flag_icons_repo - FileUtils.rm_rf('generators/flag_icons') -end - -run if __FILE__ == $PROGRAM_NAME From 1c08d82f6a77830a3505e9d4707eb0737bd1726a Mon Sep 17 00:00:00 2001 From: Ali Hamdi Ali Fadel Date: Sun, 1 Sep 2024 10:30:24 +0000 Subject: [PATCH 6/7] Use REPO_NAME when generating hero icon paths --- generators/hero.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generators/hero.rb b/generators/hero.rb index 8c222fbeb..19877e3c5 100644 --- a/generators/hero.rb +++ b/generators/hero.rb @@ -61,11 +61,11 @@ def read_and_convert_icon(icon_file_path) end def solid_icon_file_path(icon_file_name) - "generators/heroicons/optimized/24/solid/#{icon_file_name}" + "generators/#{REPO_NAME}/optimized/24/solid/#{icon_file_name}" end def outline_icon_file_path(icon_file_name) - "generators/heroicons/optimized/24/outline/#{icon_file_name}" + "generators/#{REPO_NAME}/optimized/24/outline/#{icon_file_name}" end main if __FILE__ == $PROGRAM_NAME From 201104169134c84d2a9d2392302aa722ed8ba33c Mon Sep 17 00:00:00 2001 From: Ali Hamdi Ali Fadel Date: Sun, 1 Sep 2024 10:39:04 +0000 Subject: [PATCH 7/7] Simplify generators main running loop --- generators/bootstrap.rb | 10 +--------- generators/flag.rb | 10 +--------- generators/helper.rb | 12 ++++++++++++ generators/hero.rb | 10 +--------- generators/lucide.rb | 10 +--------- 5 files changed, 16 insertions(+), 36 deletions(-) diff --git a/generators/bootstrap.rb b/generators/bootstrap.rb index 77b3301d9..c39a749be 100644 --- a/generators/bootstrap.rb +++ b/generators/bootstrap.rb @@ -39,15 +39,7 @@ def view_template }.freeze def main - clone_repo(REPO_URL, REPO_NAME) - prepare_phlex_icons_pack_directory(ICONS_PACK_PATH) - - print '⌛ Creating icon components...' - icon_file_paths.tqdm.each { create_icon_component(_1) } - puts "\r🎉 Icon components created successfully!" - - run_rubocop(ICONS_PACK_PATH) - delete_repo(REPO_NAME) + run_generator { icon_file_paths.tqdm.each { create_icon_component(_1) } } end def icon_file_paths diff --git a/generators/flag.rb b/generators/flag.rb index 0baa7316f..db87be604 100644 --- a/generators/flag.rb +++ b/generators/flag.rb @@ -29,15 +29,7 @@ def rectangle TEMPLATE def main - clone_repo(REPO_URL, REPO_NAME) - prepare_phlex_icons_pack_directory(ICONS_PACK_PATH) - - print '⌛ Creating icon components...' - icon_file_names.tqdm.each { create_icon_component(_1) } - puts "\r🎉 Icon components created successfully!" - - run_rubocop(ICONS_PACK_PATH) - delete_repo(REPO_NAME) + run_generator { icon_file_names.tqdm.each { create_icon_component(_1) } } end def icon_file_names diff --git a/generators/helper.rb b/generators/helper.rb index bfe7ad79a..88074ebf0 100644 --- a/generators/helper.rb +++ b/generators/helper.rb @@ -13,6 +13,18 @@ Metrics/MethodLength ].join(',') +def run_generator(&block) + clone_repo(REPO_URL, REPO_NAME) + prepare_phlex_icons_pack_directory(ICONS_PACK_PATH) + + print '⌛ Creating icon components...' + yield block + puts "\r🎉 Icon components created successfully!" + + run_rubocop(ICONS_PACK_PATH) + delete_repo(REPO_NAME) +end + def clone_repo(repo_url, repo_name) print "⌛ Cloning '#{repo_name}' repo..." diff --git a/generators/hero.rb b/generators/hero.rb index 19877e3c5..2283bf2f0 100644 --- a/generators/hero.rb +++ b/generators/hero.rb @@ -29,15 +29,7 @@ def outline TEMPLATE def main - clone_repo(REPO_URL, REPO_NAME) - prepare_phlex_icons_pack_directory(ICONS_PACK_PATH) - - print '⌛ Creating icon components...' - icon_file_names.tqdm.each { create_icon_component(_1) } - puts "\r🎉 Icon components created successfully!" - - run_rubocop(ICONS_PACK_PATH) - delete_repo(REPO_NAME) + run_generator { icon_file_names.tqdm.each { create_icon_component(_1) } } end def icon_file_names diff --git a/generators/lucide.rb b/generators/lucide.rb index 63f1b3ad7..342cb3311 100644 --- a/generators/lucide.rb +++ b/generators/lucide.rb @@ -25,15 +25,7 @@ def view_template TEMPLATE def main - clone_repo(REPO_URL, REPO_NAME) - prepare_phlex_icons_pack_directory(ICONS_PACK_PATH) - - print '⌛ Creating icon components...' - icon_file_paths.tqdm.each { create_icon_component(_1) } - puts "\r🎉 Icon components created successfully!" - - run_rubocop(ICONS_PACK_PATH) - delete_repo(REPO_NAME) + run_generator { icon_file_paths.tqdm.each { create_icon_component(_1) } } end def icon_file_paths