1 /* valaversionattribute.vala
3 * Copyright (C) 2013 Florian Brosch
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian Brosch <flo.brosch@gmail.com>
27 * Represents a [Version] attribute
29 public class Vala
.VersionAttribute
{
30 private weak Symbol symbol
;
32 private bool? _deprecated
;
33 private bool? _experimental
;
36 * Constructs a new VersionAttribute.
38 * @param symbol the owner
39 * @return a new VersionAttribute
42 public VersionAttribute (Symbol symbol
) {
49 * Specifies whether this symbol has been deprecated.
51 public bool deprecated
{
53 if (_deprecated
== null) {
54 _deprecated
= symbol
.get_attribute_bool ("Version", "deprecated", false)
55 || symbol
.get_attribute_string ("Version", "deprecated_since") != null
56 || symbol
.get_attribute_string ("Version", "replacement") != null
57 // [Deprecated] is deprecated
58 || symbol
.get_attribute ("Deprecated") != null;
64 symbol
.set_attribute_bool ("Version", "deprecated", _deprecated
);
69 * Specifies what version this symbol has been deprecated since.
71 public string? deprecated_since
{
73 return symbol
.get_attribute_string ("Version", "deprecated_since")
74 // [Deprecated] is deprecated
75 ?? symbol
.get_attribute_string ("Deprecated", "since");
78 symbol
.set_attribute_string ("Version", "deprecated_since", value
);
83 * Specifies the replacement if this symbol has been deprecated.
85 public string? replacement
{
87 return symbol
.get_attribute_string ("Version", "replacement")
88 // [Deprecated] is deprecated
89 ?? symbol
.get_attribute_string ("Deprecated", "replacement");
92 symbol
.set_attribute_string ("Version", "replacement", value
);
99 * Specifies whether this symbol is experimental.
101 public bool experimental
{
103 if (_experimental
== null) {
104 _experimental
= symbol
.get_attribute_bool ("Version", "experimental", false)
105 || symbol
.get_attribute_string ("Version", "experimental_until") != null
106 || symbol
.get_attribute ("Experimental") != null;
108 return _experimental
;
111 _experimental
= value
;
112 symbol
.set_attribute_bool ("Version", "experimental", value
);
117 * Specifies until which version this symbol is experimental.
119 public string? experimental_until
{
121 return symbol
.get_attribute_string ("Version", "experimental_until");
124 symbol
.set_attribute_string ("Version", "experimental_until", value
);
131 * The minimum version for {@link Vala.VersionAttribute.symbol}
133 public string? since
{
135 return symbol
.get_attribute_string ("Version", "since");
138 symbol
.set_attribute_string ("Version", "since", value
);
145 * Check to see if the symbol is experimental, deprecated or not available
146 * and emit a warning if it is.
148 public bool check (SourceReference? source_ref
= null) {
152 if (symbol
.external_package
&& deprecated
) {
153 string? package_version
= symbol
.source_reference
.file
.installed_version
;
155 if (!CodeContext
.get ().deprecated
&& (package_version
== null || deprecated_since
== null || VersionAttribute
.cmp_versions (package_version
, deprecated_since
) >= 0)) {
156 Report
.deprecated (source_ref
, "%s %s%s".printf (symbol
.get_full_name (), (deprecated_since
== null) ?
"is deprecated" : "has been deprecated since %s".printf (deprecated_since
), (replacement
== null) ?
"" : ". Use %s".printf (replacement
)));
162 if (symbol
.external_package
&& since
!= null) {
163 string? package_version
= symbol
.source_reference
.file
.installed_version
;
165 if (CodeContext
.get ().since_check
&& package_version
!= null && VersionAttribute
.cmp_versions (package_version
, since
) < 0) {
166 unowned
string filename
= symbol
.source_reference
.file
.filename
;
167 string pkg
= Path
.get_basename (filename
[0:filename
.last_index_of_char ('.')]);
168 Report
.error (source_ref
, "%s is not available in %s %s. Use %s >= %s".printf (symbol
.get_full_name (), pkg
, package_version
, pkg
, since
));
174 if (symbol
.external_package
&& experimental
) {
175 if (!CodeContext
.get ().experimental
) {
176 string? package_version
= symbol
.source_reference
.file
.installed_version
;
177 string? experimental_until
= this
.experimental_until
;
179 if (experimental_until
== null || package_version
== null || VersionAttribute
.cmp_versions (package_version
, experimental_until
) < 0) {
180 Report
.experimental (source_ref
, "%s is experimental%s".printf (symbol
.get_full_name (), (experimental_until
!= null) ?
" until %s".printf (experimental_until
) : ""));
191 * A simple version comparison function.
193 * @param v1str a version number
194 * @param v2str a version number
195 * @return an integer less than, equal to, or greater than zero, if v1str is <, == or > than v2str
196 * @see GLib.CompareFunc
198 public static int cmp_versions (string v1str
, string v2str
) {
199 string[] v1arr
= v1str
.split (".");
200 string[] v2arr
= v2str
.split (".");
203 while (v1arr
[i
] != null && v2arr
[i
] != null) {
204 int v1num
= int.parse (v1arr
[i
]);
205 int v2num
= int.parse (v2arr
[i
]);
207 if (v1num
< 0 || v2num
< 0) {
223 if (v1arr
[i
] != null && v2arr
[i
] == null) {
227 if (v1arr
[i
] == null && v2arr
[i
] != null) {