Class: Info

Inherits:
Object
  • Object
show all
Defined in:
lib/atk/yaml_info_parser.rb

Overview

setting/getting values via an object (instead of opening/closing a file)

Constant Summary collapse

@@data =

TODO: figure out a way of adding write access to the info.yaml TODO: if there is no (using_atk_version), then add one with the current version TODO:

if there is no (project) or no (advanced_setup), then assume its not an ATK setup 
(quit somehow depending on how this code was invoked)

TODO:

if there are ()'s in a key inside of the (project)
but the key isn't part of the standard keys
then issue a warning, and give them a suggestion to the next-closest key
nil
@@valid_variables =
['context', 'os']
@@valid_operators =
['is']

Class Method Summary collapse

Class Method Details

.[](element) ⇒ Object

read access to the yaml file



257
258
259
260
# File 'lib/atk/yaml_info_parser.rb', line 257

def self.[](element)
    Info.load_if_needed
    return @@data[element.to_s]
end

.dataObject

accessors



246
# File 'lib/atk/yaml_info_parser.rb', line 246

def self.data()                       Info.load_if_needed; return @@data                        end

.dependenciesObject



249
# File 'lib/atk/yaml_info_parser.rb', line 249

def self.dependencies()               Info.load_if_needed; return @@dependencies                end

.environment_variablesObject



254
# File 'lib/atk/yaml_info_parser.rb', line 254

def self.environment_variables()      Info.load_if_needed; return @@environment_variables       end

.evaluate_when_condition(data) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/atk/yaml_info_parser.rb', line 143

def self.evaluate_when_condition(data)
    case data[:variable]
    when 'context'
        raise "\n\ncontext functionality has not been implemented yet"
    when 'os'
        case data[:operator]
            when 'is'
                return OS.is?(data[:value])
            else
                raise "unknown operator: #{data[:operator]}"
        end
    else
        raise "\n\nunknown variable: #{data[:variable]}"
    end
end

.initObject



100
101
102
103
# File 'lib/atk/yaml_info_parser.rb', line 100

def self.init
    # copy the default yaml to the current dir
    FileUtils.cp(__dir__/"default_info.yaml", Dir.pwd/"info.yaml")
end

.load_from_yamlObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/atk/yaml_info_parser.rb', line 204

def self.load_from_yaml
    # this function sets up:
        # @@data
        # @@project
        # @@settings
        # and also
        # @@dependencies
        # @@project_commands
        # @@structures
        # @@local_package_managers
        # @@put_new_dependencies_under
        # @@environment_variables
    
    # get the local yaml file
    # TODO: have this search the parent dir's to find it 
    begin
        @@data = YAML.load_file(Info.source_path)
    rescue => exception
        puts "Couldn't find an info.yaml file in #{Dir.pwd}"
        exit
    end
    @@project = @@data['(project)']
    if @@project == nil
        # TODO: maybe change this to be optional in the future and have default settings
        raise "\n\nThere is no (project): key in the info.yaml\n(so ATK is unable to parse the project settings)"
    end
    
    @@settings = @@project['(advanced_setup)']
    if @@settings == nil
        # TODO: maybe change this to be optional in the future and have default settings
        raise "\n\nThere is no (advanced_setup): key in the (project) of the info.yaml\n(so ATK is unable to parse the project settings)"
    end
    Info.parse_advanced_setup(@@settings, @@settings)
    @@dependencies               = @@settings['(dependencies)']
    @@project_commands           = @@settings['(project_commands)']
    @@structures                 = @@settings['(structures)']
    @@local_package_managers     = @@settings['(local_package_managers)']
    @@put_new_dependencies_under = @@settings['(put_new_dependencies_under)']
    @@environment_variables      = @@settings['(environment_variables)']
end

.load_if_neededObject



197
198
199
200
201
202
# File 'lib/atk/yaml_info_parser.rb', line 197

def self.load_if_needed
    # if the data hasn't been loaded then load it first
    if @@data == nil
        Info.load_from_yaml
    end
end

.local_package_managersObject



252
# File 'lib/atk/yaml_info_parser.rb', line 252

def self.local_package_managers()     Info.load_if_needed; return @@local_package_managers      end

.parse_advanced_setup(unchecked_hashmap, current_project_settings) ⇒ Object

def self.parse_language_evaluations(value)

if value.is_a?(Evaluation)
    return Info.parse_language_evaluations(value.run())
elsif value.is_a?(Hash)
    new_hash = {}
    for each_key, each_value in value
        new_hash[each_key] = Info.parse_language_evaluations(each_value)
    end
    return new_hash
elsif value.is_a?(Array)
    new_array = []
    for each in value
        new_array.push(Info.parse_language_evaluations(each))
    end
    return new_array
else
    return value
end

end



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/atk/yaml_info_parser.rb', line 179

def self.parse_advanced_setup(unchecked_hashmap, current_project_settings)
    for each_key, each_value in unchecked_hashmap.clone()
        case
        # check for yaml-when statements
        when each_key =~ /\Awhen\(.*\)\z/ && each_value.is_a?(Hash)
            statement_data = Info.parse_when_statment(each_key)
            # if the when condition was true
            if Info.evaluate_when_condition(statement_data)
                # recursively parse the tree
                Info.parse_advanced_setup(each_value, current_project_settings)
            end
        # transfer all the normal keys to the current_project_settings
        else
            current_project_settings[each_key] = each_value
        end
    end
end

.parse_when_statment(statement) ⇒ Object

TODO: write tests for this function



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
# File 'lib/atk/yaml_info_parser.rb', line 106

def self.parse_when_statment(statement)
    # remove the 'when' and paraentheses
    expression = statement.sub( /when\((.*)\)/, "\\1" )
    # create the pattern for finding variables
    variable_pattern = /\A\s*--(?<variable>#{@@valid_variables.join('|')})\s+(?<operator>#{@@valid_operators.join('|')})\s+(?<value>.+)/
    groups = expression.match(variable_pattern)
    if groups == nil
        raise "\n\nIn the info.yaml file\nI couldn't any of the variables in #{statement}\nMake sure the format is:\n    when( --VARIABLE *space* OPERATOR *space* VALUE )\nvalid variables are: #{valid_variables}\nvalid operators are: #{valid_operators}"
    end
    value = groups['value'].strip
    
    substitutions = ->(value) do
        value.gsub!( /\\'/, "'" )
        value.gsub!( /\\"/, '"' )
        value.gsub!( /\\n/, "\n" )
        value.gsub!( /\\t/, "\t" )
    end
    
    # if it has single quotes
    if value =~ /'.+'/
        value.sub!( /'(.+)'/, "\\1" )
        substitutions[value]
    # if double quotes
    elsif value =~ /".+"/
        value.sub!( /"(.+)"/, "\\1" )
        substitutions[value]
    else
        raise "\n\nIn the info.yaml file\nthe value: #{value} in the statment:\n#{statement}\n\nwas invalid. It probably needs single or double quotes around it"
    end
    
    return {
        variable: groups['variable'],
        operator: groups['operator'],
        value: value
    }
end

.projectObject



247
# File 'lib/atk/yaml_info_parser.rb', line 247

def self.project()                    Info.load_if_needed; return @@project                     end

.project_commandsObject



250
# File 'lib/atk/yaml_info_parser.rb', line 250

def self.project_commands()           Info.load_if_needed; return @@project_commands            end

.put_new_dependencies_underObject



253
# File 'lib/atk/yaml_info_parser.rb', line 253

def self.put_new_dependencies_under() Info.load_if_needed; return @@put_new_dependencies_under  end

.settingsObject



248
# File 'lib/atk/yaml_info_parser.rb', line 248

def self.settings()                   Info.load_if_needed; return @@settings                    end

.source_pathObject



262
263
264
# File 'lib/atk/yaml_info_parser.rb', line 262

def self.source_path()
    return "."/"info.yaml"
end

.structuresObject



251
# File 'lib/atk/yaml_info_parser.rb', line 251

def self.structures()                 Info.load_if_needed; return @@structures                  end