Skip to content

Commit

Permalink
Merge pull request #30 from janosrusiczki/rubocoping
Browse files Browse the repository at this point in the history
Rubocoping
  • Loading branch information
janosrusiczki committed Dec 7, 2017
2 parents d091526 + b238b61 commit e66be3e
Show file tree
Hide file tree
Showing 22 changed files with 169 additions and 108 deletions.
7 changes: 6 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
inherit_from: .rubocop_todo.yml
# Do not count block length in spec files
Metrics/BlockLength:
ExcludedMethods: ['describe', 'context']

MethodLength:
Max: 15

42 changes: 0 additions & 42 deletions .rubocop_todo.yml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ In the following example, we will override the default CSS link tag by adding a
end

def html
"<link href='/#{@path}/#{@filename}' rel='stylesheet'" \
"<link href='#{output_path}/#{@filename}' rel='stylesheet' " \
"type='text/css' media='screen' />\n"
end
end
Expand All @@ -306,7 +306,7 @@ In the following example, we will override the default CSS link tag by adding a

If you already added a compressor and/or a converter, you can include your template class alongside your compressor and/or converter within the same JAPR module.

The “self.filetype” method defines the type of bundle a template will target (either `.js` or `.css`). The “html” method is where the magic happens. @pathand `@filename` instance variables are available within the class and contain the path and filename of the generated bundle, respectively. The template should return a string that contains an HTML tag pointing to the generated bundle via an `html` method.
The “self.filetype” method defines the type of bundle a template will target (either `.js` or `.css`). The “html” method is where the magic happens. `output_path` is a helper method and `@filename` is an instance variable which are available within the class and contain the path and filename of the generated bundle, respectively. The template should return a string that contains an HTML tag pointing to the generated bundle via an `html` method.

2. Run the `jekyll` command to compile your site.

Expand Down
2 changes: 1 addition & 1 deletion lib/japr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
require 'japr/asset'
require 'japr/converter'
require 'japr/compressor'
require 'japr/template'
require 'japr/templates/template_helper'
require 'japr/template'
require 'japr/templates/javascript_tag_template'
require 'japr/templates/css_tag_template'
require 'japr/pipeline'
Expand Down
1 change: 1 addition & 0 deletions lib/japr/asset.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module JAPR
# Holds an asset (file)
class Asset
def initialize(content, filename, dirname = '.')
@content = content
Expand Down
6 changes: 3 additions & 3 deletions lib/japr/compressor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module JAPR
# Base class for asset compressors
# See https://github.com/janosrusiczki/japr#asset-compression
class Compressor
extend JAPR::SubclassTracking

Expand All @@ -8,9 +10,7 @@ def initialize(content)
end

# Returns compressed content
def compressed
@compressed
end
attr_reader :compressed

# Filetype to process (e.g. '.js')
def self.filetype
Expand Down
6 changes: 3 additions & 3 deletions lib/japr/converter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module JAPR
# Base class for asset converters
# See https://github.com/janosrusiczki/japr#asset-preprocessing
class Converter
extend JAPR::SubclassTracking

Expand All @@ -9,9 +11,7 @@ def initialize(asset)
@converted = convert
end

def converted
@converted
end
attr_reader :converted

# Filetype to process (e.g. '.coffee')
def self.filetype
Expand Down
2 changes: 2 additions & 0 deletions lib/japr/extensions/jekyll/site.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module Jekyll
# Contains overrides for the needed Jekyll:Site methods
# The actual code is in JAPR::JekyllSiteExtensions
class Site
include JAPR::JekyllSiteExtensions
end
Expand Down
2 changes: 2 additions & 0 deletions lib/japr/extensions/jekyll/site_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module JAPR
# Contains overrides for the needed Jekyll:Site methods
# Included in Jekyll::Site
module JekyllSiteExtensions
def self.included(base)
base.class_eval do
Expand Down
3 changes: 3 additions & 0 deletions lib/japr/extensions/liquid/asset_tag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module JAPR
# This is a Liquid tag block extension
# See documentation here:
# https://github.com/Shopify/liquid/wiki/liquid-for-programmers#create-your-own-tag-blocks
class AssetTag < ::Liquid::Block
extend JAPR::LiquidBlockExtensions::ClassMethods
include JAPR::LiquidBlockExtensions
Expand Down
4 changes: 4 additions & 0 deletions lib/japr/extensions/liquid/asset_tags/css_asset_tag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# This comment is needed, otherwise Rubocop complains because of the
# register_tag below and a verbose comment is better than a :nodoc: :)
module JAPR
# css_asset_tag Liquid block
# See JAPR::AssetTag and JAPR::LiquidBlockExtensions
class CssAssetTag < JAPR::AssetTag
def self.tag_name
'css_asset_tag'
Expand Down
4 changes: 4 additions & 0 deletions lib/japr/extensions/liquid/asset_tags/javascript_asset_tag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# This comment is needed, otherwise Rubocop complains because of the
# register_tag below and a verbose comment is better than a :nodoc: :)
module JAPR
# javascript_asset_tag Liquid block
# See JAPR::AssetTag and JAPR::LiquidBlockExtensions
class JavaScriptAssetTag < JAPR::AssetTag
def self.tag_name
'javascript_asset_tag'
Expand Down
5 changes: 5 additions & 0 deletions lib/japr/extensions/liquid/liquid_block_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module JAPR
# Helper module used by JAPR::AssetTag as well as
# classed derived from it (Liquid tag block extensions)
# See documentation here:
# https://github.com/Shopify/liquid/wiki/liquid-for-programmers#create-your-own-tag-blocks
module LiquidBlockExtensions
# Unsurprisingly, class methods
module ClassMethods
def output_type
''
Expand Down
1 change: 1 addition & 0 deletions lib/japr/extensions/ruby/subclass_tracking.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module JAPR
# Allows classes that extend this to return an array of their subclasses
module SubclassTracking
# Record subclasses of this class (this method is automatically called by
# ruby)
Expand Down
50 changes: 27 additions & 23 deletions lib/japr/pipeline.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module JAPR
# The pipeline itself, the run method is where it all happens
# rubocop:disable ClassLength
class Pipeline
class << self
Expand All @@ -9,14 +10,19 @@ def hash(source, manifest, options = {})
Digest::MD5.hexdigest(YAML.safe_load(manifest).map! do |path|
"#{path}#{File.mtime(File.join(source, path)).to_i}"
end.join.concat(options.to_s))
rescue Exception => e
puts "Failed to generate hash from provided manifest: #{e.message}"
raise e
rescue StandardError => se
puts "Failed to generate hash from provided manifest: #{se.message}"
raise se
end
end

# Run pipeline
# Run the pipeline
# This is called from JAPR::LiquidBlockExtensions.render or,
# to be more precise, from JAPR::CssAssetTag.render and
# JAPR::JavaScriptAssetTag.render
# rubocop:disable ParameterLists
def run(manifest, prefix, source, destination, tag, type, config)
# rubocop:enable ParameterLists
# Get hash for pipeline
hash = hash(source, manifest, config)

Expand All @@ -27,12 +33,12 @@ def run(manifest, prefix, source, destination, tag, type, config)
puts "Processing '#{tag}' manifest '#{prefix}'"
pipeline = new(manifest, prefix, source, destination, type, config)
process_pipeline(hash, pipeline)
rescue Exception => e
rescue StandardError => se
# Add exception to cache
cache[hash] = e
cache[hash] = se

# Re-raise the exception
raise e
raise se
end
end

Expand All @@ -51,10 +57,6 @@ def remove_staged_assets(source, config)
config = DEFAULTS.merge(config)
staging_path = File.join(source, config['staging_path'])
FileUtils.rm_rf(staging_path)
rescue Exception => e
puts "Failed to remove staged assets: #{e.message}"
# Re-raise the exception
raise e
end

# Add prefix to output
Expand All @@ -79,7 +81,9 @@ def process_pipeline(hash, pipeline)
end

# Initialize new pipeline
# rubocop:disable ParameterLists
def initialize(manifest, prefix, source, destination, type, options = {})
# rubocop:enable ParameterLists
@manifest = manifest
@prefix = prefix
@source = source
Expand Down Expand Up @@ -114,10 +118,10 @@ def collect
File.dirname(full_path))
end
end
rescue Exception => e
rescue StandardError => se
puts 'Asset Pipeline: Failed to load assets from provided ' \
"manifest: #{e.message}"
raise e
"manifest: #{se.message}"
raise se
end

# Convert assets based on the file extension if converter is defined
Expand Down Expand Up @@ -152,10 +156,10 @@ def convert_asset(klass, asset)
if File.extname(asset.filename) == ''
asset.filename = "#{asset.filename}#{@type}"
end
rescue Exception => e
rescue StandardError => se
puts "Asset Pipeline: Failed to convert '#{asset.filename}' " \
"with '#{klass}': #{e.message}"
raise e
"with '#{klass}': #{se.message}"
raise se
end

# Bundle multiple assets into a single asset
Expand All @@ -178,10 +182,10 @@ def compress

begin
asset.content = klass.new(asset.content).compressed
rescue Exception => e
rescue StandardError => se
puts "Asset Pipeline: Failed to compress '#{asset.filename}' " \
"with '#{klass}': #{e.message}"
raise e
"with '#{klass}': #{se.message}"
raise se
end
end
end
Expand Down Expand Up @@ -219,10 +223,10 @@ def write_asset_file(directory, asset)
File.open(File.join(directory, asset.filename), 'w') do |file|
file.write(asset.content)
end
rescue Exception => e
rescue StandardError => se
puts "Asset Pipeline: Failed to save '#{asset.filename}' to " \
"disk: #{e.message}"
raise e
"disk: #{se.message}"
raise se
end
end

Expand Down
3 changes: 3 additions & 0 deletions lib/japr/template.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module JAPR
# Base class for the tag templates
# See https://github.com/janosrusiczki/japr#templates
class Template
include JAPR::TemplateHelper
extend JAPR::SubclassTracking

def initialize(path, filename)
Expand Down
2 changes: 0 additions & 2 deletions lib/japr/templates/css_tag_template.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module JAPR
# Default output for CSS assets
class CssTagTemplate < JAPR::Template
include JAPR::TemplateHelper

def self.filetype
'.css'
end
Expand Down
2 changes: 0 additions & 2 deletions lib/japr/templates/javascript_tag_template.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module JAPR
# Default output for JavaScript assets
class JavaScriptTagTemplate < JAPR::Template
include JAPR::TemplateHelper

def self.filetype
'.js'
end
Expand Down
1 change: 1 addition & 0 deletions lib/japr/templates/template_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module JAPR
# Contains helper methods used by the tag template classes
module TemplateHelper
def output_path
root_path? ? '' : "/#{@path}"
Expand Down

0 comments on commit e66be3e

Please sign in to comment.