Last active
June 9, 2020 14:27
-
-
Save edusantana/9aa2be37cf1f3d3cf04158260b5eba82 to your computer and use it in GitHub Desktop.
Create Guardfile from Rake tasks
This file contains 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
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