Skip to content

Commit 123f246

Browse files
committed
improve file watch
1 parent c314366 commit 123f246

File tree

4 files changed

+57
-36
lines changed

4 files changed

+57
-36
lines changed

src/main/kotlin/com/github/oldmegit/goframehelper/listener/Listener.kt

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import com.github.oldmegit.goframehelper.data.Bundle
44
import com.github.oldmegit.goframehelper.gf.Gf
55
import com.github.oldmegit.goframehelper.gf.GfGoMod
66
import com.github.oldmegit.goframehelper.ui.AppSettingsState
7+
import com.github.oldmegit.goframehelper.ui.Notification
78
import com.goide.GoFileType
8-
import com.intellij.notification.NotificationGroupManager
9-
import com.intellij.notification.NotificationType
109
import com.intellij.openapi.project.Project
10+
import com.intellij.openapi.vfs.VirtualFile
1111
import com.intellij.openapi.vfs.newvfs.BulkFileListener
1212
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
1313
import java.io.File
@@ -31,40 +31,45 @@ class Listener(private val project: Project): BulkFileListener {
3131
return
3232
}
3333

34-
try {
35-
val commandRaw = if (Gf.isApiFile(project, file) && Gf.enableApiWatch(project)) {
36-
Gf.gfGenCtrl(project)
37-
} else if (Gf.isLogicFile(project, file) && Gf.enableLogicWatch(project)) {
38-
Gf.gfGenService(project)
39-
} else {
40-
return
41-
}
34+
fileWatch(file)
35+
}
36+
}
4237

43-
val os = System.getProperty("os.name").lowercase(Locale.getDefault())
44-
val process = if (os.contains("windows")) {
45-
Runtime.getRuntime().exec(commandRaw, null, File(project.basePath.toString()))
46-
} else if (os.contains("linux") || os.contains("mac")) {
47-
val workDir = project.basePath
48-
val command = arrayOf("sh", "-c", "cd $workDir && $commandRaw")
49-
ProcessBuilder(*command).start()
50-
} else {
51-
throw Exception(Bundle.getMessage("gfExecNotSupport"))
52-
}
38+
// file watch exec command
39+
private fun fileWatch(file: VirtualFile) {
40+
try {
41+
// get commandRaw
42+
val commandRaw = if (Gf.isApiFile(project, file) && Gf.enableApiWatch(project)) {
43+
Gf.gfGenCtrl(project)
44+
} else if (Gf.isLogicFile(project, file) && Gf.enableLogicWatch(project)) {
45+
Gf.gfGenService(project)
46+
} else {
47+
return
48+
}
5349

54-
val code = process.waitFor()
55-
if (code != 0) {
56-
throw Exception(Bundle.getMessage("gfExecErrNotify"))
57-
}
58-
} catch (e: Exception) {
59-
val settings = AppSettingsState.getInstance(project)
60-
settings.gfEnableApiWatch = false
61-
settings.gfEnableLogicWatch = false
50+
// exec command based on different systems
51+
val os = System.getProperty("os.name").lowercase(Locale.getDefault())
52+
val process = if (os.contains("windows")) {
53+
Runtime.getRuntime().exec(commandRaw, null, File(project.basePath.toString()))
54+
} else if (os.contains("linux") || os.contains("mac")) {
55+
val workDir = project.basePath
56+
val command = arrayOf("sh", "-c", "cd $workDir && $commandRaw")
57+
ProcessBuilder(*command).start()
58+
} else {
59+
Notification.message(project, Bundle.getMessage("fileWatch.NotSupport"))
60+
return
61+
}
6262

63-
NotificationGroupManager.getInstance()
64-
.getNotificationGroup("GoFrame Help Notify")
65-
.createNotification(e.toString(), NotificationType.INFORMATION)
66-
.notify(project)
63+
val code = process.waitFor()
64+
if (code != 0) {
65+
throw Exception(code.toString())
6766
}
67+
} catch (_: Exception) {
68+
val message = Bundle.getMessage("fileWatch.ExecErr")
69+
val settings = AppSettingsState.getInstance(project)
70+
settings.gfEnableApiWatch = false
71+
settings.gfEnableLogicWatch = false
72+
Notification.message(project, message)
6873
}
6974
}
7075
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.oldmegit.goframehelper.ui
2+
3+
import com.intellij.notification.NotificationGroupManager
4+
import com.intellij.notification.NotificationType
5+
import com.intellij.openapi.project.Project
6+
7+
object Notification {
8+
fun message(project: Project, message: String) {
9+
NotificationGroupManager.getInstance()
10+
.getNotificationGroup("GoFrame Help Notify")
11+
.createNotification(message, NotificationType.INFORMATION)
12+
.notify(project)
13+
}
14+
}

src/main/resources/message/GoFrameHelperBundle.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ setting.api.watch = Enable Api watch, it will auto exec `gf gen ctrl`
99
setting.service.watch = Enable Service watch, it will auto exec `gf gen service`
1010
setting.custom.i18n = Custom i18n folder
1111

12-
gfExecErrNotify = gf-cli is not available, auto closing File Watch. You can re-enable it in Setting-Tools-GoFrame Helper.
13-
gfExecNotSupport = The current system is not supported by file watch
12+
# file watch
13+
fileWatch.ExecErr = gf-cli is not available, auto closing File Watch. You can re-enable it in Setting-Tools-GoFrame Helper.
14+
fileWatch.NotSupport = The current system is not supported by file watch

src/main/resources/message/GoFrameHelperBundle_zh.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ setting.api.watch = \u542f\u7528 Api watch, \u5b83\u4f1a\u81ea\u52a8\u6267\u884c
99
setting.service.watch = \u542f\u7528 Service watch, \u5b83\u4f1a\u81ea\u52a8\u6267\u884c `gf gen service`
1010
setting.custom.i18n = \u81ea\u5b9a\u4e49\u0069\u0031\u0038\u006e\u76ee\u5f55
1111

12-
gfExecErrNotify = \u0067\u0066\u002d\u0063\u006c\u0069\u0020\u4e0d\u53ef\u7528\uff0c\u5c06\u81ea\u52a8\u5173\u95ed\u0020\u0046\u0049\u006c\u0065\u0020\u0057\u0061\u0074\u0063\u0068\uff0c\u60a8\u53ef\u4ee5\u5728\u0020\u0053\u0065\u0074\u0074\u0069\u006e\u0067\u002d\u0054\u006f\u006f\u006c\u0073\u002d\u0047\u006f\u0046\u0072\u0061\u006d\u0065\u0020\u0048\u0065\u006c\u0070\u0065\u0072\u0020\u4e2d\u91cd\u65b0\u542f\u7528\u3002
13-
gfExecNotSupport = \u5f53\u524d\u7cfb\u7edf\u4e0d\u652f\u6301\u0020\u0066\u0069\u006c\u0065\u0020\u0077\u0061\u0074\u0063\u0068
12+
# file watch
13+
fileWatch.ExecErr = \u0067\u0066\u002d\u0063\u006c\u0069\u0020\u4e0d\u53ef\u7528\uff0c\u5c06\u81ea\u52a8\u5173\u95ed\u0020\u0046\u0049\u006c\u0065\u0020\u0057\u0061\u0074\u0063\u0068\uff0c\u60a8\u53ef\u4ee5\u5728\u0020\u0053\u0065\u0074\u0074\u0069\u006e\u0067\u002d\u0054\u006f\u006f\u006c\u0073\u002d\u0047\u006f\u0046\u0072\u0061\u006d\u0065\u0020\u0048\u0065\u006c\u0070\u0065\u0072\u0020\u4e2d\u91cd\u65b0\u542f\u7528\u3002
14+
fileWatch.NotSupport = \u5f53\u524d\u7cfb\u7edf\u4e0d\u652f\u6301\u0020\u0066\u0069\u006c\u0065\u0020\u0077\u0061\u0074\u0063\u0068

0 commit comments

Comments
 (0)