Class: Info

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

Overview

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

Defined Under Namespace

Classes: ReRaiseException, YamlFileDoesntExist

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInfo

Returns a new instance of Info.



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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/atk/info.rb', line 115

def initialize()
    # 
    # find the yaml file
    # 
    begin
        @path = Info.path
        @folder = FS.dirname(@path)
    rescue
        raise YamlFileDoesntExist, <<-HEREDOC.remove_indent
            
            
            When calling Info.new
            I looked for an info.yaml in #{Dir.pwd}
            (and I also looked in all of its parent folders)
            but I couldn't find an info.yaml file in any of them
        HEREDOC
    end
    # 
    # open the yaml file
    # 
    begin
        @data = YAML.load_file(@path)
    rescue => exception
        raise <<-HEREDOC.remove_indent
            
            When calling Info.new
            I found an info.yaml file
            however, when I tried to load it I received an error:
            #{exception}
        HEREDOC
    end
    # 
    # check the version, and parse accordingly
    # 
    @version = nil
    begin
        @version = Version.new(@data['(using_atk_version)'].to_s)
    rescue => exception
        # if no version, then don't worry about parsing
    end

    if @version.is_a?(Version)
        begin
            if @version <= Version.new("1.0.0")
                raise ReRaiseException, <<-HEREDOC.remove_indent
                    
                    So it looks like you're info.yaml file version is 1.0
                    That was the alpha version of ATK, which is the only version that will not be supported
                    
                    #{"This is likely just an accident and the only thing that needs to be done is change:".color_as :message}
                        (using_atk_version): 1.0
                    #{"to:".color_as :message}
                        (using_atk_version): 1.1.0
                HEREDOC
            elsif @version <= Version.new("1.1.0")
                self.parser_version_1_1(@data)
            else
                # FUTURE: in the future do an online check to see if the latest ATK could handle this
                raise ReRaiseException, <<-HEREDOC.remove_indent
                    
                    Hey I think you need to update atk:
                        `atk update`
                    Why?
                        The (using_atk_version) in the info.yaml is: #{@version}
                        However, I (your current version of ATK) don't know
                        how to handle that version.
                HEREDOC
            end
        rescue ReRaiseException => exception
            raise exception
        rescue => exception
            raise <<-HEREDOC.remove_indent
                
                Original error message:
                """
                #{exception}
                """
                
                This error is almost certainly not your fault
                It is likely a bug inside the atk_toolbox library
                Specifically the info.yaml parser
                
                If you agree and think this is a problem with the atk_toolbox library
                please go here:
                https://github.com/aggie-tool-kit/atk-toolbox/issues/new
                and tell us what happened before getting this message
                and also paste the original error message
                
                Sorry for the bug!
            HEREDOC
        end
    end
end

Class Method Details

.[](element) ⇒ Object



311
312
313
# File 'lib/atk/info.rb', line 311

def self.[](element)
    return (Info.new)[element]
end

.commandsObject



327
328
329
# File 'lib/atk/info.rb', line 327

def self.commands
    return (Info.new).commands
end

.dataObject



319
320
321
# File 'lib/atk/info.rb', line 319

def self.data
    return (Info.new).data
end

.folderObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/atk/info.rb', line 212

def self.folder()
    folder = FileSystem.join(Dir.pwd, "") # join with nothing to fix windows pathing
    loop do
        # if the info.yaml exists in that folder
        if FileSystem.file?( FileSystem.join(folder, "info.yaml"))
            return folder
        end

        next_location = File.dirname(folder)
        
        # if all folders exhausted
        if next_location == folder
            raise <<-HEREDOC.remove_indent
                
                #{"Couldn't find an info.yaml in the current directory or any parent directory".color_as(:bad)}
                    #{Dir.pwd}
                Are you sure you're running the command from the correct directory?
            HEREDOC
        end
        
        folder = next_location
    end
end

.initObject



294
295
296
297
298
299
300
301
302
303
304
# File 'lib/atk/info.rb', line 294

def self.init
    current_dir = Dir.pwd/"info.yaml"
    # if there isn't a info.yaml then create one
    if not File.file?(current_dir)
        # copy the default yaml to the current dir
        FileUtils.cp(__dir__/"default_info.yaml", current_dir)
        puts "info.yaml created successfully"
    else
        puts "There appears to already be an info.yaml file\nThe init method is not yet able to merge the ATK init data with the current data\n(this will be fixed in the future)"
    end
end

.pathObject



239
240
241
# File 'lib/atk/info.rb', line 239

def self.path()
    return FileSystem.join( self.folder(), "info.yaml")
end

.pathsObject



335
336
337
# File 'lib/atk/info.rb', line 335

def self.paths
    return (Info.new).paths
end

.versionObject



246
247
248
# File 'lib/atk/info.rb', line 246

def self.version()
    return (Info.new).version
end

Instance Method Details

#[](element) ⇒ Object

read access



307
308
309
# File 'lib/atk/info.rb', line 307

def [](element)
    return @data[element]
end

#commandsObject

command access



324
325
326
# File 'lib/atk/info.rb', line 324

def commands
    return @commands || {}
end

#dataObject

data access



316
317
318
# File 'lib/atk/info.rb', line 316

def data
    return @data
end

#folderObject



209
210
211
# File 'lib/atk/info.rb', line 209

def folder()
    return @folder
end

#parser_version_1_1(data) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/atk/info.rb', line 250

def parser_version_1_1(data)
    # 
    # parse the commands
    #
    begin
        @commands = data['(project)']['(commands)']
        if !(@commands.is_a?(Hash))
            @commands = {}
        end
    rescue
        @commands = {}
    end
    # 
    # parse the paths
    # 
    begin
        @paths = data['(project)']['(paths)']
        if !(@paths.is_a?(Hash))
            @paths = {}
        end
    rescue
        @paths = {}
    end
    @root = FileSystem.dirname(@path)
    for each_key, each_value in @paths
        # if its an array, just join it together
        if each_value.is_a?(Array)
            each_value = FileSystem.join(*each_value)
        end
        # make all paths absolute
        if each_value.is_a?(String)
            # remove the ./ if it exists
            if each_value =~ /\A\.\//
                each_value = each_value[2..-1]
            end
            # Dont add a source_path if its an absolute path
            if not each_value.size > 0 && each_value[0] == '/'
                # convert the path into an absolute path
                @paths[each_key] = FileSystem.join(@root, each_value)
            end
        end
    end
end

#pathObject



236
237
238
# File 'lib/atk/info.rb', line 236

def path()
    return @path
end

#pathsObject

path access



332
333
334
# File 'lib/atk/info.rb', line 332

def paths
    return @paths || {}
end

#versionObject



243
244
245
# File 'lib/atk/info.rb', line 243

def version()
    return @version
end