| 1 | # Copyrights 2003-2021 by [Mark Overmeer]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||
| 5 | # This code is part of perl distribution OODoc. It is licensed under the | ||
| 6 | # same terms as Perl itself: https://spdx.org/licenses/Artistic-2.0.html | ||
| 7 | |||
| 8 | package OODoc::Manifest; | ||
| 9 | use vars '$VERSION'; | ||
| 10 | $VERSION = '2.02'; | ||
| 11 | |||
| 12 | use base 'OODoc::Object'; | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | |||
| 17 | use IO::File; | ||
| 18 | use File::Basename 'dirname'; | ||
| 19 | use Log::Report 'oodoc'; | ||
| 20 | |||
| 21 | |||
| 22 | use overload '@{}' => sub { [ shift->files ] }; | ||
| 23 | use overload bool => sub {1}; | ||
| 24 | |||
| 25 | #------------------------------------------- | ||
| 26 | |||
| 27 | |||
| 28 | sub init($) | ||
| 29 | { my ($self, $args) = @_; | ||
| 30 | $self->SUPER::init($args) or return; | ||
| 31 | |||
| 32 | my $filename = $self->{OM_filename} = delete $args->{filename}; | ||
| 33 | |||
| 34 | $self->{O_files} = {}; | ||
| 35 | $self->read if defined $filename && -e $filename; | ||
| 36 | $self->modified(0); | ||
| 37 | $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | #------------------------------------------- | ||
| 41 | |||
| 42 | |||
| 43 | sub filename() {shift->{OM_filename}} | ||
| 44 | |||
| 45 | #------------------------------------------- | ||
| 46 | |||
| 47 | |||
| 48 | sub files() { keys %{shift->{O_files}} } | ||
| 49 | |||
| 50 | #------------------------------------------- | ||
| 51 | |||
| 52 | |||
| 53 | sub add($) | ||
| 54 | { my $self = shift; | ||
| 55 | while(@_) | ||
| 56 | { my $add = $self->relative(shift); | ||
| 57 | $self->modified(1) unless exists $self->{O_file}{$add}; | ||
| 58 | $self->{O_files}{$add}++; | ||
| 59 | } | ||
| 60 | $self; | ||
| 61 | } | ||
| 62 | |||
| 63 | #------------------------------------------- | ||
| 64 | |||
| 65 | |||
| 66 | sub read() | ||
| 67 | { my $self = shift; | ||
| 68 | my $filename = $self->filename; | ||
| 69 | my $file = IO::File->new($filename, "r") | ||
| 70 | or fault __x"cannot read manifest file {file}", file => $filename; | ||
| 71 | |||
| 72 | my @dist = $file->getlines; | ||
| 73 | $file->close; | ||
| 74 | |||
| 75 | s/\s+.*\n?$// for @dist; | ||
| 76 | $self->{O_files}{$_}++ foreach @dist; | ||
| 77 | $self; | ||
| 78 | } | ||
| 79 | |||
| 80 | #------------------------------------------- | ||
| 81 | |||
| 82 | |||
| 83 | sub modified(;$) | ||
| 84 | { my $self = shift; | ||
| 85 | @_ ? $self->{OM_modified} = @_ : $self->{OM_modified}; | ||
| 86 | } | ||
| 87 | |||
| 88 | #------------------------------------------- | ||
| 89 | |||
| 90 | |||
| 91 | sub write() | ||
| 92 | { my $self = shift; | ||
| 93 | return unless $self->modified; | ||
| 94 | my $filename = $self->filename || return $self; | ||
| 95 | |||
| 96 | my $file = IO::File->new($filename, "w") | ||
| 97 | or fault __x"cannot write manifest {file}", file => $filename; | ||
| 98 | |||
| 99 | $file->print($_, "\n") foreach sort $self->files; | ||
| 100 | $file->close; | ||
| 101 | |||
| 102 | $self->modified(0); | ||
| 103 | $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub DESTROY() { shift->write } | ||
| 107 | |||
| 108 | #------------------------------------------- | ||
| 109 | |||
| 110 | |||
| 111 | sub relative($) | ||
| 112 | { my ($self, $filename) = @_; | ||
| 113 | |||
| 114 | my $dir = dirname $self->filename; | ||
| 115 | return $filename if $dir eq '.'; | ||
| 116 | |||
| 117 | # normalize path for windows | ||
| 118 | s!\\!/!g for $filename, $dir; | ||
| 119 | |||
| 120 | if(substr($filename, 0, length($dir)+1) eq "$dir/") | ||
| 121 | { substr $filename, 0, length($dir)+1, ''; | ||
| 122 | return $filename; | ||
| 123 | } | ||
| 124 | |||
| 125 | warn "WARNING: MANIFEST file ".$self->filename | ||
| 126 | . " lists filename outside (sub)directory: $filename\n"; | ||
| 127 | |||
| 128 | $filename; | ||
| 129 | } | ||
| 130 | |||
| 131 | #------------------------------------------- | ||
| 132 | |||
| 133 | |||
| 134 | 1; |