| 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 FCM::Util::ConfigUpgrade; |
|---|
| 23 | use base qw{FCM::Class::CODE}; |
|---|
| 24 | |
|---|
| 25 | use FCM::Context::ConfigEntry; |
|---|
| 26 | |
|---|
| 27 | my %DECL_PATTERN_OF = ( |
|---|
| 28 | browser_mapping => qr{\A set::browser_mapping(?:_default|:: ([^:]+)):: (.+) \z}ixms, |
|---|
| 29 | keyword_loc => qr{\A set::(?:repos|url):: (.+) \z}ixms, |
|---|
| 30 | keyword_rev => qr{\A set::revision:: ([^:]+) :: (.+) \z}ixms, |
|---|
| 31 | ); |
|---|
| 32 | my @UPGRADE_FUNCS = ( |
|---|
| 33 | \&_upgrade_browser_mapping_decl, |
|---|
| 34 | \&_upgrade_keyword_loc_decl, |
|---|
| 35 | \&_upgrade_keyword_rev_decl, |
|---|
| 36 | ); |
|---|
| 37 | |
|---|
| 38 | # Creates the class. |
|---|
| 39 | __PACKAGE__->class({}, {action_of => {upgrade => \&_upgrade}}); |
|---|
| 40 | |
|---|
| 41 | sub _upgrade { |
|---|
| 42 | my ($attrib_ref, $config_entry) = @_; |
|---|
| 43 | if (!defined($config_entry)) { |
|---|
| 44 | return; |
|---|
| 45 | } |
|---|
| 46 | for my $func (@UPGRADE_FUNCS) { |
|---|
| 47 | $func->($config_entry); |
|---|
| 48 | if ($func->($config_entry)) { |
|---|
| 49 | return $config_entry; |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | return $config_entry; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | # Upgrades a browser mapping declaration. |
|---|
| 56 | sub _upgrade_browser_mapping_decl { |
|---|
| 57 | my ($config_entry) = @_; |
|---|
| 58 | my ($ns, $key) |
|---|
| 59 | = $config_entry->get_label() =~ $DECL_PATTERN_OF{browser_mapping}; |
|---|
| 60 | if (!$key) { |
|---|
| 61 | return; |
|---|
| 62 | } |
|---|
| 63 | $config_entry->set_label( |
|---|
| 64 | $key eq 'browser_url_template' ? 'browser.loc-tmpl' |
|---|
| 65 | : $key eq 'browser_rev_template' ? 'browser.rev-tmpl' |
|---|
| 66 | : 'browser.comp-pat' |
|---|
| 67 | ); |
|---|
| 68 | if ($ns) { |
|---|
| 69 | $config_entry->set_ns_list([$ns]); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | # Upgrades a location keyword declaration. |
|---|
| 74 | sub _upgrade_keyword_loc_decl { |
|---|
| 75 | my ($config_entry) = @_; |
|---|
| 76 | my ($ns) = $config_entry->get_label() =~ $DECL_PATTERN_OF{keyword_loc}; |
|---|
| 77 | if (!$ns) { |
|---|
| 78 | return; |
|---|
| 79 | } |
|---|
| 80 | $config_entry->set_label('location'); |
|---|
| 81 | $config_entry->get_modifier_of()->{primary} = 1; |
|---|
| 82 | $config_entry->set_ns_list([$ns]); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | # Upgrades a revision keyword declaration. |
|---|
| 86 | sub _upgrade_keyword_rev_decl { |
|---|
| 87 | my ($config_entry) = @_; |
|---|
| 88 | my ($ns, $key) = $config_entry->get_label() =~ $DECL_PATTERN_OF{keyword_rev}; |
|---|
| 89 | if (!$ns || !$key) { |
|---|
| 90 | return; |
|---|
| 91 | } |
|---|
| 92 | $config_entry->set_label('revision'); |
|---|
| 93 | $config_entry->set_ns_list([$ns, $key]); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | # ------------------------------------------------------------------------------ |
|---|
| 97 | 1; |
|---|
| 98 | __END__ |
|---|
| 99 | |
|---|
| 100 | =head1 NAME |
|---|
| 101 | |
|---|
| 102 | FCM::Util::ConfigUpgrade |
|---|
| 103 | |
|---|
| 104 | =head1 SYNOPSIS |
|---|
| 105 | |
|---|
| 106 | use FCM::Util::ConfigUpgrade; |
|---|
| 107 | $upgrade = FCM::Util::ConfigUpgrade->new(); |
|---|
| 108 | if (!$upgrade->($entry)) { |
|---|
| 109 | die($entry->get_label(), ": cannot upgrade.\n"); |
|---|
| 110 | } |
|---|
| 111 | # ... do something with $entry |
|---|
| 112 | |
|---|
| 113 | =head1 DESCRIPTION |
|---|
| 114 | |
|---|
| 115 | Provides a utility to upgrade FCM 1 configuration to FCM 2 configuration. |
|---|
| 116 | |
|---|
| 117 | =head1 METHODS |
|---|
| 118 | |
|---|
| 119 | =over 4 |
|---|
| 120 | |
|---|
| 121 | =item $class->new() |
|---|
| 122 | |
|---|
| 123 | Creates and returns a new instance of this utility. |
|---|
| 124 | |
|---|
| 125 | =item $util->($entry) |
|---|
| 126 | |
|---|
| 127 | Upgrades the content of $entry, where possible. Only keyword related |
|---|
| 128 | declarations in the FCM 1 common configuration files are currently supported. |
|---|
| 129 | |
|---|
| 130 | =back |
|---|
| 131 | |
|---|
| 132 | =head1 COPYRIGHT |
|---|
| 133 | |
|---|
| 134 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
|---|
| 135 | |
|---|
| 136 | =cut |
|---|