Class: FsTemplate::Files

Inherits:
Object
  • Object
show all
Includes:
Enumerable, FromHash
Defined in:
lib/fs_template/files.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dir_files(dir) ⇒ Object



37
38
39
40
# File 'lib/fs_template/files.rb', line 37

def dir_files(dir)
  res = Dir["#{dir}/**/*"] + Dir["#{dir}/**/.*"]
  res - [".","..",".git"]
end

.load(descriptor, ops = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/fs_template/files.rb', line 60

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



56
57
58
# File 'lib/fs_template/files.rb', line 56

def load_command(cmd,ops)
  FromCommand.new(:command => cmd, :path => ops[:path]||".").files
end

.load_dir(dir, ops = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fs_template/files.rb', line 41

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



78
79
80
81
82
83
84
# File 'lib/fs_template/files.rb', line 78

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



71
72
73
74
75
76
# File 'lib/fs_template/files.rb', line 71

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/fs_template/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
# File 'lib/fs_template/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]
      res << top_file.combined(existing)
    else
      res << top_file
    end
  end
  self.class.new(:files => res)
end

#each(&b) ⇒ Object



26
27
28
# File 'lib/fs_template/files.rb', line 26

def each(&b)
  files.each(&b)
end

#sizeObject



10
11
12
# File 'lib/fs_template/files.rb', line 10

def size
  files.size
end

#write_to!(dir) ⇒ Object



30
31
32
33
34
# File 'lib/fs_template/files.rb', line 30

def write_to!(dir)
  each do |f|
    f.write_to! dir
  end
end