Class: RuAUR::Pacman

Inherits:
Object
  • Object
show all
Defined in:
lib/ruaur/pacman.rb

Instance Method Summary collapse

Constructor Details

#initializePacman

Returns a new instance of Pacman.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruaur/pacman.rb', line 39

def initialize
    if (ScoobyDoo.where_are_you("pacman").nil?)
        raise RuAUR::Error::MissingDependency.new("pacman")
    end

    @pac_nocolor = "pacman --color=never"
    @pac_color = "pacman --color=always"
    @pac_cmd = @pac_color
    @pac_cmd = @pac_nocolor if (!RuAUR.hilight?)
    @installed = query
end

Instance Method Details

#clean(noconfirm = false) ⇒ Object



6
7
8
9
10
# File 'lib/ruaur/pacman.rb', line 6

def clean(noconfirm = false)
    puts(hilight_status("Cleaning pacman cache..."))
    system("sudo #{@pac_cmd} -Sc") if (!noconfirm)
    system("sudo #{@pac_cmd} -Sc --noconfirm") if (noconfirm)
end

#download(pkg_name, noconfirm = false) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/ruaur/pacman.rb', line 12

def download(pkg_name, noconfirm = false)
    puts(hilight_status("Downloading #{pkg_name}..."))
    if (!noconfirm)
        system("sudo #{@pac_cmd} -Sw #{pkg_name}")
    else
        system("sudo #{@pac_cmd} -Sw #{pkg_name} --noconfirm")
    end
end

#exist?(pkg_name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/ruaur/pacman.rb', line 21

def exist?(pkg_name)
    return !%x(
        #{@pac_nocolor} -Ss "^#{pkg_name.shellescape}$"
    ).empty?
end

#install(pkg_name, noconfirm = false) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruaur/pacman.rb', line 51

def install(pkg_name, noconfirm = false)
    if (@installed.include?(pkg_name))
        puts(hilight_installed("Already installed: #{pkg_name}"))
        return
    end

    puts(hilight_status("Installing #{pkg_name}..."))
    if (!noconfirm)
        system("sudo #{@pac_cmd} -S #{pkg_name} --needed")
    else
        system(
            "sudo #{@pac_cmd} -S #{pkg_name} --needed --noconfirm"
        )
    end

    @installed.merge!(query(pkg_name))
end

#install_local(pkgs, noconfirm = false) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/ruaur/pacman.rb', line 69

def install_local(pkgs, noconfirm = false)
    puts(hilight_status("Installing compiled packages..."))
    xzs = pkgs.join(" ")
    if (!noconfirm)
        system("sudo #{@pac_cmd} -U #{xzs}")
    else
        system("sudo #{@pac_cmd} -U #{xzs} --noconfirm")
    end
end

#query(pkg_name = "", info = false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruaur/pacman.rb', line 79

def query(pkg_name = "", info = false)
    results = Hash.new
    if (info)
        result = %x(#{@pac_nocolor} -Qi #{pkg_name} 2>/dev/null)
        result.strip!
        results[pkg_name] = result if (!result.empty?)
    else
        result = %x(#{@pac_nocolor} -Q #{pkg_name} 2>/dev/null)
        result.strip!
        result.split("\n").each do |l|
            name, version = l.split
            results[name] = version
        end
    end
    return results
end

#query_aur(pkg_name = "") ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/ruaur/pacman.rb', line 96

def query_aur(pkg_name = "")
    results = Hash.new
    %x(
        #{@pac_nocolor} -Qm #{pkg_name} 2>/dev/null
    ).each_line do |line|
        line = line.split
        results[line[0]] = line[1]
    end
    return results
end

#query_owns(file_names) ⇒ Object



107
108
109
# File 'lib/ruaur/pacman.rb', line 107

def query_owns(file_names)
    return %x(#{@pac_nocolor} -Qo #{file_names})
end

#remove(pkg_names, nosave = false) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/ruaur/pacman.rb', line 111

def remove(pkg_names, nosave = false)
    puts(hilight_status("Removing #{pkg_names.join(" ")}..."))
    if (!nosave)
        system("sudo #{@pac_cmd} -Rs #{pkg_names.join(" ")}")
    else
        system("sudo #{@pac_cmd} -Rns #{pkg_names.join(" ")}")
    end
end

#search(pkg_names) ⇒ Object



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
# File 'lib/ruaur/pacman.rb', line 120

def search(pkg_names)
    results = Array.new
    return results if (pkg_names.nil? || pkg_names.empty?)

    %x(
        #{@pac_nocolor} -Ss #{pkg_names}
    ).split("\n").each do |line|
        reg = "^([^\/ ]+)\/([^ ]+) ([^ ]+)( .*)?$"
        match = line.match(/#{reg}/)
        if (match)
            repo, name, version, trailing = match.captures
            repo.strip!
            name.strip!
            version.strip!
            trailing = "" if (trailing.nil?)
            trailing.strip!

            results.push(
                RuAUR::Package.new(
                    {
                        "Description" => "",
                        "Name" => name,
                        "NumVotes" => nil,
                        "URLPath" => nil,
                        "Version" => version
                    },
                    repo
                )
            )
            if (trailing.include?("[installed]"))
                results.last.installed
            end
        elsif (results.any?)
            results.last.description += " #{line.strip}"
        end
    end

    return results
end

#upgrade(noconfirm = false) ⇒ Object



160
161
162
163
164
# File 'lib/ruaur/pacman.rb', line 160

def upgrade(noconfirm = false)
    puts(hilight_status("Updating..."))
    system("sudo #{@pac_cmd} -Syyu") if (!noconfirm)
    system("sudo #{@pac_cmd} -Syyu --noconfirm") if (noconfirm)
end