Class: TerrImporter::Application::Configuration

Inherits:
Hash
  • Object
show all
Includes:
ConfigurationHelper
Defined in:
lib/terrimporter/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigurationHelper

#config_default_name, #config_example_path, #config_working_directory_exists?, #config_working_directory_path, #create_config_file, #schema_default_name, #schema_file_path

Constructor Details

#initialize(config_file = nil) ⇒ Configuration

Returns a new instance of Configuration.



11
12
13
14
# File 'lib/terrimporter/configuration.rb', line 11

def initialize(config_file = nil)
  self.config_file = config_file unless config_file.nil?

end

Instance Attribute Details

#config_fileObject

Returns the value of attribute config_file.



9
10
11
# File 'lib/terrimporter/configuration.rb', line 9

def config_file
  @config_file
end

#validationsObject

Returns the value of attribute validations.



9
10
11
# File 'lib/terrimporter/configuration.rb', line 9

def validations
  @validations
end

Instance Method Details

#additional_dynamic_javascripts?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/terrimporter/configuration.rb', line 132

def additional_dynamic_javascripts?
  !self['javascripts'].nil? and !self['javascripts']['dynamic_libraries'].nil?
end

#additional_stylesheets?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/terrimporter/configuration.rb', line 128

def additional_stylesheets?
  !self['stylesheets'].nil? and !self['stylesheets']['styles'].nil?
end

#determine_config_file_pathObject

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/terrimporter/configuration.rb', line 22

def determine_config_file_path
  unless self.config_file.nil?
    return self.config_file
  end

  valid_config_paths.each do |path|
    file_path = File.join path, config_default_name
    return file_path if File.exists?(file_path) and not file_path.include?(File.join('terrimporter', 'config')) #default config NOT valid
  end

  raise ConfigurationError, %Q{config file #{config_default_name} not found in search paths. Search paths are:
  #{valid_config_paths.join "\n"} \n If this is a new project, run with the option --init}
end

#determine_configuration_values_from_html(raw_html) ⇒ Object

Raises:



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
# File 'lib/terrimporter/configuration.rb', line 73

def determine_configuration_values_from_html(raw_html)
  css_result, js_result = raw_html.scan(/(\/terrific\/base\/(.*?)\/public\/.*base.(css|js).php)\?.*application=(.*?)(&|&)/)

  raise ConfigurationError, "Unable to extract css information from application url, content is: #{raw_html}" if css_result.nil? or css_result.size < 5
  raise ConfigurationError, "Unable to extract javascript information from application url, content is: #{raw_html}" if js_result.nil? or js_result.size < 5

  css_export_path = css_result[0]
  js_export_path = js_result[0]
  terrific_version = css_result[1]
  application = css_result[3]

  raise ConfigurationError, "Unable to determine css export path" if css_export_path.nil?
  raise ConfigurationError, "Unable to determine js export path " if js_export_path.nil?
  raise ConfigurationError, "Unable to determine terrific version" if terrific_version.nil?
  raise ConfigurationError, "Unable to determine application path" if application.nil?

  LOG.info "Determined the following configuration values from #{self['application_url']}:\n" +
               "terrific version: #{terrific_version} \n" +
               "application path: #{application}"

  self['version'] = terrific_version
  self['export_settings'] ||= {}
  self['export_settings']['application'] = application
  self['export_path'] = {'css' => css_export_path, 'js' => js_export_path}
end

#dynamic_librariesObject



109
110
111
112
# File 'lib/terrimporter/configuration.rb', line 109

def dynamic_libraries
  libraries = self['javascripts']['dynamic_libraries'].robust_split
  libraries.add_if_missing!('.js')
end

#images?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/terrimporter/configuration.rb', line 136

def images?
  !self['images'].nil?
end

#libraries_destination_pathObject



120
121
122
123
124
125
126
# File 'lib/terrimporter/configuration.rb', line 120

def libraries_destination_path
  if !self['javascripts']['libraries_destination_path'].nil?
    File.join(self['javascripts']['libraries_destination_path'])
  else
    File.join(self['javascripts']['destination_path'])
  end
end

#load_configurationObject



16
17
18
19
20
# File 'lib/terrimporter/configuration.rb', line 16

def load_configuration
  config_file_path = determine_config_file_path
  LOG.debug "Configuration file located, load from #{config_file_path}"
  validate_and_load_config(config_file_path)
end

#load_validatorObject



59
60
61
62
63
# File 'lib/terrimporter/configuration.rb', line 59

def load_validator
  LOG.debug "Loading validator from #{schema_file_path}"
  schema = Kwalify::Yaml.load_file(schema_file_path)
  Kwalify::Validator.new(schema)
end

#mandatory_present?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/terrimporter/configuration.rb', line 65

def mandatory_present?
  if self['export_path'].nil? or self['export_settings']['application'].nil? or self['application_url'].nil?
    false
  else
    true
  end
end

#modules?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/terrimporter/configuration.rb', line 140

def modules?
  !self['modules'].nil?
end

#replace_style_strings?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
# File 'lib/terrimporter/configuration.rb', line 114

def replace_style_strings?
  !self['stylesheets'].nil? and
  !self['stylesheets']['replace_strings'].nil? and
  !self['stylesheets']['replace_strings'].first.nil?
end

#stylesheetsObject



99
100
101
102
103
104
105
106
107
# File 'lib/terrimporter/configuration.rb', line 99

def stylesheets
  stylesheets = ["base.css"]
  if additional_stylesheets?
    stylesheets = stylesheets + self['stylesheets']['styles'].to_s.robust_split
  else
    LOG.debug "No additional stylesheets defined."
  end
  stylesheets.add_if_missing!('.css')
end

#valid_config_pathsObject



36
37
38
39
40
41
42
# File 'lib/terrimporter/configuration.rb', line 36

def valid_config_paths
  [
      Dir.pwd,
      File.join(Dir.pwd, 'config'),
      File.join(Dir.pwd, '.config'),
  ]
end

#validate_and_load_config(file) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/terrimporter/configuration.rb', line 44

def validate_and_load_config(file)
  LOG.debug "Validating configuration..."

  parser = Kwalify::Yaml::Parser.new(load_validator)
  document = parser.parse_file(file)

  errors = parser.errors()
  if errors && !errors.empty?
    error_message = errors.inject("") { |result, e| result << "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}\n" }
    raise ConfigurationError, error_message
  end
  self.merge! document

end