Skip to content
Open
Show file tree
Hide file tree
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
60 changes: 60 additions & 0 deletions realize/findfiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package realize

import (
"go/build"
)

// FindGoFiles returns a list of .go source files that are
// required to build the package located in dir.
// This includes .go files of all dependencies
// except for source files from the standard library.
func FindGoFiles(dir string) ([]string, error) {
p, err := build.ImportDir(dir, 0)
if err != nil {
return nil, err
}

// Keep track of the imports we still have to process.
imports := p.Imports

// Keep track of which packages we already processed
// and which directory they were imported from.
seen := make(map[string]string)
for _, i := range imports {
seen[i] = p.Dir
}

files := make([]string, 0)
for _, f := range p.GoFiles {
files = append(files, p.Dir+"/"+f)
}

// Keep going until we have no imports left.
for len(imports) > 0 {
name := imports[0]
imports = imports[1:]

p, err := build.Import(name, seen[name], 0)
if err != nil {
return nil, err
}

// Ignore the standard library packages.
if p.Goroot {
continue
}

for _, name := range p.Imports {
if _, ok := seen[name]; !ok {
seen[name] = p.Dir
imports = append(imports, name)
}
}

for _, f := range p.GoFiles {
files = append(files, p.Dir+"/"+f)
}
}

return files, nil
}
43 changes: 38 additions & 5 deletions realize/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ var (

// Watch info
type Watch struct {
Exts []string `yaml:"extensions" json:"extensions"`
Paths []string `yaml:"paths" json:"paths"`
Scripts []Command `yaml:"scripts,omitempty" json:"scripts,omitempty"`
Hidden bool `yaml:"hidden,omitempty" json:"hidden,omitempty"`
Ignore []string `yaml:"ignored_paths,omitempty" json:"ignored_paths,omitempty"`
Exts []string `yaml:"extensions" json:"extensions"`
Paths []string `yaml:"paths" json:"paths"`
Scripts []Command `yaml:"scripts,omitempty" json:"scripts,omitempty"`
Hidden bool `yaml:"hidden,omitempty" json:"hidden,omitempty"`
Ignore []string `yaml:"ignored_paths,omitempty" json:"ignored_paths,omitempty"`
Dependencies bool `yaml:"dependencies,omitempty" json:"dependencies,omitempty"`
}

type Ignore struct {
Expand Down Expand Up @@ -128,6 +129,22 @@ func (p *Project) Before() {
base, _ := filepath.Abs(p.Path)
base = filepath.Join(base, dir)
if _, err := os.Stat(base); err == nil {
if p.Watcher.Dependencies {
// Find all relevant Go files to this package and add them.
if files, err := FindGoFiles(base); err != nil {
p.Err(err)
} else {
for _, path := range files {
info, err := os.Stat(path)
if err != nil {
p.Err(err)
} else {
p.walk(path, info, nil)
}
}
}
}

if err := filepath.Walk(base, p.walk); err != nil {
p.Err(err)
}
Expand Down Expand Up @@ -318,6 +335,22 @@ L:
continue
}
if fi.IsDir() {
if p.Watcher.Dependencies {
// This directory changed so find all relevant Go files again.
if files, err := FindGoFiles(event.Name); err != nil {
p.Err(err)
} else {
for _, path := range files {
info, err := os.Stat(path)
if err != nil {
p.Err(err)
} else {
p.walk(path, info, nil)
}
}
}
}

filepath.Walk(event.Name, p.walk)
} else {
// stop and restart
Expand Down