Open-This

 view release on metacpan or  search on metacpan

lib/Open/This.pm  view on Meta::CPAN


    # Ansible: The error appears to be in '/path/to/file':
    if ( $$text =~ m{'(.*)'} ) {
        my $maybe_file = $1;
        $$text = $maybe_file if -e $maybe_file;
    }
}

sub _maybe_extract_subroutine_name {
    my $text = shift;    # scalar ref

    if ( $$text =~ s{::(\w+)\(.*\)}{} ) {
        return $1;
    }
    return undef;
}

sub _maybe_extract_line_number_via_sub_name {
    my $file_name = shift;
    my $sub_name  = shift;

    if ( $file_name && $sub_name && open( my $fh, '<', $file_name ) ) {
        my $line_number = 1;
        while ( my $line = <$fh> ) {
            if ( $line =~ m{sub $sub_name\b} ) {
                return $line_number;
            }
            ++$line_number;
        }
    }
}

sub _maybe_extract_file_from_url {
    my $text = shift;

    require_module('URI');
    my $uri = URI->new($$text);

    my @parts = $uri->path_segments;

    # Is this a GitHub(ish) URL?
    if ( $parts[3] eq 'blob' ) {
        my $file = path( @parts[ 5 .. scalar @parts - 1 ] );

        return unless $file->is_file;

        $file = $file->stringify;

        if ( $uri->fragment && $uri->fragment =~ m{\A[\d]\z} ) {
            $file .= ':' . $uri->fragment;
        }
        return $file if $file;
    }
}

sub _maybe_find_local_file {
    my $text          = shift;
    my $possible_name = module_notional_filename($text);
    my @dirs
        = exists $ENV{OPEN_THIS_LIBS}
        ? split m{,}, $ENV{OPEN_THIS_LIBS}
        : ( 'lib', 't/lib' );

    for my $dir (@dirs) {
        my $path = path( $dir, $possible_name );
        if ( $path->is_file ) {
            return "$path";
        }
    }
    return undef;
}

sub _maybe_git_diff_path {
    my $file_name = shift;

    if ( $file_name =~ m|^[ab]/(.+)$| ) {
        if ( -e path($1) ) {
            return $1;
        }
    }

    return undef;
}

# search for binaries in path

sub _which {
    my $maybe_file = shift;
    return unless $maybe_file;

    require_module('File::Spec');

    # we only want binary names here -- no paths
    my ( $volume, $dir, $file ) = File::Spec->splitpath($maybe_file);
    return if $dir;

    my @path = File::Spec->path;
    for my $path (@path) {
        my $abs = path( $path, $file );
        return $abs->stringify if $abs->is_file;
    }

    return;
}

sub _find_go_files {
    my $text           = shift;
    my $threshold_init = shift || 5000;

    my $file_name;
    my $iter      = path('.')->iterator( { recurse => 1 } );
    my $threshold = $threshold_init;
    while ( my $path = $iter->() ) {
        next unless $path->is_file;    # dirs will never match anything
        ( $threshold, $file_name ) = ( 0, "$path" )
            if $path->basename eq $text;
        last if --$threshold == 0;
    }
    if ( $threshold == 0 && !$file_name ) {
        warn "Only $threshold_init files searched recursively";
    }



( run in 0.772 second using v1.01-cache-2.11-cpan-71847e10f99 )