Aion-Annotation
view release on metacpan or search on metacpan
lib/Aion/Annotation/Reader.pm view on Meta::CPAN
package Aion::Annotation::Reader;
# СÑиÑÑваÑÐµÐ»Ñ Ð°Ð½Ð½Ð¾ÑаÑий и комменÑаÑиев
use common::sense;
use Symbol qw//;
use Time::Local qw/timelocal/;
use overload fallback => 1,
'*{}' => sub { shift->{f} },
'-X' => \&_fileop,
'<>' => sub { shift->next },
'&{}' => sub {
my ($self) = @_;
sub { scalar $self->next }
},
'@{}' => sub { [shift->next] },
'""' => sub { my ($self) = @_; "${\__PACKAGE__}<$self->{type},$self->{path}>" },
;
# ÐеÑолÑнÑе пÑÑи Ð´Ð»Ñ ÑканиÑованиÑ
use Aion::Env AION_ANNOTATION_LIB => (default => 'lib');
# ÐиÑекÑоÑÐ¸Ñ Ð² коÑоÑÑÑ ÑкладÑваÑÑ ÑÐ°Ð¹Ð»Ñ ÐºÐ¾Ð½ÑигÑÑаÑии
use Aion::Env AION_ANNOTATION_INI => (default => 'etc/annotation');
# ÐиÑекÑоÑÐ¸Ñ Ñ ÐºÐµÑем
use Aion::Env AION_ANNOTATION_CACHE => (default => 'var/cache');
use constant {
READ_REMARKS => '-remarks',
READ_MTIME => '-mtime',
MODULES_MTIME_FILE => "modules.mtime.ini",
REMARKS_FILE => "remarks.ini",
LINE_REGEX => qr/^([\w:]+)#(\w*),(\d+)=(.*)$/,
MTIME_REGEX => qr/^(?<module>[\w:]+)=(?<year>\d{4})-(?<mon>\d{2})-(?<mday>\d{2}) (?<hour>\d{2}):(?<min>\d{2}):(?<sec>\d{2})$/,
};
my %DETECT = (ann => \&detect_annotation, mtime => \&detect_mtime, remarks => \&detect_remark);
sub new {
my ($cls, $ann) = @_;
my $type = $ann eq READ_REMARKS? 'remarks':
$ann eq READ_MTIME? 'mtime': 'ann';
my $file =
$type eq 'remarks'? join("/", AION_ANNOTATION_INI, REMARKS_FILE):
$type eq 'mtime'? join("/", AION_ANNOTATION_CACHE, MODULES_MTIME_FILE):
join("", AION_ANNOTATION_INI, "/", $ann, ".ann");
my $f = Symbol::gensym;
open $f, "<:encoding(utf8)", $file or die "$file: $!";
bless {f => $f, path => $file, type => $type, detect => $DETECT{$type}}, ref $cls || $cls;
}
sub path { shift->{path} }
# СледÑÑÑий ÑлеменÑ
sub next {
my ($self) = @_;
my $f = $self->{f};
my $detect = $self->{detect};
if(wantarray) {
my @ann;
while(<$f>) {
push @ann, $detect->($self, $_);
}
return @ann;
}
my $line = <$f> // return undef;
$detect->($self, $line);
}
sub DESTROY {
my ($self) = @_;
close $self->{f};
}
my %OP;
sub _fileop {
my ($self, $op) = @_;
local $_ = $self->{f};
($OP{$op} //= eval "sub { -$op }" // die)->()
}
# РаÑпознаÑÑ Ð°Ð½Ð½Ð¾ÑаÑиÑ
sub detect_annotation {
my ($self, $line) = @_;
warn "$self->{path} corrupt on line $.!" unless my ($pkg, $name, $lineno, $ann) = $line =~ LINE_REGEX;
+{
pkg => $pkg,
name => $name,
line => $lineno,
annotation => $ann,
};
}
# РаÑпознаÑÑ ÐºÐ¾Ð¼Ð¼ÐµÐ½ÑаÑий
sub detect_remark {
my ($self, $line) = @_;
warn "$self->{path} corrupt on line $.!" unless my ($pkg, $name, $lineno, $remark) = $line =~ LINE_REGEX;
$remark = join "\n", map { s/\\(.)/$1/gr } split /\\n/, $remark, -1;
+{
pkg => $pkg,
name => $name,
line => $lineno,
remark => $remark,
};
}
( run in 0.740 second using v1.01-cache-2.11-cpan-234fa220139 )