8cab57e9815bff65c59d7078dd0a5b43fcf999b9
[platform/upstream/libzypp.git] / zypp / repo / RepoVariables.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/repo/RepoVariables.h
10  */
11 #ifndef ZYPP_REPO_VARIABLES_H_
12 #define ZYPP_REPO_VARIABLES_H_
13
14 #include <string>
15 #include "zypp/base/Function.h"
16 #include "zypp/base/ValueTransform.h"
17 #include "zypp/Url.h"
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 {
22   ///////////////////////////////////////////////////////////////////
23   namespace repo
24   {
25     ///////////////////////////////////////////////////////////////////
26     /// \class RepoVarExpand
27     /// \brief Functor expanding repo variables in a string
28     ///
29     /// Known variables are determined by a callback function taking a variable
30     /// name and returning a pointer to the variable value or \c nullptr if unset.
31     ///
32     /// The \c $ character introduces variable expansion. A valid variable name
33     /// is any non-empty case-insensitive sequence of <tt>[[:alnum:]_]</tt>.
34     /// The variable name to be expanded may be enclosed in braces, which are
35     /// optional but serve to protect the variable to be expanded from characters
36     /// immediately following it which could be interpreted as part of the name.
37     ///
38     /// When braces are used, the matching ending brace is the first \c } not
39     /// escaped by a backslash and not within an embedded variable expansion.
40     /// Within braces only \c $, \c } and \c backslash are escaped by a
41     /// backslash. There is no escaping outside braces, to stay comaptible
42     /// with \c YUM (which does not support braces).
43     ///
44     /// <ul>
45     /// <li> \c ${variable}
46     /// If \c variable is unset the original is preserved like in \c YUM.
47     /// Otherwise, the value of \c variable is substituted.</li>
48     ///
49     /// <li> \c ${variable:-word} (default value)
50     /// If \c variable is unset or empty, the expansion of \c word is substituted.
51     /// Otherwise, the value of \c variable is substituted.</li>
52     ///
53     /// <li> \c ${variable:+word} (alternate value)
54     /// If variable is unset or empty nothing is substituted.
55     /// Otherwise, the expansion of \c word is substituted.</li>
56     /// </ul>
57     struct RepoVarExpand
58     {
59       /** Function taking a variable name and returning a pointer to the variable value or \c nullptr if unset. */
60       typedef function<const std::string * ( const std::string & )> VarRetriever;
61
62       /** Return a copy of \a value_r with embedded variables expanded. */
63       std::string operator()( const std::string & value_r, VarRetriever varRetriever_r ) const;
64 #ifndef SWIG // Swig treats it as syntax error
65       /** \overload moving */
66       std::string operator()( std::string && value_r, VarRetriever varRetriever_r ) const;
67 #endif
68     };
69
70     /**
71      * \short Functor replacing repository variables
72      *
73      * Replaces the built-in '$arch', '$basearch' and '$releasever' ans also
74      * custom repo variables (\see \ref zypp-repovars) in a string with the
75      * assigned values.
76      *
77      * Additionally $releasever_major and $releasever_minor can be used
78      * to refer to $releasever major number (everything up to the 1st \c '.' )
79      * and minor number (everything after the 1st \c '.' ).
80      *
81      * \note The $releasever value is overwritten by the environment
82      * variable \c ZYPP_REPO_RELEASEVER. This might  be handy for
83      * distribution upogrades like this:
84      * \code
85      *   $ export ZYPP_REPO_RELEASEVER=13.2
86      *   $ zypper lr -u
87      *   $ zypper dup
88      *   ....upgrades to 13.2...
89      * \endcode
90      * The same can be achieved by using zyppers --releasever global option:
91      * \code
92      *   $ zypper --releasever 13.2 lr -u
93      *   $ zypper --releasever 13.2 dup
94      *   ....upgrades to 13.2...
95      * \endcode
96      * (see \ref zypp-envars)
97      *
98      * \code
99      * Example:
100      * ftp://user:secret@site.net/$arch/ -> ftp://user:secret@site.net/i686/
101      * http://site.net/?basearch=$basearch -> http://site.net/?basearch=i386
102      * \endcode
103      *
104      * \see \ref RepoVarExpand for supported variable syntax.
105      */
106     struct RepoVariablesStringReplacer : public std::unary_function<const std::string &, std::string>
107     {
108       std::string operator()( const std::string & value_r ) const;
109 #ifndef SWIG // Swig treats it as syntax error
110       /** \overload moving */
111       std::string operator()( std::string && value_r ) const;
112 #endif
113     };
114
115     /**
116      * \short Functor replacing repository variables
117      *
118      * Replaces repository variables in the path and query part of the URL.
119      * \see RepoVariablesStringReplacer
120      */
121     struct RepoVariablesUrlReplacer : public std::unary_function<const Url &, Url>
122     {
123       Url operator()( const Url & url_r ) const;
124     };
125   } // namespace repo
126   ///////////////////////////////////////////////////////////////////
127
128   /** \relates RepoVariablesStringReplacer Helper managing repo variables replaced strings */
129   typedef base::ValueTransform<std::string, repo::RepoVariablesStringReplacer> RepoVariablesReplacedString;
130
131   /** \relates RepoVariablesStringReplacer Helper managing repo variables replaced string lists */
132   typedef base::ContainerTransform<std::list<std::string>, repo::RepoVariablesStringReplacer> RepoVariablesReplacedStringList;
133
134   /** \relates RepoVariablesUrlReplacer Helper managing repo variables replaced urls */
135   typedef base::ValueTransform<Url, repo::RepoVariablesUrlReplacer> RepoVariablesReplacedUrl;
136
137   /** \relates RepoVariablesUrlReplacer Helper managing repo variables replaced url lists */
138   typedef base::ContainerTransform<std::list<Url>, repo::RepoVariablesUrlReplacer> RepoVariablesReplacedUrlList;
139
140 } // namespace zypp
141 ///////////////////////////////////////////////////////////////////
142
143 #endif