Created
October 4, 2014 04:53
-
-
Save HashNuke/98b3fb74b293cd37fb26 to your computer and use it in GitHub Desktop.
INCOMPLETE - WORK IN PROGRESS. Supposed to be used as Fantastic.Rotors.start(options)
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
defmodule Fantastic.Rotors do | |
import Rotor.BasicRotors | |
import CoffeeRotor | |
import SassRotor | |
# NOT TESTED won't work | |
# TODO digested output has to be stored in the application env, as a map, so that it's accessible in the templates. | |
# so the hash would contain %{"file name" => "digest"} | |
def start(options) do | |
options = determine_options(options) | |
File.mkdir_p!(options[:output_path]) | |
rotor_options = %{manual: true} | |
Rotor.watch :coffeescripts, options.javascript_paths, fn(_changed_files, all_files)-> | |
read_files(all_files) | |
|> coffee | |
|> concat | |
|> digested_output_to(options[:output_path], "js") | |
end, rotor_options | |
Rotor.watch :stylesheets, options.stylesheet_paths, fn(_changed_files, all_files)-> | |
read_files(all_files) | |
|> sass | |
|> concat | |
|> digested_output_to(options[:output_path], "css") | |
end, rotor_options | |
#TODO should output the same file name with digest | |
# Rotor.watch :other_paths, other_asset_paths(options.base_path), fn(_changed_files, all_files)-> | |
# read_files(all_files) | |
# # then copy each file with digest name | |
# end, rotor_options | |
end | |
defp other_asset_paths(base_path) do | |
File.ls!(".") | |
|> Enum.filter(&File.dir?/1) | |
|> Enum.map( fn(dir)-> "#{base_path}/#{dir}/*" end ) | |
end | |
defp determine_options(options) do | |
default_options = %{ | |
base_path: "priv/assets", | |
use_digest: true, | |
output_path: "priv/static/assets" | |
} | |
merge_options(default_options, options) | |
end | |
defp merge_options(options1, options2) do | |
options = Map.merge(options1, options2) | |
Map.put(options, :javascript_paths, ["#{options.base_path}/javascripts"]) | |
|> Map.put(:stylesheet_paths, ["#{options.base_path}/stylesheets"]) | |
end | |
defp digested_output_to(contents, output_dir, extension) do | |
digest = calculate_digest(contents) | |
"#{output_dir}/app-#{digest}.#{extension}" | |
end | |
defp calculate_digest(contents) do | |
:crypto.sha_mac("rotor", contents) |> Base.encode16 |> String.downcase | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment