Skip to content

Instantly share code, notes, and snippets.

@afiore
Last active December 14, 2015 10:59
Show Gist options
  • Save afiore/5076167 to your computer and use it in GitHub Desktop.
Save afiore/5076167 to your computer and use it in GitHub Desktop.
Some overlooked features of Rake, Ruby's build tool
require 'yaml'
require 'json'
require 'logger'
# Some useful, and often overlooked features of Rake, Ruby's build tool.
directory "logs"
desc "Log tasks execution to a file"
task "log" => ['logs'] do
MyLogger.instance
end
directory "foo/bar/baz"
desc "Write stuff"
file "foo/bar/baz/stuff.txt" => ['log', 'foo/bar/baz'] do
MyLogger.instance.info "writing stuff.txt"
File.open("foo/bar/baz/stuff.txt", "w") do |f|
f.write("bla bla bla")
end
MyLogger.instance.info "done writing stuff.txt"
end
desc "write more stuff"
file "foo/bar/baz/more_stuff.txt" => ['foo/bar/baz/stuff.txt'] do
File.open("foo/bar/baz/more_stuff.txt", "w") do |f|
f.write("more stuff")
end
MyLogger.instance.info "written even more_stuff.txt"
end
desc "Automagically synthesise a task for converting JSON files into YAML."
directory "yamls"
rule /\.yml$/ => [
proc { |task_name| "json/#{File.basename(task_name, '.yml')}.json" },
'yamls',
'log'
] do |t|
data = JSON.parse(File.read(t.source))
MyLogger.instance.info "writing document #{t.name}"
File.open(t.name, "w+") do |f|
f.write(data.to_yaml)
end
end
class MyLogger < Logger
def self.instance
@logger ||= Logger.new("logs/#{build_id}.log")
end
def self.build_id
@build_id ||= Time.now.to_i
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment