aboutsummaryrefslogtreecommitdiff
path: root/doc/api/ruby/example_package_id.rb
blob: 112f4af9a9788b00a06d9d05e735a192a99ef044 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
# vim: set sw=4 sts=4 et tw=100 :

=begin description
This example demonstrates how to use PackageID.

See "example_action.rb" for more on actions. See "example_metadata_key.rb" for more on 
metadata keys. See "example_mask.rb" for more on masks.
=end

require 'Paludis'
require 'example_command_line'

include Paludis

exit_status = 0

# We start with an Environment, respecting the user's '--environment' choice.
env = EnvironmentFactory.instance.create(ExampleCommandLine.instance.environment)

# Fetch package IDs for installed 'sys-apps/paludis'
ids = env[Selection::AllVersionsSorted.new(
    Generator::Matches.new(Paludis::parse_user_package_dep_spec("sys-apps/paludis", env, []), []))]

# For each ID:
ids.each do | id |
    puts "#{id}:"

    # Start by outputting some basic properties:
    puts "    Name: ".ljust(40) + id.name
    puts "    Version: ".ljust(40) + id.version.to_s
    puts "    Repository: ".ljust(40) + id.repository_name

    # The PackageID.canonical_form method should be used when
    # outputting a package
    puts "    PackageIDCanonicalForm::Full: ".ljust(40) + id.canonical_form(PackageIDCanonicalForm::Full)
    puts "    PackageIDCanonicalForm::Version: ".ljust(40) + id.canonical_form(PackageIDCanonicalForm::Version)
    puts "    PackageIDCanonicalForm::NoVersion: ".ljust(40) + id.canonical_form(PackageIDCanonicalForm::NoVersion)

    # Let's see what keys we have. Other examples cover keys in depth,
    # so we'll just use the basic methods here.
    puts "    Keys: ".ljust(40)
    id.each_metadata do |key|
        puts "        #{key.raw_name}: ".ljust(40) + key.human_name
    end

    # And what about masks? Again, these are covered in depth
    # elsewhere.
    if id.masked?
        puts "    Masks: ".ljust(40)
        id.masks.each do |mask|
            puts "        #{mask.key}: ".ljust(40) + mask.description
        end
    end

    # Let's see which actions we support. There's no particularly nice
    # way of doing this, since it's not something we'd expect to be
    # doing.
    actions = []
    actions << "install" if id.supports_action(SupportsActionTest.new(InstallAction))
    actions << "installed" if id.supports_action(SupportsActionTest.new(InstalledAction))
    actions << "uninstall" if id.supports_action(SupportsActionTest.new(UninstallAction))
    actions << "pretend" if id.supports_action(SupportsActionTest.new(PretendAction))
    actions << "config" if id.supports_action(SupportsActionTest.new(ConfigAction))
    actions << "fetch" if id.supports_action(SupportsActionTest.new(FetchAction))
    actions << "info" if id.supports_action(SupportsActionTest.new(InfoAction))

    puts "    Actions: ".ljust(40) + actions.join(' ')

    puts
end

exit exit_status