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