Skip to content

Commit 4af086f

Browse files
authored
Add -scala_gazelle_imports_file (#129)
1 parent 1f03f78 commit 4af086f

File tree

7 files changed

+161
-19
lines changed

7 files changed

+161
-19
lines changed

build/stack/gazelle/scala/cache/cache.pb.go

Lines changed: 91 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/stack/gazelle/scala/cache/cache.proto

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ option go_package = "github.com/stackb/scala-gazelle/build/stack/gazelle/scala/c
88
option java_package = "build.stack.gazelle.scala.cache";
99
option java_multiple_files = true;
1010

11-
// Cache represents the scala gazelle cache.
11+
// Cache represents the scala gazelle file parse rule cache.
1212
message Cache {
1313
// package_count is the number of packages visited
1414
// during the generation phase.
@@ -20,3 +20,9 @@ message Cache {
2020
string key = 3;
2121
}
2222

23+
// Resolved imports is a mapping between a fully-qualified scala import type and
24+
// the bazel label that provides it.
25+
message ResolvedImports {
26+
map<string,string> imports = 1;
27+
}
28+

language/scala/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ go_library(
1717
"fix.go",
1818
"flags.go",
1919
"generate.go",
20+
"imports.go",
2021
"kinds.go",
2122
"known_rule_registry.go",
2223
"language.go",

language/scala/flags.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
existingScalaTestRuleFlagName = "existing_scala_test_rule"
2727
existingScalaRuleCoverageFlagName = "existing_scala_rule_coverage"
2828
scalaGazelleCacheFileFlagName = "scala_gazelle_cache_file"
29+
scalaGazelleImportsFileFlagName = "scala_gazelle_imports_file"
2930
scalaGazelleDebugProcessFileFlagName = "scala_gazelle_debug_process"
3031
scalaGazelleCacheKeyFlagName = "scala_gazelle_cache_key"
3132
scalaGazellePrintCacheKeyFlagName = "scala_gazelle_print_cache_key"
@@ -39,6 +40,7 @@ func (sl *scalaLang) RegisterFlags(flags *flag.FlagSet, cmd string, c *config.Co
3940
flags.BoolVar(&sl.printCacheKey, scalaGazellePrintCacheKeyFlagName, true, "if a cache key is set, print the version for auditing purposes")
4041
flags.BoolVar(&sl.existingScalaRuleCoverageFlagValue, existingScalaRuleCoverageFlagName, true, "report coverage statistics")
4142
flags.StringVar(&sl.cacheFileFlagValue, scalaGazelleCacheFileFlagName, "", "optional path a cache file (.json or .pb)")
43+
flags.StringVar(&sl.importsFileFlagValue, scalaGazelleImportsFileFlagName, "", "optional path to an imports file where resolved imports should be written (.json or .pb)")
4244
flags.StringVar(&sl.cacheKeyFlagValue, scalaGazelleCacheKeyFlagName, "", "optional string that can be used to bust the cache file")
4345
flags.StringVar(&sl.cpuprofileFlagValue, cpuprofileFileFlagName, "", "optional path a cpuprofile file (.prof)")
4446
flags.StringVar(&sl.memprofileFlagValue, memprofileFileFlagName, "", "optional path a memory profile file (.prof)")
@@ -236,6 +238,16 @@ func (sl *scalaLang) setupCache() error {
236238
return nil
237239
}
238240

241+
func (sl *scalaLang) dumpResolvedImportMap() {
242+
if sl.importsFileFlagValue == "" {
243+
return
244+
}
245+
filename := os.ExpandEnv(sl.importsFileFlagValue)
246+
if err := sl.writeResolvedImportsMapFile(filename); err != nil {
247+
log.Fatalf("writing resolved imports: %v", err)
248+
}
249+
}
250+
239251
func (sl *scalaLang) setupCpuProfiling(workDir string) error {
240252
if sl.cpuprofileFlagValue != "" {
241253
if !filepath.IsAbs(sl.cpuprofileFlagValue) {

language/scala/imports.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package scala
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/bazelbuild/bazel-gazelle/label"
10+
scpb "github.com/stackb/scala-gazelle/build/stack/gazelle/scala/cache"
11+
"github.com/stackb/scala-gazelle/pkg/protobuf"
12+
)
13+
14+
func (sl *scalaLang) writeResolvedImportsMapFile(filename string) error {
15+
imports := &scpb.ResolvedImports{
16+
Imports: make(map[string]string),
17+
}
18+
19+
for _, sym := range sl.globalScope.GetSymbols("") {
20+
dep := "NO_LABEL"
21+
if sym.Label != label.NoLabel {
22+
dep = sym.Label.String()
23+
}
24+
imports.Imports[sym.Name] = dep
25+
}
26+
27+
if filepath.Ext(filename) == ".txt" {
28+
f, err := os.Create(filename)
29+
if err != nil {
30+
return fmt.Errorf("create: %w", err)
31+
}
32+
for k, v := range imports.Imports {
33+
fmt.Fprintln(f, k, v)
34+
}
35+
if err := f.Close(); err != nil {
36+
return fmt.Errorf("close: %w", err)
37+
}
38+
} else {
39+
if err := protobuf.WriteFile(filename, imports); err != nil {
40+
return err
41+
}
42+
}
43+
44+
log.Printf("Wrote scala-gazelle import map %s", filename)
45+
46+
return nil
47+
}

language/scala/language.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type scalaLang struct {
3434
cacheFileFlagValue string
3535
// cacheKeyFlagValue is the main cache key, if enabled
3636
cacheKeyFlagValue string
37+
// importsFileFlagValue is the name of a file to dump resolved import map to, if enabled
38+
importsFileFlagValue string
3739
// symbolProviderNamesFlagValue is a repeatable list of resolver to enable
3840
symbolProviderNamesFlagValue collections.StringSlice
3941
// conflictResolverNamesFlagValue is a repeatable list of conflict resolver

language/scala/lifecycle.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func (sl *scalaLang) onEnd() {
3737
}
3838
}
3939

40+
sl.dumpResolvedImportMap()
4041
sl.reportCoverage(log.Printf)
4142
sl.stopCpuProfiling()
4243
sl.stopMemoryProfiling()

0 commit comments

Comments
 (0)