aboutsummaryrefslogtreecommitdiff
path: root/ruby/package_database.cc
blob: 88f0e146bff7cf51b37b3e6adae4794291fa6f31 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
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
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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis_ruby.hh>
#include <paludis/package_database.hh>
#include <paludis/util/stringify.hh>
#include <ruby.h>

using namespace paludis;
using namespace paludis::ruby;

#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x)

namespace
{
    static VALUE c_package_database;
    static VALUE c_package_database_install_state;

    /*
     * call-seq:
     *     favourite_repository -> String
     *
     * Fetch the name of our 'favourite' repository
     */
    VALUE
    package_database_favourite_repository(VALUE self)
    {
        try
        {
            PackageDatabase::Pointer * self_ptr;
            Data_Get_Struct(self, PackageDatabase::Pointer, self_ptr);
            return rb_str_new2(stringify((*self_ptr)->favourite_repository()).c_str());
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     fetch_unique_qualified_package_name(package_name) -> QualifiedPackageName
     *
     * Disambiguate a package name
     */
    VALUE
    package_database_fetch_unique_qualified_package_name(VALUE self, VALUE pkg)
    {
        try
        {
            PackageDatabase::Pointer * self_ptr;
            Data_Get_Struct(self, PackageDatabase::Pointer, self_ptr);
            return rb_str_new2(stringify((*self_ptr)->fetch_unique_qualified_package_name(
                            PackageNamePart(StringValuePtr(pkg)))).c_str());
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     query(atom, install_state) -> Array
     *
     *  Query the repositoty. Returns an array of PackageDatabaseEntry
     */
    VALUE
    package_database_query(VALUE self, VALUE atom, VALUE state)
    {
        try
        {
            PackageDatabase::Pointer * self_ptr;
            Data_Get_Struct(self, PackageDatabase::Pointer, self_ptr);

            PackageDatabaseEntryCollection::ConstPointer items((*self_ptr)->query(
                        value_to_package_dep_atom(atom),
                        static_cast<InstallState>(NUM2INT(state))));

            VALUE result(rb_ary_new());
            for (PackageDatabaseEntryCollection::Iterator i(items->begin()),
                    i_end(items->end()) ; i != i_end ; ++i)
                rb_ary_push(result, package_database_entry_to_value(*i));
            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     repositories -> Array
     *
     *  Returns an array of Repository
     */
    VALUE
    package_database_repositories(VALUE self)
    {
        try
        {
            PackageDatabase::Pointer * self_ptr;
            Data_Get_Struct(self, PackageDatabase::Pointer, self_ptr);

            VALUE result(rb_ary_new());
            for (PackageDatabase::RepositoryIterator r((*self_ptr)->begin_repositories()),
                    r_end((*self_ptr)->end_repositories()) ; r != r_end ; ++r)
                rb_ary_push(result, repository_to_value(*r));

            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     fetch_repository(repository_name) -> Repository
     *
     *  Fetch a named repository.
     */
    VALUE
    package_database_fetch_repository(VALUE self, VALUE name)
    {
        try
        {
            PackageDatabase::Pointer * self_ptr;
            Data_Get_Struct(self, PackageDatabase::Pointer, self_ptr);

            return repository_to_value((*self_ptr)->fetch_repository(RepositoryName(StringValuePtr(name))));
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     better_repository(repository_name_a, repository_name_b) -> String
     *
     * Which repository is better?
     */
    VALUE
    package_database_better_repository(VALUE self, VALUE name1, VALUE name2)
    {
        try
        {
            PackageDatabase::Pointer * self_ptr;
            Data_Get_Struct(self, PackageDatabase::Pointer, self_ptr);

            return rb_str_new2(stringify((*self_ptr)->better_repository(
                        RepositoryName(StringValuePtr(name1)),
                        RepositoryName(StringValuePtr(name2))
                        )).c_str());
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    void do_register_package_database()
    {
        /*
         * Document-class: Paludis::PackageDatabase
         *
         * A PackageDatabase can be queried for Package instances.
         */
        c_package_database = rb_define_class_under(paludis_module(), "PackageDatabase", rb_cObject);
        rb_funcall(c_package_database, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_package_database, "favourite_repository", RUBY_FUNC_CAST(&package_database_favourite_repository), 0);
        rb_define_method(c_package_database, "fetch_unique_qualified_package_name",
                RUBY_FUNC_CAST(&package_database_fetch_unique_qualified_package_name), 1);
        rb_define_method(c_package_database, "query",
                RUBY_FUNC_CAST(&package_database_query), 2);
        rb_define_method(c_package_database, "repositories",
                RUBY_FUNC_CAST(&package_database_repositories), 0);
        rb_define_method(c_package_database, "fetch_repository",
                RUBY_FUNC_CAST(&package_database_fetch_repository), 1);
        rb_define_method(c_package_database, "better_repository",
                RUBY_FUNC_CAST(&package_database_better_repository), 2);
        /*
         * Document-module: Paludis::InstallState
         *
         * Do we want only installed packages, only installable packages, or any package when querying?
         */
        c_package_database_install_state = rb_define_class_under(paludis_module(), "InstallState", rb_cObject);
        for (InstallState l(static_cast<InstallState>(0)), l_end(last_install_state) ; l != l_end ;
                l = static_cast<InstallState>(static_cast<int>(l) + 1))
            rb_define_const(c_package_database_install_state, value_case_to_RubyCase(stringify(l)).c_str(), INT2FIX(l));

        // cc_enum_special<paludis/package_database.hh, InstallState, c_package_database_install_state>
    }
}

VALUE
paludis::ruby::package_database_to_value(PackageDatabase::Pointer m)
{
    PackageDatabase::Pointer * m_ptr(0);
    try
    {
        m_ptr = new PackageDatabase::Pointer(m);
        return Data_Wrap_Struct(c_package_database, 0, &Common<PackageDatabase::Pointer>::free, m_ptr);
    }
    catch (const std::exception & e)
    {
        delete m_ptr;
        exception_to_ruby_exception(e);
    }
}

RegisterRubyClass::Register paludis_ruby_register_package_database PALUDIS_ATTRIBUTE((used))
    (&do_register_package_database);