diff options
Diffstat (limited to 'interactive.go')
-rw-r--r-- | interactive.go | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/interactive.go b/interactive.go new file mode 100644 index 0000000..34c2f68 --- /dev/null +++ b/interactive.go | |||
@@ -0,0 +1,131 @@ | |||
1 | /* | ||
2 | |||
3 | This file is part of CompileTree (https://github.com/Pacien/CompileTree) | ||
4 | |||
5 | CompileTree is free software: you can redistribute it and/or modify | ||
6 | it under the terms of the GNU Affero General Public License as published by | ||
7 | the Free Software Foundation, either version 3 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | CompileTree is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with CompileTree. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | */ | ||
19 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "fmt" | ||
24 | "github.com/howeyc/fsnotify" | ||
25 | "os" | ||
26 | "path" | ||
27 | "strings" | ||
28 | "time" | ||
29 | ) | ||
30 | |||
31 | func watch(dirPath string, watcher *fsnotify.Watcher) *fsnotify.Watcher { | ||
32 | watcher.Watch(dirPath) | ||
33 | dirs := explore(dirPath) | ||
34 | for _, dir := range dirs { | ||
35 | if !strings.HasPrefix(dir, *settings.outputDir) { | ||
36 | err := watcher.Watch(dir) | ||
37 | if err != nil { | ||
38 | fmt.Println(err) | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | return watcher | ||
43 | } | ||
44 | |||
45 | func parseParents(dir, sourceDir string) map[string][]byte { | ||
46 | dirs := strings.Split(strings.TrimPrefix(dir, sourceDir), "/") | ||
47 | elements := make(map[string][]byte) | ||
48 | for _, dir := range dirs { | ||
49 | elements = parse(path.Join(sourceDir, dir), elements, false) | ||
50 | } | ||
51 | return elements | ||
52 | } | ||
53 | |||
54 | func interactive(sourceDir, outputDir string) { | ||
55 | |||
56 | // compile the whole site | ||
57 | compiled(sourceDir, outputDir) | ||
58 | |||
59 | // watch the source dir | ||
60 | watcher, err := fsnotify.NewWatcher() | ||
61 | if err != nil { | ||
62 | fmt.Println(err) | ||
63 | } | ||
64 | defer watcher.Close() | ||
65 | watcher = watch(sourceDir, watcher) | ||
66 | |||
67 | for { | ||
68 | select { | ||
69 | case ev := <-watcher.Event: | ||
70 | fmt.Println(ev) | ||
71 | |||
72 | // ignore hidden files | ||
73 | if isHidden(ev.Name) { | ||
74 | break | ||
75 | } | ||
76 | |||
77 | // manage watchers | ||
78 | if ev.IsDelete() || ev.IsRename() { | ||
79 | err = watcher.RemoveWatch(ev.Name) | ||
80 | if err != nil { | ||
81 | fmt.Println(err) | ||
82 | return | ||
83 | } | ||
84 | } else if ev.IsCreate() && isDir(ev.Name) { | ||
85 | watcher = watch(ev.Name, watcher) | ||
86 | } | ||
87 | |||
88 | dir, _ := path.Split(ev.Name) | ||
89 | |||
90 | // remove previously compiled files | ||
91 | if ev.IsDelete() || ev.IsRename() || ev.IsModify() { | ||
92 | var err error | ||
93 | if isDir(ev.Name) || !isParsable(ev.Name) { | ||
94 | err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(ev.Name, sourceDir))) | ||
95 | } else { | ||
96 | err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(dir, sourceDir))) | ||
97 | } | ||
98 | if err != nil { | ||
99 | fmt.Println(err) | ||
100 | return | ||
101 | } | ||
102 | } | ||
103 | |||
104 | // recompile changed files | ||
105 | if ev.IsCreate() || ev.IsModify() { | ||
106 | if isDir(ev.Name) { | ||
107 | elements := parseParents(ev.Name, sourceDir) | ||
108 | dirPath := path.Join(sourceDir, strings.TrimPrefix(ev.Name, sourceDir)) | ||
109 | go compile(dirPath, elements, sourceDir, outputDir, true) | ||
110 | go copyFiles(dirPath, sourceDir, outputDir, true) | ||
111 | } else { | ||
112 | dirPath := path.Join(sourceDir, strings.TrimPrefix(dir, sourceDir)) | ||
113 | if isParsable(path.Ext(ev.Name)) { | ||
114 | elements := parseParents(dir, sourceDir) | ||
115 | go compile(dirPath, elements, sourceDir, outputDir, true) | ||
116 | } | ||
117 | go copyFiles(dirPath, sourceDir, outputDir, false) | ||
118 | } | ||
119 | } | ||
120 | |||
121 | // sleep some milliseconds to prevent early exit | ||
122 | time.Sleep(time.Millisecond * 100) | ||
123 | |||
124 | // wait until all tasks are completed | ||
125 | wait.Wait() | ||
126 | |||
127 | case err := <-watcher.Error: | ||
128 | fmt.Println(err) | ||
129 | } | ||
130 | } | ||
131 | } | ||