Module: FileManager

Defined in:
lib/dinner/filemanager.rb

Overview

A module for locating files and creating the build folder

Class Method Summary collapse

Class Method Details

.init_build(html) ⇒ Object

Create the build folder if it does not exist. If it does exist, prunes it of old files. Then it moves in the unprocessed page-files. @TODO: Split this into two functions, one to create the build folder if it doesn’t exist, and one to delete files which will be replaced



35
36
37
38
39
40
41
42
43
44
# File 'lib/dinner/filemanager.rb', line 35

def self.init_build(html)
  if Dir.exists?(ConfigManager.config[:build_folder])
    Find.find("#{Dir.pwd}/#{ConfigManager.config[:build_folder]}") do |path|
      File.delete(path) if File.extname(path) == ".html"
    end
  else
    Dir.mkdir "#{Dir.pwd}/#{ConfigManager.config[:build_folder]}"
  end
  self.push_files(html)
end

.locate_html(with_includes, in_build = false) ⇒ Object

Find the html files and return a hash containing an array of page files (:files) and an array of include files (:includes)



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dinner/filemanager.rb', line 5

def self.locate_html(with_includes,in_build = false)
  html = { :files => {}}
  if with_includes
    html[:includes] = {}
  end
  if in_build
    dir = "#{Dir.pwd}/#{ConfigManager.config[:build_folder]}"
  else
    dir = Dir.pwd
  end
  Find.find(dir) do |path|
    if FileTest.directory?(path)
      if (File.basename(path) == ConfigManager.config[:build_folder]) && !in_build
        Find.prune
      else
        next
      end
    else
      if path =~ /.*\/[^_][^\/]*\.html$/
        html[:files][File.basename(path)] = path
      elsif path =~ /.*\/[_][^\/]*\.html$/ and with_includes
        html[:includes][(File.basename(path)[1..-1])] = path
      end
    end
  end
  return html
end

.push_files(html) ⇒ Object



46
47
48
49
50
# File 'lib/dinner/filemanager.rb', line 46

def self.push_files(html)
  html[:files].each_value do |path|
    FileUtils.cp(path,"#{Dir.pwd}/#{ConfigManager.config[:build_folder]}")
  end
end