Class: Bones::App::FileManager
- Includes:
- Colors
- Defined in:
- lib/bones/app/file_manager.rb
Constant Summary collapse
- Error =
Class.new(StandardError)
Constants included from Colors
Instance Attribute Summary collapse
-
#archive ⇒ Object
Returns the value of attribute archive.
-
#destination ⇒ Object
Returns the value of attribute destination.
-
#source ⇒ Object
Returns the value of attribute source.
-
#verbose ⇒ Object
(also: #verbose?)
Returns the value of attribute verbose.
Instance Method Summary collapse
- #_checkout(repotype) ⇒ Object
-
#_cp(file, msg = true) ⇒ Object
Copy a file from the Bones prototype project location to the user specified project location.
- #_erb(name) ⇒ Object
- #_erb_binding(name) ⇒ Object
-
#_files_to_copy ⇒ Object
Returns a list of the files to copy from the source directory to the destination directory.
- #_rename(filename, name) ⇒ Object
- #archive_destination ⇒ Object
- #copy ⇒ Object
-
#initialize(opts = {}) ⇒ FileManager
constructor
A new instance of FileManager.
-
#repository ⇒ Object
(also: #repository?)
If the source is a repository this method returns the type of repository.
-
#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.
Methods included from Colors
Constructor Details
#initialize(opts = {}) ⇒ FileManager
Returns a new instance of 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
#archive ⇒ Object
Returns the value of attribute archive.
11 12 13 |
# File 'lib/bones/app/file_manager.rb', line 11 def archive @archive end |
#destination ⇒ Object
Returns the value of attribute destination.
10 11 12 |
# File 'lib/bones/app/file_manager.rb', line 10 def destination @destination end |
#source ⇒ Object
Returns the value of attribute source.
11 12 13 |
# File 'lib/bones/app/file_manager.rb', line 11 def source @source end |
#verbose ⇒ Object 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), trim_mode: '-').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_copy ⇒ Object
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_destination ⇒ Object
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 |
#copy ⇒ Object
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 |
#repository ⇒ Object 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 |