From 4abb10d807a59461d7b30586ce1b7b4f49cf69a8 Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Thu, 19 Oct 2023 22:18:38 +0200 Subject: [PATCH] fix: unify cache folder handling --- internal/esbuild/sass.go | 13 +++++-------- internal/phplint/download.go | 12 +++++------- internal/phplint/wasm.go | 3 ++- internal/system/cache.go | 12 ++++++++++++ 4 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 internal/system/cache.go diff --git a/internal/esbuild/sass.go b/internal/esbuild/sass.go index 9a0d6171..10dfc880 100644 --- a/internal/esbuild/sass.go +++ b/internal/esbuild/sass.go @@ -10,14 +10,16 @@ import ( "net/http" "os" "os/exec" + "path" "path/filepath" "runtime" "strings" + "github.com/FriendsOfShopware/shopware-cli/internal/system" "github.com/FriendsOfShopware/shopware-cli/logging" ) -const dartSassVersion = "1.67.0" +const dartSassVersion = "1.69.4" //go:embed static/variables.scss var scssVariables []byte @@ -30,14 +32,9 @@ func downloadDartSass(ctx context.Context) (string, error) { return path, nil } - cacheDir, err := os.UserCacheDir() - if err != nil { - cacheDir = "/tmp" - } - - cacheDir += "/dart-sass-" + dartSassVersion + cacheDir := path.Join(system.GetShopwareCliCacheDir(), "dart-sass", dartSassVersion) - expectedPath := fmt.Sprintf("%s/sass", cacheDir) + expectedPath := path.Join(cacheDir, "sass") if _, err := os.Stat(expectedPath); err == nil { return expectedPath, nil diff --git a/internal/phplint/download.go b/internal/phplint/download.go index 6dfd3179..5d4d83ca 100644 --- a/internal/phplint/download.go +++ b/internal/phplint/download.go @@ -8,23 +8,21 @@ import ( "os" "path" + "github.com/FriendsOfShopware/shopware-cli/internal/system" "github.com/FriendsOfShopware/shopware-cli/logging" ) -func getShopwareCliCacheDir() string { - cacheDir, _ := os.UserCacheDir() - - return path.Join(cacheDir, "shopware-cli") -} - func findPHPWasmFile(ctx context.Context, phpVersion string) ([]byte, error) { expectedFile := "php-" + phpVersion + ".wasm" - expectedPathLocation := path.Join(getShopwareCliCacheDir(), "wasm", "php", expectedFile) + expectedPathLocation := path.Join(system.GetShopwareCliCacheDir(), "wasm", "php", expectedFile) if _, err := os.Stat(expectedPathLocation); err == nil { + logging.FromContext(ctx).Infof("Using existing PHP %s wasm build from %s", phpVersion, expectedPathLocation) return os.ReadFile(expectedPathLocation) } + logging.FromContext(ctx).Infof("Downloading PHP %s wasm build", phpVersion) + downloadUrl := "https://github.com/FriendsOfShopware/php-cli-wasm-binaries/releases/download/1.0.0/" + expectedFile r, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadUrl, nil) diff --git a/internal/phplint/wasm.go b/internal/phplint/wasm.go index 5b6b4bd4..1dd6795f 100644 --- a/internal/phplint/wasm.go +++ b/internal/phplint/wasm.go @@ -4,12 +4,13 @@ import ( "context" "path" + "github.com/FriendsOfShopware/shopware-cli/internal/system" "github.com/tetratelabs/wazero" "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" ) func getWazeroRuntime(ctx context.Context) (wazero.Runtime, error) { - cache, err := wazero.NewCompilationCacheWithDir(path.Join(getShopwareCliCacheDir(), "wasm", "cache")) + cache, err := wazero.NewCompilationCacheWithDir(path.Join(system.GetShopwareCliCacheDir(), "wasm", "cache")) if err != nil { return nil, err } diff --git a/internal/system/cache.go b/internal/system/cache.go new file mode 100644 index 00000000..03d6b68f --- /dev/null +++ b/internal/system/cache.go @@ -0,0 +1,12 @@ +package system + +import ( + "os" + "path" +) + +func GetShopwareCliCacheDir() string { + cacheDir, _ := os.UserCacheDir() + + return path.Join(cacheDir, "shopware-cli") +}