class Bones::App::FileManager
Constants
- Error
Attributes
Public Class Methods
# File lib/bones/app/file_manager.rb, line 16 def initialize( opts = {} ) self.source = opts[:source] self.destination = opts[:destination] self.verbose = opts[:verbose] @out = opts[:stdout] || $stdout @err = opts[:stderr] || $stderr end
Public Instance Methods
# File lib/bones/app/file_manager.rb, line 89 def _checkout( repotype ) case repotype when :git system('git', 'clone', source, destination) FileUtils.rm_rf(File.join(destination, '.git')) when :svn system('svn', 'export', source, destination) else raise Error, "Unknown repository type '#{repotype}'." end end
Copy a file from the Bones prototype project location to the user specified project location. A message will be displayed to the screen indicating that the file is being created.
# File lib/bones/app/file_manager.rb, line 176 def _cp( file, msg = true ) dir = File.dirname(file) dir = (dir == '.' ? destination : File.join(destination, dir)) dst = File.join(dir, File.basename(file)) src = File.join(source, file) (test(?e, dst) ? updating(dst) : creating(dst)) if msg FileUtils.mkdir_p(dir) FileUtils.cp src, dst FileUtils.chmod(File.stat(src).mode, dst) end
# File lib/bones/app/file_manager.rb, line 119 def _erb( name ) binding = _erb_binding(name) Dir.glob(File.join(destination, '**', '*'), File::FNM_DOTMATCH).each do |fn| next unless test(?f, fn) if File.extname(fn) != '.bns' creating(fn) next end new_fn = fn.sub(/\.bns$/, '') creating(new_fn) txt = ERB.new(File.read(fn), nil, '-').result(binding) File.open(new_fn, 'w') {|fd| fd.write(txt)} FileUtils.chmod(File.stat(fn).mode, new_fn) FileUtils.rm_f(fn) end self end
# File lib/bones/app/file_manager.rb, line 141 def _erb_binding( name ) obj = Object.new class << obj alias :__binding__ :binding instance_methods.each {|m| undef_method m unless m[/^(__|object_id)/]} def binding(name) classname = name.tr('-','_').split('_').map {|x| x.capitalize}.join __binding__ end end obj.binding name end
Returns a list of the files to copy from the source directory to the destination directory.
# File lib/bones/app/file_manager.rb, line 157 def _files_to_copy rgxp = /\A#{source}\/?/ exclude = /tmp$|bak$|~$|CVS|\.svn/ ary = Dir.glob(File.join(source, '**', '*'), File::FNM_DOTMATCH).map do |filename| next if exclude =~ filename next if test(?d, filename) filename.sub rgxp, '' end ary.compact! ary.sort! ary end
# File lib/bones/app/file_manager.rb, line 103 def _rename( filename, name ) newname = filename.gsub(/NAME/, name) if filename != newname raise "cannot rename '#{filename}' to '#{newname}' - file already exists" if test(?e, newname) FileUtils.mv(filename, newname) end if test(?d, newname) Dir.glob(File.join(newname, '*')).each {|fn| _rename(fn, name)} end newname end
# File lib/bones/app/file_manager.rb, line 47 def archive_destination return false unless test(?e, destination) archiving(destination) FileUtils.rm_rf(archive) FileUtils.mv(destination, archive) true end
# File lib/bones/app/file_manager.rb, line 58 def copy if repository? _checkout(repository) else _files_to_copy.each {|fn| _cp(fn)} end self end
Sets the destination where files will be copied to. At the same time an archive directory is configured. This is simply the destination directory with a '.archive' extension.
# File lib/bones/app/file_manager.rb, line 29 def destination=( str ) @destination = str @archive = str + '.archive' if str end
If the source is a repository this method returns the type of repository.
This will be :git for Git repositories and :svn for Subversion
repositories. Otherwise, nil
is returned.
# File lib/bones/app/file_manager.rb, line 38 def repository return :git if source =~ /\.git\/?$/i return :svn if source =~ /^(svn(\+ssh)?|https?|file):\/\//i nil end
Gernate a new destination folder by copying files from the source, rename files and directories that contain “NAME”, and perform ERB templating on “.bns” files. The name use used for file/folder renaming and ERB templating.
# File lib/bones/app/file_manager.rb, line 72 def template( name ) name = name.to_s return if name.empty? if repository? _checkout(repository) else _files_to_copy.each {|fn| _cp(fn, false)} end self.destination = _rename(destination, name) _erb(name) self end
Private Instance Methods
# File lib/bones/app/file_manager.rb, line 192 def archiving( filename ) return unless verbose? @put.puts " #{colorize('archiving', :cyan)} #{filename}" end
# File lib/bones/app/file_manager.rb, line 197 def creating( filename ) return unless verbose? @out.puts " #{colorize('creating', :green)} #{filename}" end
# File lib/bones/app/file_manager.rb, line 202 def updating( filename ) return unless verbose? @out.puts " #{colorize('updating', :yellow)} #{filename}" end