Class: Overapp::Files
- Inherits:
-
Object
- Object
- Overapp::Files
- Includes:
- Enumerable, FromHash
- Defined in:
- lib/overapp/files.rb
Class Method Summary collapse
- .dir_files(dir) ⇒ Object
- .load(descriptor, ops = {}) ⇒ Object
- .load_command(cmd, ops) ⇒ Object
- .load_dir(dir, ops = {}) ⇒ Object
- .load_repo(url) ⇒ Object
- .write_combined(base_dir, top_dir, output_dir) ⇒ Object
Instance Method Summary collapse
- #add(ops) ⇒ Object
- #apply(on_top) ⇒ Object
- #each(&b) ⇒ Object
- #size ⇒ Object
- #write_to!(dir) ⇒ Object
Class Method Details
.dir_files(dir) ⇒ Object
40 41 42 43 |
# File 'lib/overapp/files.rb', line 40 def dir_files(dir) res = Dir["#{dir}/**/*"] + Dir["#{dir}/**/.*"] res - [".","..",".git"] end |
.load(descriptor, ops = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/overapp/files.rb', line 63 def load(descriptor, ops={}) raise "bad #{descriptor}" if descriptor.blank? if ops[:type] == :command load_command(descriptor,ops) elsif descriptor =~ /\.git/ load_repo(descriptor) else load_dir(descriptor,ops) end end |
.load_command(cmd, ops) ⇒ Object
59 60 61 |
# File 'lib/overapp/files.rb', line 59 def load_command(cmd,ops) FromCommand.new(:command => cmd, :path => ops[:path]||".").files end |
.load_dir(dir, ops = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/overapp/files.rb', line 44 def load_dir(dir,ops={}) raise "Bad dir" unless dir.present? raise "Dir not there #{dir}" unless FileTest.exist?(dir) res = new res.file_class = ops[:file_class] if ops[:file_class] dir_files(dir).each do |full_file| if FileTest.file?(full_file) f = full_file.gsub("#{dir}/","") raise "bad #{f}" if f == full_file res.add :file => f, :body => File.read(full_file) end end res end |
.load_repo(url) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/overapp/files.rb', line 81 def load_repo(url) dir = "/tmp/#{rand(1000000000000000000)}" ec "git clone #{url} #{dir} 2>&1", :silent => true load dir ensure ec "rm -rf #{dir}", :silent => true end |
.write_combined(base_dir, top_dir, output_dir) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/overapp/files.rb', line 74 def write_combined(base_dir, top_dir, output_dir) base = load(base_dir) top = load(top_dir) combined = base.apply(top) combined.write_to! output_dir end |
Instance Method Details
#add(ops) ⇒ Object
7 8 9 |
# File 'lib/overapp/files.rb', line 7 def add(ops) files << file_class.new(:path => ops[:file], :full_body => ops[:body]) end |
#apply(on_top) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/overapp/files.rb', line 13 def apply(on_top) res = files.clone on_top.each do |top_file| existing = res.find { |x| x.path == top_file.path } if existing res -= [existing] new_file = top_file.combined(existing) res << new_file if new_file elsif top_file.has_note? raise "cannot overlay onto missing file #{top_file.path}" else res << top_file end end self.class.new(:files => res) end |
#each(&b) ⇒ Object
29 30 31 |
# File 'lib/overapp/files.rb', line 29 def each(&b) files.each(&b) end |
#size ⇒ Object
10 11 12 |
# File 'lib/overapp/files.rb', line 10 def size files.size end |
#write_to!(dir) ⇒ Object
33 34 35 36 37 |
# File 'lib/overapp/files.rb', line 33 def write_to!(dir) each do |f| f.write_to! dir end end |