Skip to content

Commit

Permalink
Add -scala_gazelle_imports_file (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcj authored Sep 26, 2024
1 parent 1f03f78 commit 4af086f
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 19 deletions.
109 changes: 91 additions & 18 deletions build/stack/gazelle/scala/cache/cache.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion build/stack/gazelle/scala/cache/cache.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ option go_package = "github.com/stackb/scala-gazelle/build/stack/gazelle/scala/c
option java_package = "build.stack.gazelle.scala.cache";
option java_multiple_files = true;

// Cache represents the scala gazelle cache.
// Cache represents the scala gazelle file parse rule cache.
message Cache {
// package_count is the number of packages visited
// during the generation phase.
Expand All @@ -20,3 +20,9 @@ message Cache {
string key = 3;
}

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

1 change: 1 addition & 0 deletions language/scala/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
"fix.go",
"flags.go",
"generate.go",
"imports.go",
"kinds.go",
"known_rule_registry.go",
"language.go",
Expand Down
12 changes: 12 additions & 0 deletions language/scala/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
existingScalaTestRuleFlagName = "existing_scala_test_rule"
existingScalaRuleCoverageFlagName = "existing_scala_rule_coverage"
scalaGazelleCacheFileFlagName = "scala_gazelle_cache_file"
scalaGazelleImportsFileFlagName = "scala_gazelle_imports_file"
scalaGazelleDebugProcessFileFlagName = "scala_gazelle_debug_process"
scalaGazelleCacheKeyFlagName = "scala_gazelle_cache_key"
scalaGazellePrintCacheKeyFlagName = "scala_gazelle_print_cache_key"
Expand All @@ -39,6 +40,7 @@ func (sl *scalaLang) RegisterFlags(flags *flag.FlagSet, cmd string, c *config.Co
flags.BoolVar(&sl.printCacheKey, scalaGazellePrintCacheKeyFlagName, true, "if a cache key is set, print the version for auditing purposes")
flags.BoolVar(&sl.existingScalaRuleCoverageFlagValue, existingScalaRuleCoverageFlagName, true, "report coverage statistics")
flags.StringVar(&sl.cacheFileFlagValue, scalaGazelleCacheFileFlagName, "", "optional path a cache file (.json or .pb)")
flags.StringVar(&sl.importsFileFlagValue, scalaGazelleImportsFileFlagName, "", "optional path to an imports file where resolved imports should be written (.json or .pb)")
flags.StringVar(&sl.cacheKeyFlagValue, scalaGazelleCacheKeyFlagName, "", "optional string that can be used to bust the cache file")
flags.StringVar(&sl.cpuprofileFlagValue, cpuprofileFileFlagName, "", "optional path a cpuprofile file (.prof)")
flags.StringVar(&sl.memprofileFlagValue, memprofileFileFlagName, "", "optional path a memory profile file (.prof)")
Expand Down Expand Up @@ -236,6 +238,16 @@ func (sl *scalaLang) setupCache() error {
return nil
}

func (sl *scalaLang) dumpResolvedImportMap() {
if sl.importsFileFlagValue == "" {
return
}
filename := os.ExpandEnv(sl.importsFileFlagValue)
if err := sl.writeResolvedImportsMapFile(filename); err != nil {
log.Fatalf("writing resolved imports: %v", err)
}
}

func (sl *scalaLang) setupCpuProfiling(workDir string) error {
if sl.cpuprofileFlagValue != "" {
if !filepath.IsAbs(sl.cpuprofileFlagValue) {
Expand Down
47 changes: 47 additions & 0 deletions language/scala/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package scala

import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/bazelbuild/bazel-gazelle/label"
scpb "github.com/stackb/scala-gazelle/build/stack/gazelle/scala/cache"
"github.com/stackb/scala-gazelle/pkg/protobuf"
)

func (sl *scalaLang) writeResolvedImportsMapFile(filename string) error {
imports := &scpb.ResolvedImports{
Imports: make(map[string]string),
}

for _, sym := range sl.globalScope.GetSymbols("") {
dep := "NO_LABEL"
if sym.Label != label.NoLabel {
dep = sym.Label.String()
}
imports.Imports[sym.Name] = dep
}

if filepath.Ext(filename) == ".txt" {
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("create: %w", err)
}
for k, v := range imports.Imports {
fmt.Fprintln(f, k, v)
}
if err := f.Close(); err != nil {
return fmt.Errorf("close: %w", err)
}
} else {
if err := protobuf.WriteFile(filename, imports); err != nil {
return err
}
}

log.Printf("Wrote scala-gazelle import map %s", filename)

return nil
}
2 changes: 2 additions & 0 deletions language/scala/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type scalaLang struct {
cacheFileFlagValue string
// cacheKeyFlagValue is the main cache key, if enabled
cacheKeyFlagValue string
// importsFileFlagValue is the name of a file to dump resolved import map to, if enabled
importsFileFlagValue string
// symbolProviderNamesFlagValue is a repeatable list of resolver to enable
symbolProviderNamesFlagValue collections.StringSlice
// conflictResolverNamesFlagValue is a repeatable list of conflict resolver
Expand Down
1 change: 1 addition & 0 deletions language/scala/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (sl *scalaLang) onEnd() {
}
}

sl.dumpResolvedImportMap()
sl.reportCoverage(log.Printf)
sl.stopCpuProfiling()
sl.stopMemoryProfiling()
Expand Down

0 comments on commit 4af086f

Please sign in to comment.