Class: AtkPackage

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

Instance Method Summary collapse

Constructor Details

#initialize(package_name) ⇒ AtkPackage

Returns a new instance of AtkPackage.



219
220
221
222
223
# File 'lib/atk/atk_info.rb', line 219

def initialize(package_name)
    @init_name = package_name
    @package_info_loaded = false
    @dont_exist = {}
end

Instance Method Details

#cache_locationObject



264
265
266
267
268
269
# File 'lib/atk/atk_info.rb', line 264

def cache_location()
    if @cache_location == nil
        @cache_location = Atk.paths['repos']/self.cache_name
    end
    return @cache_location
end

#cache_nameObject



253
254
255
256
257
258
259
260
261
262
# File 'lib/atk/atk_info.rb', line 253

def cache_name()
    if @cache_name == nil
        repo_url = self.url()
        require 'digest'
        repo_hash = Digest::MD5.hexdigest(repo_url)
        repo_name = Git.repo_name(repo_url)
        @cache_name = repo_name+"_"+repo_hash
    end
    return @cache_name
end

#ensure_cachedObject



271
272
273
274
275
276
277
278
# File 'lib/atk/atk_info.rb', line 271

def ensure_cached()
    if @is_cached == nil
        puts "self.cache_location is: #{self.cache_location} "
        puts "self.url is: #{self.url} "
        Git.ensure_cloned_and_up_to_date(self.cache_location, self.url)
        @is_cached = true
    end
end

#ensure_package_infoObject

parse package



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/atk/atk_info.rb', line 283

def ensure_package_info()
    if not @package_info_loaded
        @package_info_loaded = true
        self.ensure_cached()
        FS.in_dir(self.cache_location) do
            if not FS.file?("./info.yaml")
                @dont_exist[:yaml_file] = true
            end
            begin
                # must be in top most dir
                @info = YAML.load_file("./info.yaml")
            rescue => exception
                @dont_exist[:correctly_formatted_yaml_file] = true
            end
            
            # attempt to load a version
            begin
                version = Version.new(@info['(using_atk_version)'])
            rescue => exception
                version = nil
                @dont_exist[:using_atk_version] = true
            end
            # if there is a version
            if version.is_a?(Version)
                # if the version is really old
                if version <= Version.new("1.0.0")
                    raise <<-HEREDOC.remove_indent
                        
                        
                        It appears that the #{self.simple_name()} package is using
                        the alpha version of ATK (1.0.0), which is no longer supported.
                        This is probably just a simple versioning mistake.
                        Please ask the maintainer of the #{self.simple_name()} package to
                        update it to a newer ATK package format
                    HEREDOC
                elsif version <= Version.new("1.1.0")
                    self.parser_version_1_1(@info)
                else
                    raise <<-HEREDOC.remove_indent
                        
                        
                        The package #{self.simple_name()} has a (using_atk_version)
                        that is newer than the currently installed ATK can handle:
                        version: #{version}
                        
                        This means either
                            1. ATK needs to be updated (which you can do with #{'atk update'.color_as :code})
                            2. The package has specified a version of ATK that doesn't exist
                    HEREDOC
                end  
            end
        end
    end
end

#parser_version_1_1(info) ⇒ Object

(using_atk_version) 1.1.0

(package):

(actions):
     (run): *run command as string*


344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/atk/atk_info.rb', line 344

def parser_version_1_1(info)
    # 
    # inits:
    #    @package_info: nil
    #    @actions: {}
    #    @run: nil
    # 
    if @info.is_a?(Hash)
        @package_info = @info['(package)']
        if @package_info.is_a?(Hash)
            @actions = @package_info['(actions)']
        else 
            @dont_exist[:package_info] = true
        end
        if not @actions.is_a?(Hash)
            @actions = {}
            @dont_exist[:actions] = true
        end
        
        @run = @actions['(run)']
        if not @run.is_a?(String)
            @dont_exist[:run_action] = true
        end
    end
end

#run(arguments) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/atk/atk_info.rb', line 370

def run(arguments)
    self.ensure_package_info()
    # if it exists, run it
    if @run.is_a?(String)
        FS.in_dir(self.cache_location) do
            system(@run + Console.make_arguments_appendable(arguments))
            return $?.success?
        end
    # if not, explain why not
    else
        custom_message = <<-HEREDOC.remove_indent
        
            When trying 
                to perform the #{"run".color_as :code} action
                on the #{self.simple_name.to_s.color_as :key_term} module
                with these arguments: #{arguments.inspect.to_s.color_as :argument}
            
            There was an issue because:
        HEREDOC
        
        # FUTURE: make a more standardized error reporting tool and it that here
        
        good = ->(message) do
            "#{message.color_as :good}"
        end
        bad = ->(message) do
            "#{message.color_as :bad}"
        end
        
        if @dont_exist[:yaml_file]
            custom_message += <<-HEREDOC.remove_indent
                #{bad("there was no info.yaml for that package")}
                and an info.yaml is the location for defining a run action 
                
            HEREDOC
        elsif @dont_exist[:correctly_formatted_yaml_file]
            custom_message += <<-HEREDOC.remove_indent
                #{good("there was a info.yaml for that package")}
                #{bad("the info.yaml is not parseable")}
                and an info.yaml is the location for defining a run action
                
            HEREDOC
        elsif @dont_exist[:using_atk_version]
            custom_message += <<-HEREDOC.remove_indent
                #{good("there was a info.yaml for that package")}
                #{good("the info.yaml was parseable")}
                #{@dont_exist[:using_atk_version] && bad("the info.yaml didn't have a (using_atk_version) key")}
                #{@dont_exist[:package_info]      && bad("the info.yaml didn't have a (package_info) key")}
                #{@dont_exist[:actions]           && bad("the info.yaml didn't have a (package_info): (actions) key")}
                #{@dont_exist[:run_action]        && bad("the info.yaml didn't have a (package_info): (actions): (run) key")}
            HEREDOC
        end
        
        raise <<-HEREDOC.remove_indent
            
            
            #{custom_message}
            
            This is almost certainly a problem with the package
            Please contact the maintainer of #{self.simple_name}
            and let them know about the above issue
        HEREDOC
    end
end

#simple_nameObject



225
226
227
228
229
230
231
232
233
234
# File 'lib/atk/atk_info.rb', line 225

def simple_name
    if @simple_name == nil
        source = @init_name.strip
        # if its starts with "atk/", just remove that part
        source = source.sub( /^atk\//, "" )
        # if it starts with "https://github.com/", just remove that part
        @simple_name = source.sub( /^https:\/\/github.com\//, "" )
    end
    return @simple_name
end

#urlObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/atk/atk_info.rb', line 236

def url
    if @url == nil
        simple_name = self.simple_name()
        # if the package name does not have a slash in it, then assume it is a core / approved package
        if not (simple_name =~ /.*\/.*/) 
            raise "That package #{@init_name} doesn't seem to be a core package"
        # if it does have a slash, and isn't a full url, then assume its a github repo
        elsif not simple_name.start_with?(/https?:\/\//)
            repo_url = "https://github.com/"+simple_name
        else
            repo_url = simple_name
        end
        @url = repo_url
    end
    return @url
end