source: LMDZ6/branches/Amaury_dev/tools/fcm/lib/FCM1/Keyword.pm @ 5099

Last change on this file since 5099 was 5095, checked in by abarral, 4 months ago

Revert cosp*/ from the trunk, as it's external code
Add missing bits from FCM2 source

File size: 5.2 KB
Line 
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#-------------------------------------------------------------------------------
19use strict;
20use warnings;
21
22package FCM1::Keyword;
23
24use 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
37sub 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
45sub 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
53sub 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
61sub _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
751;
76__END__
77
78=head1 NAME
79
80FCM1::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
103Provides a compatibility layer for code in FCM1::* name space by wrapping the
104keyword related functions in L<FCM::Util|FCM::Util>. An instance of
105L<FCM::Util|FCM::Util> must be set via the set_util($value) function before
106using the other functions.
107
108=head1 FUNCTIONS
109
110=over 4
111
112=item expand($loc)
113
114Expands FCM keywords in $loc and returns the result.
115
116If $loc is a I<fcm> scheme URI, the leading part (before any "/" or "@"
117characters) of the URI opaque is the namespace of a FCM location keyword. This
118is expanded into the actual value. Optionally, $loc can be suffixed with a peg
119revision (an "@" followed by any characters). If a peg revision is a FCM
120revision keyword, it is expanded into the actual revision.
121
122=item expand($loc,$rev)
123
124Same as C<expand($loc)>, but $loc should not contain a peg revision. Returns a
125list containing the expanded version of $loc and $rev.
126
127=item get_browser_url($loc)
128
129Given a repository $loc in a known keyword namespace, returns the corresponding
130URL for the code browser.
131
132Optionally, $loc can be suffixed with a peg revision (an "@" followed by any
133characters).
134
135=item get_browser_url($loc,$rev)
136
137Same as get_browser_url($loc), but the revision should be specified using $rev
138but not pegged with $loc.
139
140=item get_util()
141
142Returns the L<FCM::Util|FCM::Util> instance (set by set_util($value)).
143
144=item set_util($value)
145
146Sets the L<FCM::Util|FCM::Util> instance.
147
148=item unexpand($loc)
149
150Does the opposite of expand($loc). Returns the FCM location keyword equivalence
151of $loc. If the $loc can be mapped using 2 or more namespaces, the namespace
152that results in the longest substitution is used. Optionally, $loc can be
153suffixed with a peg revision (an "@" followed by any characters). If a peg
154revision is a known revision, it is turned into its corresponding revision
155keyword.
156
157=item unexpand($loc,$rev)
158
159Same as unexpand($loc), but $loc should not contain a peg revision. Returns a
160list containing the unexpanded version of $loc and $rev
161
162=item
163
164=back
165
166=head1 SEE ALSO
167
168L<FCM::System|FCM::System>,
169L<FCM::Util|FCM::Util>
170
171=head1 COPYRIGHT
172
173Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
174
175=cut
Note: See TracBrowser for help on using the repository browser.