aboutsummaryrefslogtreecommitdiff
path: root/interactive.go
diff options
context:
space:
mode:
Diffstat (limited to 'interactive.go')
-rw-r--r--interactive.go131
1 files changed, 0 insertions, 131 deletions
diff --git a/interactive.go b/interactive.go
deleted file mode 100644
index 8ce32ca..0000000
--- a/interactive.go
+++ /dev/null
@@ -1,131 +0,0 @@
1/*
2
3 This file is part of FoldaWeb <https://github.com/Pacien/FoldaWeb>
4
5 FoldaWeb 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 FoldaWeb 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 FoldaWeb. If not, see <http://www.gnu.org/licenses/>.
17
18*/
19
20package main
21
22import (
23 "fmt"
24 "github.com/Pacien/fcmd"
25 "github.com/howeyc/fsnotify"
26 "os"
27 "path"
28 "strings"
29)
30
31func watch(dirPath string, watcher *fsnotify.Watcher) *fsnotify.Watcher {
32 watcher.Watch(dirPath)
33 dirs, _ := fcmd.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
45func parseParents(dir, sourceDir string, exts []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, exts, false)
50 }
51 return elements
52}
53
54func interactive(sourceDir, outputDir string, exts []string, saveAs string) {
55
56 // compile the whole site
57 compiled(sourceDir, outputDir, exts, saveAs)
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 fcmd.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() && fcmd.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 fcmd.IsDir(ev.Name) || !isParsable(ev.Name, exts) {
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 fcmd.IsDir(ev.Name) {
107 elements := parseParents(ev.Name, sourceDir, exts)
108 dirPath := path.Join(sourceDir, strings.TrimPrefix(ev.Name, sourceDir))
109 wait.Add(2)
110 go compile(dirPath, elements, sourceDir, outputDir, saveAs, exts, true)
111 go copyFiles(dirPath, sourceDir, outputDir, exts, true)
112 } else {
113 dirPath := path.Join(sourceDir, strings.TrimPrefix(dir, sourceDir))
114 if isParsable(path.Ext(ev.Name), exts) {
115 elements := parseParents(dir, sourceDir, exts)
116 wait.Add(1)
117 go compile(dirPath, elements, sourceDir, outputDir, saveAs, exts, true)
118 }
119 wait.Add(1)
120 go copyFiles(dirPath, sourceDir, outputDir, exts, false)
121 }
122 }
123
124 // wait until all tasks are completed
125 wait.Wait()
126
127 case err := <-watcher.Error:
128 fmt.Println(err)
129 }
130 }
131}