Skip to content

Commit

Permalink
ADO-2646 - FontAwesome 5 Pro
Browse files Browse the repository at this point in the history
* We are looking to use pro-only FontAwesome 5 icons in an
  upcoming feature. We have a perpetual enterprise license
  for FontAwesome Pro, but we are forbidden from distributing
  the webfont files to "non-creators" in the license.
* Uploading the webfont files to this open-source package
  is considered a breach of the pro license (according to
  fontawesome support), so we need to create conditions
  to "enable" the pro icon set specifically for our
  organization's use.
* Symlink in the pro fontawesome files into
  'vendor/assets/fonts/font-awesome` when the configuration
  value of `font_awesome_pro_source_path` is provided.
* Because the files are symlinked into the vendored asset
  directory, they override the free files provided by
  the `font-awesome-sass` gem [1] in the asset pipeline.
* The `font-awesome-sass` gem does not provide unicode
  mappings for the pro-only icons. If we wish to use
  them, we need to define the unicode mapping in
  the `font_awesome_pro.scss` file.

[1]: FortAwesome/font-awesome-sass#146 (comment)
SEE: https://dev.azure.com/AMA-Ent/AMA-Ent/_workitems/edit/2646
  • Loading branch information
Jesse Doyle committed Sep 5, 2019
1 parent 0c42806 commit ba3b255
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.bundle/
vendor/bundle
vendor/assets/fonts/font-awesome
log/*.log
pkg/
spec/dummy/db/*.sqlite3
Expand Down
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Metrics/BlockLength:
Exclude:
- 'spec/**/*'
- '*.gemspec'
- 'lib/ama/tasks/assets.rake'
2 changes: 2 additions & 0 deletions .simplecov
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
SimpleCov.start 'rails' do
SimpleCov.minimum_coverage 100.00
add_filter 'lib/ama/styles/version'
add_filter 'lib/ama/styles/engine'
add_filter 'lib/ama/styles/internal/chat'
add_filter 'lib/ama/styles/internal/commands/deploy'
add_filter 'lib/ama/styles/internal/environment'
add_filter 'lib/ama/tasks'
end
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem that will carry and deploy AMA assets.

```ruby
Rails.configuration.assets.precompile += %w(shared.css)
Rails.configuration.font_awesome_pro_source_path = '~/source/to/font-awesome'
Rails.configuration.stylesheet_resolver.remote = Rails.env.production?
```
* Add the `ASSET_CLOUDFRONT_URL` variable to the relevant `.env` file for the application.
Expand Down
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require File.expand_path('../config/application', __FILE__)
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
load File.join('lib/tasks/assets.rake')
RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new
task default: %w[spec rubocop]
5 changes: 5 additions & 0 deletions app/assets/stylesheets/font_awesome_pro.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
* NOTE: font-awesome-sass does not define the pro-only icons. If you want
* use one, you must define it below.
*/
.#{$fa-css-prefix}-tire:before { content: fa-content(\f631); }
1 change: 1 addition & 0 deletions app/assets/stylesheets/shared.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@import "webfonts/index";
@import "font-awesome-sprockets";
@import "font-awesome";
@import "font_awesome_pro";
@import "foundation_and_overrides";
@import "foundation";
@import "layout/index";
Expand Down
1 change: 0 additions & 1 deletion bin/assets
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require_relative '../config/application'
require_relative '../lib/ama/styles/internal/base'
AMA::Styles::Internal::Environment.new(environment: 'development').load!
require_relative '../lib/ama_styles'
load File.join('lib/ama/tasks/assets.rake')

question = 'Please enter branch name: (default = master)'
branch = HighLine.new.ask(question) do |q|
Expand Down
22 changes: 19 additions & 3 deletions lib/ama/styles/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@ module Styles
class Engine < ::Rails::Engine
isolate_namespace AMA::Styles

config.before_initialize do
Rails.configuration.stylesheet_resolver = Resolver.new
Rails.configuration.cloudfront_cache_store = Cache.build_store(
config.before_initialize do |app|
app.config.stylesheet_resolver = Resolver.new
app.config.cloudfront_cache_store = Cache.build_store(
:redis_store,
db: 3,
namespace: 'assets',
host: Rails.configuration.redis_endpoint,
raw: true
)
end

config.after_initialize do |app|
# are we using bundled assets? (i.e. dev/local)
if app.config.stylesheet_resolver.local? && Rails.env.development?
# is a source location provided for the font awesome pro font files?
if app.config.font_awesome_pro_source_path.present?
# are there already pro files in the application asset folder?
unless app.root.join('vendor', 'assets', 'fonts', 'font-awesome').exist?
Rake::Task['assets:fa5_pro'].invoke(
app.config.font_awesome_pro_source_path,
app.root.to_s
)
end
end
end
end
end
end
end
1 change: 0 additions & 1 deletion lib/ama/styles/internal/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# in a host application.

# Development Dependencies
require 'rake'
require 'aws-sdk'
require 'rest-client'
require 'rest_client/jogger'
Expand Down
10 changes: 10 additions & 0 deletions lib/ama/styles/internal/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def log(msg)

def precompile_assets
Rake::Task['assets:copy_raw_assets'].invoke
if font_awesome_pro?
Rake::Task['assets:fa5_pro'].invoke(
Rails.configuration.font_awesome_pro_source_path,
GEM_ROOT_PATH
)
end
Rake::Task['assets:precompile'].invoke
end

Expand All @@ -66,6 +72,10 @@ def assets_files
end
end

def font_awesome_pro?
Rails.configuration.font_awesome_pro_source_path.present?
end

def upload_files
assets_files.each do |file|
path = Pathname.new(file)
Expand Down
1 change: 1 addition & 0 deletions lib/ama/styles/internal/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def configure_assets
config.eager_load = false
config.assets.css_compressor = :sass
config.assets.precompile += %w[shared.css]
config.font_awesome_pro_source_path = ENV['FONT_AWESOME_PRO_SOURCE_PATH']
end

def configure_api
Expand Down
4 changes: 4 additions & 0 deletions lib/ama/styles/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def asset_path
remote ? custom_asset_url : PRIMARY_STYLESHEET_NAME
end

def local?
!remote
end

private

def custom_asset_url
Expand Down
2 changes: 1 addition & 1 deletion lib/ama/styles/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module AMA
module Styles
VERSION = '2.8.0'
VERSION = '3.0.0'
end
end
29 changes: 29 additions & 0 deletions lib/ama/tasks/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,33 @@ namespace :assets do
bucket = client.bucket(Rails.configuration.assets_bucket_name)
AMA::Styles::Internal::Deployment.new(bucket: bucket, log_output: true).run
end

desc 'Copy raw assets'
task :copy_raw_assets do
assets_path = File.join(Rails.root, 'app', 'assets', 'raw')
assets = Dir.glob("#{assets_path}/**/**", File::FNM_DOTMATCH).map do |file|
next unless File.file?(file)
expanded_path = Pathname.new(File.expand_path(file))
asset_path = Pathname.new(assets_path)
expanded_path.relative_path_from(asset_path).to_s
end.compact
assets.each do |asset|
source_file = File.join(assets_path, asset)
dest_file = File.join(Rails.root, 'public', 'assets', 'raw', asset)
FileUtils.mkdir_p(File.dirname(dest_file))
FileUtils.copy_file source_file, dest_file, true
end
end

desc 'Link FontAwesome 5 Pro into vendor/assets/fonts/font-awesome'
task :fa5_pro, %i[source destination] do |_task, args|
source_path = File.join(args[:source], '*.{eot,svg,ttf,woff,woff2}')
font_wildcard = File.expand_path(source_path)
destination = File.join(args[:destination], 'vendor', 'assets', 'fonts', 'font-awesome')
destination_path = File.expand_path(destination)
FileUtils.mkdir_p(destination)
Dir.glob(font_wildcard).each do |file|
FileUtils.ln_s(file, destination_path)
end
end
end
3 changes: 3 additions & 0 deletions lib/ama_styles.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

# Third Party
require 'rake'
require 'font-awesome-sass'
require 'foundation/rails'
require 'dotenv/rails-now'
Expand All @@ -10,3 +11,5 @@
require 'ama/styles/base'
require 'ama/styles/resolver'
require 'ama/styles/engine'

load File.join(AMA::Styles::Globals::GEM_ROOT_PATH, 'lib/ama/tasks/assets.rake')
20 changes: 0 additions & 20 deletions lib/tasks/assets.rake

This file was deleted.

1 change: 1 addition & 0 deletions spec/dummy/config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

Rails.application.config.eager_load = false
Rails.application.config.assets.css_compressor = :sass
Rails.application.config.font_awesome_pro_source_path = '~/src/font-awesome'
Rails.application.config.assets.precompile += %w(shared.css)
File renamed without changes.
2 changes: 1 addition & 1 deletion spec/support/shared_examples/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
RSpec.shared_context 'rake' do
let(:rake) { Rake::Application.new }
let(:task_name) { self.class.top_level_description }
let(:task_path) { "/lib/tasks/#{task_name.split(':').first}" }
let(:task_path) { "/lib/ama/tasks/#{task_name.split(':').first}" }
subject { rake[task_name] }

def loaded_files_excluding_current_rake_file
Expand Down

0 comments on commit ba3b255

Please sign in to comment.