# lib/tasks/dir_1/dir_2/create_files.rb
# ------------
# Tasks here are loaded into the dir_1:dir_2 namespace because it is inside
# the dir_1/dir_2 subdirectory
# ------------
desc "My exampled namespaced task"
task :xtask => :environment do
# task can be executed with `bundle exec rake dir_1:dir_2:xtask`
end
# lib/tasks/load_tasks.rake
# ------------
# Looks at every .rb file in the lib/tasks and adds them to rake within the
# namespace of their subdirectories
# ------------
# Important to note we are looking specifically for .rb files, so they
# aren't added to the global rake namespace
Dir.glob("#{Rails.root}/lib/tasks/**/*.rb").each do |file|
path = file.split('/')
namespaces = path[(path.index('tasks') + 1)..-2]
filename = path.last
if namespaces.size > 0
namespace namespaces[0].to_sym do
if namespaces.size > 1
while namespaces.size > 1
namespaces = namespaces[1..-1]
namespace namespaces[0].to_sym do
load file if namespaces.size > 0
end
end
else
load file
end
end
else
load file
end
end