From 3b0baf4e7e864f8a9329dde6b7f63e8163e31d07 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Sat, 14 Nov 2020 19:21:06 +0800 Subject: [PATCH 01/13] Hotreload Hotreload1 --- server/cmd/gva/run.go | 47 ++++++++++++++ server/utils/cmd_Task.go | 83 +++++++++++++++++++++++++ server/utils/cmd_monitor.go | 118 ++++++++++++++++++++++++++++++++++++ 3 files changed, 248 insertions(+) create mode 100644 server/cmd/gva/run.go create mode 100644 server/utils/cmd_Task.go create mode 100644 server/utils/cmd_monitor.go diff --git a/server/cmd/gva/run.go b/server/cmd/gva/run.go new file mode 100644 index 00000000..c291e9b2 --- /dev/null +++ b/server/cmd/gva/run.go @@ -0,0 +1,47 @@ +/* +Copyright © 2020 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package gva + +import ( + "github.com/spf13/cobra" +) + +// runCmd represents the run command +var runCmd = &cobra.Command{ + Use: "run", + Short: "running go codes with hot-compiled-like feature", + Long: ` + The "run" command is used for running go codes with hot-compiled-like feature, + which compiles and runs the go codes asynchronously when codes change. +`, + Run: func(cmd *cobra.Command, args []string) { + // todo 未实现 + }, +} + +func init() { + rootCmd.AddCommand(runCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // runCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // runCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/server/utils/cmd_Task.go b/server/utils/cmd_Task.go new file mode 100644 index 00000000..f1d0ac1e --- /dev/null +++ b/server/utils/cmd_Task.go @@ -0,0 +1,83 @@ +package utils + +import ( + "os" + "os/exec" + "strings" + "sync" +) + +type RunTask interface { + AddTask() + RunTask() +} + +// T: Task任务 +type T struct { + sync.Mutex + + // ch: 获取时间channel + ch chan struct{} + + // 记录pid 用于kill + pid int + + // 执行shell命令 + exec.Cmd +} + +// 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 + } + } + 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(), + } + 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:]...) + } + return t +} + +func (t *T) AddTask() { + t.Lock() + defer t.Unlock() + if len(t.ch) == 1 { + // 代表已经有任务了 + // 直接丢弃这次任务 + return + } + t.ch <- struct{}{} +} + +func (t *T) RunTask() { + for { + _, ok := <-t.ch + if !ok { + return + } + // todo 执行任务 + + } +} diff --git a/server/utils/cmd_monitor.go b/server/utils/cmd_monitor.go new file mode 100644 index 00000000..65a0f5a1 --- /dev/null +++ b/server/utils/cmd_monitor.go @@ -0,0 +1,118 @@ +package utils + +import ( + "errors" + "fmt" + "github.com/fsnotify/fsnotify" + "io/ioutil" + "os" + "path/filepath" +) + +// Watch: 监控对象 +type Watch struct { + *fsnotify.Watcher +} + +func NewWatch() *Watch { + obj, _ := fsnotify.NewWatcher() + return &Watch{obj} +} + +// Watch: 监控对象 +func (w *Watch) Watch(path string) error { + // 先转化为绝对路径 + path, err := filepath.Abs(path) + if err != nil { + return err + } + // 判断是单个文件还是目录文件 + fileInfo, err := os.Stat(path) + if err != nil { + return err + } + // 判断是否是目录 添加监控 + if fileInfo.IsDir() { + // dir + err = w.watchDir(path) + + } else { + err = w.watchFile(path) + + } + if err != nil { + return err + } + c := make(chan error) + // 启动监控 + go func() { + for { + select { + case even, ok := <-w.Events: + if !ok { + // close + fmt.Println("Errors close") + c <- errors.New("errors close") + return + } + // 判断时间 + fmt.Println("even", even) + switch { + // todo 待处理 + case even.Op&fsnotify.Create == fsnotify.Create: + //这里获取新创建文件的信息,如果是目录,则加入监控中 + fmt.Println("创建文件 : ", even.Name) + _ = w.Add(even.Name) + case even.Op&fsnotify.Write == fsnotify.Write: + fmt.Println("修改 : ", even.Name) + case even.Op&fsnotify.Remove == fsnotify.Remove: + fmt.Println("删除 : ", even.Name) + _ = w.Remove(even.Name) + case even.Op&fsnotify.Rename == fsnotify.Rename: + fmt.Println("重命名 : ", even.Name) + _ = w.Remove(even.Name) + } + case err = <-w.Errors: + c <- err + return + } + } + }() + + return <-c +} + +// watchDir: 处理监控目录 +func (w *Watch) watchDir(path string) error { + // 先将自己添加到监控 + err := w.Add(path) + + if err != nil { + return err + } + fileSlice, err := ioutil.ReadDir(path) + if err != nil { + return err + } + for _, f := range fileSlice { + fPath := filepath.Join(path, f.Name()) + if !f.IsDir() { + // todo 这里加一些条件筛选添加监控的内容 + err = w.watchFile(fPath) + if err != nil { + return err + } + } else { + err := w.watchDir(fPath) + if err != nil { + return err + } + } + } + return err +} + +// watchDir: 处理监控单文件 +func (w *Watch) watchFile(path string) error { + return w.Add(path) +} From 6779054110d227b2f9d823c9621d2d223d60e2a0 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Sat, 14 Nov 2020 19:36:22 +0800 Subject: [PATCH 02/13] Update cmd_Task.go c2 --- server/utils/cmd_Task.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/server/utils/cmd_Task.go b/server/utils/cmd_Task.go index f1d0ac1e..fe9fbd61 100644 --- a/server/utils/cmd_Task.go +++ b/server/utils/cmd_Task.go @@ -1,6 +1,7 @@ package utils import ( + "fmt" "os" "os/exec" "strings" @@ -72,12 +73,33 @@ func (t *T) AddTask() { } func (t *T) RunTask() { + // 先run服务 + err := t.Run() + if err != nil { + return + } for { _, ok := <-t.ch if !ok { return } // todo 执行任务 + // 先编译新的文件 然后 kill 然后执行 + fmt.Println(kill(t.Cmd.Process.Pid)) + err = t.Run() + if err != nil { + fmt.Println(err) + return + } + + } +} +// kill: +func kill(pid int) error { + p, err := os.FindProcess(pid) + if err != nil { + return err } + return p.Kill() } From 70b2ee3f97303a5817ac19b8f6c73f8f9d8f726a Mon Sep 17 00:00:00 2001 From: SliverHorn Date: Sat, 14 Nov 2020 23:15:57 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81api=E5=B1=82=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=B7=B7=E5=85=A5service=E5=B1=82=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/api/v1/sys_auto_code.go | 50 +++----------------------- server/service/sys_api.go | 22 ------------ server/service/sys_auto_code.go | 63 +++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 67 deletions(-) diff --git a/server/api/v1/sys_auto_code.go b/server/api/v1/sys_auto_code.go index 9f1beb6f..679f4be3 100644 --- a/server/api/v1/sys_auto_code.go +++ b/server/api/v1/sys_auto_code.go @@ -30,51 +30,11 @@ func CreateTemp(c *gin.Context) { return } if a.AutoCreateApiToSql { - apiList := [6]model.SysApi{ - { - Path: "/" + a.Abbreviation + "/" + "create" + a.StructName, - Description: "新增" + a.Description, - ApiGroup: a.Abbreviation, - Method: "POST", - }, - { - Path: "/" + a.Abbreviation + "/" + "delete" + a.StructName, - Description: "删除" + a.Description, - ApiGroup: a.Abbreviation, - Method: "DELETE", - }, - { - Path: "/" + a.Abbreviation + "/" + "delete" + a.StructName + "ByIds", - Description: "批量删除" + a.Description, - ApiGroup: a.Abbreviation, - Method: "DELETE", - }, - { - Path: "/" + a.Abbreviation + "/" + "update" + a.StructName, - Description: "更新" + a.Description, - ApiGroup: a.Abbreviation, - Method: "PUT", - }, - { - Path: "/" + a.Abbreviation + "/" + "find" + a.StructName, - Description: "根据ID获取" + a.Description, - ApiGroup: a.Abbreviation, - Method: "GET", - }, - { - Path: "/" + a.Abbreviation + "/" + "get" + a.StructName + "List", - Description: "获取" + a.Description + "列表", - ApiGroup: a.Abbreviation, - Method: "GET", - }, - } - for _, v := range apiList { - if err := service.AutoCreateApi(v); err != nil { - global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err)) - c.Writer.Header().Add("success", "false") - c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!")) - return - } + if err := service.AutoCreateApi(&a); err != nil { + global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err)) + c.Writer.Header().Add("success", "false") + c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!")) + return } } err := service.CreateTemp(a) diff --git a/server/service/sys_api.go b/server/service/sys_api.go index 7ec062d4..40df486d 100644 --- a/server/service/sys_api.go +++ b/server/service/sys_api.go @@ -33,28 +33,6 @@ func DeleteApi(api model.SysApi) (err error) { return err } -//@author: [piexlmax](https://github.com/piexlmax) -//@function: CreateApi -//@description: 自动创建api数据, -//@param: api model.SysApi -//@return: err error - -func AutoCreateApi(api model.SysApi) (err error) { - err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { - var fApi model.SysApi - var txErr error - fxErr := tx.Where("path = ? AND method = ?", api.Path, api.Method).First(&fApi).Error - if errors.Is(fxErr, gorm.ErrRecordNotFound) { - txErr = tx.Create(&api).Error - if txErr != nil { - return txErr - } - } - return nil - }) - return err -} - //@author: [piexlmax](https://github.com/piexlmax) //@function: GetAPIInfoList //@description: 分页获取数据, diff --git a/server/service/sys_auto_code.go b/server/service/sys_auto_code.go index d5d9681f..dbdc7b5d 100644 --- a/server/service/sys_auto_code.go +++ b/server/service/sys_auto_code.go @@ -7,6 +7,7 @@ import ( "gin-vue-admin/model" "gin-vue-admin/model/request" "gin-vue-admin/utils" + "gorm.io/gorm" "io/ioutil" "os" "path/filepath" @@ -207,3 +208,65 @@ func addAutoMoveFile(data *tplData) { } } } + + +//@author: [piexlmax](https://github.com/piexlmax) +//@author: [SliverHorn](https://github.com/SliverHorn) +//@function: CreateApi +//@description: 自动创建api数据, +//@param: a *model.AutoCodeStruct +//@return: error + +func AutoCreateApi(a *model.AutoCodeStruct) (err error) { + var apiList = []model.SysApi{ + { + Path: "/" + a.Abbreviation + "/" + "create" + a.StructName, + Description: "新增" + a.Description, + ApiGroup: a.Abbreviation, + Method: "POST", + }, + { + Path: "/" + a.Abbreviation + "/" + "delete" + a.StructName, + Description: "删除" + a.Description, + ApiGroup: a.Abbreviation, + Method: "DELETE", + }, + { + Path: "/" + a.Abbreviation + "/" + "delete" + a.StructName + "ByIds", + Description: "批量删除" + a.Description, + ApiGroup: a.Abbreviation, + Method: "DELETE", + }, + { + Path: "/" + a.Abbreviation + "/" + "update" + a.StructName, + Description: "更新" + a.Description, + ApiGroup: a.Abbreviation, + Method: "PUT", + }, + { + Path: "/" + a.Abbreviation + "/" + "find" + a.StructName, + Description: "根据ID获取" + a.Description, + ApiGroup: a.Abbreviation, + Method: "GET", + }, + { + Path: "/" + a.Abbreviation + "/" + "get" + a.StructName + "List", + Description: "获取" + a.Description + "列表", + ApiGroup: a.Abbreviation, + Method: "GET", + }, + } + err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { + for _, v := range apiList { + var api model.SysApi + if err := tx.Where("path = ? AND method = ?", v.Path, v.Method).First(&api).Error; err != nil { + return err + } + if err := tx.Create(&v).Error; err != nil { // 遇到错误时回滚事务 + return err + } + } + return nil + }) + return err +} \ No newline at end of file From fa78d240f203c6bd86adaebf68e5724c34909be3 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Sun, 15 Nov 2020 15:51:16 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E7=83=AD=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 缺少具体配置 监控文件以及防抖 --- server/cmd/gva/run.go | 6 +- server/utils/cmd_Task.go | 133 +++++++++++++++++++++++------------- server/utils/cmd_monitor.go | 13 +++- 3 files changed, 100 insertions(+), 52 deletions(-) diff --git a/server/cmd/gva/run.go b/server/cmd/gva/run.go index c291e9b2..6eb88339 100644 --- a/server/cmd/gva/run.go +++ b/server/cmd/gva/run.go @@ -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() }, } diff --git a/server/utils/cmd_Task.go b/server/utils/cmd_Task.go index fe9fbd61..2eded600 100644 --- a/server/utils/cmd_Task.go +++ b/server/utils/cmd_Task.go @@ -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 } diff --git a/server/utils/cmd_monitor.go b/server/utils/cmd_monitor.go index 65a0f5a1..187be57e 100644 --- a/server/utils/cmd_monitor.go +++ b/server/utils/cmd_monitor.go @@ -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: 处理监控目录 From 030abc7039dc462b3d45df4a93452e68ca7db74b Mon Sep 17 00:00:00 2001 From: SliverHorn Date: Mon, 16 Nov 2020 00:11:21 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E8=B7=AF=E5=BE=84=E4=BB=A3=E7=A0=81,?= =?UTF-8?q?=E4=BF=AE=E6=94=B9go.mod=E7=9A=84go=E7=89=88=E6=9C=AC=E4=B8=BA1?= =?UTF-8?q?.14?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/cmd/gva/run.go | 4 +++- server/go.mod | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/server/cmd/gva/run.go b/server/cmd/gva/run.go index 6eb88339..f5905752 100644 --- a/server/cmd/gva/run.go +++ b/server/cmd/gva/run.go @@ -18,6 +18,7 @@ package gva import ( "gin-vue-admin/utils" "github.com/spf13/cobra" + "os" ) // runCmd represents the run command @@ -31,7 +32,8 @@ var runCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { w := utils.NewWatch() t := utils.NewT() - go w.Watch("./", t) + path, _ := os.Getwd() + go w.Watch(path, t) t.RunTask() }, } diff --git a/server/go.mod b/server/go.mod index 230164e2..358cc460 100644 --- a/server/go.mod +++ b/server/go.mod @@ -1,6 +1,6 @@ module gin-vue-admin -go 1.12 +go 1.14 require ( github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect From d6b726e8ca8c098874bc687e5d6f359b20ed5889 Mon Sep 17 00:00:00 2001 From: 1319612909 <1319612909@qq.com> Date: Mon, 16 Nov 2020 14:11:10 +0800 Subject: [PATCH 06/13] =?UTF-8?q?menu=20=E4=B8=89=E7=BA=A7=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config.yaml | 2 +- web/src/style/main.scss | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/server/config.yaml b/server/config.yaml index b336c7cc..1d38f046 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -56,7 +56,7 @@ mysql: config: 'charset=utf8mb4&parseTime=True&loc=Local' db-name: 'qmPlus' username: 'root' - password: 'Aa@6447985' + password: 'root123' max-idle-conns: 10 max-open-conns: 100 log-mode: false diff --git a/web/src/style/main.scss b/web/src/style/main.scss index f7cd1eba..b849814e 100644 --- a/web/src/style/main.scss +++ b/web/src/style/main.scss @@ -605,6 +605,17 @@ li { } .is-active { background-color: #1890ff; + // 关闭三级菜单二级菜单样式 + ul{ + border:none; + } + } + // 关闭三级菜单二级菜单样式 + .is-active.is-opened{ + background-color: #191a23; + ul{ + border:none; + } } } } From 4a78d222d6a01f1c15673843848c97e5fcf5623f Mon Sep 17 00:00:00 2001 From: SliverHorn Date: Mon, 16 Nov 2020 18:38:20 +0800 Subject: [PATCH 07/13] =?UTF-8?q?=E8=A7=A3=E5=86=B3err=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/cmd/datas/AuthorityMenu.go | 5 +++-- server/cmd/datas/apis.go | 10 ++++++--- server/cmd/datas/authorities.go | 10 ++++++--- server/cmd/datas/authority_Ids.go | 10 ++++++--- server/cmd/datas/authority_menus.go | 10 ++++++--- server/cmd/datas/casbins.go | 10 ++++++--- server/cmd/datas/customers.go | 10 ++++++--- server/cmd/datas/dictionaries.go | 10 ++++++--- server/cmd/datas/dictionary_details.go | 10 ++++++--- server/cmd/datas/files.go | 11 +++++++--- server/cmd/datas/init.go | 30 +++++++++++--------------- server/cmd/datas/menus.go | 10 ++++++--- server/cmd/datas/users.go | 10 ++++++--- 13 files changed, 93 insertions(+), 53 deletions(-) diff --git a/server/cmd/datas/AuthorityMenu.go b/server/cmd/datas/AuthorityMenu.go index dd6afe46..c7645738 100644 --- a/server/cmd/datas/AuthorityMenu.go +++ b/server/cmd/datas/AuthorityMenu.go @@ -5,9 +5,10 @@ import ( "gorm.io/gorm" ) -func InitAuthorityMenu(db *gorm.DB) (err error) { +func InitAuthorityMenu(db *gorm.DB) { if err := db.Exec("CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `authority_menu` AS select `sys_base_menus`.`id` AS `id`,`sys_base_menus`.`created_at` AS `created_at`, `sys_base_menus`.`updated_at` AS `updated_at`, `sys_base_menus`.`deleted_at` AS `deleted_at`, `sys_base_menus`.`menu_level` AS `menu_level`,`sys_base_menus`.`parent_id` AS `parent_id`,`sys_base_menus`.`path` AS `path`,`sys_base_menus`.`name` AS `name`,`sys_base_menus`.`hidden` AS `hidden`,`sys_base_menus`.`component` AS `component`, `sys_base_menus`.`title` AS `title`,`sys_base_menus`.`icon` AS `icon`,`sys_base_menus`.`sort` AS `sort`,`sys_authority_menus`.`sys_authority_authority_id` AS `authority_id`,`sys_authority_menus`.`sys_base_menu_id` AS `menu_id`,`sys_base_menus`.`keep_alive` AS `keep_alive`,`sys_base_menus`.`default_menu` AS `default_menu` from (`sys_authority_menus` join `sys_base_menus` on ((`sys_authority_menus`.`sys_base_menu_id` = `sys_base_menus`.`id`)))").Error; err != nil { color.Danger.Println("authority_menu视图已存在!") + return } - return nil + color.Info.Println("authority_menu视图创建成功!") } diff --git a/server/cmd/datas/apis.go b/server/cmd/datas/apis.go index 4dda72e2..7ac93541 100644 --- a/server/cmd/datas/apis.go +++ b/server/cmd/datas/apis.go @@ -4,6 +4,7 @@ import ( "gin-vue-admin/global" "gin-vue-admin/model" "github.com/gookit/color" + "os" "time" "gorm.io/gorm" @@ -78,8 +79,8 @@ var Apis = []model.SysApi{ {global.GVA_MODEL{ID: 67, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "/email/emailTest", "发送测试邮件", "email", "POST"}, } -func InitSysApi(db *gorm.DB) (err error) { - return db.Transaction(func(tx *gorm.DB) error { +func InitSysApi(db *gorm.DB) { + if err := db.Transaction(func(tx *gorm.DB) error { if tx.Where("id IN ?", []int{1, 67}).Find(&[]model.SysApi{}).RowsAffected == 2 { color.Danger.Println("sys_apis表的初始数据已存在!") return nil @@ -88,5 +89,8 @@ func InitSysApi(db *gorm.DB) (err error) { return err } return nil - }) + }); err != nil { + color.Warn.Printf("[Mysql]--> sys_apis 表的初始数据失败,err: %v\n", err) + os.Exit(0) + } } diff --git a/server/cmd/datas/authorities.go b/server/cmd/datas/authorities.go index e47715df..c1acaa0f 100644 --- a/server/cmd/datas/authorities.go +++ b/server/cmd/datas/authorities.go @@ -2,6 +2,7 @@ package datas import ( "github.com/gookit/color" + "os" "time" "gin-vue-admin/model" @@ -14,8 +15,8 @@ var Authorities = []model.SysAuthority{ {CreatedAt: time.Now(), UpdatedAt: time.Now(), AuthorityId: "9528", AuthorityName: "测试角色", ParentId: "0"}, } -func InitSysAuthority(db *gorm.DB) (err error) { - return db.Transaction(func(tx *gorm.DB) error { +func InitSysAuthority(db *gorm.DB) { + if err := db.Transaction(func(tx *gorm.DB) error { if tx.Where("authority_id IN ? ", []string{"888", "9528"}).Find(&[]model.SysAuthority{}).RowsAffected == 2 { color.Danger.Println("sys_authorities表的初始数据已存在!") return nil @@ -24,5 +25,8 @@ func InitSysAuthority(db *gorm.DB) (err error) { return err } return nil - }) + }); err != nil { + color.Warn.Printf("[Mysql]--> sys_authorities 表的初始数据失败,err: %v\n", err) + os.Exit(0) + } } diff --git a/server/cmd/datas/authority_Ids.go b/server/cmd/datas/authority_Ids.go index 126ae4dd..47328ddb 100644 --- a/server/cmd/datas/authority_Ids.go +++ b/server/cmd/datas/authority_Ids.go @@ -3,6 +3,7 @@ package datas import ( "github.com/gookit/color" "gorm.io/gorm" + "os" ) type SysDataAuthorityId struct { @@ -18,8 +19,8 @@ var DataAuthorityId = []SysDataAuthorityId{ {"9528", "9528"}, } -func InitSysDataAuthorityId(db *gorm.DB) (err error) { - return db.Table("sys_data_authority_id").Transaction(func(tx *gorm.DB) error { +func InitSysDataAuthorityId(db *gorm.DB) { + if err := db.Table("sys_data_authority_id").Transaction(func(tx *gorm.DB) error { if tx.Where("sys_authority_authority_id IN ?", []string{"888", "9528"}).Find(&[]SysDataAuthorityId{}).RowsAffected == 5 { color.Danger.Println("sys_data_authority_id表的初始数据已存在!") return nil @@ -28,5 +29,8 @@ func InitSysDataAuthorityId(db *gorm.DB) (err error) { return err } return nil - }) + }); err != nil { + color.Warn.Printf("[Mysql]--> sys_data_authority_id 表的初始数据失败,err: %v\n", err) + os.Exit(0) + } } diff --git a/server/cmd/datas/authority_menus.go b/server/cmd/datas/authority_menus.go index d5d314ed..08e9c8a1 100644 --- a/server/cmd/datas/authority_menus.go +++ b/server/cmd/datas/authority_menus.go @@ -3,6 +3,7 @@ package datas import ( "github.com/gookit/color" "gorm.io/gorm" + "os" ) type SysAuthorityMenus struct { @@ -66,8 +67,8 @@ var AuthorityMenus = []SysAuthorityMenus{ {"9528", 20}, } -func InitSysAuthorityMenus(db *gorm.DB) (err error) { - return db.Table("sys_authority_menus").Transaction(func(tx *gorm.DB) error { +func InitSysAuthorityMenus(db *gorm.DB) { + if err := db.Table("sys_authority_menus").Transaction(func(tx *gorm.DB) error { if tx.Where("sys_authority_authority_id IN ?", []string{"888", "8881", "9528"}).Find(&[]SysAuthorityMenus{}).RowsAffected == 53 { color.Danger.Println("sys_authority_menus表的初始数据已存在!") return nil @@ -76,5 +77,8 @@ func InitSysAuthorityMenus(db *gorm.DB) (err error) { return err } return nil - }) + }); err != nil { + color.Warn.Printf("[Mysql]--> sys_authority_menus 表的初始数据失败,err: %v\n", err) + os.Exit(0) + } } diff --git a/server/cmd/datas/casbins.go b/server/cmd/datas/casbins.go index 6632733b..95f2380a 100644 --- a/server/cmd/datas/casbins.go +++ b/server/cmd/datas/casbins.go @@ -4,6 +4,7 @@ import ( gormadapter "github.com/casbin/gorm-adapter/v3" "github.com/gookit/color" "gorm.io/gorm" + "os" ) var Carbines = []gormadapter.CasbinRule{ @@ -151,8 +152,8 @@ var Carbines = []gormadapter.CasbinRule{ {PType: "p", V0: "9528", V1: "/autoCode/createTemp", V2: "POST"}, } -func InitCasbinModel(db *gorm.DB) (err error) { - return db.Transaction(func(tx *gorm.DB) error { +func InitCasbinModel(db *gorm.DB) { + if err := db.Transaction(func(tx *gorm.DB) error { if tx.Where("p_type = ? AND v0 IN ?", "p", []string{"888", "8881", "9528"}).Find(&[]gormadapter.CasbinRule{}).RowsAffected == 142 { color.Danger.Println("casbin_rule表的初始数据已存在!") return nil @@ -161,5 +162,8 @@ func InitCasbinModel(db *gorm.DB) (err error) { return err } return nil - }) + }); err != nil { + color.Warn.Printf("[Mysql]--> casbin_rule 表的初始数据失败,err: %v\n", err) + os.Exit(0) + } } diff --git a/server/cmd/datas/customers.go b/server/cmd/datas/customers.go index d2f91b00..b919f916 100644 --- a/server/cmd/datas/customers.go +++ b/server/cmd/datas/customers.go @@ -3,6 +3,7 @@ package datas import ( "gin-vue-admin/global" "github.com/gookit/color" + "os" "time" "gin-vue-admin/model" @@ -13,8 +14,8 @@ var Customers = []model.ExaCustomer{ {GVA_MODEL: global.GVA_MODEL{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, CustomerName: "测试客户", CustomerPhoneData: "1761111111", SysUserID: 1, SysUserAuthorityID: "888"}, } -func InitExaCustomer(db *gorm.DB) (err error) { - return db.Transaction(func(tx *gorm.DB) error { +func InitExaCustomer(db *gorm.DB) { + if err := db.Transaction(func(tx *gorm.DB) error { if tx.Where("id IN ? ", []int{1}).Find(&[]model.ExaCustomer{}).RowsAffected == 1 { color.Danger.Println("exa_customers表的初始数据已存在!") return nil @@ -23,5 +24,8 @@ func InitExaCustomer(db *gorm.DB) (err error) { return err } return nil - }) + }); err != nil { + color.Warn.Printf("[Mysql]--> exa_customers 表的初始数据失败,err: %v\n", err) + os.Exit(0) + } } diff --git a/server/cmd/datas/dictionaries.go b/server/cmd/datas/dictionaries.go index c3567225..b111c962 100644 --- a/server/cmd/datas/dictionaries.go +++ b/server/cmd/datas/dictionaries.go @@ -3,13 +3,14 @@ package datas import ( "gin-vue-admin/global" "github.com/gookit/color" + "os" "time" "gin-vue-admin/model" "gorm.io/gorm" ) -func InitSysDictionary(db *gorm.DB) (err error) { +func InitSysDictionary(db *gorm.DB) { var status = new(bool) *status = true Dictionaries := []model.SysDictionary{ @@ -20,7 +21,7 @@ func InitSysDictionary(db *gorm.DB) (err error) { {GVA_MODEL: global.GVA_MODEL{ID: 5, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库字符串", Type: "string", Status: status, Desc: "数据库字符串"}, {GVA_MODEL: global.GVA_MODEL{ID: 6, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库bool类型", Type: "bool", Status: status, Desc: "数据库bool类型"}, } - return db.Transaction(func(tx *gorm.DB) error { + if err := db.Transaction(func(tx *gorm.DB) error { if tx.Where("id IN ?", []int{1, 6}).Find(&[]model.SysDictionary{}).RowsAffected == 2 { color.Danger.Println("sys_dictionaries表的初始数据已存在!") return nil @@ -29,5 +30,8 @@ func InitSysDictionary(db *gorm.DB) (err error) { return err } return nil - }) + }); err != nil { + color.Warn.Printf("[Mysql]--> sys_dictionaries 表的初始数据失败,err: %v\n", err) + os.Exit(0) + } } \ No newline at end of file diff --git a/server/cmd/datas/dictionary_details.go b/server/cmd/datas/dictionary_details.go index 9a1cfd81..40c87b98 100644 --- a/server/cmd/datas/dictionary_details.go +++ b/server/cmd/datas/dictionary_details.go @@ -3,13 +3,14 @@ package datas import ( "gin-vue-admin/global" "github.com/gookit/color" + "os" "time" "gin-vue-admin/model" "gorm.io/gorm" ) -func InitSysDictionaryDetail(db *gorm.DB) (err error) { +func InitSysDictionaryDetail(db *gorm.DB) { status := new(bool) *status = true DictionaryDetail := []model.SysDictionaryDetail{ @@ -37,7 +38,7 @@ func InitSysDictionaryDetail(db *gorm.DB) (err error) { {global.GVA_MODEL{ID: 22, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "longtext", 9, status, 9, 5}, {global.GVA_MODEL{ID: 23, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "tinyint", 0, status, 0, 6}, } - return db.Transaction(func(tx *gorm.DB) error { + if err := db.Transaction(func(tx *gorm.DB) error { if tx.Where("id IN ?", []int{1, 23}).Find(&[]model.SysDictionaryDetail{}).RowsAffected == 2 { color.Danger.Println("sys_dictionary_details表的初始数据已存在!") return nil @@ -46,5 +47,8 @@ func InitSysDictionaryDetail(db *gorm.DB) (err error) { return err } return nil - }) + }); err != nil { + color.Warn.Printf("[Mysql]--> sys_dictionary_details 表的初始数据失败,err: %v\n", err) + os.Exit(0) + } } diff --git a/server/cmd/datas/files.go b/server/cmd/datas/files.go index 05e88a37..5aada17a 100644 --- a/server/cmd/datas/files.go +++ b/server/cmd/datas/files.go @@ -3,6 +3,7 @@ package datas import ( "gin-vue-admin/global" "github.com/gookit/color" + "os" "time" "gin-vue-admin/model" @@ -14,8 +15,8 @@ var Files = []model.ExaFileUploadAndDownload{ {global.GVA_MODEL{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}, "logo.png", "http://qmplusimg.henrongyi.top/1576554439myAvatar.png", "png", "1587973709logo.png"}, } -func InitExaFileUploadAndDownload(db *gorm.DB) (err error) { - return db.Transaction(func(tx *gorm.DB) error { +func InitExaFileUploadAndDownload(db *gorm.DB) { + if err := db.Transaction(func(tx *gorm.DB) error { if tx.Where("id IN ?", []int{1, 2}).Find(&[]model.ExaFileUploadAndDownload{}).RowsAffected == 2 { color.Danger.Println("exa_file_upload_and_downloads表的初始数据已存在!") return nil @@ -23,6 +24,10 @@ func InitExaFileUploadAndDownload(db *gorm.DB) (err error) { if err := tx.Create(&Files).Error; err != nil { // 遇到错误时回滚事务 return err } + color.Info.Println("[Mysql]-->初始化数据成功") return nil - }) + }); err != nil { + color.Warn.Printf("[Mysql]--> exa_file_upload_and_downloads 表的初始数据失败,err: %v\n", err) + os.Exit(0) + } } diff --git a/server/cmd/datas/init.go b/server/cmd/datas/init.go index 7e7e9cd2..96917318 100644 --- a/server/cmd/datas/init.go +++ b/server/cmd/datas/init.go @@ -9,24 +9,18 @@ import ( ) func InitMysqlData(db *gorm.DB) { - var err error - err = InitSysApi(db) - err = InitSysUser(db) - err = InitExaCustomer(db) - err = InitCasbinModel(db) - err = InitSysAuthority(db) - err = InitSysBaseMenus(db) - err = InitAuthorityMenu(db) - err = InitSysDictionary(db) - err = InitSysAuthorityMenus(db) - err = InitSysDataAuthorityId(db) - err = InitSysDictionaryDetail(db) - err = InitExaFileUploadAndDownload(db) - if err != nil { - color.Warn.Printf("[Mysql]-->初始化数据失败,err: %v\n", err) - os.Exit(0) - } - color.Info.Println("[Mysql]-->初始化数据成功") + InitSysApi(db) + InitSysUser(db) + InitExaCustomer(db) + InitCasbinModel(db) + InitSysAuthority(db) + InitSysBaseMenus(db) + InitAuthorityMenu(db) + InitSysDictionary(db) + InitSysAuthorityMenus(db) + InitSysDataAuthorityId(db) + InitSysDictionaryDetail(db) + InitExaFileUploadAndDownload(db) } func InitMysqlTables(db *gorm.DB) { diff --git a/server/cmd/datas/menus.go b/server/cmd/datas/menus.go index acd7a799..69a9c1d0 100644 --- a/server/cmd/datas/menus.go +++ b/server/cmd/datas/menus.go @@ -3,6 +3,7 @@ package datas import ( "gin-vue-admin/global" "github.com/gookit/color" + "os" "time" "gin-vue-admin/model" @@ -39,8 +40,8 @@ var BaseMenus = []model.SysBaseMenu{ {GVA_MODEL: global.GVA_MODEL{ID: 27, CreatedAt: time.Now(), UpdatedAt: time.Now()}, MenuLevel: 0, ParentId: "0", Path: "state", Name: "state", Hidden: false, Component: "view/system/state.vue", Sort: 6, Meta: model.Meta{Title: "服务器状态", Icon: "cloudy"}}, } -func InitSysBaseMenus(db *gorm.DB) (err error) { - return db.Transaction(func(tx *gorm.DB) error { +func InitSysBaseMenus(db *gorm.DB) { + if err := db.Transaction(func(tx *gorm.DB) error { if tx.Where("id IN ?", []int{1, 27}).Find(&[]model.SysBaseMenu{}).RowsAffected == 2 { color.Danger.Println("sys_base_menus表的初始数据已存在!") return nil @@ -49,5 +50,8 @@ func InitSysBaseMenus(db *gorm.DB) (err error) { return err } return nil - }) + }); err != nil { + color.Warn.Printf("[Mysql]--> sys_base_menus 表的初始数据失败,err: %v\n", err) + os.Exit(0) + } } diff --git a/server/cmd/datas/users.go b/server/cmd/datas/users.go index c2353ab6..e0b69829 100644 --- a/server/cmd/datas/users.go +++ b/server/cmd/datas/users.go @@ -3,6 +3,7 @@ package datas import ( "gin-vue-admin/global" "github.com/gookit/color" + "os" "time" "gin-vue-admin/model" @@ -15,8 +16,8 @@ var Users = []model.SysUser{ {GVA_MODEL: global.GVA_MODEL{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}, UUID: uuid.NewV4(), Username: "a303176530", Password: "3ec063004a6f31642261936a379fde3d", NickName: "QMPlusUser", HeaderImg: "http://qmplusimg.henrongyi.top/1572075907logo.png", AuthorityId: "9528"}, } -func InitSysUser(db *gorm.DB) (err error) { - return db.Transaction(func(tx *gorm.DB) error { +func InitSysUser(db *gorm.DB) { + if err := db.Transaction(func(tx *gorm.DB) error { if tx.Where("id IN ?", []int{1, 2}).Find(&[]model.SysUser{}).RowsAffected == 2 { color.Danger.Println("sys_users表的初始数据已存在!") return nil @@ -25,5 +26,8 @@ func InitSysUser(db *gorm.DB) (err error) { return err } return nil - }) + }); err != nil { + color.Warn.Printf("[Mysql]--> sys_users 表的初始数据失败,err: %v\n", err) + os.Exit(0) + } } From 65118ef9d1d9faf89f21193a79ae7298cd88871e Mon Sep 17 00:00:00 2001 From: SliverHorn Date: Mon, 16 Nov 2020 20:40:51 +0800 Subject: [PATCH 08/13] From->Form --- server/service/sys_auto_code.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/service/sys_auto_code.go b/server/service/sys_auto_code.go index dbdc7b5d..afeca1eb 100644 --- a/server/service/sys_auto_code.go +++ b/server/service/sys_auto_code.go @@ -202,7 +202,7 @@ func addAutoMoveFile(data *tplData) { if strings.Contains(data.autoCodePath, "js") { data.autoMoveFilePath = filepath.Join("../", "web", "src", dir, base) } else if strings.Contains(data.autoCodePath, "form") { - data.autoMoveFilePath = filepath.Join("../", "web", "src", "view", filepath.Base(filepath.Dir(filepath.Dir(data.autoCodePath))), strings.TrimSuffix(base, filepath.Ext(base))+"From.vue") + data.autoMoveFilePath = filepath.Join("../", "web", "src", "view", filepath.Base(filepath.Dir(filepath.Dir(data.autoCodePath))), strings.TrimSuffix(base, filepath.Ext(base))+"Form.vue") } else if strings.Contains(data.autoCodePath, "table") { data.autoMoveFilePath = filepath.Join("../", "web", "src", "view", filepath.Base(filepath.Dir(filepath.Dir(data.autoCodePath))), base) } From 2bca2e3151baf25b92455764f3d9970e69be3e07 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Mon, 16 Nov 2020 20:55:47 +0800 Subject: [PATCH 09/13] =?UTF-8?q?=E7=83=AD=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改bug 优化增加回显 --- server/utils/cmd_Task.go | 44 +++++++++++++++++++++--------- server/utils/cmd_monitor.go | 53 ++++++++++++++++++++++--------------- 2 files changed, 63 insertions(+), 34 deletions(-) diff --git a/server/utils/cmd_Task.go b/server/utils/cmd_Task.go index 2eded600..a040179a 100644 --- a/server/utils/cmd_Task.go +++ b/server/utils/cmd_Task.go @@ -1,7 +1,9 @@ package utils import ( + "bytes" "fmt" + "io" "os" "os/exec" "path/filepath" @@ -85,7 +87,7 @@ func (t *T) DefaultF(ch chan struct{}) error { // 判断是否有makefile _, err := os.Stat(filepath.Join("Makefile")) - if runtime.GOOS != "windows" && err != nil { + if runtime.GOOS != "windows" && err == nil { _, err := exec.LookPath("make") if err == nil { cmd = exec.Command("makefile") @@ -105,12 +107,12 @@ func (t *T) DefaultF(ch chan struct{}) error { default: buildCmd = exec.Command("go", "build", "-o", "gva", "main.go") } - err = buildCmd.Run() - fmt.Println("build 执行完成") if err != nil { return err } + fmt.Println("build 执行完成") + // 执行 switch runtime.GOOS { @@ -121,15 +123,7 @@ func (t *T) DefaultF(ch chan struct{}) error { } makefile: // 开始执行任务 - err = cmd.Start() - if err != nil { - return err - } - t.p = cmd.Process - fmt.Println("pid", t.p.Pid) - go func() { - err = cmd.Wait() - }() + t.echo(cmd) <-ch // 回收资源 err = cmd.Process.Kill() @@ -138,3 +132,29 @@ makefile: t.closeChan <- struct{}{} return err } + +// echo: 封装回显 +func (t *T) echo(cmd *exec.Cmd) error { + var stdoutBuf bytes.Buffer + stdoutIn, _ := cmd.StdoutPipe() + var errStdout, errStderr error + stdout := io.MultiWriter(os.Stdout, &stdoutBuf) + err := cmd.Start() + if err != nil { + return err + } + go func() { + _, errStdout = io.Copy(stdout, stdoutIn) + }() + t.p = cmd.Process + fmt.Println("pid", t.p.Pid) + go func() { + _ = cmd.Wait() + if errStdout != nil || errStderr != nil { + fmt.Printf("failed to capture stdout or stderr\n") + } + outStr := string(stdoutBuf.Bytes()) + fmt.Printf("\nout:\n%s\n", outStr) + }() + return nil +} diff --git a/server/utils/cmd_monitor.go b/server/utils/cmd_monitor.go index 187be57e..cc1f58ec 100644 --- a/server/utils/cmd_monitor.go +++ b/server/utils/cmd_monitor.go @@ -55,33 +55,24 @@ func (w *Watch) Watch(path string, t *T) error { c <- errors.New("errors close") return } - // 判断时间 - fmt.Println("even", even) + // 判断事件 switch { - // todo 待处理 case even.Op&fsnotify.Create == fsnotify.Create: //这里获取新创建文件的信息,如果是目录,则加入监控中 fmt.Println("创建文件 : ", even.Name) //t.AddTask() _ = w.Add(even.Name) + w.addTask(t, 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() + fmt.Println("修改文件 : ", even.Name) + w.addTask(t, even.Name) + case even.Op&fsnotify.Remove == fsnotify.Remove || even.Op&fsnotify.Rename == fsnotify.Rename: + fmt.Println("删除或重命名文件 : ", even.Name) _ = w.Remove(even.Name) + w.addTask(t, even.Name) } case err = <-w.Errors: - fmt.Println("79", err) + fmt.Println("even Error:", err) c <- err return } @@ -106,10 +97,12 @@ func (w *Watch) watchDir(path string) error { for _, f := range fileSlice { fPath := filepath.Join(path, f.Name()) if !f.IsDir() { - // todo 这里加一些条件筛选添加监控的内容 - err = w.watchFile(fPath) - if err != nil { - return err + // 判断是否可监控的文件 + if chickPower(fPath) { + err = w.watchFile(fPath) + if err != nil { + return err + } } } else { err := w.watchDir(fPath) @@ -123,5 +116,21 @@ func (w *Watch) watchDir(path string) error { // watchDir: 处理监控单文件 func (w *Watch) watchFile(path string) error { - return w.Add(path) + var err error + if chickPower(path) { + err = w.Add(path) + } + return err +} + +// chickPower: 判断是否在可控范围内 +func chickPower(name string) bool { + return filepath.Ext(name) == ".go" || filepath.Ext(name) == ".yaml" +} + +// addTask: 偏函数 简化发送任务 +func (w *Watch) addTask(t *T, name string) { + if chickPower(name) { + t.AddTask() + } } From b71e126ba01518754132e91db6222418b895eda8 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Mon, 16 Nov 2020 21:07:57 +0800 Subject: [PATCH 10/13] Update cmd_Task.go --- server/utils/cmd_Task.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/utils/cmd_Task.go b/server/utils/cmd_Task.go index a040179a..5c3d4f30 100644 --- a/server/utils/cmd_Task.go +++ b/server/utils/cmd_Task.go @@ -90,7 +90,7 @@ func (t *T) DefaultF(ch chan struct{}) error { if runtime.GOOS != "windows" && err == nil { _, err := exec.LookPath("make") if err == nil { - cmd = exec.Command("makefile") + cmd = exec.Command("make") goto makefile } } From b50ff46a960abc7e7193dfe6ef25cddc615c8bed Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Mon, 16 Nov 2020 21:31:38 +0800 Subject: [PATCH 11/13] c2 --- server/utils/cmd_Task.go | 5 ++++- server/utils/cmd_monitor.go | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/server/utils/cmd_Task.go b/server/utils/cmd_Task.go index 5c3d4f30..93f086de 100644 --- a/server/utils/cmd_Task.go +++ b/server/utils/cmd_Task.go @@ -50,6 +50,9 @@ func newT(f func(chan struct{}) error) *T { } func (t *T) AddTask() { + if len(t.ch) == 1 { + return + } t.Lock() defer t.Unlock() if len(t.ch) == 1 { @@ -90,7 +93,7 @@ func (t *T) DefaultF(ch chan struct{}) error { if runtime.GOOS != "windows" && err == nil { _, err := exec.LookPath("make") if err == nil { - cmd = exec.Command("make") + cmd = exec.Command("make", "run") goto makefile } } diff --git a/server/utils/cmd_monitor.go b/server/utils/cmd_monitor.go index cc1f58ec..779afd01 100644 --- a/server/utils/cmd_monitor.go +++ b/server/utils/cmd_monitor.go @@ -62,7 +62,6 @@ func (w *Watch) Watch(path string, t *T) error { fmt.Println("创建文件 : ", even.Name) //t.AddTask() _ = w.Add(even.Name) - w.addTask(t, even.Name) case even.Op&fsnotify.Write == fsnotify.Write: fmt.Println("修改文件 : ", even.Name) w.addTask(t, even.Name) From 67e3efb38661d38307ec36a85dc174318dd6d482 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Mon, 16 Nov 2020 22:12:44 +0800 Subject: [PATCH 12/13] bug log++ --- server/utils/cmd_Task.go | 1 + server/utils/cmd_monitor.go | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/server/utils/cmd_Task.go b/server/utils/cmd_Task.go index 93f086de..b32fef23 100644 --- a/server/utils/cmd_Task.go +++ b/server/utils/cmd_Task.go @@ -60,6 +60,7 @@ func (t *T) AddTask() { // 直接丢弃这次任务 return } + fmt.Println("::::发送任务->>>>>>>>") t.ch <- struct{}{} } diff --git a/server/utils/cmd_monitor.go b/server/utils/cmd_monitor.go index 779afd01..d95e35b7 100644 --- a/server/utils/cmd_monitor.go +++ b/server/utils/cmd_monitor.go @@ -124,12 +124,15 @@ func (w *Watch) watchFile(path string) error { // chickPower: 判断是否在可控范围内 func chickPower(name string) bool { - return filepath.Ext(name) == ".go" || filepath.Ext(name) == ".yaml" + name = filepath.Ext(name) + fmt.Println(name) + return name == ".go" || name == ".yaml" } // addTask: 偏函数 简化发送任务 func (w *Watch) addTask(t *T, name string) { if chickPower(name) { + fmt.Println("Add Task->>>>>>") t.AddTask() } } From 378ea1fadc0b188cefd290d65814ba6a72a322b8 Mon Sep 17 00:00:00 2001 From: SliverHorn Date: Tue, 17 Nov 2020 11:50:19 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E9=94=81=E5=AE=9Agorm-adapter/v3?= =?UTF-8?q?=E7=9A=84=E7=89=88=E6=9C=AC=E4=B8=BAv3.0.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/go.mod | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/go.mod b/server/go.mod index 358cc460..93071cd6 100644 --- a/server/go.mod +++ b/server/go.mod @@ -56,3 +56,5 @@ require ( gorm.io/driver/mysql v0.3.0 gorm.io/gorm v1.20.5 ) + +replace github.com/casbin/gorm-adapter/v3 => github.com/casbin/gorm-adapter/v3 v3.0.2 \ No newline at end of file