From 3142401ed4bd9b9e965506d99c1a98aa65a1a14f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20B=C4=82LTEANU?= Date: Sun, 6 Oct 2024 22:44:52 +0300 Subject: [PATCH] Adds rake task to download SVG icons from Tabler Github repository and generates a sprite file. diff --git a/Gemfile b/Gemfile index 46eeb9276..b57b3bd9d 100644 --- a/Gemfile +++ b/Gemfile @@ -95,6 +95,7 @@ end group :development do gem 'listen', '~> 3.3' gem 'yard', require: false + gem 'svg_sprite', require: false end group :test do diff --git a/config/icon_source.yml b/config/icon_source.yml new file mode 100644 index 000000000..0add1f19d --- /dev/null +++ b/config/icon_source.yml @@ -0,0 +1,171 @@ +- name: edit + svg: pencil +- name: add + svg: circle-plus +- name: copy + svg: copy +- name: del + svg: trash +- name: save + svg: device-floppy +- name: download + svg: download +- name: attachment + svg: paperclip +- name: time-add + svg: clock-plus +- name: time + svg: clock +- name: fav + svg: star +- name: copy-link + svg: clipboard-copy +- name: 3-bullets + svg: dots +- name: history + svg: history +- name: folder + svg: folder +- name: folder-open + svg: folder-open +- name: lock + svg: lock +- name: unlock + svg: lock-open +- name: link-break + svg: link-off +- name: zoom-in + svg: zoom-in +- name: zoom-out + svg: zoom-out +- name: settings + svg: settings +- name: news + svg: news +- name: user + svg: user +- name: group + svg: users-group +- name: bookmarked + svg: bookmark +- name: bookmark-delete + svg: bookmark-off +- name: bookmark-add + svg: bookmark-plus +- name: import + svg: database-export +- name: summary + svg: bolt +- name: angle-down + svg: chevron-down +- name: angle-right + svg: chevron-right +- name: angle-up + svg: chevron-up +- name: email + svg: mail +- name: email-disabled + svg: mail-off +- name: stats + svg: chart-bar +- name: reorder + svg: menu-order +- name: close + svg: square-x +- name: checked + svg: check +- name: reload + svg: refresh +- name: link + svg: link +- name: plugins + svg: puzzle +- name: roles + svg: shield-cog +- name: list + svg: list +- name: workflows + svg: jump-rope +- name: server-authentication + svg: server +- name: table-multiple + svg: refresh +- name: projects + svg: packages +- name: package + svg: package +- name: custom-fields + svg: input-check +- name: help + svg: info-circle +- name: changeset + svg: code +- name: clear-query + svg: square-x +- name: warning + svg: alert-triangle +- name: comments + svg: message +- name: comment + svg: message +- name: arrow-right + svg: arrow-big-right +- name: wiki-page + svg: notebook +- name: bullet-end + svg: circle-arrow-left +- name: bullet-go + svg: circle-arrow-right +- name: bullet-go-end + svg: diamonds +- name: move + svg: arrow-forward-up +- name: cancel + svg: arrow-back-up +- name: document + svg: file-text +- name: issue + svg: note +- name: issue-closed + svg: square-check +- name: issue-edit + svg: edit +- name: issue-note + svg: message-plus +- name: file + svg: file +- name: text-plain + svg: file-text +- name: text-x-c + svg: file-code +- name: text-x-csharp + svg: brand-c-sharp +- name: text-x-java + svg: file-code +- name: text-x-php + svg: file-type-php +- name: text-x-ruby + svg: file-code +- name: text-xml + svg: file-type-xml +- name: text-css + svg: file-type-css +- name: text-html + svg: file-type-html +- name: image-gif + svg: file +- name: image-jpeg + svg: file-type-jpg +- name: image-png + svg: file-type-png +- name: image-tiff + svg: file +- name: application-javascript + svg: file-type-js +- name: application-pdf + svg: file-type-pdf +- name: application-zip + svg: file-type-zip +- name: application-gzip + svg: file-zip + diff --git a/lib/tasks/icons.rake b/lib/tasks/icons.rake new file mode 100644 index 000000000..3aec9c34b --- /dev/null +++ b/lib/tasks/icons.rake @@ -0,0 +1,127 @@ +# frozen_string_literal: true +unless Rails.env.production? + require "svg_sprite" + + SOURCE = URI.parse("https://raw.githubusercontent.com") + SOURCE_PATH = "/tabler/tabler-icons/main/icons" + ICON_VERSION = "outline" + + namespace :icons do + + desc 'Downloads default SVG icons' + task :download do + icons_mapping = YAML.load_file(Rails.root.join('config/icon_source.yml')) + destination_path = Rails.root.join("tmp", "icons", "default") + + download_sgv_icons(icons_mapping, destination_path) + end + + desc "Generates SVG sprite file default SVG icons" + task :sprite do + input_path = Rails.root.join("tmp", "icons", "default") + sprite_path = Rails.root.join('app', 'assets', 'images', 'icons.svg') + + generate_svg_sprite(input_path, sprite_path) + end + + desc 'Downloads default SVG icons and generates a SVG sprite from the icons' + task :generate do + Rake::Task["icons:download"].execute + Rake::Task["icons:sprite"].execute + end + + namespace :plugin do + desc 'Downloads SVG icons for plugin' + task :download do + name = ENV['NAME'] + + if name.nil? + abort "The VERSION argument requires a plugin NAME." + end + + icons_mapping_path = Rails.root.join('plugins', name, 'config', 'icon_source.yml') + unless File.file?(icons_mapping_path) + abort "Icon source file for #{name} plugin not found in #{icons_mapping_path}." + end + + icons_mapping = YAML.load_file(icons_mapping_path) + destination_path = Rails.root.join("tmp", "icons", name) + + download_sgv_icons(icons_mapping, destination_path) + end + + desc "Generates SVG sprite for plugin" + task :sprite do + name = ENV['NAME'] + + if name.nil? + abort "The VERSION argument requires a plugin NAME." + end + + input_path = Rails.root.join("tmp", "icons", name) + sprite_path = Rails.root.join('plugins', name, 'app', 'assets', 'images', 'icons.svg') + + generate_svg_sprite(input_path, sprite_path) + end + + desc 'Downloads SVG icons and generates sprite for plugin' + task :generate do + Rake::Task["icons:plugin:download"].execute + Rake::Task["icons:plugin:sprite"].execute + end + end + end + + def download_sgv_icons(icons_mapping, destination) + http = Net::HTTP.new(SOURCE.host, SOURCE.port) + http.use_ssl = true + + FileUtils.rm_rf(destination) + FileUtils.mkdir_p(destination) + + icons_mapping.each do |v| + name = v['name'] + svg = v['svg'] + + http.start do |h| + source = "#{SOURCE_PATH}/#{ICON_VERSION}/#{svg}.svg" + + puts "Downloading #{name} from #{source}..." + req = Net::HTTP::Get.new(source) + res = h.request(req) + + case res + when Net::HTTPSuccess + target = File.join(destination, "#{name}.svg") + File.open(target, 'w') do |f| + f.write res.body + end + else + puts "Error when trying to download the icon for #{name}" + puts res.inspect + exit 1 + end + end + end + end + + def generate_svg_sprite(input_path, sprite_path) + SvgSprite.call( + input: input_path, + name: 'icon', + css_path: File.join(input_path, 'icons.css'), + sprite_path: sprite_path, + optimize: true + ) + + doc = Nokogiri::XML(sprite_path) + + doc.traverse do |node| + node.keys.each do |attribute| + node.delete attribute if ["fill", "stroke", "stroke-width"].include?(attribute) + end + end + + File.write(sprite_path, doc.to_xml) + end +end -- 2.39.5 (Apple Git-154)