Class: Bones::App::FileManager

Inherits:
Object
  • Object
show all
Defined in:
lib/bones/app/file_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ FileManager

Returns a new instance of FileManager.



14
15
16
17
18
19
20
21
# File 'lib/bones/app/file_manager.rb', line 14

def initialize( opts = {} )
  self.source = opts[:source]
  self.destination = opts[:destination]
  self.verbose = opts[:verbose]

  @out = opts[:stdout] || $stdout
  @err = opts[:stderr] || $stderr
end

Instance Attribute Details

#archiveObject

Returns the value of attribute archive.



9
10
11
# File 'lib/bones/app/file_manager.rb', line 9

def archive
  @archive
end

#destinationObject

Returns the value of attribute destination.



9
10
11
# File 'lib/bones/app/file_manager.rb', line 9

def destination
  @destination
end

#sourceObject

Returns the value of attribute source.



9
10
11
# File 'lib/bones/app/file_manager.rb', line 9

def source
  @source
end

#verboseObject Also known as: verbose?

Returns the value of attribute verbose.



9
10
11
# File 'lib/bones/app/file_manager.rb', line 9

def verbose
  @verbose
end

Instance Method Details

#_checkout(repotype) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bones/app/file_manager.rb', line 78

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 "unknown repository type '#{repotype}'"
  end
end

#_cp(file) ⇒ Object

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.



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/bones/app/file_manager.rb', line 158

def _cp( file )
  dir = File.dirname(file)
  dir = (dir == '.' ? destination : File.join(destination, dir))
  dst = File.join(dir,  File.basename(file))
  src = File.join(source, file)

  @out.puts(test(?e, dst) ? "updating #{dst}" : "creating #{dst}") if verbose?
  FileUtils.mkdir_p(dir)
  FileUtils.cp src, dst

  FileUtils.chmod(File.stat(src).mode, dst)
end

#_erb(name) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bones/app/file_manager.rb', line 108

def _erb( name )
  binding = _erb_binding(name)

  Dir.glob(File.join(destination, '**', '*')).each do |fn|
    next unless test(?f, fn) and '.bns' == File.extname(fn)

    txt = ERB.new(File.read(fn), nil, '-').result(binding)
    File.open(fn.sub(%r/\.bns$/, ''), 'w') {|fd| fd.write(txt)}
    FileUtils.rm_f(fn)
  end
  self
end

#_erb_binding(name) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/bones/app/file_manager.rb', line 123

def _erb_binding( name )
  obj = Object.new
  class << obj
    alias :__binding__ :binding
    instance_methods.each {|m| undef_method m unless m[%r/^(__|object_id)/]}
    def binding(name)
      classname = name.tr('-','_').split('_').map {|x| x.capitalize}.join
      __binding__
    end
  end
  obj.binding name
end

#_files_to_copyObject

Returns a list of the files to copy from the source directory to the destination directory.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/bones/app/file_manager.rb', line 139

def _files_to_copy
  rgxp = %r/\A#{source}\/?/
  exclude = %r/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) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bones/app/file_manager.rb', line 92

def _rename( filename, name )
  newname = filename.gsub(%r/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_destinationObject



45
46
47
48
49
50
51
52
# File 'lib/bones/app/file_manager.rb', line 45

def archive_destination
  return false unless test(?e, destination)

  @out.puts "archiving #{destination}" if verbose?
  FileUtils.rm_rf(archive)
  FileUtils.mv(destination, archive)
  true
end

#copyObject



56
57
58
59
60
61
62
# File 'lib/bones/app/file_manager.rb', line 56

def copy
  if repository?
    _checkout(repository)
  else
    _files_to_copy.each {|fn| _cp(fn)}
  end
end

#finalize(name) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/bones/app/file_manager.rb', line 66

def finalize( name )
  name = name.to_s
  return if name.empty?

  self.destination = _rename(destination, name)
  _erb(name)

  self
end

#repositoryObject Also known as: repository?

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.



36
37
38
39
40
# File 'lib/bones/app/file_manager.rb', line 36

def repository
  return :git if source =~ %r/\.git\/?$/i
  return :svn if source =~ %r/^(svn(\+ssh)?|https?|file):\/\//i
  nil
end