Class: Kwatable::Main

Inherits:
Object
  • Object
show all
Includes:
Assertion
Defined in:
lib/kwatable/main.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertion

assert, assert!, unreachable

Constructor Details

#initialize(argv = ARGV) ⇒ Main

Returns a new instance of Main.



32
33
34
# File 'lib/kwatable/main.rb', line 32

def initialize(argv=ARGV)
  @argv = argv
end

Class Method Details

.main(argv = ARGV) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/kwatable/main.rb', line 123

def self.main(argv=ARGV)
  begin
    main = Main.new(ARGV)
    output = main.execute()
    print output if output
  rescue KwatableError => ex
    $stderr.puts "[ERROR] #{ex.message()}"
  end
end

Instance Method Details

#executeObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kwatable/main.rb', line 36

def execute()
  options, properties, filenames = _parse_options(@argv)

  ## help or version
  if options[?h] || options[?v]
    puts _version() if options[?v]
    return unless options[?h]
    puts _template_info(options)       if  options[?t]
    puts _usage()                      if !options[?t]
    puts _available_template(options)  if !options[?t]
    return
  end

  ## option check
  if dir = options[?d]
    #* key=:dir_notfound  msg="-d %s: directory not found."
    _option_error(:dir_notfound, dir) unless test(?e, dir)
    #* key=:dir_notadir  msg="-d %s: not a directory."
    _option_error(:dir_notadir, dir) unless test(?d, dir)
  end

  ## load and validate datafile
  tabledefs = []
  check_only = options[?c]
  s = '' if check_only
  filenames.each do |filename|
    tabledef, content = _load_yaml(filename, options[?T])
    tabledefs << tabledef
    unless options[?u]
      errors = Kwatable.validate(tabledef, content)
      if !errors || errors.empty?
        s << "#{filename}: valid.\n" if check_only
      else
        s << "#{filename}: INVALID!\n" if check_only
        s2 = errors.sort.collect { |err|
          "%s:%d: %s: %s" % [filename, err.linenum, err.path, err.message]
        }.join("\n")
        if check_only
          s << s2 << "\n"
        else
          #* key=:tabledef_validation_error  msg="schema validation error.\n%s"
          _option_error(:tabledef_validation_error, s2)
        end
      end
    end
  end
  return s if check_only

  ## merge tabledefs
  tabledef = tabledefs.shift
  tabledefs.each do |tdef|
    tabledef = _merge_tabledefs(tabledef, tdef)
  end

  ## manipulation
  manipulator = Manipulator.new()
  manipulator.manipulate(tabledef)
  $stderr.puts tabledef.to_yaml if options[?D]

  ## template filename
  filename = options[?t]
  unless filename
    return nil if options[?D]
    #* key=:template_notspecified  msg="template is not specified."
    _option_error(:template_notspecified)
  end
  template_pathlist = _template_pathlist(options[?I])
  template_filename = _find_template(filename, template_pathlist)

  ## apply template
  hash = { :options=>options, :properties=>properties,
           :template_filename=>template_filename,
           :template_pathlist=>template_pathlist,
         }
  hash[:tables] = tabledef['tables']
  context = _create_context(hash)
  output = Util.eval_template(template_filename, context)
  output_files = context.instance_variable_get("@output_files")
  if output_files
    output_files.each do |filename, content, message_key|
      _write_file(filename, content, message_key, options)
    end
    output = nil
  end
  return output
end