class Maximus::Stylestats

Produce CSS statistics @since 0.1.0

Public Instance Methods

result() click to toggle source

Requires node @see Statistic#initialize

# File lib/maximus/statistics/stylestats.rb, line 8
def result

  return if @settings[:stylestats].blank?

  node_module_exists('stylestats')

  if @path.blank?
    @path = is_rails? ? "#{@config.working_dir}/public/assets/**/*.css" : "#{@config.working_dir}/**/*.css"
  end

  if @path.is_a?(Array)
    css_files = @path
  else
    compile_scss if @settings[:compile_assets]
    css_files = find_css
  end

  css_files.each { |file| stylestats_report(file) }

  destroy_assets if @settings[:compile_assets]
  @output

end

Private Instance Methods

compile_scss() click to toggle source

Find all CSS files or compile.

Uses sprockets if Rails; Sass engine otherwise. Compass is supported @return [#compile_scss_rails, compile_scss, Array] CSS files

# File lib/maximus/statistics/stylestats.rb, line 40
def compile_scss
  puts "\nCompiling assets for stylestats...".color(:blue)
  if is_rails?

    # Get rake tasks
    Rails.application.load_tasks unless @config.is_dev?
    compile_scss_rails
  else

    load_compass

    compile_scss
  end
end
compile_scss_normal() click to toggle source

Turn scss files into css files Skips if the file starts with an underscore @see find_css_files @since 0.1.5

# File lib/maximus/statistics/stylestats.rb, line 98
def compile_scss_normal
  Dir["#{@path}.scss"].select { |f| File.file? f }.each do |file|
    next if File.basename(file).chr == '_'
    scss_file = File.open(file, 'rb') { |f| f.read }

    output_file = File.open( file.split('.').reverse.drop(1).reverse.join('.'), "w" )
    output_file << Sass::Engine.new(scss_file, { syntax: :scss, quiet: true, style: :compressed }).render
    output_file.close
  end
end
compile_scss_rails() click to toggle source

Turns scss files into css files with the asset pipeline @see find_css_files @since 0.1.5 @return [Array] compiled css files

# File lib/maximus/statistics/stylestats.rb, line 83
def compile_scss_rails
  searched_files = []
  # I'd rather Rake::Task but it's not working in different directories
  if @config.is_dev?
     # @todo review that this may not be best practice, but it's really noisy in the console
    quietly { `rake -f #{@config.working_dir}/Rakefile assets:precompile` }
  else
    `rake -f #{@config.working_dir}/Rakefile assets:precompile`
  end
end
destroy_assets() click to toggle source

Remove all assets created @since 0.1.5

# File lib/maximus/statistics/stylestats.rb, line 111
def destroy_assets

  if is_rails?
    # I'd rather Rake::Task but it's not working in different directories
    if @config.is_dev?
      # @todo review that this may not be best practice, but it's really noisy in the console
      quietly { `rake -f #{@config.working_dir}/Rakefile assets:clobber` }
    else
      `rake -f #{@config.working_dir}/Rakefile assets:clobber`
    end
  end

end
find_css(path = @path) click to toggle source

Find all css files @param path [String] globbed file path @return [Array] paths to compiled CSS files

# File lib/maximus/statistics/stylestats.rb, line 128
def find_css(path = @path)
  Dir.glob(path).select { |f| File.file? f }.map { |file| file }
end
load_compass() click to toggle source

Load Compass paths if the gem exists @see find_css_files @since 0.1.5

# File lib/maximus/statistics/stylestats.rb, line 58
def load_compass
  if Gem::Specification::find_all_by_name('compass').any?
    require 'compass'
    Compass.sass_engine_options[:load_paths].each do |path|
      Sass.load_paths << path
    end
  end
end
load_scss_load_paths() click to toggle source

Add directories to load paths @todo This function is here in case older versions of SCSS will need it

because there shouldn't be a need to load paths, but there might be a need
in older versions of SCSS, which should be tested (although the SCSSLint)
dependency may dictate our scss version

@since 0.1.5

# File lib/maximus/statistics/stylestats.rb, line 73
def load_scss_load_paths
  Dir.glob(@path).select { |d| File.directory? d}.each do |directory|
    Sass.load_paths << directory
  end
end
stylestats_report(file) click to toggle source

Present stylestat result of a CSS file Deletes file at end @since 0.1.6 @see result @param file [String] path to file

# File lib/maximus/statistics/stylestats.rb, line 137
def stylestats_report(file)
  # For Rails, we only want the name of the compiled asset, because we know it'll live in public/assets.
  #   If this isn't Rails, sure, give me the full path because the directory structure is likely unique
  pretty_name = is_rails? ? file.split('/').pop.gsub(/(-{1}[a-z0-9]{32}*\.{1}){1}/, '.') : file

  puts "#{'stylestats'.color(:green)}: #{pretty_name}\n\n"

  # include JSON formatter unless we're in dev
  stylestats = `stylestats #{file} --config=#{@settings[:stylestats]} #{'--type=json' unless @config.is_dev?}`
  refine(stylestats, pretty_name)

  File.delete(file)
end