Class: Bones::App::FileManager

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

Constant Summary collapse

Error =
Class.new(StandardError)

Constants included from Colors

Colors::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Colors

#colorize, #colorize?

Constructor Details

#initialize(opts = {}) ⇒ FileManager



16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#archiveObject

Returns the value of attribute archive



11
12
13
# File 'lib/bones/app/file_manager.rb', line 11

def archive
  @archive
end

#destinationObject

Returns the value of attribute destination



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

def destination
  @destination
end

#sourceObject

Returns the value of attribute source



11
12
13
# File 'lib/bones/app/file_manager.rb', line 11

def source
  @source
end

#verboseObject Also known as: verbose?

Returns the value of attribute verbose



11
12
13
# File 'lib/bones/app/file_manager.rb', line 11

def verbose
  @verbose
end

Instance Method Details

#_checkout(repotype) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ 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.



176
177
178
179
180
181
182
183
184
185
186
187
188
# 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) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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(%r/\.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) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# 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[%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.



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

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



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bones/app/file_manager.rb', line 103

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



47
48
49
50
51
52
53
54
# 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

#copyObject



58
59
60
61
62
63
64
65
# 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

#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.



38
39
40
41
42
# File 'lib/bones/app/file_manager.rb', line 38

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

#template(name) ⇒ Object

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.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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