File Coverage

blib/lib/Jenkins/i18n/License.pm
Criterion Covered Total %
statement 45 45 100.0
branch 7 10 70.0
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 65 68 95.5


line stmt bran cond sub pod time code
1             package Jenkins::i18n::License;
2              
3 1     1   101566 use 5.014004;
  1         13  
4 1     1   6 use strict;
  1         2  
  1         42  
5 1     1   7 use warnings;
  1         2  
  1         31  
6 1     1   6 use Carp qw(confess);
  1         2  
  1         44  
7 1     1   6 use File::Path qw(make_path);
  1         2  
  1         64  
8 1     1   492 use DateTime::Tiny;
  1         1034  
  1         31  
9 1     1   550 use Hash::Util qw(lock_keys);
  1         2889  
  1         6  
10              
11             =pod
12              
13             =head1 NAME
14              
15             Jenkins::i18n::License
16              
17             =head1 SYNOPSIS
18              
19             use Jenkins::i18n::License;
20             my $license = Jenkins::i18n::License->new;
21             $license->print_license($some_file);
22             my $data_ref = $license->read_license;
23              
24             =head1 DESCRIPTION
25              
26             This class handles all license requirements for new translation files created.
27              
28             It is intended to be used to provide the license text on new properties files.
29              
30             =cut
31              
32             our $VERSION = '0.10';
33              
34             =head1 METHODS
35              
36             =head2 new
37              
38             Creates a new instance of the class and return it.
39              
40             Doesn't expect any parameter.
41              
42             =cut
43              
44             sub new {
45 1     1 1 99 my $class = shift;
46 1         17 my $now = DateTime::Tiny->now;
47 1         93 my $self = {
48             current_year => $now->year,
49             content => undef
50             };
51 1         40 bless $self, $class;
52 1         3 lock_keys( %{$self} );
  1         7  
53 1         28 return $self;
54             }
55              
56             =head2 print
57              
58             Print the license text to a file.
59              
60             Expects as parameters:
61              
62             =over
63              
64             =item 1
65              
66             the complete path to the file
67              
68             =back
69              
70             =cut
71              
72             sub print {
73 2     2 1 4017 my ( $self, $file ) = @_;
74 2 100       22 confess 'The complete path to the file parameter is required'
75             unless ($file);
76              
77             # only dirs part is desired
78 1         22 my $dirs = ( File::Spec->splitpath($file) )[1];
79 1 50       510 make_path($dirs) unless ( -d $dirs );
80 1 50       82 open( my $out, '>', $file ) or confess "Cannot write to $file: $!\n";
81 1         9 my $data_ref = $self->read;
82              
83 1         4 foreach my $line ( @{$data_ref} ) {
  1         5  
84 22         54 print $out "#$line";
85             }
86              
87 1         71 close($out);
88             }
89              
90             =head2 read
91              
92             Returns the license, as an array reference.
93              
94             The license itself will include a line referencing the translators, which will
95             include the current year.
96              
97             =cut
98              
99             sub read {
100 2     2 1 8 my $self = shift;
101              
102 2 100       7 unless ( $self->{content} ) {
103 1         24 my @license = ;
104 1         5 my $additional_license_index = 4;
105              
106             # right after the copyright line
107 1 50       5 confess
108             "Unexpected license content, expected only a new line at 4 index"
109             unless ( $license[$additional_license_index] eq "\n" );
110              
111             $license[$additional_license_index]
112             = ' Copyright (c) ' # a space in the beginning is required
113 1         5 . $self->{current_year} . "- Jenkins contributors.\n";
114              
115 1         3 $self->{content} = \@license;
116             }
117              
118 2         19 return $self->{content};
119             }
120              
121             =head1 AUTHOR
122              
123             Alceu Rodrigues de Freitas Junior, Earfreitas@cpan.orgE
124              
125             =head1 COPYRIGHT AND LICENSE
126              
127             This software is copyright (c) 2022 of Alceu Rodrigues de Freitas Junior,
128             Earfreitas@cpan.orgE
129              
130             This file is part of Jenkins Translation Tool project.
131              
132             Jenkins Translation Tool is free software: you can redistribute it and/or
133             modify it under the terms of the GNU General Public License as published by the
134             Free Software Foundation, either version 3 of the License, or (at your option)
135             any later version.
136              
137             Jenkins Translation Tool is distributed in the hope that it will be useful, but
138             WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
139             FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
140             details.
141              
142             You should have received a copy of the GNU General Public License along with
143             Jenkins Translation Tool. If not, see (http://www.gnu.org/licenses/).
144              
145             The original C script was licensed through the MIT
146             License, copyright (c) 2004, Kohsuke Kawaguchi, Sun Microsystems, Inc., and a
147             number of other of contributors. Translations files generated by the Jenkins
148             Translation Tool CLI are distributed with the same MIT License.
149              
150             =cut
151              
152             1;
153              
154             __DATA__