| 1 | #------------------------------------------------------------------------------- |
|---|
| 2 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
|---|
| 3 | # |
|---|
| 4 | # This file is part of FCM, tools for managing and building source code. |
|---|
| 5 | # |
|---|
| 6 | # FCM is free software: you can redistribute it and/or modify |
|---|
| 7 | # it under the terms of the GNU General Public License as published by |
|---|
| 8 | # the Free Software Foundation, either version 3 of the License, or |
|---|
| 9 | # (at your option) any later version. |
|---|
| 10 | # |
|---|
| 11 | # FCM is distributed in the hope that it will be useful, |
|---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | # GNU General Public License for more details. |
|---|
| 15 | # |
|---|
| 16 | # You should have received a copy of the GNU General Public License |
|---|
| 17 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 18 | #------------------------------------------------------------------------------- |
|---|
| 19 | use strict; |
|---|
| 20 | use warnings; |
|---|
| 21 | |
|---|
| 22 | package FCM1::Keyword; |
|---|
| 23 | |
|---|
| 24 | use FCM::Context::Locator; |
|---|
| 25 | |
|---|
| 26 | # Returns/Sets the FCM 2 utility functional object. |
|---|
| 27 | { my $UTIL; |
|---|
| 28 | sub get_util { |
|---|
| 29 | $UTIL; |
|---|
| 30 | } |
|---|
| 31 | sub set_util { |
|---|
| 32 | $UTIL = $_[0]; |
|---|
| 33 | } |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | # Expands (the keywords in) the specfied location (and REV), and returns them |
|---|
| 37 | sub expand { |
|---|
| 38 | my ($in_loc, $in_rev) = @_; |
|---|
| 39 | my $target = $in_rev ? $in_loc . '@' . $in_rev : $in_loc; |
|---|
| 40 | my $locator = FCM::Context::Locator->new($target); |
|---|
| 41 | _unparse_loc(get_util()->loc_as_normalised($locator), $in_rev); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | # Returns the corresponding browser URL for the input VC location |
|---|
| 45 | sub get_browser_url { |
|---|
| 46 | my ($in_loc, $in_rev) = @_; |
|---|
| 47 | my $target = $in_rev ? $in_loc . '@' . $in_rev : $in_loc; |
|---|
| 48 | my $locator = FCM::Context::Locator->new($target); |
|---|
| 49 | get_util()->loc_browser_url($locator); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | # Un-expands the specfied location (and REV) to keywords, and returns them |
|---|
| 53 | sub unexpand { |
|---|
| 54 | my ($in_loc, $in_rev) = @_; |
|---|
| 55 | my $target = $in_rev ? $in_loc . '@' . $in_rev : $in_loc; |
|---|
| 56 | my $locator = FCM::Context::Locator->new($target); |
|---|
| 57 | _unparse_loc(get_util()->loc_as_keyword($locator), $in_rev); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | # If $in_rev, returns (LOC, REV). Otherwise, returns LOC@REV |
|---|
| 61 | sub _unparse_loc { |
|---|
| 62 | my ($loc, $in_rev) = @_; |
|---|
| 63 | if (!$loc) { |
|---|
| 64 | return; |
|---|
| 65 | } |
|---|
| 66 | if ($in_rev) { |
|---|
| 67 | my ($l, $r) = $loc =~ qr{\A (.*?) @([^@]+) \z}msx; |
|---|
| 68 | if ($l && $r) { |
|---|
| 69 | return ($l, $r); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | return $loc; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | 1; |
|---|
| 76 | __END__ |
|---|
| 77 | |
|---|
| 78 | =head1 NAME |
|---|
| 79 | |
|---|
| 80 | FCM1::Keyword |
|---|
| 81 | |
|---|
| 82 | =head1 SYNOPSIS |
|---|
| 83 | |
|---|
| 84 | use FCM1::Keyword; |
|---|
| 85 | |
|---|
| 86 | $loc = FCM1::Keyword::expand('fcm:namespace/path@rev-keyword'); |
|---|
| 87 | $loc = FCM1::Keyword::unexpand('svn://host/namespace/path@1234'); |
|---|
| 88 | |
|---|
| 89 | ($loc, $rev) = FCM1::Keyword::expand('fcm:namespace/path', 'rev-keyword'); |
|---|
| 90 | ($loc, $rev) = FCM1::Keyword::unexpand('svn://host/namespace/path', 1234); |
|---|
| 91 | |
|---|
| 92 | $loc = FCM1::Keyword::get_browser_url('fcm:namespace/path'); |
|---|
| 93 | $loc = FCM1::Keyword::get_browser_url('svn://host/namespace/path'); |
|---|
| 94 | |
|---|
| 95 | $loc = FCM1::Keyword::get_browser_url('fcm:namespace/path@1234'); |
|---|
| 96 | $loc = FCM1::Keyword::get_browser_url('svn://host/namespace/path@1234'); |
|---|
| 97 | |
|---|
| 98 | $loc = FCM1::Keyword::get_browser_url('fcm:namespace/path', 1234); |
|---|
| 99 | $loc = FCM1::Keyword::get_browser_url('svn://host/namespace/path', 1234); |
|---|
| 100 | |
|---|
| 101 | =head1 DESCRIPTION |
|---|
| 102 | |
|---|
| 103 | Provides a compatibility layer for code in FCM1::* name space by wrapping the |
|---|
| 104 | keyword related functions in L<FCM::Util|FCM::Util>. An instance of |
|---|
| 105 | L<FCM::Util|FCM::Util> must be set via the set_util($value) function before |
|---|
| 106 | using the other functions. |
|---|
| 107 | |
|---|
| 108 | =head1 FUNCTIONS |
|---|
| 109 | |
|---|
| 110 | =over 4 |
|---|
| 111 | |
|---|
| 112 | =item expand($loc) |
|---|
| 113 | |
|---|
| 114 | Expands FCM keywords in $loc and returns the result. |
|---|
| 115 | |
|---|
| 116 | If $loc is a I<fcm> scheme URI, the leading part (before any "/" or "@" |
|---|
| 117 | characters) of the URI opaque is the namespace of a FCM location keyword. This |
|---|
| 118 | is expanded into the actual value. Optionally, $loc can be suffixed with a peg |
|---|
| 119 | revision (an "@" followed by any characters). If a peg revision is a FCM |
|---|
| 120 | revision keyword, it is expanded into the actual revision. |
|---|
| 121 | |
|---|
| 122 | =item expand($loc,$rev) |
|---|
| 123 | |
|---|
| 124 | Same as C<expand($loc)>, but $loc should not contain a peg revision. Returns a |
|---|
| 125 | list containing the expanded version of $loc and $rev. |
|---|
| 126 | |
|---|
| 127 | =item get_browser_url($loc) |
|---|
| 128 | |
|---|
| 129 | Given a repository $loc in a known keyword namespace, returns the corresponding |
|---|
| 130 | URL for the code browser. |
|---|
| 131 | |
|---|
| 132 | Optionally, $loc can be suffixed with a peg revision (an "@" followed by any |
|---|
| 133 | characters). |
|---|
| 134 | |
|---|
| 135 | =item get_browser_url($loc,$rev) |
|---|
| 136 | |
|---|
| 137 | Same as get_browser_url($loc), but the revision should be specified using $rev |
|---|
| 138 | but not pegged with $loc. |
|---|
| 139 | |
|---|
| 140 | =item get_util() |
|---|
| 141 | |
|---|
| 142 | Returns the L<FCM::Util|FCM::Util> instance (set by set_util($value)). |
|---|
| 143 | |
|---|
| 144 | =item set_util($value) |
|---|
| 145 | |
|---|
| 146 | Sets the L<FCM::Util|FCM::Util> instance. |
|---|
| 147 | |
|---|
| 148 | =item unexpand($loc) |
|---|
| 149 | |
|---|
| 150 | Does the opposite of expand($loc). Returns the FCM location keyword equivalence |
|---|
| 151 | of $loc. If the $loc can be mapped using 2 or more namespaces, the namespace |
|---|
| 152 | that results in the longest substitution is used. Optionally, $loc can be |
|---|
| 153 | suffixed with a peg revision (an "@" followed by any characters). If a peg |
|---|
| 154 | revision is a known revision, it is turned into its corresponding revision |
|---|
| 155 | keyword. |
|---|
| 156 | |
|---|
| 157 | =item unexpand($loc,$rev) |
|---|
| 158 | |
|---|
| 159 | Same as unexpand($loc), but $loc should not contain a peg revision. Returns a |
|---|
| 160 | list containing the unexpanded version of $loc and $rev |
|---|
| 161 | |
|---|
| 162 | =item |
|---|
| 163 | |
|---|
| 164 | =back |
|---|
| 165 | |
|---|
| 166 | =head1 SEE ALSO |
|---|
| 167 | |
|---|
| 168 | L<FCM::System|FCM::System>, |
|---|
| 169 | L<FCM::Util|FCM::Util> |
|---|
| 170 | |
|---|
| 171 | =head1 COPYRIGHT |
|---|
| 172 | |
|---|
| 173 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
|---|
| 174 | |
|---|
| 175 | =cut |
|---|