From 2d80683cbec20b81e90a796662b117a25217c294 Mon Sep 17 00:00:00 2001 From: Ink33 Date: Sun, 13 Mar 2022 22:24:05 +0800 Subject: [PATCH] feat: simplify code --- .github/workflows/test.yml | 11 ++++------- .travis.yml | 6 ++---- README.md | 10 ---------- bootstrap/static.go | 18 ++++++------------ build.sh | 1 - pkg/util/path.go | 6 ------ 6 files changed, 12 insertions(+), 40 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1df53014..944b015a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,13 +2,12 @@ name: Test on: pull_request: - branches: + branches: - master push: - branches: [ master ] + branches: [master] jobs: - test: name: Test runs-on: ubuntu-18.04 @@ -26,10 +25,8 @@ jobs: - name: Build static files run: | - cd assets - mkdir build - cd build - touch test.html + mkdir assets/build + touch assets/build/test.html - name: Test run: go test -coverprofile=coverage.txt -covermode=atomic ./... diff --git a/.travis.yml b/.travis.yml index 2acbb53a..275b4c38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,10 +5,8 @@ node_js: "12.16.3" git: depth: 1 before_script: - - cd assets - - mkdir build - - cd mkdir - - touch test.html + - mkdir assets/build + - touch assets/build/test.html script: - go test -coverprofile=coverage.txt -covermode=atomic ./... after_success: diff --git a/README.md b/README.md index 358f10b1..76ebf8a3 100644 --- a/README.md +++ b/README.md @@ -86,21 +86,11 @@ yarn install # 开始构建 yarn run build -# 可选: 删除map文件 -cd build -find . -name "*.map" | xargs rm -f ``` -#### 嵌入静态资源 - -当前版本无需手动嵌入静态资源,请直接进行下一步。 - #### 编译项目 ```shell -# 回到项目主目录 -cd ../ - # 获得当前版本号、Commit export COMMIT_SHA=$(git rev-parse --short HEAD) export VERSION=$(git describe --tags) diff --git a/bootstrap/static.go b/bootstrap/static.go index 973174d7..f767f04e 100644 --- a/bootstrap/static.go +++ b/bootstrap/static.go @@ -7,7 +7,6 @@ import ( "io" "io/fs" "net/http" - "os" "path/filepath" "github.com/pkg/errors" @@ -59,8 +58,9 @@ func InitStatic() { util.Log().Panic("无法初始化静态资源, %s", err) } - StaticFS = &GinFS{} - StaticFS.(*GinFS).FS = http.FS(embedFS) + StaticFS = &GinFS{ + FS: http.FS(embedFS), + } } // 检查静态资源的版本 f, err := StaticFS.Open("version.json") @@ -113,7 +113,7 @@ func Eject() { if !d.IsDir() { // 写入文件 - out, err := util.CreatNestedFile(filepath.Join(util.ExecPath(), StaticFolder, relPath)) + out, err := util.CreatNestedFile(filepath.Join(util.RelativePath(""), StaticFolder, relPath)) defer out.Close() if err != nil { @@ -125,12 +125,6 @@ func Eject() { if _, err := io.Copy(out, bufio.NewReader(obj)); err != nil { return errors.Errorf("无法写入文件[%s], %s, 跳过...", relPath, err) } - } else { - // 创建目录 - if err := os.MkdirAll(filepath.Join(util.ExecPath(), StaticFolder, relPath), 0755); err != nil { - return errors.Errorf("无法创建目录[%s], %s, 跳过...", relPath, err) - } - util.Log().Info("创建目录 [%s]...", relPath) } return nil } @@ -138,8 +132,8 @@ func Eject() { // util.Log().Info("开始导出内置静态资源...") err = fs.WalkDir(embedFS, ".", walk) if err != nil { - util.Log().Error("导出内置静态资源遇到错误:, %s", err) + util.Log().Error("导出内置静态资源遇到错误:%s", err) return } util.Log().Info("内置静态资源导出完成") -} \ No newline at end of file +} diff --git a/build.sh b/build.sh index c93170e2..c1ec24e0 100755 --- a/build.sh +++ b/build.sh @@ -27,7 +27,6 @@ buildAssets() { cd $REPO/assets - yarn --update-checksums yarn install yarn run build cd build diff --git a/pkg/util/path.go b/pkg/util/path.go index 6cba7122..2dd8aefe 100644 --- a/pkg/util/path.go +++ b/pkg/util/path.go @@ -57,9 +57,3 @@ func RelativePath(name string) string { return filepath.Join(filepath.Dir(e), name) } - -// ExecPath 获取可执行文件的路径 -func ExecPath() string { - e, _ := os.Executable() - return filepath.Dir(e) -}