App-ManagePoolStyleRepo

 view release on metacpan or  search on metacpan

lib/App/ManagePoolStyleRepo.pm  view on Meta::CPAN

    {
        for my $pool_dir (grep { $_ =~ $pool_pattern } @dir_entries) {
            local $CWD = $pool_dir;
            for my $item_path (glob "*") {
                my $row;
                $row = get_item_metadata(item_path=>$item_path, _skip_cd=>1);
                $row->{dir} = $pool_dir;
                push @rows, $row;
            }
        }
    }

  FILTER: {
        my @frows;
      ROW:
        for my $row (@rows) {
            if ($args{has_tags}) {
                my $matches;
                for my $tag (@{ $args{has_tags} }) {
                    do { $matches++; last } if $row->{tags} &&
                        grep { $tag eq $_ } @{ $row->{tags} };
                }
                next ROW unless $matches;
            }
            if ($args{lacks_tags}) {
                my $matches = 1;
                for my $tag (@{ $args{lacks_tags} }) {
                    do { $matches = 0; last } if $row->{tags} &&
                        grep { $tag eq $_ } @{ $row->{tags} };
                }
                next ROW unless $matches;
            }
            if (defined $q_lc) {
                my $matches;
                for my $field (@$searchable_fields) {
                    do { $matches++; last } if defined $row->{$field} && index(lc($row->{$field}), $q_lc) >= 0;
                }
                next ROW unless $matches;
            }
            push @frows, $row;
        }
        @rows = @frows;
    }

    unless ($args{detail}) {
        @rows = map { $_->{title} } @rows;
    }

    [200, "OK", \@rows];
}

$SPEC{update_index} = {
    v => 1.1,
    args => {
        %args_common,
    },
    features => {
        #dry_run => 1,
    },
};
sub update_index {
    require File::Path;
    require File::Temp;

    my %args = @_;

    my $res = list_items(
        %args,
        detail=>1,
    );
    return $res unless $res->[0] == 200;

    local $CWD = $args{repo_path};
    my $tmpdir = File::Temp::tempdir("index.tmp.XXXXXXXX", DIR => $args{repo_path});
    (my $tmpname = $tmpdir) =~ s!.+/!!;
    $CWD = $tmpname;

  CREATE_TITLE_INDEX:
    {
        mkdir "by-title";
        local $CWD = "by-title";
        for my $item (@{ $res->[2] }) {
            my $target = "../../$item->{dir}/$item->{filename}";
            my $link   = $item->{title};
            symlink $target, $link or warn "Can't symlink $link -> $target: $!";
        }
    } # CREATE_TITLE_INDEX

  CREATE_TAG_INDEX:
    {
        mkdir "by-tag";
        local $CWD = "by-tag";

        # collect all tags
        my %tags;
        for my $item (@{ $res->[2] }) {
            next unless $item->{tags};
            for my $tag (@{ $item->{tags} }) {
                (my $tagdir = $tag) =~ s!-+!/!g;
                File::Path::mkpath($tagdir) unless $tags{$tag}++;
                my $num_level = 1; $num_level++ while $tagdir =~ m!/!g;
                my $target = "../../".("../" x $num_level).$item->{dir}."/$item->{filename}";
                my $link = "$tagdir/$item->{title}";
                symlink $target, $link or warn "Can't symlink $link -> $target: $!";
            }
        }

    } # CREATE_TAG_INDEX

    $CWD = "..";
    File::Path::rmtree("index");
    rename $tmpname, "index" or die "Can't rename $tmpname -> index: $!";
    #system "mv", $tmpname, "index" or die "Can't move $tmpname -> index: $!";

    [200];
}

1;
# ABSTRACT: Manage pool-style repo directory

__END__



( run in 0.628 second using v1.01-cache-2.11-cpan-2398b32b56e )