File: Transform.pm

package info (click to toggle)
devscripts 2.25.30
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 9,504 kB
  • sloc: perl: 27,293; sh: 12,873; python: 4,466; makefile: 382
file content (149 lines) | stat: -rw-r--r-- 5,970 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package Devscripts::Uscan::WatchSource::Transform;

use Moo::Role;
use Devscripts::Uscan::Output;

use constant {
    ANY_VERSION    => '(?:[-_]?[Vv]?(\d[\-+\.:\~\da-zA-Z]*))',
    STABLE_VERSION => '(?:[-_]?[Vv]?((?:[1-9]\d*)(?:\.\d+){2}))',
    # From semver.org
    SEMANTIC_VERSION =>
'(?:[-_]?[Vv]?((?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?:[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?))',
    ARCHIVE_EXT =>
      '(?i)(?:\.(?:tar\.xz|tar\.bz2|tar\.gz|tar\.zstd?|zip|tgz|tbz|txz))',
    DEB_EXT => '(?:[\+~](debian|dfsg|ds|deb)(\.)?(\d+)?$)',

    ALIASES => {
        date           => 'gitdate',
        pretty         => 'gitpretty',
        versionregex   => 'matchingpattern',
        versionpattern => 'matchingpattern',
    },
};
use constant SIGNATURE_EXT => ARCHIVE_EXT . '(?:\.(?:asc|pgp|gpg|sig|sign))';

sub transformWatchSource {
    my ($self, $args) = @_;

    foreach my $watchSource (@{ $self->watchOptions }) {

        foreach my $alias (keys %{&ALIASES}) {
            if (defined $watchSource->{$alias}) {
                my $nk = ALIASES->{$alias};
                if (defined $watchSource->{$nk}) {
                    uscan_die "Key $nk is declared and its alias $nk too";
                    return;
                }
                $watchSource->{$nk} = delete $watchSource->{$alias};
            }
        }

        my $templates;
        while ($watchSource->{template}) {
            if ($watchSource->{template} !~ /^\w+$/) {
                uscan_die qq'Malformed template "$watchSource->{template}"';
                return;
            }
            $watchSource->{template} = ucfirst(lc $watchSource->{template});
            my $pkg = "Devscripts::Uscan::Templates::$watchSource->{template}";
            $templates->{current} = $watchSource->{template};
            eval "require $pkg";
            if ($@) {
                uscan_die qq'Template "$watchSource->{template}" is unknown,'
                  . "maybe you should upgrade devscripts:\n"
                  . $@;
                return;
            }
            my $transform = eval "$pkg->can('transform')";
            if (!$transform) {
                uscan_die
qq'Template "$watchSource->{template}" has no transform function';
                return;
            }

          # Templates can use Version-Type to define type of versions to accept
            $watchSource->{versiontype} ||= 'ANY_VERSION';
            unless ($watchSource->{versiontype} =~ /^[A-Z_]+$/
                and __PACKAGE__->can($watchSource->{versiontype})) {
                uscan_die 'Bad Version-Type';
                return;
            }
            $watchSource->{versiontype} = "\@$watchSource->{versiontype}\@";
            my $tmp = eval { $transform->($watchSource) };
            if ($@) {
                uscan_die "$pkg failed: $@";
                return;
            }
            unless ($tmp) {
                uscan_die "$pkg didn't return a watchsource";
                return;
            }
            $watchSource = $tmp;
            if (    $watchSource->{template}
                and $templates->{current} eq $watchSource->{template}) {
                uscan_debug "$pkg missed to delete template field";
                delete $watchSource->{template};
                last;
            }
            if ($watchSource->{template}) {
                $watchSource->{template}
                  = ucfirst(lc $watchSource->{template});
                if ($templates->{ $watchSource->{template} }) {
                    uscan_die
"Template look detected ($watchSource->{template} recalled)";
                    return;
                }
                $templates->{ $watchSource->{template} }++;
            }
        }

        foreach my $k (keys %$watchSource) {
            # Handle @FOO@ substitutions
            $watchSource->{$k} =~ s/\@PACKAGE\@/$args->{package}/g;
            $watchSource->{$k} =~ s/\@ANY_VERSION\@/ANY_VERSION/ge;
            $watchSource->{$k} =~ s/\@STABLE_VERSION\@/STABLE_VERSION/ge;
            $watchSource->{$k} =~ s/\@SEMANTIC_VERSION\@/SEMANTIC_VERSION/ge;
            $watchSource->{$k} =~ s/\@ARCHIVE_EXT\@/ARCHIVE_EXT/ge;
            $watchSource->{$k} =~ s/\@SIGNATURE_EXT\@/SIGNATURE_EXT/ge;
            $watchSource->{$k} =~ s/\@DEB_EXT\@/DEB_EXT/ge;
            $watchSource->{$k} =~ s/\@COMPONENT\@/$watchSource->{component}/g;

            if ($watchSource->{$k} eq 'auto') {
                # dversionmangle=auto is replaced by s/@DEB_EXT@//
                if ($k eq 'dversionmangle') {
                    $watchSource->{$k} = 's/' . DEB_EXT . '//';
                }
                # filenamemangle=auto is replaced by
                # s/.*?(@ANY_VERSION@@ARCHIVE_EXT@)/@PACKAGE@-$1/
                # But @PACKAGE@ is replaced by @PACKAGE@-@COMPONENT@ when
                # watch source is a component
                elsif ($k eq 'filenamemangle') {
                    $watchSource->{$k}
                      = 's/.*?[-_]*('
                      . ANY_VERSION
                      . ARCHIVE_EXT . ')/'
                      . $args->{package}
                      . (
                        $watchSource->{component}
                        ? '-' . $watchSource->{component}
                        : ''
                      ) . '-$1/';
                } elsif ($k eq 'uversionmangle') {
                    $watchSource->{$k}
                      = 's/\d\K[_.+-]?((?i)dev|pre|alpha|beta|rc)[.-]?/~$1/';
                }
            }
        }

        # When global "Version-Schema" is "checksum", the main watch source has
        # to be "group"
        if (    $watchSource->{versionschema}
            and $watchSource->{versionschema} eq 'checksum'
            and !$watchSource->{component}) {
            $watchSource->{versionschema} = 'group';
        }
    }
    return 1;
}

1;