Class: FsTemplate::Files
- Inherits:
-
Object
- Object
- FsTemplate::Files
- Includes:
- FromHash
- Defined in:
- lib/fs_template/files.rb
Class Method Summary collapse
- .dir_files(dir) ⇒ Object
- .load(descriptor) ⇒ Object
- .load_dir(dir) ⇒ 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
36 37 38 39 |
# File 'lib/fs_template/files.rb', line 36 def dir_files(dir) res = Dir["#{dir}/**/*"] + Dir["#{dir}/**/.*"] res - [".","..",".git"] end |
.load(descriptor) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/fs_template/files.rb', line 54 def load(descriptor) raise "bad #{descriptor}" if descriptor.blank? if descriptor =~ /\.git/ load_repo(descriptor) else load_dir(descriptor) end end |
.load_dir(dir) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fs_template/files.rb', line 40 def load_dir(dir) raise "Bad dir" unless dir.present? raise "Dir not there" unless FileTest.exist?(dir) res = new 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
70 71 72 73 74 75 76 |
# File 'lib/fs_template/files.rb', line 70 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
63 64 65 66 67 68 |
# File 'lib/fs_template/files.rb', line 63 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
6 7 8 |
# File 'lib/fs_template/files.rb', line 6 def add(ops) files << file_class.new(:path => ops[:file], :full_body => ops[:body]) end |
#apply(on_top) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/fs_template/files.rb', line 12 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] res << top_file.combined(existing) else res << top_file end end self.class.new(:files => res) end |
#each(&b) ⇒ Object
25 26 27 |
# File 'lib/fs_template/files.rb', line 25 def each(&b) files.each(&b) end |
#size ⇒ Object
9 10 11 |
# File 'lib/fs_template/files.rb', line 9 def size files.size end |
#write_to!(dir) ⇒ Object
29 30 31 32 33 |
# File 'lib/fs_template/files.rb', line 29 def write_to!(dir) each do |f| f.write_to! dir end end |