Skip to content

Commit

Permalink
Add include_file option (#337)
Browse files Browse the repository at this point in the history
* Add `include_file` option
  • Loading branch information
silverwind authored Oct 24, 2022
1 parent b2e6005 commit 25c287e
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
2 changes: 2 additions & 0 deletions air_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ include_ext = ["go", "tpl", "tmpl", "html"]
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# Watch these directories if you specified.
include_dir = []
# Watch these files.
include_file = []
# Exclude files.
exclude_file = []
# Exclude specific regular expressions.
Expand Down
2 changes: 2 additions & 0 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type cfgBuild struct {
ExcludeDir []string `toml:"exclude_dir"`
IncludeDir []string `toml:"include_dir"`
ExcludeFile []string `toml:"exclude_file"`
IncludeFile []string `toml:"include_file"`
ExcludeRegex []string `toml:"exclude_regex"`
ExcludeUnchanged bool `toml:"exclude_unchanged"`
FollowSymlink bool `toml:"follow_symlink"`
Expand Down Expand Up @@ -207,6 +208,7 @@ func defaultConfig() Config {
IncludeExt: []string{"go", "tpl", "tmpl", "html"},
IncludeDir: []string{},
ExcludeFile: []string{},
IncludeFile: []string{},
ExcludeDir: []string{"assets", "tmp", "vendor", "testdata"},
ArgsBin: []string{},
ExcludeRegex: []string{"_test.go"},
Expand Down
9 changes: 6 additions & 3 deletions runner/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func (e *Engine) watching(root string) error {
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
// NOTE: path is absolute
if info != nil && !info.IsDir() {
if e.checkIncludeFile(path) {
return e.watchPath(path)
}
return nil
}
// exclude tmp dir
Expand Down Expand Up @@ -138,7 +141,7 @@ func (e *Engine) watching(root string) error {
return filepath.SkipDir
}
if isIn {
return e.watchDir(path)
return e.watchPath(path)
}
return nil
})
Expand Down Expand Up @@ -174,7 +177,7 @@ func (e *Engine) cacheFileChecksums(root string) error {
return err
}
if linkInfo.IsDir() {
err = e.watchDir(link)
err = e.watchPath(link)
if err != nil {
return err
}
Expand Down Expand Up @@ -204,7 +207,7 @@ func (e *Engine) cacheFileChecksums(root string) error {
})
}

func (e *Engine) watchDir(path string) error {
func (e *Engine) watchPath(path string) error {
if err := e.watcher.Add(path); err != nil {
e.watcherLog("failed to watch %s, error: %s", path, err.Error())
return err
Expand Down
63 changes: 63 additions & 0 deletions runner/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ bin = "tmp/main"
exclude_regex = []
exclude_dir = ["test"]
exclude_file = ["main.go"]
include_file = ["test/not_a_test.go"]
`
if err := ioutil.WriteFile(dftTOML, []byte(config), 0644); err != nil {
Expand All @@ -679,6 +680,7 @@ exclude_file = ["main.go"]
assert.Equal(t, []string{"test"}, engine.config.Build.ExcludeDir)
// add new config
assert.Equal(t, []string{"main.go"}, engine.config.Build.ExcludeFile)
assert.Equal(t, []string{"test/not_a_test.go"}, engine.config.Build.IncludeFile)
assert.Equal(t, "go build .", engine.config.Build.Cmd)

}
Expand Down Expand Up @@ -794,3 +796,64 @@ func TestCreateNewDir(t *testing.T) {
time.Sleep(2 * time.Second)

}

func TestShouldIncludeIncludedFile(t *testing.T) {
port, f := GetPort()
f()
t.Logf("port: %d", port)

tmpDir := initTestEnv(t, port)

if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}

config := `
[build]
cmd = "true" # do nothing
full_bin = "sh main.sh"
include_ext = ["sh"]
include_dir = ["nonexist"] # prevent default "." watch from taking effect
include_file = ["main.sh"]
`
if err := ioutil.WriteFile(dftTOML, []byte(config), 0644); err != nil {
t.Fatal(err)
}

err := os.WriteFile("main.sh", []byte("#!/bin/sh\nprintf original > output"), 0755)
if err != nil {
t.Fatal(err)
}

engine, err := NewEngine(dftTOML, false)
if err != nil {
t.Fatal(err)
}
go func() {
engine.Run()
}()

time.Sleep(time.Second * 1)

bytes, err := os.ReadFile("output")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, []byte("original"), bytes)

t.Logf("start change main.sh")
go func() {
err := os.WriteFile("main.sh", []byte("#!/bin/sh\nprintf modified > output"), 0755)
if err != nil {
t.Fatalf("Error updating file: %s.", err)
}
}()

time.Sleep(time.Second * 3)

bytes, err = os.ReadFile("output")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, []byte("modified"), bytes)
}
17 changes: 17 additions & 0 deletions runner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ func (e *Engine) checkIncludeDir(path string) (bool, bool) {
return false, walkDir
}

func (e *Engine) checkIncludeFile(path string) bool {
cleanName := cleanPath(e.config.rel(path))
iFile := e.config.Build.IncludeFile
if len(iFile) == 0 { // ignore empty
return false
}
if cleanName == "." {
return false
}
for _, d := range iFile {
if d == cleanName {
return true
}
}
return false
}

func (e *Engine) isIncludeExt(path string) bool {
ext := filepath.Ext(path)
for _, v := range e.config.Build.IncludeExt {
Expand Down
13 changes: 13 additions & 0 deletions runner/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,16 @@ func TestNestStructArrayValueOverride(t *testing.T) {
setValue2Struct(v, "Build.ExcludeDir", "dir1,dir2")
assert.Equal(t, []string{"dir1", "dir2"}, c.Build.ExcludeDir)
}

func TestCheckIncludeFile(t *testing.T) {
e := Engine{
config: &Config{
Build: cfgBuild{
IncludeFile: []string{"main.go"},
},
},
}
assert.Equal(t, e.checkIncludeFile("main.go"), true)
assert.Equal(t, e.checkIncludeFile("no.go"), false)
assert.Equal(t, e.checkIncludeFile("."), false)
}

0 comments on commit 25c287e

Please sign in to comment.