-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2642272
commit 5d5a0ca
Showing
92 changed files
with
93 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,90 @@ | ||
package runner | ||
|
||
import ( | ||
"log" | ||
"os/exec" | ||
|
||
"github.com/TencentBlueKing/beego-runtime/utils" | ||
"embed" | ||
"github.com/TencentBlueKing/beego-runtime/conf" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func runCollectstatics() { | ||
staticDir, err := utils.GetStaticDirPath() | ||
if err != nil { | ||
log.Fatalf("get static files dir failed: %v\n", err) | ||
//go:embed views | ||
var views embed.FS | ||
|
||
//go:embed static | ||
var static embed.FS | ||
|
||
//go:embed data | ||
var apiGwData embed.FS | ||
|
||
func copyFiles(sourceDir, targetDir string, dirEntries []fs.DirEntry, Fs embed.FS) { | ||
for _, entry := range dirEntries { | ||
sourcePath := filepath.Join(sourceDir, entry.Name()) | ||
targetPath := filepath.Join(targetDir, entry.Name()) | ||
|
||
if entry.IsDir() { | ||
// 如果是文件夹,则递归拷贝文件夹中的内容 | ||
subDirEntries, err := Fs.ReadDir(sourcePath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = os.MkdirAll(targetPath, 0755) | ||
if err != nil { | ||
panic(err) | ||
} | ||
copyFiles(sourcePath, targetPath, subDirEntries, Fs) | ||
} else { | ||
// 如果是文件,则拷贝文件内容 | ||
fileData, err := Fs.ReadFile(sourcePath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = os.WriteFile(targetPath, fileData, 0644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
viewPath, err := utils.GetViewPath() | ||
} | ||
|
||
func syncFile() { | ||
ViewDirEntries, err := views.ReadDir("views") | ||
if err != nil { | ||
log.Fatalf("get view path failed: %v\n", err) | ||
panic(err) | ||
} | ||
definitionPath, err := utils.GetApigwDefinitionPath() | ||
StaticDirEntries, err := static.ReadDir("static") | ||
if err != nil { | ||
log.Fatalf("get apigw definition path failed: %v\n", err) | ||
panic(err) | ||
} | ||
resourcesPath, err := utils.GetApigwResourcesPath() | ||
|
||
err = os.MkdirAll("./views", 0755) | ||
if err != nil { | ||
log.Fatalf("get apigw resources path failed: %v\n", err) | ||
panic(err) | ||
} | ||
|
||
cmds := []*exec.Cmd{ | ||
exec.Command("cp", "-r", staticDir, "."), | ||
exec.Command("cp", "-r", viewPath, "."), | ||
exec.Command("cp", "-r", definitionPath, "."), | ||
exec.Command("cp", "-r", resourcesPath, "."), | ||
err = os.MkdirAll("./static", 0755) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, c := range cmds { | ||
log.Printf("run collect static command: %v\n", c) | ||
if err := c.Run(); err != nil { | ||
log.Fatalf("collect static failed: %v\n", err) | ||
copyFiles("views", "./views", ViewDirEntries, views) | ||
copyFiles("static", "./static", StaticDirEntries, static) | ||
|
||
if !conf.IsDevMode() { | ||
|
||
err = os.MkdirAll("./data", 0755) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
ApiGwDirEntries, err := apiGwData.ReadDir("data") | ||
if err != nil { | ||
panic(err) | ||
} | ||
copyFiles("data", "./data", ApiGwDirEntries, apiGwData) | ||
} | ||
} | ||
|
||
func runCollectstatics() { | ||
syncFile() | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1 @@ | ||
package utils | ||
|
||
import ( | ||
"os" | ||
"path" | ||
|
||
"github.com/TencentBlueKing/beego-runtime/info" | ||
"golang.org/x/mod/module" | ||
) | ||
|
||
func GetApigwDefinitionPath() (string, error) { | ||
baseDir, err := GetModulePath("github.com/TencentBlueKing/beego-runtime", info.Version()) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return path.Join(baseDir, "data/api-definition.yml"), nil | ||
} | ||
|
||
func GetApigwResourcesPath() (string, error) { | ||
baseDir, err := GetModulePath("github.com/TencentBlueKing/beego-runtime", info.Version()) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return path.Join(baseDir, "data/api-resources.yml"), nil | ||
} | ||
|
||
func GetStaticDirPath() (string, error) { | ||
baseDir, err := GetModulePath("github.com/TencentBlueKing/beego-runtime", info.Version()) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return path.Join(baseDir, "static"), nil | ||
} | ||
|
||
func GetViewPath() (string, error) { | ||
baseDir, err := GetModulePath("github.com/TencentBlueKing/beego-runtime", info.Version()) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return path.Join(baseDir, "views"), nil | ||
} | ||
|
||
func GetModulePath(name, version string) (string, error) { | ||
// first we need GOMODCACHE | ||
cache, ok := os.LookupEnv("GOMODCACHE") | ||
if !ok { | ||
cache = path.Join(os.Getenv("GOPATH"), "pkg", "mod") | ||
} | ||
|
||
// then we need to escape path | ||
escapedPath, err := module.EscapePath(name) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// version also | ||
escapedVersion, err := module.EscapeVersion(version) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return path.Join(cache, escapedPath+"@"+escapedVersion), nil | ||
} |