Last active
September 10, 2023 05:50
-
-
Save goulvench/5e808f89958655d304dabf039512ab33 to your computer and use it in GitHub Desktop.
Font Awesome automation for BridgetownRB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
say_status :font_awesome, "Installing Font Awesome..." | |
run "yarn add @fortawesome/fontawesome-free esbuild-plugin-copy" | |
gsub_file "esbuild.config.js", "const esbuildOptions = {}", <<~JS.strip | |
const path = require("path") | |
const esbuildCopy = require('esbuild-plugin-copy').default | |
/** | |
* @typedef { import("esbuild").BuildOptions } BuildOptions | |
* @type {BuildOptions} | |
*/ | |
const esbuildOptions = { | |
plugins: [ | |
esbuildCopy({ | |
assets: { | |
from: [path.resolve(__dirname, 'node_modules/@fortawesome/fontawesome-free/webfonts/*')], | |
to: [path.resolve(__dirname, 'frontend/fonts/')], | |
}, | |
verbose: false | |
}), | |
] | |
} | |
JS | |
scss_imports = <<~SCSS | |
$fa-font-path: "../fonts"; | |
@import "~@fortawesome/fontawesome-free/scss/fontawesome.scss"; | |
@import "~@fortawesome/fontawesome-free/scss/solid.scss"; | |
@import "~@fortawesome/fontawesome-free/scss/brands.scss"; | |
SCSS | |
scss_file_path = "frontend/styles/index.scss" | |
if File.exist?(scss_file_path) | |
prepend_to_file scss_file_path, scss_imports | |
else | |
create_file scss_file_path, scss_imports | |
end | |
create_builder "icon.rb", <<~RUBY | |
class Builders::Icon < SiteBuilder | |
def build | |
helper :icon | |
helper :link_icon | |
end | |
def icon(icon, classes = "", style: :solid, fixed: true, rotate: nil, flip: nil, size: nil, animate: nil, **attributes) | |
classes << " fa-" + icon.to_s.gsub("_", "-") | |
classes << " fa\#{style.to_s[0].downcase}" # Solid/regular/thin/brands | |
classes << " fa-fw" if fixed # Fixed-width | |
classes << " fa-rotate-\#{rotate}" if rotate | |
classes << " fa-flip-\#{flip}" if flip | |
classes << " fa-\#{size}" if size | |
classes << " fa-\#{animate.to_s.gsub("_", "-").downcase}" if animate | |
attributes["aria-hidden"] = "true" | |
attributes["class"] = classes | |
attributes = attributes.collect { |key, value| "\#{key}=\"\#{value}\"" } | |
<<~ICON.strip.html_safe | |
<i \#{attributes.join(" ")}></i> | |
ICON | |
end | |
def link_icon(icon, name, url, **options) | |
icon_attributes = options.delete(:icon) || {} | |
icon_class = icon_attributes.delete(:class) || "" | |
options[:href] = url | |
attributes = options.collect { |key, value| "\#{key}=\"\#{value.to_s.strip}\"" } | |
<<~LINK.strip.html_safe | |
<a \#{attributes.join(" ")}> | |
\#{icon(icon, icon_class, **icon_attributes)} | |
\#{name} | |
</a> | |
LINK | |
end | |
end | |
RUBY | |
say_status :font_awesome, "Font Awesome is now configured." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment