#!/usr/bin/env raku

# Gets a list of methods from each file listed on the command line, then prints out methods which have the same name
# in all the mentioned files

sub	MAIN(*@files) {
	my $proc = run 'ack', 'method', |@files, :out, :err;
	$proc.err.slurp(:close).say;

	my %methods;
	for $proc.out.lines(:close) -> $line {
		my ($filename, $lineno, @text) = $line.split(/\:/);
		my $text = join ':', @text;

		my $module = $filename;
		$module ~~ s/^lib\/(.*)\.rakumod$/$0/;
		$module ~~ s:g/\//::/;

		$text ~~ s/^\s*(.*)\s*$/$0/;
		my @method-parts = $text.split(/\s+/);
		my $method-name = @method-parts[1];
		$method-name ~~ s/^(\!?<[\w\-]>+).*/$0/;

		%methods{$module}{$method-name} = join " ", @method-parts;
	}

	my @unique-keys = gather {
		for %methods.keys -> $module {
			take %methods{$module}.keys
		}
	}.flat.unique.sort;

	say @unique-keys.raku;

	UNIQUE-KEY: for @unique-keys -> $method-name {
		for %methods.keys -> $module {
			%methods{$module}{$method-name}:exists or next UNIQUE-KEY;
		}
		for %methods.keys -> $module {
			printf "%-30s %-20s %s\n", $method-name, $module, %methods{$module}{$method-name};
		}
	}
}
