Aion-Annotation

 view release on metacpan or  search on metacpan

lib/Aion/Annotation.pm  view on Meta::CPAN

	for my $annotation_name (sort keys %$ann) {
		my $pkgs = $ann->{$annotation_name};
		my $path = $self->annotation_path($annotation_name);
		if(!keys %$pkgs) {
			erase($path) if -e $path;
			next;
		}
		open my $f, ">:utf8", $path or do { warn "$path not writed: $!" };
		for my $pkg (sort keys %$pkgs) {
			my $subs = $pkgs->{$pkg};
			for my $sub (sort keys %$subs) {
				my $annotation = $subs->{$sub};
				print $f "$pkg#$sub,$_->[0]=$_->[1]\n" for @$annotation;
			}
		}
		close $f;
	}
	
	# Удаляем временна файлов, которых уже нет в проекте
	for my $pkg (keys %$modules_mtime) {
		delete $modules_mtime->{$pkg} if !exists $exists{$pkg};
	}
	
	# Сохраняем время последнего изменения файлов
	my $mtime_path = mkpath $self->modules_mtime_path;

	open my $f, ">:utf8", $mtime_path or do { warn "$mtime_path not writed: $!" };
	printf $f "%s=%s\n", $_, strftime('%Y-%m-%d %H:%M:%S', localtime $modules_mtime->{$_}) for sort grep { $modules_mtime->{$_} } keys %$modules_mtime;
	close $f;

	# Сохраняем комментарии
	my $remark_path = $self->remark_path;
	open my $f, ">:utf8", $remark_path or do { warn "$remark_path not writed: $!" };
	for my $pkg (sort keys %$remark) {
		next if !exists $exists{$pkg};
		my $subs = $remark->{$pkg};
		for my $sub (sort keys %$subs) {
			my ($line, $rem) = @{$subs->{$sub}};
			print $f "$pkg#$sub,$line=", join("\\n", @$rem), "\n" if @$rem;
		}
	}
	close $f;
	
	$self
}

# Путь к файлу аннотаций
sub annotation_path {
	my ($self, $annotation_name) = @_;
	return $self->ini . "/$annotation_name.ann";
};

1;

__END__

=encoding utf-8

=head1 NAME

Aion::Annotation - processes annotations in perl modules

=head1 VERSION

0.0.3

=head1 SYNOPSIS

lib/For/Test.pm file:

	package For::Test;
	# The package for testing
	#@deprecated for_test
	
	#@deprecated
	#@todo add1
	# Is property
	#   readonly
	has abc => (is => 'ro');
	
	#@todo add2
	#@param Int $a
	#@param Int[] $r
	sub xyz {}
	
	1;



	use Aion::Annotation;
	
	Aion::Annotation->new->scan;
	
	open my $f, '<', 'var/cache/modules.mtime.ini' or die $!; my @modules_mtime = <$f>; chop for @modules_mtime; close $f;
	open my $f, '<', 'etc/annotation/remarks.ini' or die $!; my @remarks = <$f>; chop for @remarks; close $f;
	open my $f, '<', 'etc/annotation/todo.ann' or die $!; my @todo = <$f>; chop for @todo; close $f;
	open my $f, '<', 'etc/annotation/deprecated.ann' or die $!; my @deprecated = <$f>; chop for @deprecated; close $f;
	open my $f, '<', 'etc/annotation/param.ann' or die $!; my @param = <$f>; chop for @param; close $f;
	
	0+@modules_mtime  # -> 1
	$modules_mtime[0] # ~> ^For::Test=\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$
	\@remarks         # --> ['For::Test#,4=The package for testing', 'For::Test#abc,9=Is property\n  readonly']
	\@todo            # --> ['For::Test#abc,6=add1', 'For::Test#xyz,11=add2']
	\@deprecated      # --> ['For::Test#,3=for_test', 'For::Test#abc,5=']
	\@param           # --> ['For::Test#xyz,12=Int $a', 'For::Test#xyz,13=Int[] $r']

=head1 DESCRIPTION

C<Aion::Annotation> scans the perl modules in the B<lib> directory and prints them to the corresponding files in the B<etc/annotation> directory.

You can change B<lib> through the C<LIB> config, and B<etc/annotation> through the C<INI> config.

=over

=item 1. B<modules.mtime.ini> stores the times of the last module update.

=item 2. B<remarks.ini> stores comments for routines, properties and packages.

=item 3. The B<name.ann> files save annotations by their names.

=back



( run in 1.262 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )