1
1
package mill .runner
2
2
3
3
import mill .api .{SystemStreams , internal }
4
- import mill .main .client .DebugLog
5
4
import mill .util .{Colors , Watchable }
6
5
7
6
import java .io .InputStream
@@ -14,8 +13,6 @@ import scala.util.Using
14
13
*/
15
14
@ internal
16
15
object Watching {
17
- private final val enableDebugLog = false
18
-
19
16
case class Result [T ](watched : Seq [Watchable ], error : Option [String ], result : T )
20
17
21
18
trait Evaluate [T ] {
@@ -24,11 +21,13 @@ object Watching {
24
21
25
22
/**
26
23
* @param useNotify whether to use filesystem based watcher. If it is false uses polling.
24
+ * @param serverDir the directory for storing logs of the mill server
27
25
*/
28
26
case class WatchArgs (
29
27
setIdle : Boolean => Unit ,
30
28
colors : Colors ,
31
- useNotify : Boolean
29
+ useNotify : Boolean ,
30
+ serverDir : os.Path
32
31
)
33
32
34
33
def watchLoop [T ](
@@ -81,7 +80,8 @@ object Watching {
81
80
streams.in,
82
81
watchables,
83
82
watchArgs.colors,
84
- useNotify = watchArgs.useNotify
83
+ useNotify = watchArgs.useNotify,
84
+ serverDir = watchArgs.serverDir
85
85
)
86
86
}
87
87
}
@@ -95,7 +95,8 @@ object Watching {
95
95
stdin : InputStream ,
96
96
watched : Seq [Watchable ],
97
97
colors : Colors ,
98
- useNotify : Boolean
98
+ useNotify : Boolean ,
99
+ serverDir : os.Path
99
100
): Boolean = {
100
101
setIdle(true )
101
102
val (watchedPollables, watchedPathsSeq) = watched.partitionMap {
@@ -121,16 +122,17 @@ object Watching {
121
122
enterKeyPressed
122
123
}
123
124
124
- if (useNotify) {
125
+ if (useNotify) Using .resource(os.write.outputStream(serverDir / " fsNotifyWatchLog" )) { watchLog =>
126
+ def writeToWatchLog (s : String ): Unit = {
127
+ watchLog.write(s.getBytes(java.nio.charset.StandardCharsets .UTF_8 ))
128
+ watchLog.write('\n ' )
129
+ }
130
+
125
131
@ volatile var pathChangesDetected = false
126
132
127
133
// oslib watch only works with folders, so we have to watch the parent folders instead
128
134
129
- if (enableDebugLog) DebugLog .println(
130
- colors.info(
131
- s " [watch:watched-paths:unfiltered] ${watchedPathsSet.toSeq.sorted.mkString(" \n " )}"
132
- ).toString
133
- )
135
+ writeToWatchLog(s " [watched-paths:unfiltered] ${watchedPathsSet.toSeq.sorted.mkString(" \n " )}" )
134
136
135
137
val workspaceRoot = mill.api.WorkspaceRoot .workspaceRoot
136
138
@@ -156,38 +158,28 @@ object Watching {
156
158
val filterPaths = pathsUnderWorkspaceRoot.flatMap { path =>
157
159
path.relativeTo(workspaceRoot).segments.inits.map(segments => workspaceRoot / segments)
158
160
}
159
- if (enableDebugLog) DebugLog .println(colors.info(
160
- s " [watch:watched-paths:filtered] ${filterPaths.toSeq.sorted.mkString(" \n " )}"
161
- ).toString())
161
+ writeToWatchLog(s " [watched-paths:filtered] ${filterPaths.toSeq.sorted.mkString(" \n " )}" )
162
162
163
163
Using .resource(os.watch.watch(
164
164
// Just watch the root folder
165
165
Seq (workspaceRoot),
166
166
filter = path => {
167
167
val shouldBeWatched =
168
168
filterPaths.contains(path) || watchedPathsSet.exists(watchedPath => path.startsWith(watchedPath))
169
- if (enableDebugLog) {
170
- DebugLog .println(colors.info(s " [watch:filter] $path (shouldBeWatched= $shouldBeWatched) " ).toString)
171
- }
169
+ writeToWatchLog(s " [filter] (shouldBeWatched= $shouldBeWatched) $path" )
172
170
shouldBeWatched
173
171
},
174
172
onEvent = changedPaths => {
175
173
// Make sure that the changed paths are actually the ones in our watch list and not some adjacent files in the
176
174
// same folder
177
175
val hasWatchedPath =
178
176
changedPaths.exists(p => watchedPathsSet.exists(watchedPath => p.startsWith(watchedPath)))
179
- if (enableDebugLog) DebugLog .println(colors.info(
180
- s " [watch:changed-paths] hasWatchedPath= $hasWatchedPath) ${changedPaths.mkString(" \n " )}"
181
- ).toString)
177
+ writeToWatchLog(s " [changed-paths] (hasWatchedPath= $hasWatchedPath) ${changedPaths.mkString(" \n " )}" )
182
178
if (hasWatchedPath) {
183
179
pathChangesDetected = true
184
180
}
185
181
},
186
- logger =
187
- if (enableDebugLog) (eventType, data) => {
188
- DebugLog .println(colors.info(s " [watch] $eventType: ${pprint.apply(data)}" ).toString)
189
- }
190
- else (_, _) => {}
182
+ logger = (eventType, data) => writeToWatchLog(s " [watch:event] $eventType: ${pprint.apply(data).plainText}" )
191
183
)) { _ =>
192
184
doWatch(notifiablesChanged = () => pathChangesDetected)
193
185
}
0 commit comments