From a6f9f1383fa699c56dddecc5489a9ceeee4fdeaa Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 19 Oct 2021 09:27:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 14 +-- share/app.go | 234 ------------------------------------ share/{vars.go => const.go} | 0 share/types.go | 51 ++++++++ share/utils.go | 178 +++++++++++++++++++++++++++ 5 files changed, 236 insertions(+), 241 deletions(-) rename share/{vars.go => const.go} (100%) diff --git a/Makefile b/Makefile index 5fc241f1..f39ef7ce 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ GOFMT ?= gofmt "-s" PACKAGES ?= $(shell $(GO) list ./...) VETPACKAGES ?= $(shell $(GO) list ./... | grep -v /examples/) GOFILES := $(shell find . -name "*.go") -VERSION := $(shell grep 'const VERSION =' share/vars.go |awk '{print $$4}' |sed 's/\"//g') +VERSION := $(shell grep 'const VERSION =' share/const.go |awk '{print $$4}' |sed 's/\"//g') # ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) TESTFOLDER := $(shell $(GO) list ./... | grep -vE 'examples|tests*|config') @@ -123,8 +123,8 @@ gen-bindata: xiang: bindata if [ ! -z "${XIANG_DOMAIN}" ]; then \ - mv share/vars.go share/vars.go.bak; \ - sed "s/*.iqka.com/$(XIANG_DOMAIN)/g" share/vars.go.bak > share/vars.go; \ + mv share/const.go share/const.go.bak; \ + sed "s/*.iqka.com/$(XIANG_DOMAIN)/g" share/const.go.bak > share/const.go; \ fi; GOOS=linux GOARCH=amd64 go build -v -o .tmp/xiang-linux-amd64 @@ -136,8 +136,8 @@ xiang: bindata chmod +x dist/bin/xiang-*-* if [ ! -z "${XIANG_DOMAIN}" ]; then \ - rm share/vars.go; \ - mv share/vars.go.bak share/vars.go; \ + rm share/const.go; \ + mv share/const.go.bak share/const.go; \ fi; .PHONY: release @@ -164,8 +164,8 @@ release: clean # 制品 if [ ! -z "${XIANG_DOMAIN}" ]; then \ - mv dist/release/share/vars.go dist/release/share/vars.go.bak; \ - sed "s/*.iqka.com/$(XIANG_DOMAIN)/g" dist/release/share/vars.go.bak > dist/release/share/vars.go; \ + mv dist/release/share/const.go dist/release/share/const.go.bak; \ + sed "s/*.iqka.com/$(XIANG_DOMAIN)/g" dist/release/share/const.go.bak > dist/release/share/const.go; \ fi; cd dist/release && GOOS=linux GOARCH=amd64 go build -v -o ../../.tmp/xiang-${VERSION}-linux-amd64 diff --git a/share/app.go b/share/app.go index 5218f10d..e0f6fed8 100644 --- a/share/app.go +++ b/share/app.go @@ -1,69 +1,8 @@ package share -import ( - "io/ioutil" - "os" - "path" - "path/filepath" - "strings" - - "github.com/yaoapp/kun/exception" - "github.com/yaoapp/kun/maps" - "github.com/yaoapp/xiang/data" -) - // App 应用信息 var App AppInfo -// AppInfo 应用信息 -type AppInfo struct { - Name string `json:"name,omitempty"` - Short string `json:"short,omitempty"` - Version string `json:"version,omitempty"` - Description string `json:"description,omitempty"` - Icons maps.MapStrSync `json:"icons,omitempty"` - Storage AppStorage `json:"storage,omitempty"` - Option map[string]interface{} `json:"option,omitempty"` -} - -// AppStorage 应用存储 -type AppStorage struct { - Default string `json:"default"` - Buckets map[string]string `json:"buckets,omitempty"` - S3 map[string]interface{} `json:"s3,omitempty"` - OSS *AppStorageOSS `json:"oss,omitempty"` - COS map[string]interface{} `json:"cos,omitempty"` -} - -// AppStorageOSS 阿里云存储 -type AppStorageOSS struct { - Endpoint string `json:"endpoint,omitempty"` - ID string `json:"id,omitempty"` - Secret string `json:"secret,omitempty"` - RoleArn string `json:"roleArn,omitempty"` - SessionName string `json:"sessionName,omitempty"` -} - -// Script 脚本文件类型 -type Script struct { - Name string - Type string - Content []byte - File string -} - -// AppRoot 应用目录 -type AppRoot struct { - APIs string - Flows string - Models string - Plugins string - Tables string - Charts string - Screens string - Data string -} - // Public 输出公共信息 func (app AppInfo) Public() AppInfo { app.Storage.COS = nil @@ -71,176 +10,3 @@ func (app AppInfo) Public() AppInfo { app.Storage.S3 = nil return app } - -// GetAppPlugins 遍历应用目录,读取文件列表 -func GetAppPlugins(root string, typ string) []Script { - files := []Script{} - root = path.Join(root, "/") - filepath.Walk(root, func(file string, info os.FileInfo, err error) error { - if err != nil { - exception.Err(err, 500).Throw() - return err - } - if strings.HasSuffix(file, typ) { - files = append(files, GetAppPluginFile(root, file)) - } - return nil - }) - return files -} - -// GetAppPluginFile 读取文件 -func GetAppPluginFile(root string, file string) Script { - name := GetAppPluginFileName(root, file) - return Script{ - Name: name, - Type: "plugin", - File: file, - } -} - -// GetAppPluginFileName 读取文件 -func GetAppPluginFileName(root string, file string) string { - filename := strings.TrimPrefix(file, root+"/") - namer := strings.Split(filename, ".") - nametypes := strings.Split(namer[0], "/") - name := strings.Join(nametypes, ".") - return name -} - -// GetAppFilesFS 遍历应用目录,读取文件列表 -func GetAppFilesFS(root string, typ string) []Script { - files := []Script{} - root = path.Join(root, "/") - filepath.Walk(root, func(filepath string, info os.FileInfo, err error) error { - if err != nil { - exception.Err(err, 500).Throw() - return err - } - if strings.HasSuffix(filepath, typ) { - files = append(files, GetAppFile(root, filepath)) - } - - return nil - }) - return files -} - -// GetAppFile 读取文件 -func GetAppFile(root string, filepath string) Script { - name := GetAppFileName(root, filepath) - file, err := os.Open(filepath) - if err != nil { - exception.Err(err, 500).Throw() - } - - defer file.Close() - content, err := ioutil.ReadAll(file) - if err != nil { - exception.Err(err, 500).Throw() - } - return Script{ - Name: name, - Type: "app", - Content: content, - } -} - -// GetAppFileName 读取文件 -func GetAppFileName(root string, file string) string { - filename := strings.TrimPrefix(file, root+"/") - namer := strings.Split(filename, ".") - nametypes := strings.Split(namer[0], "/") - name := strings.Join(nametypes, ".") - return name -} - -// GetAppFileBaseName 读取文件base -func GetAppFileBaseName(root string, file string) string { - filename := strings.TrimPrefix(file, root+"/") - namer := strings.Split(filename, ".") - return filepath.Join(root, namer[0]) -} - -// GetFilesFS 遍历目录,读取文件列表 -func GetFilesFS(root string, typ string) []Script { - files := []Script{} - root = path.Join(root, "/") - filepath.Walk(root, func(path string, info os.FileInfo, err error) error { - if err != nil { - exception.Err(err, 500).Throw() - return err - } - if strings.HasSuffix(path, typ) { - files = append(files, GetFile(root, path)) - } - return nil - }) - return files -} - -// GetFile 读取文件 -func GetFile(root string, path string) Script { - filename := strings.TrimPrefix(path, root+"/") - name, typ := GetTypeName(filename) - file, err := os.Open(path) - if err != nil { - exception.Err(err, 500).Throw() - } - - defer file.Close() - content, err := ioutil.ReadAll(file) - if err != nil { - exception.Err(err, 500).Throw() - } - return Script{ - Name: name, - Type: typ, - Content: content, - } -} - -// GetFileName 读取文件 -func GetFileName(root string, file string) string { - filename := strings.TrimPrefix(file, root+"/") - name, _ := GetTypeName(filename) - return name -} - -// GetFileBaseName 读取文件base -func GetFileBaseName(root string, file string) string { - filename := strings.TrimPrefix(file, root+"/") - namer := strings.Split(filename, ".") - return filepath.Join(root, namer[0]) -} - -// GetFilesBin 从 bindata 中读取文件列表 -func GetFilesBin(root string, typ string) []Script { - files := []Script{} - binfiles := data.AssetNames() - for _, path := range binfiles { - if strings.HasSuffix(path, typ) { - file := strings.TrimPrefix(path, root+"/") - name, typ := GetTypeName(file) - content, err := data.Asset(path) - if err != nil { - exception.Err(err, 500).Throw() - } - files = append(files, Script{ - Name: name, - Type: typ, - Content: content, - }) - } - } - return files -} - -// GetTypeName 读取类型 -func GetTypeName(path string) (name string, typ string) { - namer := strings.Split(path, ".") - nametypes := strings.Split(namer[0], "/") - name = strings.Join(nametypes[1:], ".") - typ = nametypes[0] - return name, typ -} diff --git a/share/vars.go b/share/const.go similarity index 100% rename from share/vars.go rename to share/const.go diff --git a/share/types.go b/share/types.go index d4990bd6..293e2ec2 100644 --- a/share/types.go +++ b/share/types.go @@ -1,5 +1,7 @@ package share +import "github.com/yaoapp/kun/maps" + // API API 配置数据结构 type API struct { Name string `json:"-"` @@ -38,3 +40,52 @@ type Render struct { Props map[string]interface{} `json:"props,omitempty"` Components map[string]interface{} `json:"components,omitempty"` } + +// AppInfo 应用信息 +type AppInfo struct { + Name string `json:"name,omitempty"` + Short string `json:"short,omitempty"` + Version string `json:"version,omitempty"` + Description string `json:"description,omitempty"` + Icons maps.MapStrSync `json:"icons,omitempty"` + Storage AppStorage `json:"storage,omitempty"` + Option map[string]interface{} `json:"option,omitempty"` +} + +// AppStorage 应用存储 +type AppStorage struct { + Default string `json:"default"` + Buckets map[string]string `json:"buckets,omitempty"` + S3 map[string]interface{} `json:"s3,omitempty"` + OSS *AppStorageOSS `json:"oss,omitempty"` + COS map[string]interface{} `json:"cos,omitempty"` +} + +// AppStorageOSS 阿里云存储 +type AppStorageOSS struct { + Endpoint string `json:"endpoint,omitempty"` + ID string `json:"id,omitempty"` + Secret string `json:"secret,omitempty"` + RoleArn string `json:"roleArn,omitempty"` + SessionName string `json:"sessionName,omitempty"` +} + +// Script 脚本文件类型 +type Script struct { + Name string + Type string + Content []byte + File string +} + +// AppRoot 应用目录 +type AppRoot struct { + APIs string + Flows string + Models string + Plugins string + Tables string + Charts string + Screens string + Data string +} diff --git a/share/utils.go b/share/utils.go index 17e6fa98..9ba1a304 100644 --- a/share/utils.go +++ b/share/utils.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/yaoapp/kun/exception" + "github.com/yaoapp/xiang/data" ) // Walk 遍历应用目录,读取文件列表 @@ -59,3 +60,180 @@ func DirNotExists(dir string) bool { } return false } + +// ************************************************ +// 警告: 以下函数将被弃用 +// ************************************************ + +// GetAppPlugins 遍历应用目录,读取文件列表 +func GetAppPlugins(root string, typ string) []Script { + files := []Script{} + root = path.Join(root, "/") + filepath.Walk(root, func(file string, info os.FileInfo, err error) error { + if err != nil { + exception.Err(err, 500).Throw() + return err + } + if strings.HasSuffix(file, typ) { + files = append(files, GetAppPluginFile(root, file)) + } + return nil + }) + return files +} + +// GetAppPluginFile 读取文件 +func GetAppPluginFile(root string, file string) Script { + name := GetAppPluginFileName(root, file) + return Script{ + Name: name, + Type: "plugin", + File: file, + } +} + +// GetAppPluginFileName 读取文件 +func GetAppPluginFileName(root string, file string) string { + filename := strings.TrimPrefix(file, root+"/") + namer := strings.Split(filename, ".") + nametypes := strings.Split(namer[0], "/") + name := strings.Join(nametypes, ".") + return name +} + +// GetAppFilesFS 遍历应用目录,读取文件列表 +func GetAppFilesFS(root string, typ string) []Script { + files := []Script{} + root = path.Join(root, "/") + filepath.Walk(root, func(filepath string, info os.FileInfo, err error) error { + if err != nil { + exception.Err(err, 500).Throw() + return err + } + if strings.HasSuffix(filepath, typ) { + files = append(files, GetAppFile(root, filepath)) + } + + return nil + }) + return files +} + +// GetAppFile 读取文件 +func GetAppFile(root string, filepath string) Script { + name := GetAppFileName(root, filepath) + file, err := os.Open(filepath) + if err != nil { + exception.Err(err, 500).Throw() + } + + defer file.Close() + content, err := ioutil.ReadAll(file) + if err != nil { + exception.Err(err, 500).Throw() + } + return Script{ + Name: name, + Type: "app", + Content: content, + } +} + +// GetAppFileName 读取文件 +func GetAppFileName(root string, file string) string { + filename := strings.TrimPrefix(file, root+"/") + namer := strings.Split(filename, ".") + nametypes := strings.Split(namer[0], "/") + name := strings.Join(nametypes, ".") + return name +} + +// GetAppFileBaseName 读取文件base +func GetAppFileBaseName(root string, file string) string { + filename := strings.TrimPrefix(file, root+"/") + namer := strings.Split(filename, ".") + return filepath.Join(root, namer[0]) +} + +// GetFilesFS 遍历目录,读取文件列表 +func GetFilesFS(root string, typ string) []Script { + files := []Script{} + root = path.Join(root, "/") + filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + if err != nil { + exception.Err(err, 500).Throw() + return err + } + if strings.HasSuffix(path, typ) { + files = append(files, GetFile(root, path)) + } + return nil + }) + return files +} + +// GetFile 读取文件 +func GetFile(root string, path string) Script { + filename := strings.TrimPrefix(path, root+"/") + name, typ := GetTypeName(filename) + file, err := os.Open(path) + if err != nil { + exception.Err(err, 500).Throw() + } + + defer file.Close() + content, err := ioutil.ReadAll(file) + if err != nil { + exception.Err(err, 500).Throw() + } + return Script{ + Name: name, + Type: typ, + Content: content, + } +} + +// GetFileName 读取文件 +func GetFileName(root string, file string) string { + filename := strings.TrimPrefix(file, root+"/") + name, _ := GetTypeName(filename) + return name +} + +// GetFileBaseName 读取文件base +func GetFileBaseName(root string, file string) string { + filename := strings.TrimPrefix(file, root+"/") + namer := strings.Split(filename, ".") + return filepath.Join(root, namer[0]) +} + +// GetFilesBin 从 bindata 中读取文件列表 +func GetFilesBin(root string, typ string) []Script { + files := []Script{} + binfiles := data.AssetNames() + for _, path := range binfiles { + if strings.HasSuffix(path, typ) { + file := strings.TrimPrefix(path, root+"/") + name, typ := GetTypeName(file) + content, err := data.Asset(path) + if err != nil { + exception.Err(err, 500).Throw() + } + files = append(files, Script{ + Name: name, + Type: typ, + Content: content, + }) + } + } + return files +} + +// GetTypeName 读取类型 +func GetTypeName(path string) (name string, typ string) { + namer := strings.Split(path, ".") + nametypes := strings.Split(namer[0], "/") + name = strings.Join(nametypes[1:], ".") + typ = nametypes[0] + return name, typ +}