Class: Kodekopelli::FileGenerator

Inherits:
Object
  • Object
show all
Includes:
ERB::Util, WEBrick
Defined in:
lib/kodekopelli/file_generator.rb

Overview

The FileGenerator class coordinates the necessary actions to generate output files from one or more definitions files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties_hash = nil) ⇒ FileGenerator

Returns a new FileGenerator.

:call-seq:

FileGenerator.new( properties_hash=nil ) -> file generator


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kodekopelli/file_generator.rb', line 19

def initialize(properties_hash = nil)
  # Stores properties for property and attribute value expansions
  # and system configuration
  @property_hash = Kodekopelli::FrozenKeyHash.new
  
  unless(properties_hash.nil?)
    properties_hash.each_pair { |key,value|
      @property_hash[key] = value
    }
  end
  
  # Tracks attributes defined in the definitions
  # file(s); also provides for nested contexts.
  @context_stack = []

  # Allows for node-specific context creation
  @node_stack = []

  # Context containing those attributes that
  # are relevant to the node currently being
  # processed.
  @current_context = {}
  
  @logger = MinimalLogger.new
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/kodekopelli/file_generator.rb', line 12

def logger
  @logger
end

Instance Method Details

#process(files) ⇒ Object

Processes one or more Kodekopelli definitions files, given an array of file locations.

:call-seq:

process(array)


188
189
190
191
192
193
# File 'lib/kodekopelli/file_generator.rb', line 188

def process(files)
  @logger.raw(Kodekopelli.)
  files.each { |current_file|
    process_definitions(current_file)
  }
end

#run(runtime_args) ⇒ Object

:nodoc:



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/kodekopelli/file_generator.rb', line 68

def run(runtime_args) # :nodoc:
  properties_file = nil
  
  opts = OptionParser.new
  
  opts.banner = "Usage: kodekopelli [options] [configfile [configfile2 [configfile3] ...]]"
  opts.separator ""
  opts.separator "Core Options:"
    
  opts.on("-h", "--help", "print this message") { 
    puts opts.to_s
    return
  }
  
  opts.on("--version", "print the version information and exit") {
	puts "Kodekopelli version #{$KODEKOPELLI_VERSION}"
	return
  }
  
  opts.on("-q", "--quiet", "be extra quiet in log messages") {
    @logger.silent = true 
  }
  
  opts.on("-v", "--verbose", "be extra verbose in log messages") {
    @logger.verbose = true 
  }
  
  opts.on("-l", "--logo", "print the Kodekopelli logo and exit") {
    puts Kodekopelli.
    return
  }
  
  opts.on("--props k1=v1,k2=v2,...", Array,
    "use the property key/value pair(s)", "specified") do |properties|
    properties.each { |current_property|
      first_delim_index = current_property.index('=')
      if(first_delim_index > 0)
        # Don't bother processing properties that don't have
        # delimiters or in which no property name was
        # specified.
        current_property_key = current_property[0,first_delim_index]
        current_property_value = current_property[first_delim_index + 1, current_property.length-1]
        
       @property_hash[current_property_key]= current_property_value
        
      end
     }
  end
  
  opts.on("--propsfile file", "load all properties from file with --props", "taking precedence") do |propsfile|
    properties_file = propsfile
  end
  
  opts.separator ""
  opts.separator "Other Options:"
  
  opts.on("--server [port]", Integer, "launch an HTTP server with the current",
             "folder as the context root using,", "optionally, the port specified",
             "(defaults to port 80)") do |port|
    port_number = port || 80
    
    s = HTTPServer.new(
      :Port         => port_number,
      :DocumentRoot => Dir::pwd
    )

    trap("INT") {
    @logger.info("Shutting down HTTP server listening on port [#{port_number}]...")
    s.shutdown 
    }
    
    @logger.info("Starting HTTP server listening on port [#{port_number}]...")
    @logger.info("Context root location:  [#{Dir::pwd}]")
    s.start

    return
  end
  
  opts.on("--system", "display all system properties available to", "Kodekopelli and exit") {
    puts "*** SYSTEM PROPERTIES (#{Time.now.to_s}) ***"
    puts sprintf("%-30s     %s", "<KEY>", "<VALUE>")
    ENV.to_hash.each_pair {|key,value|
      puts sprintf("%-30s => %s", key, value)
    }
    return
  }
  
  # Perform a destructive parse, leaving only the
  # configuration files, if any were specified.
  begin
    opts.parse!(runtime_args)
  rescue => cl_err
    puts "Unable to process command line options:  #{cl_err.to_s}"
    puts opts.to_s
    exit(1)
  end
  
  unless(properties_file.nil?)
    digest_properties_file(properties_file)
  end
  
  if(runtime_args.size > 0)
    begin
      process(runtime_args)
    rescue => err
      @logger.always("Root cause:  #{err.to_s}")
      @logger.always("Build failed.")
      exit(1)
    end
  else
    puts "No Kodekopelli configuration files specified."
    puts opts.to_s
  end
end