class Bones::App::FileManager

Constants

Error

Attributes

archive[RW]
destination[R]
source[RW]
verbose[RW]
verbose?[RW]

Public Class Methods

new( opts = {} ) click to toggle source
# 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

_checkout( repotype ) click to toggle source
# 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
_cp( file, msg = true ) click to toggle source

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
_erb( name ) click to toggle source
# 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
_erb_binding( name ) click to toggle source
# 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
_files_to_copy() click to toggle source

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
_rename( filename, name ) click to toggle source
# 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
archive_destination() click to toggle source
# 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
copy() click to toggle source
# 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
destination=( str ) click to toggle source

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
repository() click to toggle source

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
Also aliased as: repository?
repository?()
Alias for: repository
template( name ) click to toggle source

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

archiving( filename ) click to toggle source
# File lib/bones/app/file_manager.rb, line 192
def archiving( filename )
  return unless verbose?
  @put.puts "    #{colorize('archiving', :cyan)} #{filename}"
end
creating( filename ) click to toggle source
# File lib/bones/app/file_manager.rb, line 197
def creating( filename )
  return unless verbose?
  @out.puts "    #{colorize('creating', :green)} #{filename}"
end
updating( filename ) click to toggle source
# File lib/bones/app/file_manager.rb, line 202
def updating( filename )
  return unless verbose?
  @out.puts "    #{colorize('updating', :yellow)} #{filename}"
end