Skip to content

Commit 25c287e

Browse files
authored
Add include_file option (#337)
* Add `include_file` option
1 parent b2e6005 commit 25c287e

File tree

6 files changed

+103
-3
lines changed

6 files changed

+103
-3
lines changed

air_example.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ include_ext = ["go", "tpl", "tmpl", "html"]
1818
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
1919
# Watch these directories if you specified.
2020
include_dir = []
21+
# Watch these files.
22+
include_file = []
2123
# Exclude files.
2224
exclude_file = []
2325
# Exclude specific regular expressions.

runner/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type cfgBuild struct {
4343
ExcludeDir []string `toml:"exclude_dir"`
4444
IncludeDir []string `toml:"include_dir"`
4545
ExcludeFile []string `toml:"exclude_file"`
46+
IncludeFile []string `toml:"include_file"`
4647
ExcludeRegex []string `toml:"exclude_regex"`
4748
ExcludeUnchanged bool `toml:"exclude_unchanged"`
4849
FollowSymlink bool `toml:"follow_symlink"`
@@ -207,6 +208,7 @@ func defaultConfig() Config {
207208
IncludeExt: []string{"go", "tpl", "tmpl", "html"},
208209
IncludeDir: []string{},
209210
ExcludeFile: []string{},
211+
IncludeFile: []string{},
210212
ExcludeDir: []string{"assets", "tmp", "vendor", "testdata"},
211213
ArgsBin: []string{},
212214
ExcludeRegex: []string{"_test.go"},

runner/engine.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ func (e *Engine) watching(root string) error {
111111
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
112112
// NOTE: path is absolute
113113
if info != nil && !info.IsDir() {
114+
if e.checkIncludeFile(path) {
115+
return e.watchPath(path)
116+
}
114117
return nil
115118
}
116119
// exclude tmp dir
@@ -138,7 +141,7 @@ func (e *Engine) watching(root string) error {
138141
return filepath.SkipDir
139142
}
140143
if isIn {
141-
return e.watchDir(path)
144+
return e.watchPath(path)
142145
}
143146
return nil
144147
})
@@ -174,7 +177,7 @@ func (e *Engine) cacheFileChecksums(root string) error {
174177
return err
175178
}
176179
if linkInfo.IsDir() {
177-
err = e.watchDir(link)
180+
err = e.watchPath(link)
178181
if err != nil {
179182
return err
180183
}
@@ -204,7 +207,7 @@ func (e *Engine) cacheFileChecksums(root string) error {
204207
})
205208
}
206209

207-
func (e *Engine) watchDir(path string) error {
210+
func (e *Engine) watchPath(path string) error {
208211
if err := e.watcher.Add(path); err != nil {
209212
e.watcherLog("failed to watch %s, error: %s", path, err.Error())
210213
return err

runner/engine_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ bin = "tmp/main"
665665
exclude_regex = []
666666
exclude_dir = ["test"]
667667
exclude_file = ["main.go"]
668+
include_file = ["test/not_a_test.go"]
668669
669670
`
670671
if err := ioutil.WriteFile(dftTOML, []byte(config), 0644); err != nil {
@@ -679,6 +680,7 @@ exclude_file = ["main.go"]
679680
assert.Equal(t, []string{"test"}, engine.config.Build.ExcludeDir)
680681
// add new config
681682
assert.Equal(t, []string{"main.go"}, engine.config.Build.ExcludeFile)
683+
assert.Equal(t, []string{"test/not_a_test.go"}, engine.config.Build.IncludeFile)
682684
assert.Equal(t, "go build .", engine.config.Build.Cmd)
683685

684686
}
@@ -794,3 +796,64 @@ func TestCreateNewDir(t *testing.T) {
794796
time.Sleep(2 * time.Second)
795797

796798
}
799+
800+
func TestShouldIncludeIncludedFile(t *testing.T) {
801+
port, f := GetPort()
802+
f()
803+
t.Logf("port: %d", port)
804+
805+
tmpDir := initTestEnv(t, port)
806+
807+
if err := os.Chdir(tmpDir); err != nil {
808+
t.Fatal(err)
809+
}
810+
811+
config := `
812+
[build]
813+
cmd = "true" # do nothing
814+
full_bin = "sh main.sh"
815+
include_ext = ["sh"]
816+
include_dir = ["nonexist"] # prevent default "." watch from taking effect
817+
include_file = ["main.sh"]
818+
`
819+
if err := ioutil.WriteFile(dftTOML, []byte(config), 0644); err != nil {
820+
t.Fatal(err)
821+
}
822+
823+
err := os.WriteFile("main.sh", []byte("#!/bin/sh\nprintf original > output"), 0755)
824+
if err != nil {
825+
t.Fatal(err)
826+
}
827+
828+
engine, err := NewEngine(dftTOML, false)
829+
if err != nil {
830+
t.Fatal(err)
831+
}
832+
go func() {
833+
engine.Run()
834+
}()
835+
836+
time.Sleep(time.Second * 1)
837+
838+
bytes, err := os.ReadFile("output")
839+
if err != nil {
840+
t.Fatal(err)
841+
}
842+
assert.Equal(t, []byte("original"), bytes)
843+
844+
t.Logf("start change main.sh")
845+
go func() {
846+
err := os.WriteFile("main.sh", []byte("#!/bin/sh\nprintf modified > output"), 0755)
847+
if err != nil {
848+
t.Fatalf("Error updating file: %s.", err)
849+
}
850+
}()
851+
852+
time.Sleep(time.Second * 3)
853+
854+
bytes, err = os.ReadFile("output")
855+
if err != nil {
856+
t.Fatal(err)
857+
}
858+
assert.Equal(t, []byte("modified"), bytes)
859+
}

runner/util.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ func (e *Engine) checkIncludeDir(path string) (bool, bool) {
107107
return false, walkDir
108108
}
109109

110+
func (e *Engine) checkIncludeFile(path string) bool {
111+
cleanName := cleanPath(e.config.rel(path))
112+
iFile := e.config.Build.IncludeFile
113+
if len(iFile) == 0 { // ignore empty
114+
return false
115+
}
116+
if cleanName == "." {
117+
return false
118+
}
119+
for _, d := range iFile {
120+
if d == cleanName {
121+
return true
122+
}
123+
}
124+
return false
125+
}
126+
110127
func (e *Engine) isIncludeExt(path string) bool {
111128
ext := filepath.Ext(path)
112129
for _, v := range e.config.Build.IncludeExt {

runner/util_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,16 @@ func TestNestStructArrayValueOverride(t *testing.T) {
274274
setValue2Struct(v, "Build.ExcludeDir", "dir1,dir2")
275275
assert.Equal(t, []string{"dir1", "dir2"}, c.Build.ExcludeDir)
276276
}
277+
278+
func TestCheckIncludeFile(t *testing.T) {
279+
e := Engine{
280+
config: &Config{
281+
Build: cfgBuild{
282+
IncludeFile: []string{"main.go"},
283+
},
284+
},
285+
}
286+
assert.Equal(t, e.checkIncludeFile("main.go"), true)
287+
assert.Equal(t, e.checkIncludeFile("no.go"), false)
288+
assert.Equal(t, e.checkIncludeFile("."), false)
289+
}

0 commit comments

Comments
 (0)