Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FLIPPED-AURORA
gin-vue-admin
提交
fa78d240
G
gin-vue-admin
项目概览
FLIPPED-AURORA
/
gin-vue-admin
大约 1 年 前同步成功
通知
332
Star
18154
Fork
5505
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gin-vue-admin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fa78d240
编写于
11月 15, 2020
作者:
S
songzhibin97
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
热更新
缺少具体配置 监控文件以及防抖
上级
70b2ee3f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
100 addition
and
52 deletion
+100
-52
server/cmd/gva/run.go
server/cmd/gva/run.go
+5
-1
server/utils/cmd_Task.go
server/utils/cmd_Task.go
+84
-49
server/utils/cmd_monitor.go
server/utils/cmd_monitor.go
+11
-2
未找到文件。
server/cmd/gva/run.go
浏览文件 @
fa78d240
...
...
@@ -16,6 +16,7 @@ limitations under the License.
package
gva
import
(
"gin-vue-admin/utils"
"github.com/spf13/cobra"
)
...
...
@@ -28,7 +29,10 @@ var runCmd = &cobra.Command{
which compiles and runs the go codes asynchronously when codes change.
`
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
// todo 未实现
w
:=
utils
.
NewWatch
()
t
:=
utils
.
NewT
()
go
w
.
Watch
(
"./"
,
t
)
t
.
RunTask
()
},
}
...
...
server/utils/cmd_Task.go
浏览文件 @
fa78d240
...
...
@@ -4,7 +4,8 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"path/filepath"
"runtime"
"sync"
)
...
...
@@ -17,46 +18,31 @@ type RunTask interface {
type
T
struct
{
sync
.
Mutex
// ch: 获取
时间
channel
// ch: 获取
事件
channel
ch
chan
struct
{}
// 记录pid 用于kill
pid
int
closeChan
chan
struct
{}
// 执行shell命令
exec
.
Cmd
// 记录process对象
p
*
os
.
Process
// f: 执行任务
f
func
(
chan
struct
{})
error
}
// NewT: 实例化方法
func
NewT
(
path
string
,
args
[]
string
,
environment
...
[]
string
)
*
T
{
env
:=
os
.
Environ
()
if
len
(
environment
)
>
0
{
for
k
,
v
:=
range
environment
[
0
]
{
env
[
k
]
=
v
}
}
func
NewT
()
*
T
{
return
newT
(
nil
)
}
func
newT
(
f
func
(
chan
struct
{})
error
)
*
T
{
t
:=
&
T
{
Mutex
:
sync
.
Mutex
{},
ch
:
make
(
chan
struct
{}),
Cmd
:
exec
.
Cmd
{
Path
:
path
,
Args
:
[]
string
{
path
},
Env
:
env
,
Stdin
:
os
.
Stdin
,
Stdout
:
os
.
Stdout
,
Stderr
:
os
.
Stderr
,
ExtraFiles
:
make
([]
*
os
.
File
,
0
),
},
pid
:
os
.
Getpid
(),
Mutex
:
sync
.
Mutex
{},
ch
:
make
(
chan
struct
{},
1
),
closeChan
:
make
(
chan
struct
{}),
f
:
f
,
}
t
.
Dir
,
_
=
os
.
Getwd
()
if
len
(
args
)
>
0
{
// Exclude of current binary path.
start
:=
0
if
strings
.
EqualFold
(
path
,
args
[
0
])
{
start
=
1
}
t
.
Args
=
append
(
t
.
Args
,
args
[
start
:
]
...
)
if
f
==
nil
{
t
.
f
=
t
.
DefaultF
}
return
t
}
...
...
@@ -73,33 +59,82 @@ func (t *T) AddTask() {
}
func
(
t
*
T
)
RunTask
()
{
fmt
.
Println
(
"进入"
)
// 这里做的make 是用于关闭上一个执行的任务
ch
:=
make
(
chan
struct
{})
// 先run服务
err
:=
t
.
Run
()
if
err
!=
nil
{
return
}
go
t
.
f
(
ch
)
for
{
_
,
ok
:=
<-
t
.
ch
ch
<-
struct
{}{}
if
!
ok
{
return
}
// todo 执行任务
// 先编译新的文件 然后 kill 然后执行
fmt
.
Println
(
kill
(
t
.
Cmd
.
Process
.
Pid
))
err
=
t
.
Run
()
if
err
!=
nil
{
fmt
.
Println
(
err
)
return
// 等待上一个关闭
<-
t
.
closeChan
go
t
.
f
(
ch
)
}
}
// DefaultF: 默认的StartFunction
func
(
t
*
T
)
DefaultF
(
ch
chan
struct
{})
error
{
var
buildCmd
*
exec
.
Cmd
var
cmd
*
exec
.
Cmd
// 判断是否有makefile
_
,
err
:=
os
.
Stat
(
filepath
.
Join
(
"Makefile"
))
if
runtime
.
GOOS
!=
"windows"
&&
err
!=
nil
{
_
,
err
:=
exec
.
LookPath
(
"make"
)
if
err
==
nil
{
cmd
=
exec
.
Command
(
"makefile"
)
goto
makefile
}
}
// 检测系统是否有编译环境
_
,
err
=
exec
.
LookPath
(
"go"
)
if
err
!=
nil
{
return
err
}
// build
switch
runtime
.
GOOS
{
case
"windows"
:
buildCmd
=
exec
.
Command
(
"go"
,
"build"
,
"-o"
,
"gva.exe"
,
"main.go"
)
default
:
buildCmd
=
exec
.
Command
(
"go"
,
"build"
,
"-o"
,
"gva"
,
"main.go"
)
}
}
// kill:
func
kill
(
pid
int
)
error
{
p
,
err
:=
os
.
FindProcess
(
pid
)
err
=
buildCmd
.
Run
()
fmt
.
Println
(
"build 执行完成"
)
if
err
!=
nil
{
return
err
}
// 执行
switch
runtime
.
GOOS
{
case
"windows"
:
cmd
=
exec
.
Command
(
"gva.exe"
)
default
:
cmd
=
exec
.
Command
(
"./gva"
)
}
makefile
:
// 开始执行任务
err
=
cmd
.
Start
()
if
err
!=
nil
{
return
err
}
return
p
.
Kill
()
t
.
p
=
cmd
.
Process
fmt
.
Println
(
"pid"
,
t
.
p
.
Pid
)
go
func
()
{
err
=
cmd
.
Wait
()
}()
<-
ch
// 回收资源
err
=
cmd
.
Process
.
Kill
()
fmt
.
Println
(
"kill err"
,
err
)
// 发送关闭完成信号
t
.
closeChan
<-
struct
{}{}
return
err
}
server/utils/cmd_monitor.go
浏览文件 @
fa78d240
...
...
@@ -20,7 +20,7 @@ func NewWatch() *Watch {
}
// Watch: 监控对象
func
(
w
*
Watch
)
Watch
(
path
string
)
error
{
func
(
w
*
Watch
)
Watch
(
path
string
,
t
*
T
)
error
{
// 先转化为绝对路径
path
,
err
:=
filepath
.
Abs
(
path
)
if
err
!=
nil
{
...
...
@@ -62,24 +62,33 @@ func (w *Watch) Watch(path string) error {
case
even
.
Op
&
fsnotify
.
Create
==
fsnotify
.
Create
:
//这里获取新创建文件的信息,如果是目录,则加入监控中
fmt
.
Println
(
"创建文件 : "
,
even
.
Name
)
//t.AddTask()
_
=
w
.
Add
(
even
.
Name
)
case
even
.
Op
&
fsnotify
.
Write
==
fsnotify
.
Write
:
fmt
.
Println
(
"修改 : "
,
even
.
Name
)
fmt
.
Println
(
filepath
.
Ext
(
even
.
Name
))
if
filepath
.
Ext
(
even
.
Name
)
==
".go"
{
fmt
.
Println
(
"send addtask:"
,
even
.
Name
)
t
.
AddTask
()
}
case
even
.
Op
&
fsnotify
.
Remove
==
fsnotify
.
Remove
:
fmt
.
Println
(
"删除 : "
,
even
.
Name
)
//t.AddTask()
_
=
w
.
Remove
(
even
.
Name
)
case
even
.
Op
&
fsnotify
.
Rename
==
fsnotify
.
Rename
:
fmt
.
Println
(
"重命名 : "
,
even
.
Name
)
//t.AddTask()
_
=
w
.
Remove
(
even
.
Name
)
}
case
err
=
<-
w
.
Errors
:
fmt
.
Println
(
"79"
,
err
)
c
<-
err
return
}
}
}()
return
<-
c
}
// watchDir: 处理监控目录
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录