Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refactor generators #10

Merged
merged 7 commits into from
Sep 1, 2024
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
69 changes: 69 additions & 0 deletions generators/bootstrap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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
run_generator { icon_file_paths.tqdm.each { create_icon_component(_1) } }
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
129 changes: 0 additions & 129 deletions generators/bootstrap_icons.rb

This file was deleted.

68 changes: 68 additions & 0 deletions generators/flag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 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
run_generator { icon_file_names.tqdm.each { create_icon_component(_1) } }
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
122 changes: 0 additions & 122 deletions generators/flag_icons.rb

This file was deleted.

Loading