Skip to content

Commit 3409634

Browse files
committed
feat(runner): add include_dir (#40)
1 parent c671635 commit 3409634

File tree

4 files changed

+54
-29
lines changed

4 files changed

+54
-29
lines changed

Diff for: air_example.conf

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# Working directory
44
# . or absolute path, please note that the directories following must be under root.
55
root = "."
6-
# Optional! If `watch_dir` is empty, use `root`.
7-
watch_dir = ""
86
tmp_dir = "tmp"
97

108
[build]
@@ -14,16 +12,18 @@ cmd = "go build -o ./tmp/main main.go"
1412
bin = "tmp/main"
1513
# Customize binary.
1614
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
17-
# This log file places in your tmp_dir.
18-
log = "air_errors.log"
1915
# Watch these filename extensions.
2016
include_ext = ["go", "tpl", "tmpl", "html"]
2117
# Ignore these filename extensions or directories.
2218
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
19+
# Watch these directories if you specified.
20+
include_dir = []
2321
# It's not necessary to trigger build each time file changes if it's too frequent.
2422
delay = 1000 # ms
2523
# Stop to run old binary when build errors occur.
2624
stop_on_error = true
25+
# This log file places in your tmp_dir.
26+
log = "air_errors.log"
2727

2828
[log]
2929
# Show log time

Diff for: runner/config.go

+13-21
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ const (
1616
)
1717

1818
type config struct {
19-
Root string `toml:"root"`
20-
WatchDir string `toml:"watch_dir"`
21-
TmpDir string `toml:"tmp_dir"`
22-
Build cfgBuild `toml:"build"`
23-
Color cfgColor `toml:"color"`
24-
Log cfgLog `toml:"log"`
25-
Misc cfgMisc `toml:"misc"`
19+
Root string `toml:"root"`
20+
TmpDir string `toml:"tmp_dir"`
21+
Build cfgBuild `toml:"build"`
22+
Color cfgColor `toml:"color"`
23+
Log cfgLog `toml:"log"`
24+
Misc cfgMisc `toml:"misc"`
2625
}
2726

2827
type cfgBuild struct {
@@ -32,6 +31,7 @@ type cfgBuild struct {
3231
Log string `toml:"log"`
3332
IncludeExt []string `toml:"include_ext"`
3433
ExcludeDir []string `toml:"exclude_dir"`
34+
IncludeDir []string `toml:"include_dir"`
3535
Delay int `toml:"delay"`
3636
StopOnError bool `toml:"stop_on_error"`
3737
}
@@ -106,13 +106,12 @@ func defaultConfig() config {
106106
CleanOnExit: false,
107107
}
108108
return config{
109-
Root: ".",
110-
TmpDir: "tmp",
111-
WatchDir: "",
112-
Build: build,
113-
Color: color,
114-
Log: log,
115-
Misc: misc,
109+
Root: ".",
110+
TmpDir: "tmp",
111+
Build: build,
112+
Color: color,
113+
Log: log,
114+
Misc: misc,
116115
}
117116
}
118117

@@ -216,13 +215,6 @@ func dftConfPath() (string, error) {
216215
return filepath.Join(wd, dftConf), nil
217216
}
218217

219-
func (c *config) watchDirRoot() string {
220-
if c.WatchDir != "" {
221-
return c.fullPath(c.WatchDir)
222-
}
223-
return c.Root
224-
}
225-
226218
func (c *config) buildLogPath() string {
227219
return filepath.Join(c.tmpPath(), c.Build.Log)
228220
}

Diff for: runner/engine.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (e *Engine) Run() {
6969
if err = e.checkRunEnv(); err != nil {
7070
os.Exit(1)
7171
}
72-
if err = e.watching(e.config.watchDirRoot()); err != nil {
72+
if err = e.watching(e.config.Root); err != nil {
7373
os.Exit(1)
7474
}
7575

@@ -109,7 +109,15 @@ func (e *Engine) watching(root string) error {
109109
e.watcherLog("!exclude %s", e.config.rel(path))
110110
return filepath.SkipDir
111111
}
112-
return e.watchDir(path)
112+
isIn, walkDir := e.checkIncludeDir(path)
113+
if !walkDir {
114+
e.watcherLog("!exclude %s", e.config.rel(path))
115+
return filepath.SkipDir
116+
}
117+
if isIn {
118+
return e.watchDir(path)
119+
}
120+
return nil
113121
})
114122
}
115123

Diff for: runner/util.go

+27-2
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,40 @@ func cleanPath(path string) string {
6969
}
7070

7171
func (e *Engine) isExcludeDir(path string) bool {
72-
rp := e.config.rel(path)
72+
cleanName := cleanPath(e.config.rel(path))
7373
for _, d := range e.config.Build.ExcludeDir {
74-
if cleanPath(rp) == d {
74+
if cleanName == d {
7575
return true
7676
}
7777
}
7878
return false
7979
}
8080

81+
// return isIncludeDir, walkDir
82+
func (e *Engine) checkIncludeDir(path string) (bool, bool) {
83+
cleanName := cleanPath(e.config.rel(path))
84+
iDir := e.config.Build.IncludeDir
85+
if len(iDir) == 0 { // ignore empty
86+
return true, true
87+
}
88+
if cleanName == "." {
89+
return false, true
90+
}
91+
walkDir := false
92+
for _, d := range iDir {
93+
if d == cleanName {
94+
return true, true
95+
}
96+
if strings.HasPrefix(cleanName, d) { // current dir is sub-directory of `d`
97+
return true, true
98+
}
99+
if strings.HasPrefix(d, cleanName) { // `d` is sub-directory of current dir
100+
walkDir = true
101+
}
102+
}
103+
return false, walkDir
104+
}
105+
81106
func (e *Engine) isIncludeExt(path string) bool {
82107
ext := filepath.Ext(path)
83108
for _, v := range e.config.Build.IncludeExt {

0 commit comments

Comments
 (0)