Module: ACGT

Extended by:
ACGT
Included in:
ACGT
Defined in:
lib/acgt.rb

Constant Summary collapse

VERSION =
"0.0.1".freeze

Instance Method Summary collapse

Instance Method Details

#edit_templateObject



34
35
36
# File 'lib/acgt.rb', line 34

def edit_template
  @options[:edit] = true
end

#get_template_file(template) ⇒ Object



38
39
40
# File 'lib/acgt.rb', line 38

def get_template_file template
  get_template_file_from_cwd(template) || get_template_file_from_home(template)
end

#get_template_file_from(dir, template) ⇒ Object



50
51
52
53
# File 'lib/acgt.rb', line 50

def get_template_file_from dir, template
  file = File.join(File.absolute_path(dir), "#{ template }.mote")
  file if File.exists?(file)
end

#get_template_file_from_cwd(template) ⇒ Object



42
43
44
# File 'lib/acgt.rb', line 42

def get_template_file_from_cwd template
  get_template_file_from File.join(Dir.pwd, ".acgt"), template
end

#get_template_file_from_home(template) ⇒ Object



46
47
48
# File 'lib/acgt.rb', line 46

def get_template_file_from_home template
  get_template_file_from(File.join(ENV["HOME"], ".acgt"), template)
end

#helpObject



10
11
12
13
# File 'lib/acgt.rb', line 10

def help
  readme = File.expand_path("../README", File.dirname(__FILE__))
  exec "${PAGER:-less} #{ readme }"
end

#output(path) ⇒ Object



15
16
17
# File 'lib/acgt.rb', line 15

def output path
  @options[:output] = File.absolute_path(path)
end

#parse(params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/acgt.rb', line 19

def parse params
  {}.tap do |vars|
    params.each do |param|
      name, value = param.split(":", 2)
      name = name.to_sym
      if vars[name]
        vars[name] = [vars[name]] unless vars[name].is_a?(Array)
        vars[name] << value if value && !value.empty?
      else
        vars[name] = value
      end
    end
  end
end

#render(template, params) ⇒ Object



55
56
57
58
# File 'lib/acgt.rb', line 55

def render template, params
  vars = parse params
  Mote.parse(File.read(template), self, vars.keys).call(vars)
end

#run(template, params) ⇒ Object



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
# File 'lib/acgt.rb', line 60

def run template, params
  file = get_template_file template

  if @options[:edit]
    if file.nil?
      file = File.join(ENV["HOME"], ".acgt", "#{template}.mote")
      STDERR.puts "Creating template #{ file }"
    end
    exec "$EDITOR #{ file }"
  end

  if file.nil?
    STDERR.puts "Template #{template} doesn't exists in #{Dir.pwd}/.acgt or ~/.acgt."
    exit 1
  end

  dna = render file, params

  if @options[:output]
    output = File.absolute_path(@options[:output])
    STDOUT.puts "Generating #{output}"
    File.open(output, "w") do |fp|
      fp.write dna
    end
  else
    STDOUT.puts dna
  end
end