Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
Created June 7, 2012 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixpulvis/2890564 to your computer and use it in GitHub Desktop.
Save nixpulvis/2890564 to your computer and use it in GitHub Desktop.
Dynamic content require (for interpreted languages)
# returns an array of files that are required by the given file
def get_requirements( file )
requirements = Array.new
File.readlines("#{file}.rb").each do |line|
regex = /(?<=require.')(.*)(?=';)/ # matches the requirements in a file
requirement = line.match(regex)
if requirement
requirements << requirement.to_s
end
end
return requirements
end
# requires all the files in the given directory
def require_all( dir )
@dir = dir
@to_require = Dir.entries @dir
@to_require.delete '.' and @to_require.delete '..' # remove unneeded dirs
@to_require.map! { |e| e.sub(/.rb$/, '') }
# loads file {name from array} and all requirements recursively
def bubble_load( root_file )
puts "LOADING: #{root_file}"
# load requirments
get_requirements("#{@dir}/#{root_file}").each do |file|
if @to_require.include? "#{file}"
bubble_load "#{file}"
end
end
# load file
require_relative "#{@dir}/#{root_file}"
@to_require.delete "#{root_file}"
end
# requires (via bubble) first file in @to_require untill to_require is empty
until @to_require.empty? do
puts "\033[31m"+"STARTING LOAD @: #{@to_require.first}"+"\033[0m"
bubble_load @to_require.first
end
end
require_all 'protogen'
@nixpulvis
Copy link
Author

This is generally a awful way to go about doing things. However it's kinda cool :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment