diff --git a/internal/sidekick/internal/config/config.go b/internal/sidekick/internal/config/config.go index b94363c9a5..bc1acc6089 100644 --- a/internal/sidekick/internal/config/config.go +++ b/internal/sidekick/internal/config/config.go @@ -66,10 +66,11 @@ type Config struct { // GeneralConfig contains configuration parameters that affect Parsers and Codecs, including the // selection of parser and codec. type GeneralConfig struct { - Language string `toml:"language,omitempty"` - SpecificationFormat string `toml:"specification-format,omitempty"` - SpecificationSource string `toml:"specification-source,omitempty"` - ServiceConfig string `toml:"service-config,omitempty"` + Language string `toml:"language,omitempty"` + SpecificationFormat string `toml:"specification-format,omitempty"` + SpecificationSource string `toml:"specification-source,omitempty"` + ServiceConfig string `toml:"service-config,omitempty"` + IgnoredDirectories []string `toml:"ignored-directories,omitempty"` } // LoadConfig loads the top-level configuration file and validates its contents. @@ -130,6 +131,7 @@ func mergeConfigs(rootConfig, local *Config) (*Config, error) { General: GeneralConfig{ Language: rootConfig.General.Language, SpecificationFormat: rootConfig.General.SpecificationFormat, + IgnoredDirectories: rootConfig.General.IgnoredDirectories, }, Source: map[string]string{}, Codec: map[string]string{}, diff --git a/internal/sidekick/internal/config/config_test.go b/internal/sidekick/internal/config/config_test.go index e9a4db346b..b8e10479ca 100644 --- a/internal/sidekick/internal/config/config_test.go +++ b/internal/sidekick/internal/config/config_test.go @@ -69,6 +69,7 @@ func TestLoadRootConfig(t *testing.T) { General: GeneralConfig{ Language: "root-language", SpecificationFormat: "root-specification-format", + IgnoredDirectories: []string{"a", "b"}, }, Source: map[string]string{ "s1": "v1", @@ -101,6 +102,7 @@ func TestMergeLocalForGeneral(t *testing.T) { General: GeneralConfig{ Language: "root-language", SpecificationFormat: "root-specification-format", + IgnoredDirectories: []string{"a", "b"}, }, } @@ -123,6 +125,7 @@ func TestMergeLocalForGeneral(t *testing.T) { SpecificationFormat: "local-specification-format", SpecificationSource: "local-specification-source", ServiceConfig: "local-service-config", + IgnoredDirectories: []string{"a", "b"}, }, Codec: map[string]string{}, Source: map[string]string{}, diff --git a/internal/sidekick/refreshall.go b/internal/sidekick/refreshall.go index 1990a46bf1..d6a302ffd8 100644 --- a/internal/sidekick/refreshall.go +++ b/internal/sidekick/refreshall.go @@ -22,6 +22,7 @@ import ( "os" "path" "path/filepath" + "slices" "strings" "sync" @@ -68,7 +69,7 @@ func refreshAll(rootConfig *config.Config, cmdLine *CommandLine) error { if err != nil { return err } - directories, err := findAllDirectories() + directories, err := findAllDirectories(override) if err != nil { return err } @@ -104,28 +105,18 @@ func refreshAll(rootConfig *config.Config, cmdLine *CommandLine) error { return errors.Join(failures...) } -func findAllDirectories() ([]string, error) { +func findAllDirectories(config *config.Config) ([]string, error) { var result []string err := fs.WalkDir(os.DirFS("."), ".", func(path string, d fs.DirEntry, _ error) error { if d.IsDir() { return nil } - dir := filepath.Dir(path) - - // TODO(https://github.com/googleapis/librarian/issues/1563): do not - // harcode - if dir == "dart" { - return nil - } - ignored := []string{ - "dart/", // Testing - "target/package/", // The output from `cargo package` - } - for _, candidate := range ignored { - if strings.Contains(dir, candidate) { + for _, ignore := range config.General.IgnoredDirectories { + if isInPath(ignore, path) { return nil } } + dir := filepath.Dir(path) if d.Name() == ".sidekick.toml" && dir != "." { result = append(result, dir) } @@ -136,3 +127,8 @@ func findAllDirectories() ([]string, error) { } return result, nil } + +func isInPath(dir, path string) bool { + components := strings.Split(path, string(filepath.Separator)) + return slices.Contains(components, dir) +} diff --git a/internal/sidekick/refreshall_test.go b/internal/sidekick/refreshall_test.go index 3e25c1153d..9b708e73d6 100644 --- a/internal/sidekick/refreshall_test.go +++ b/internal/sidekick/refreshall_test.go @@ -21,3 +21,26 @@ func TestRefreshAll(t *testing.T) { t.Fatal(err) } } + +func TestIsInPath(t *testing.T) { + type TestCase struct { + Dir string + Path string + Want bool + } + testCases := []TestCase{ + {"dart", "dart", true}, + {"dart", "dartboard", false}, + {"dart", "dart/v2", true}, + {"dart", "dart/v2/d2/d4", true}, + {"dart", "a/b/c/d/dart/v2/d2/d4", true}, + {"generator", "dart/v2", false}, + {"generator", "generator/", true}, + } + for _, test := range testCases { + got := isInPath(test.Dir, test.Path) + if got != test.Want { + t.Errorf("got (%v) != want (%v) for %q in %q", got, test.Want, test.Dir, test.Path) + } + } +}