Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update copy_to_directory tool to accept the name of its workspace and automatically include files from it #488

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions tools/copy_to_directory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type config struct {
Verbose bool `json:"verbose"`

ReplacePrefixesKeys []string
TargetWorkspace *string
}

type copyMap map[string]fileInfo
Expand All @@ -52,7 +53,7 @@ type pathSet map[string]bool
var copySet = copyMap{}
var mkdirSet = pathSet{}

func parseConfig(configPath string) (*config, error) {
func parseConfig(configPath string, wkspName *string) (*config, error) {
f, err := os.Open(configPath)
if err != nil {
return nil, fmt.Errorf("failed to open config file: %w", err)
Expand All @@ -70,6 +71,7 @@ func parseConfig(configPath string) (*config, error) {
}

cfg.ReplacePrefixesKeys = maps.Keys(cfg.ReplacePrefixes)
cfg.TargetWorkspace = wkspName

return &cfg, nil
}
Expand Down Expand Up @@ -230,7 +232,9 @@ func (w *walker) copyPath(cfg *config, file fileInfo) error {
outputRoot := path.Dir(outputPath)

// apply include_external_repositories (if the file is from an external repository)
if file.Workspace != "" {
// automatically include files from the same workspace as this target, even if
// that is an external workspace with respect to `__main__`
if file.Workspace != "" && (cfg.TargetWorkspace == nil || file.Workspace != *cfg.TargetWorkspace) {
match, err := anyGlobsMatch(cfg.IncludeExternalRepositories, file.Workspace)
if err != nil {
return err
Expand Down Expand Up @@ -376,12 +380,20 @@ func main() {
}
}

if len(args) != 1 {
fmt.Println("Usage: copy_to_directory config_file")
if len(args) != 1 && len(args) != 2 {
fmt.Println("Usage: copy_to_directory config_file [workspace_name]")
os.Exit(1)
}

cfg, err := parseConfig(args[0])
configFile := args[0]

// Read workspace arg if present.
var wksp *string = nil
if len(args) >= 2 {
wksp = &args[1]
}

cfg, err := parseConfig(configFile, wksp)
if err != nil {
log.Fatal(err)
}
Expand Down