Skip to content

Instantly share code, notes, and snippets.

@edusantana
Last active June 9, 2020 14:27
Show Gist options
  • Save edusantana/9aa2be37cf1f3d3cf04158260b5eba82 to your computer and use it in GitHub Desktop.
Save edusantana/9aa2be37cf1f3d3cf04158260b5eba82 to your computer and use it in GitHub Desktop.
Create Guardfile from Rake tasks
ERB_TEMPLATE = <<~HEREDOC
guard :shell do
<% all_tasks.each do |t, files| %>
watch(%r{^(<%= files.join('|') %>)$}) do |m|
system("rake <%= t %>")
end
<% end %>
end
HEREDOC
desc "Generates a Guardfile from Rake tasks"
task :guard do
app = Rake::application
app.init
app.load_rakefile
all_tasks = {}
app.tasks.each do |t|
t.sources.each do |src|
if File.file?(src) then
all_tasks[t.name] = [] unless all_tasks[t.name]
all_tasks[t.name] << src
end
end
end
template = ERB.new(ERB_TEMPLATE)
File.write('Guardfile', template.result(binding))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment