Module: Mkfiles

Defined in:
lib/mkfiles.rb,
lib/mkfiles/config.rb,
lib/mkfiles/version.rb,
lib/mkfiles/create_file.rb,
lib/mkfiles/extract_yaml.rb,
lib/mkfiles/create_entries.rb,
lib/mkfiles/absolutize_paths.rb,
lib/mkfiles/create_directory.rb,
lib/mkfiles/generate_from_file.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.absolutize_paths(start_directory, rel_paths = []) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/mkfiles/absolutize_paths.rb', line 3

def self.absolutize_paths(start_directory, rel_paths=[])
  rel_paths = rel_paths.uniq
  start_dir = File.absolute_path start_directory
  if rel_paths.map{|p| p.chomp("/")}.uniq.size != rel_paths.size
    raise ArgumentError.new('file and directory with the same name.')
  end
  abs_paths = []
  rel_paths.each do |path|
    abs_paths << (start_dir + "/" + path)
  end
  return abs_paths
end

.create_directory(abs_path) ⇒ Object



5
6
7
8
9
# File 'lib/mkfiles/create_directory.rb', line 5

def self.create_directory(abs_path)

  FileUtils.mkdir_p abs_path

end

.create_entries(abs_paths) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/mkfiles/create_entries.rb', line 9

def self.create_entries(abs_paths)

  abs_paths.each do |path|
    self.create_file(path) if path[-1] != "/"
    self.create_directory(path.chop) if path[-1] == "/"
  end

end

.create_file(abs_path) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/mkfiles/create_file.rb', line 6

def self.create_file(abs_path)
  
  path = Pathname.new(abs_path)
  FileUtils.mkdir_p path.dirname
  File.open(abs_path, "w") {|file| }

end

.extract_yaml(filename) ⇒ Object



6
7
8
9
10
11
# File 'lib/mkfiles/extract_yaml.rb', line 6

def self.extract_yaml(filename)
  yaml = open(filename) do |file|
    YAML.load file.read
  end
  Config.new(yaml)
end

.generate_from_file(filename) ⇒ Object



7
8
9
10
11
12
# File 'lib/mkfiles/generate_from_file.rb', line 7

def self.generate_from_file(filename)
  conf = self.extract_yaml(filename)
  self.create_entries(
    self.absolutize_paths(conf.start_place, conf.sub_paths)
  )
end