| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | # vim: set ts=2 sw=2 tw=0: | ||
| 5 | # vim: set expandtab: | ||
| 6 | |||
| 7 | package Rex::Repositorio::Repository::Plain; | ||
| 8 | |||
| 9 | use Moose; | ||
| 10 | use Try::Tiny; | ||
| 11 | use File::Basename qw'basename dirname'; | ||
| 12 | use Data::Dumper; | ||
| 13 | use Carp; | ||
| 14 | use Params::Validate qw(:all); | ||
| 15 | use File::Spec; | ||
| 16 | use File::Path 'make_path'; | ||
| 17 | use IO::All; | ||
| 18 | use JSON::XS; | ||
| 19 | use Mojo::DOM; | ||
| 20 | |||
| 21 | our $VERSION = '1.2.1'; # VERSION | ||
| 22 | |||
| 23 | extends "Rex::Repositorio::Repository::Base"; | ||
| 24 | |||
| 25 | sub mirror { | ||
| 26 | my ( $self, %option ) = @_; | ||
| 27 | |||
| 28 | $self->app->logger->info('Collecting files. This may take a while...'); | ||
| 29 | |||
| 30 | my @dirs = ( $self->repo->{url} ); | ||
| 31 | my @files; | ||
| 32 | |||
| 33 | for my $dir (@dirs) { | ||
| 34 | $dir = "$dir/" if ( $dir !~ m{/$} ); | ||
| 35 | my $content = $self->download($dir); | ||
| 36 | |||
| 37 | #my $dom = Mojo::DOM->new($content); | ||
| 38 | my @links = ( $content =~ m/<a[^>+]href=["']?([^"'>]+)["'>]/ig ); | ||
| 39 | |||
| 40 | $self->app->logger->debug("Following ${dir}..."); | ||
| 41 | |||
| 42 | push @dirs, map { "${dir}${_}" } | ||
| 43 | grep { $_ =~ m{/$} } | ||
| 44 | grep { $_ !~ m{^\.} } @links; | ||
| 45 | push @files, map { "${dir}${_}" } | ||
| 46 | grep { $_ !~ m{/$} } @links; | ||
| 47 | } | ||
| 48 | |||
| 49 | my $p_count = 0; | ||
| 50 | my $p_total = scalar(@files); | ||
| 51 | $self->app->logger->info('Downloading packages...'); | ||
| 52 | for my $file (@files) { | ||
| 53 | |||
| 54 | my $path = $file; | ||
| 55 | my $repo_url = $self->repo->{url}; | ||
| 56 | $p_count++; | ||
| 57 | $self->app->logger->info("${p_count}/${p_total} ${path}"); | ||
| 58 | |||
| 59 | $path =~ s/$repo_url//g; | ||
| 60 | my $local_file = File::Spec->catdir( | ||
| 61 | $self->app->get_repo_dir(repo => $self->repo->{name}), $path ); | ||
| 62 | make_path(basename($local_file)); | ||
| 63 | |||
| 64 | $self->download_package( | ||
| 65 | url => $file, | ||
| 66 | name => basename($path), | ||
| 67 | dest => $local_file, | ||
| 68 | cb => sub { | ||
| 69 | }, | ||
| 70 | force => $option{force}, | ||
| 71 | update_file => $option{update_files}, | ||
| 72 | ); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub init { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | my $repo_dir = $self->app->get_repo_dir(repo => $self->repo->{name}); | ||
| 80 | make_path($repo_dir); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub add_file { | ||
| 84 | my $self = shift; | ||
| 85 | my %option = validate( | ||
| 86 | @_, | ||
| 87 | { | ||
| 88 | file => { | ||
| 89 | type => SCALAR | ||
| 90 | }, | ||
| 91 | } | ||
| 92 | ); | ||
| 93 | |||
| 94 | my $dest = File::Spec->catdir($self->app->get_repo_dir(repo => $self->repo->{name}), basename( $option{file} )); | ||
| 95 | |||
| 96 | $self->add_file_to_repo( source => $option{file}, dest => $dest ); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub remove_file { | ||
| 100 | my $self = shift; | ||
| 101 | |||
| 102 | my %option = validate( | ||
| 103 | @_, | ||
| 104 | { | ||
| 105 | file => { | ||
| 106 | type => SCALAR | ||
| 107 | }, | ||
| 108 | } | ||
| 109 | ); | ||
| 110 | |||
| 111 | my $file = File::Spec->catdir($self->app->get_repo_dir(repo => $self->repo->{name}), $option{file} ); | ||
| 112 | |||
| 113 | $self->remove_file_from_repo( file => $file ); | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; |