Skip to content

Commit 1fa0699

Browse files
authoredApr 14, 2024
add watch option to automatically build on source file modification (#35)
1 parent 98af72b commit 1fa0699

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed
 

‎src/main/groovy/muddler/App.groovy

+46-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class App {
2525
v longOpt: 'version', 'Print version information and exit'
2626
g longOpt: 'generate', 'Create a new muddler project by answering some questions.'
2727
d longOpt: 'default', 'Create a new default muddler template project named MyProject in the MyProject directory'
28+
w longOpt: 'watch', 'Watch the src directory for changes and rebuild the package when they occur'
2829
}
2930
def options = cli.parse(args)
3031
if (!options) {
@@ -54,6 +55,18 @@ class App {
5455
return
5556
}
5657

58+
build()
59+
60+
if (options.w) {
61+
watch()
62+
return
63+
}
64+
65+
System.exit(0)
66+
}
67+
68+
static void build() {
69+
def e = new Echo()
5770
def srcDir = new File('./src')
5871
if (!srcDir.exists()) {
5972
println "muddler requires a src directory to read your package contents from, and cannot find it. Please see https://github.com/demonnic/muddler#usage for more information on the file layout for muddler."
@@ -233,6 +246,38 @@ class App {
233246
}
234247
}
235248
e.echo("Build completed successfully!")
236-
System.exit(0)
249+
}
250+
251+
static Map<String, Long> mapFileModificationTimes(File directory) {
252+
def fileModificationTimes = [:]
253+
directory.eachFileRecurse { file ->
254+
if (file.isFile()) {
255+
fileModificationTimes[file.path] = file.lastModified()
256+
}
257+
}
258+
return fileModificationTimes
259+
}
260+
261+
static void watch() {
262+
def e = new Echo()
263+
def srcDir = new File('./src')
264+
if (!srcDir.exists()) {
265+
println 'muddler requires a src directory to read your package contents from, and cannot find it. Please see'
266+
return
267+
}
268+
269+
def previousFileModificationTimes = mapFileModificationTimes(srcDir)
270+
271+
while (true) {
272+
Thread.sleep(1000)
273+
274+
def currentFileModificationTimes = mapFileModificationTimes(srcDir)
275+
276+
if (currentFileModificationTimes != previousFileModificationTimes) {
277+
e.echo('Change detected in source directory')
278+
build()
279+
previousFileModificationTimes = currentFileModificationTimes
280+
}
281+
}
237282
}
238283
}

0 commit comments

Comments
 (0)
Please sign in to comment.