Skip to content

Instantly share code, notes, and snippets.

@newfront
Created February 17, 2012 04:46
Show Gist options
  • Save newfront/1850753 to your computer and use it in GitHub Desktop.
Save newfront/1850753 to your computer and use it in GitHub Desktop.
Ruby Helper Module for Automatic creating directories recursively from a path
#!/usr/bin/env ruby
#require 'pp'
# File and Directory Writer Module
# (Note) Paths Regexp is intended for Mac OSX
module Helper
module Directory
# check if a directory is a directory ( use internal methods)
def self.is_dir?(path)
path = path.to_s unless path.is_a? String
is_dir = false
unless !File.directory?(path)
is_dir = true
end
return is_dir
end
# write a new directory if and only if the directory hasn't been built
# Allow
# writing a full directory tree ( break the path based on /, ensure that each piece of the
# directory chain exists )
def self.write(path,&block)
# check that the path terminates without a forward slash
# check if the path is a compound path
# if compound path, check if the full path exists
# if not, check if the full path minus one exists
# (ex) /Users/name/Desktop/cool_files/all
error = false
unless path.is_a? String
path = path.to_s
end
path_parts = path.gsub(/^\//,'').gsub(/\//,',').split(/\,/)
#pp path_parts
# check if path_parts[0..path_parts.size-1].join("/") is a directory
# all, cool_writer, Desktop, shaines1, Users
for @j in 0..path_parts.size-1 do
#puts path_parts[0..path_parts.size-@j].to_s
# more overhead, checks from the base of the tree
#puts path_parts[0..@j].to_s
tmp_path = "/"+path_parts[0..@j].join("/").to_s
unless !self.is_dir?(tmp_path)
puts "is a directory: #{tmp_path}... #{path}"
else
puts "isn't a directory: #{tmp_path}"
# make a directory
unless !Dir.mkdir(tmp_path)
puts "directory written: #{tmp_path}...#{path}"
else
error = true
end
end
end
# cleanup, and return true or call Proc
unless error
unless !block_given?
block.call(true)
else
return true
end
else
unless !block_given?
block.call(false)
else
return false
end
end
end
end
module FileReader
# open up a file for reading
# read the contents into memory or chunks
# pass read value through block (optional) for use elsewhere
def self.read(path,type="r",&block)
unless type == "rb"
rtype = "r"
else
rtype = "rb"
end
contents = ''
File.open(path,rtype) do |f|
unless !block_given?
block.call(f)
end
f.each_line{|line|
contents << line
}
return contents
end
puts contents.inspect
end
end
end
#!/usr/bin/env ruby
$: << File.join(File.dirname(__FILE__), "")
$: << $APP_ROOT = File.expand_path(File.dirname(__FILE__))
require $APP_ROOT+"/helper/directory"
@path = File.join($APP_ROOT, '/libs/modules/protected')
Helper::Directory.write(@path) do |result|
unless !result do
# be creative, now that you have written your directory, write some files to it
contents = ''
File.open('/some/template/file/', r) do |f|
f.each_line{|line|
#do something fancy from some sort of config or something with each line of the template,
# it is easy to do a string replace with known keys: ex: {SITE_NAME} is committed to {SITE_MISSION}
# line.gsub(/{SITE_NAME}/,"Posterous") unless !line.match(/{SITE_NAME}/)
# line.gsub(/{SITE_MISSION}/,"bringing you a simple blogging experience") unless !line.match(/{SITE_MISSION}/)
contents << line
}
File.open(File.join(@path,'/welcome.txt'),'w') {|f| f.write(contents)}
end
end
return false
end
#!/usr/bin/env ruby
$: << File.join(File.dirname(__FILE__), "")
$: << $APP_ROOT = File.expand_path(File.dirname(__FILE__))
require $APP_ROOT+"/helper/directory"
@path = File.join($APP_ROOT, '/libs/modules/protected')
Helper::Directory.write(@path) do |result|
unless !result do
# be creative, now that you have written your directory, write some files to it
contents = ''
File.open('/some/template/file/', r) do |f|
f.each_line{|line|
#do something fancy from some sort of config or something with each line of the template,
# it is easy to do a string replace with known keys: ex: {SITE_NAME} is committed to {SITE_MISSION}
# line.gsub(/{SITE_NAME}/,"Posterous") unless !line.match(/{SITE_NAME}/)
# line.gsub(/{SITE_MISSION}/,"bringing you a simple blogging experience") unless !line.match(/{SITE_MISSION}/)
contents << line
}
File.open(File.join(@path,'/welcome.part.html'),'w') {|f| f.write(contents)}
end
end
return false
end
<header>
<h3>{SITE_NAME} is committed to {SITE_MISSION}</h3>
</header>
<section>
<div id="message">
{MESSAGE}
</div>
</section>
<footer>
<div>{FOOTER}</div>
</footer>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment