our
$VERSION
=
'0.10'
;
use
Test::Legal::Util
qw/ annotate_copyright deannotate_copyright load_meta write_LICENSE /
;
use
Sub::Exporter
-setup
=> {
exports
=> [
qw/ disable_test_builder annotate_dirs deannotate_dirs/
,
copyright_ok
=> \
'_build_copyright_ok'
,
license_ok
=> \
'_build_license_ok'
],
groups
=> {
default
=> [
qw/ copyright_ok license_ok /
],
core
=> [
qw/ copyright_ok license_ok /
]
},
collectors
=> [
qw/ defaults /
]
};
dirs
=> [
qw/ lib script /
],
};
my
$tb
= new Test::Builder ;
END {
$tb
->done_testing; }
sub
disable_test_builder {
sub
ok{};
sub
done_testing{};
$tb
=
bless
{}
}
sub
_values {
my
(
$arg
,
$defaults
) =
@_
;
$arg
//= {};
$defaults
//= {};
return
unless
ref
$arg
eq
'HASH'
;
return
unless
ref
$defaults
eq
'HASH'
;
$arg
= { %{DEFAULTS()},
%$defaults
,
%$arg
};
(
$arg
->{ meta }) = load_meta(
$arg
->{base}) ||
die
'no META file in dir "'
.
$arg
->{base}.
qq("\n)
;
$arg
;
}
sub
_in_mode {
my
(
$arg
,
$mode
) =
@_
;
return
unless
$mode
;
return
unless
ref
$arg
eq
'HASH'
;
return
unless
exists
$arg
->{actions};
first {
$_
=~ /^
$mode
$/i} @{
$arg
->{actions}};
}
sub
set_of_files {
my
(
$pat
,
@dirs
) =
@_
;
$pat
//=
'Copyright (C)'
;
$pat
=
qr/\Q$pat\E/
i;
my
@all_files
= File::Find::Rule->file->name(
qr/.*(\.pm|\.pl)$/
o)->in(
@dirs
);
my
@copyrighted
= File::Find::Rule->file->name(
qr/.*(\.pm|\.pl)$/
o)->
grep
(
$pat
)->in(
@dirs
);
List::Compare->new( \
@all_files
, \
@copyrighted
);
}
sub
annotate_dirs {
my
(
$pat
,
@dirs
) =
@_
;
my
$l
= set_of_files (
$pat
,
@dirs
) ;
my
@without_c
=
$l
->get_unique ;
return
(0,0)
unless
@without_c
;
DEBUG
"Without copyright:\n\t"
.
join
"\n\t"
,
@without_c
;
unless
($::opts->{yes}) {
return
(0,
scalar
@without_c
)
unless
(prompt
'-yes'
,
'Add copyright to all files that need it?'
) ;
}
DEBUG
"Updating..."
;
my
$num
= annotate_copyright(\
@without_c
,
$pat
) || 0;
$l
= set_of_files (
$pat
,
@dirs
) ;
my
@remain
=
$l
->get_unique;
DEBUG
"Remain without copyrigh:\n\t"
.
join
"\n\t"
,
@remain
if
@remain
;
(
$num
,
scalar
@remain
);
}
sub
deannotate_dirs {
my
(
$pat
,
@dirs
) =
@_
;
my
$l
= set_of_files (
$pat
,
@dirs
) ;
my
@with_c
=
$l
->get_intersection ;
return
(0,0)
unless
@with_c
;
DEBUG
"Have copyright:\n\t"
.
join
"\n\t"
,
@with_c
;
unless
($::opts->{yes}) {
return
(0,
scalar
@with_c
)
unless
(prompt
'-yes'
,
'Remove copyright from all files?'
) ;
}
DEBUG
"Updating..."
;
my
$num
= deannotate_copyright(\
@with_c
,
$pat
) || 0;
$l
= set_of_files (
$pat
,
@dirs
) ;
my
@remain
=
$l
->get_intersection ;
DEBUG
"Remain copyrighted:\n\t"
.
join
"\n\t"
,
@remain
if
@remain
;
(
$num
,
scalar
@remain
);
}
sub
_build_copyright_ok {
my
(
$class
,
$fun
,
$arg
,
$defaults
) =
@_
;
$arg
= _values(
$arg
,
$defaults
->{defaults});
my
@dirs
=
map
{
$arg
->{base} .
"/$_"
} @{
$arg
->{dirs}};
sub
{
return
(
'noop'
,
$arg
)
if
_in_mode(
$arg
,
'noop'
);
my
$pat
=
shift
;
$pat
//=
'Copyright (C)'
;
my
$l
= set_of_files(
$pat
,
@dirs
);
if
( (_in_mode(
$arg
,
'fix'
)) && (
$l
->get_unique) ) {
$tb
->note(
'adding Copyright notices'
)
if
annotate_copyright([
$l
->get_unique],
undef
);
$l
= set_of_files(
$pat
,
@dirs
);
}
$tb
->ok( 0,
$_
)
for
$l
->get_unique ;
$tb
->ok( 1,
$_
)
for
$l
->get_intersection;
$l
->get_unique;
}
}
sub
_build_license_ok {
my
(
$class
,
$fun
,
$arg
,
$defaults
) =
@_
;
$arg
= _values(
$arg
,
$defaults
->{defaults});
sub
{
return
(
'noop'
,
$arg
)
if
_in_mode(
$arg
,
'noop'
);
my
$has_file
= -f
$arg
->{base}.
'/LICENSE'
;
if
((_in_mode(
$arg
,
'fix'
)) && (!
$has_file
)) {
$tb
->note(
'added LICENSE'
)
if
write_LICENSE(
$arg
->{base});
}
$tb
->ok( -f
$arg
->{base}.
'/LICENSE'
,
'dist contains LICENSE file'
);
$tb
->ok( @{[
$arg
->{meta}->license]} > 0 ,
'META mentions license'
);
}
}
1;