-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoimport
executable file
·55 lines (48 loc) · 1 KB
/
autoimport
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! /usr/bin/perl
# Quick and dirty auto-importer for Go-sources.
use strict;
my %lib;
open LIB, "cd $ENV{GOROOT}/pkg && find .|" or die;
while (<LIB>) {
chop;
next if !s/[.]a$//;
s/^\.\/$ENV{GOOS}_$ENV{GOARCH}\///;
/([^\/]+)$/;
$lib{$1} = $_;
}
close LIB;
my $file = $ARGV[0];
$file =~ s/\.go$//;
my $orig = "";
my $source = "";
while (<>) {
$orig .= $_;
s/\/\/.*$/\n/;
$source .= $_;
}
while ($source =~ /\/[*]/) {
my $p = $-[0];
$source =~ /[*]\// or last;
substr($source, $p, $+[0]-$p) = "";
}
my %used;
$used{$1} = 1 while $source =~ /([a-z0-9]+)\./g;
my @imports;
for (sort keys %used) {
next if $_ eq $file;
if (-f "$_.go") {
push @imports, $_
} else {
push @imports, $lib{$_} if defined $lib{$_};
}
}
my $import = "";
if (scalar @imports) {
$import .= "import (\n";
$import .= " \"". join("\"\n \"", sort @imports). "\"\n";
$import .= ")\n";
}
$orig =~ s/import "[^"]+"\n?//g;
$orig =~ s/import \([^)]+\)\n?//g;
$orig =~ s/(package \S+\n\n?)/$1$import/;
print $orig;