| 1 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use Modern::Perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package SVN::Tree; | ||
| 5 | { | ||
| 6 | $SVN::Tree::DIST = 'SVN-Tree'; | ||
| 7 | } | ||
| 8 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | our $VERSION = '0.005'; # VERSION | ||
| 11 | 1 | ▶ | use List::MoreUtils 'any'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use Path::Class; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use SVN::Core; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use SVN::Fs; | ||
| 15 | use SVN::Repos; | ||
| 16 | use Tree::Path::Class; | ||
| 17 | use Moose; | ||
| 18 | use MooseX::Has::Options; | ||
| 19 | use MooseX::Types::Moose qw(ArrayRef HashRef Maybe); | ||
| 20 | use MooseX::Types::SVN 'SvnRoot'; | ||
| 21 | use MooseX::MarkAsMethods autoclean => 1; | ||
| 22 | |||
| 23 | has root => ( qw(:rw :required :coerce), isa => SvnRoot ); | ||
| 24 | |||
| 25 | has tree => ( | ||
| 26 | qw(:ro :required :lazy), | ||
| 27 | isa => 'Tree::Path::Class', | ||
| 28 | writer => '_set_tree', | ||
| 29 | init_arg => undef, | ||
| 30 | default => sub { $_[0]->_root_to_tree( $_[0]->root ) }, | ||
| 31 | ); | ||
| 32 | |||
| 33 | has projects => ( | ||
| 34 | qw(:ro :required :lazy_build), | ||
| 35 | isa => ArrayRef [ Maybe ['Tree::Path::Class'] ], | ||
| 36 | writer => '_set_projects', | ||
| 37 | init_arg => undef, | ||
| 38 | ); | ||
| 39 | |||
| 40 | has branches => ( | ||
| 41 | qw(:ro :required :lazy_build), | ||
| 42 | isa => HashRef [ ArrayRef [ Maybe ['Tree::Path::Class'] ] ], | ||
| 43 | writer => '_set_branches', | ||
| 44 | init_arg => undef, | ||
| 45 | ); | ||
| 46 | |||
| 47 | sub _build_projects { | ||
| 48 | my $self = shift; | ||
| 49 | my $tree = $self->tree; | ||
| 50 | return [$tree] if _match_svn_dirs( _children_value($tree) ); | ||
| 51 | return [ $tree->children ] | ||
| 52 | if _match_svn_dirs( map { _children_value($_) } $tree->children ); | ||
| 53 | return []; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _match_svn_dirs { | ||
| 57 | my @dirs = @_; | ||
| 58 | return any { $_ ~~ [qw(trunk branches tags)] } @dirs; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _children_value { | ||
| 62 | return map { $_->value->stringify } shift->children; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _build_branches { | ||
| 66 | my $self = shift; | ||
| 67 | my %branches; | ||
| 68 | for my $project ( @{ $self->projects } ) { | ||
| 69 | @{ $branches{ $project->value->stringify } } | ||
| 70 | = map { _trunk_or_branches($_) } $project->children; | ||
| 71 | } | ||
| 72 | return \%branches; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _trunk_or_branches { | ||
| 76 | my $tree = shift; | ||
| 77 | given ( $tree->value ) { | ||
| 78 | when ('trunk') { return $tree } | ||
| 79 | when ('branches') { | ||
| 80 | my $path = $tree->path; | ||
| 81 | return | ||
| 82 | map { Tree::Path::Class->new( dir( $path, $_->value ) ) } | ||
| 83 | $tree->children; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 | # recreate tree every time root is changed | ||
| 90 | after root => sub { | ||
| 91 | my ( $self, $root ) = @_; | ||
| 92 | return if !$root; | ||
| 93 | $self->_set_tree( $self->_root_to_tree($root) ); | ||
| 94 | $self->_set_projects( $self->_build_projects ); | ||
| 95 | $self->_set_branches( $self->_build_branches ); | ||
| 96 | return; | ||
| 97 | }; | ||
| 98 | |||
| 99 | sub _root_to_tree { | ||
| 100 | my ( $self, $root ) = @_; | ||
| 101 | my $tree = Tree::Path::Class->new(q{/}); | ||
| 102 | my $entries_ref = $root->dir_entries(q{/}); | ||
| 103 | while ( my ( $name => $dirent ) = each %{$entries_ref} ) { | ||
| 104 | $tree->add_child( $self->_dirent_to_tree( "/$name" => $dirent ) ); | ||
| 105 | } | ||
| 106 | return $tree; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _dirent_to_tree { | ||
| 110 | my ( $self, $entry_name, $dirent ) = @_; | ||
| 111 | |||
| 112 | my $name = $dirent->name; | ||
| 113 | my $tree = Tree::Path::Class->new(); | ||
| 114 | |||
| 115 | given ( $dirent->kind ) { | ||
| 116 | ## no critic (Variables::ProhibitPackageVars) | ||
| 117 | when ($SVN::Node::file) { $tree->set_value( file($name) ) } | ||
| 118 | when ($SVN::Node::dir) { | ||
| 119 | $tree->set_value( dir($name) ); | ||
| 120 | my %child_entries = %{ $self->root->dir_entries($entry_name) }; | ||
| 121 | while ( my ( $child_name => $child_dirent ) | ||
| 122 | = each %child_entries ) | ||
| 123 | { | ||
| 124 | $tree->add_child( | ||
| 125 | $self->_dirent_to_tree( | ||
| 126 | "$entry_name/$child_name" => $child_dirent, | ||
| 127 | ), | ||
| 128 | ); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | return $tree; | ||
| 133 | } | ||
| 134 | |||
| 135 | __PACKAGE__->meta->make_immutable(); | ||
| 136 | no Moose; | ||
| 137 | 1; | ||
| 138 | |||
| 139 | # ABSTRACT: SVN::Fs plus Tree::Path::Class | ||
| 140 | |||
| 141 | __END__ | ||