Class: POI::App

Inherits:
Object
  • Object
show all
Includes:
Constants, Utils
Defined in:
lib/poi/app.rb

Constant Summary

Constants included from Constants

Constants::POI_DEFAULTS

Instance Method Summary collapse

Methods included from Utils

#parents

Instance Method Details

#dispatch(options) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/poi/app.rb', line 45

def dispatch(options)
    if (options[:pack])
        pack(options)
    elsif (!options[:file])
        puts @opt.optparser if (!options[:help])
    else
        expand(options)
    end
end

#expand(options) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/poi/app.rb', line 55

def expand(options)
    lines = open(options[:file]).readlines
    data = {};
    x = 0;

    while lines[x].strip != '.' && x < lines.size do
        data = JSON.load(File.read(POI_DEFAULTS)) if File.exists? POI_DEFAULTS
        data.merge!(JSON.load(File.read(".poi_vars"))) if File.exists? ".poi_vars"
        line = lines[x].strip
        if (line =~ /^(.+)\s(\d+)$/)
            file = $1.strip
            size = $2.to_i
            texts = lines[x+1, size]
            parent = parents(file);
            FileUtils.mkdir_p(parent)
            if (options[:delete])
                File.delete(file) if File.exists?(file)
                puts "Deleting #{file} ..."
                while (parent.size > 0 && Dir.open(parent).count <= 2 && parent != ".") do
                    Dir.delete(parent)
                    puts "Deleting #{parent} ..."
                    parent = parents(parent)
                end
            else
                File.open(file, "w") do |f|
                    puts "Generating #{file} ..."
                    f << Mustache.render(texts.join, data)
                end
            end
            x += size
        end
        x += 1
    end
    puts "Done!"
end

#pack(options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/poi/app.rb', line 24

def pack(options)
    target = options[:target] ? File.open(options[:target], "w") : STDOUT
        if (File.exists?(options[:pack]))
            File.readlines(options[:pack]).map {|l| l.strip }.map {|l| Dir.glob(l)}.flatten
                .tap {|fs| fs.insert(0, POI_DEFAULTS) if File.exists?(POI_DEFAULTS) and !fs.include?(POI_DEFAULTS)}
                .select {|l| l != options[:target]}.each do |l|
                    file = l.strip
                    unless File.directory?(file)
                        puts "Packing #{file} ..." unless target == STDOUT
                        content = File.readlines(file) 
                        target.puts("#{file} #{content.size}")
                        target.puts(content.join) if (!content.empty?)
                        target.puts
                    end
                end
        end
    target.print(".")
    puts "Done" unless target == STDOUT
    target.close unless target == STDOUT
end

#run(argv) ⇒ Object



18
19
20
21
22
# File 'lib/poi/app.rb', line 18

def run(argv)
    @opt = POI::Opt.new
    @opt.parse(argv)
    dispatch(@opt.options)
end