Skip to content

Commit c69cb58

Browse files
author
jld3103
authored
Merge pull request #172 from go-flutter-desktop/cache-invalidation
Add automatic and manual cache invalidation
2 parents ab692e2 + b2fbd6e commit c69cb58

File tree

8 files changed

+82
-42
lines changed

8 files changed

+82
-42
lines changed

cmd/build.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"runtime"
99
"strings"
1010

11-
"github.com/go-flutter-desktop/hover/internal/enginecache"
12-
1311
"github.com/hashicorp/go-version"
1412
"github.com/otiai10/copy"
1513
"github.com/spf13/cobra"
@@ -18,9 +16,11 @@ import (
1816
"github.com/go-flutter-desktop/hover/internal/androidmanifest"
1917
"github.com/go-flutter-desktop/hover/internal/build"
2018
"github.com/go-flutter-desktop/hover/internal/config"
19+
"github.com/go-flutter-desktop/hover/internal/enginecache"
2120
"github.com/go-flutter-desktop/hover/internal/fileutils"
2221
"github.com/go-flutter-desktop/hover/internal/log"
2322
"github.com/go-flutter-desktop/hover/internal/pubspec"
23+
internalVersion "github.com/go-flutter-desktop/hover/internal/version"
2424
"github.com/go-flutter-desktop/hover/internal/versioncheck"
2525
)
2626

@@ -576,7 +576,7 @@ func buildGoBinary(targetOS string, vmArguments []string) {
576576
}
577577
}
578578

579-
versioncheck.CheckForHoverUpdate(hoverVersion())
579+
versioncheck.CheckForHoverUpdate(internalVersion.HoverVersion())
580580

581581
if buildOrRunOpenGlVersion == "none" {
582582
log.Warnf("The '--opengl=none' flag makes go-flutter incompatible with texture plugins!")

cmd/clean-cache.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
"github.com/pkg/errors"
7+
"github.com/spf13/cobra"
8+
9+
"github.com/go-flutter-desktop/hover/internal/enginecache"
10+
"github.com/go-flutter-desktop/hover/internal/log"
11+
)
12+
13+
var cachePath string
14+
15+
func init() {
16+
cleanCacheCmd.Flags().StringVar(&cachePath, "cache-path", enginecache.DefaultCachePath(), "The path that hover uses to cache dependencies such as the Flutter engine .so/.dll")
17+
rootCmd.AddCommand(cleanCacheCmd)
18+
}
19+
20+
var cleanCacheCmd = &cobra.Command{
21+
Use: "clean-cache",
22+
Short: "Clean cached engine files",
23+
Args: func(cmd *cobra.Command, args []string) error {
24+
if len(args) > 0 {
25+
return errors.New("does not take arguments")
26+
}
27+
return nil
28+
},
29+
Run: func(cmd *cobra.Command, args []string) {
30+
err := os.RemoveAll(enginecache.BaseEngineCachePath(cachePath))
31+
if err != nil {
32+
log.Errorf("Failed to delete engine cache directory: %v", err)
33+
os.Exit(1)
34+
}
35+
},
36+
}

cmd/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
"github.com/pkg/errors"
1515

1616
"github.com/go-flutter-desktop/hover/internal/build"
17-
"github.com/go-flutter-desktop/hover/internal/flutterversion"
1817
"github.com/go-flutter-desktop/hover/internal/log"
1918
"github.com/go-flutter-desktop/hover/internal/pubspec"
19+
"github.com/go-flutter-desktop/hover/internal/version"
2020
)
2121

2222
// assertInFlutterProject asserts this command is executed in a flutter project
@@ -48,7 +48,7 @@ func assertHoverInitialized() {
4848
}
4949

5050
func checkFlutterChannel() {
51-
channel := flutterversion.FlutterChannel()
51+
channel := version.FlutterChannel()
5252
ignoreWarning := os.Getenv("HOVER_IGNORE_CHANNEL_WARNING")
5353
if channel != "beta" && ignoreWarning != "true" {
5454
log.Warnf("⚠ The go-flutter project tries to stay compatible with the beta channel of Flutter.")

cmd/docker.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/go-flutter-desktop/hover/internal/build"
1414
"github.com/go-flutter-desktop/hover/internal/log"
1515
"github.com/go-flutter-desktop/hover/internal/logstreamer"
16+
"github.com/go-flutter-desktop/hover/internal/version"
1617
)
1718

1819
func dockerHoverBuild(targetOS string, packagingTask packaging.Task, buildFlags []string, vmArguments []string) {
@@ -83,11 +84,11 @@ func dockerHoverBuild(targetOS string, packagingTask packaging.Task, buildFlags
8384
dockerArgs = append(dockerArgs, "--env", "HOVER_IN_DOCKER_BUILD_VMARGS="+strings.Join(vmArguments, ","))
8485
}
8586

86-
version := hoverVersion()
87-
if version == "(devel)" {
88-
version = "latest"
87+
hoverVersion := version.HoverVersion()
88+
if hoverVersion == "(devel)" {
89+
hoverVersion = "latest"
8990
}
90-
dockerImage := "goflutter/hover:" + version
91+
dockerImage := "goflutter/hover:" + hoverVersion
9192
dockerArgs = append(dockerArgs, dockerImage)
9293
targetOSAndPackaging := targetOS
9394
if packName := packagingTask.Name(); packName != "" {

cmd/doctor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515

1616
"github.com/go-flutter-desktop/hover/internal/build"
1717
"github.com/go-flutter-desktop/hover/internal/config"
18-
"github.com/go-flutter-desktop/hover/internal/flutterversion"
1918
"github.com/go-flutter-desktop/hover/internal/log"
19+
"github.com/go-flutter-desktop/hover/internal/version"
2020
)
2121

2222
func init() {
@@ -35,8 +35,8 @@ var doctorCmd = &cobra.Command{
3535
Run: func(cmd *cobra.Command, args []string) {
3636
assertInFlutterProject()
3737

38-
version := hoverVersion()
39-
log.Infof("Hover version %s running on %s", version, runtime.GOOS)
38+
hoverVersion := version.HoverVersion()
39+
log.Infof("Hover version %s running on %s", hoverVersion, runtime.GOOS)
4040

4141
log.Infof("Sharing flutter version")
4242
cmdFlutterVersion := exec.Command(build.FlutterBin(), "--version")
@@ -47,7 +47,7 @@ var doctorCmd = &cobra.Command{
4747
log.Errorf("Flutter --version failed: %v", err)
4848
}
4949

50-
engineCommitHash := flutterversion.FlutterRequiredEngineVersion()
50+
engineCommitHash := version.FlutterRequiredEngineVersion()
5151
log.Infof("Flutter engine commit: %s", log.Au().Magenta("https://github.com/flutter/engine/commit/"+engineCommitHash))
5252

5353
checkFlutterChannel()

cmd/version.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ package cmd
22

33
import (
44
"fmt"
5-
"os"
6-
"runtime/debug"
7-
"sync"
8-
9-
"github.com/go-flutter-desktop/hover/internal/log"
105
"github.com/pkg/errors"
116
"github.com/spf13/cobra"
7+
8+
"github.com/go-flutter-desktop/hover/internal/version"
129
)
1310

1411
func init() {
@@ -25,24 +22,7 @@ var versionCmd = &cobra.Command{
2522
return nil
2623
},
2724
Run: func(cmd *cobra.Command, args []string) {
28-
version := hoverVersion()
25+
version := version.HoverVersion()
2926
fmt.Printf("Hover %s\n", version)
3027
},
3128
}
32-
33-
var (
34-
hoverVersionValue string
35-
hoverVersionOnce sync.Once
36-
)
37-
38-
func hoverVersion() string {
39-
hoverVersionOnce.Do(func() {
40-
buildInfo, ok := debug.ReadBuildInfo()
41-
if !ok {
42-
log.Errorf("Cannot obtain version information from hover build. To resolve this, please go-get hover using Go 1.13 or newer.")
43-
os.Exit(1)
44-
}
45-
hoverVersionValue = buildInfo.Main.Version
46-
})
47-
return hoverVersionValue
48-
}

internal/enginecache/cache.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"github.com/pkg/errors"
1818

1919
"github.com/go-flutter-desktop/hover/internal/build"
20-
"github.com/go-flutter-desktop/hover/internal/flutterversion"
2120
"github.com/go-flutter-desktop/hover/internal/log"
21+
"github.com/go-flutter-desktop/hover/internal/version"
2222
)
2323

2424
func createSymLink(oldname, newname string) error {
@@ -173,7 +173,11 @@ func EngineConfig(targetOS string, mode build.Mode) string {
173173

174174
//noinspection GoNameStartsWithPackageName
175175
func EngineCachePath(targetOS, cachePath string, mode build.Mode) string {
176-
return filepath.Join(cachePath, "hover", "engine", EngineConfig(targetOS, mode))
176+
return filepath.Join(BaseEngineCachePath(cachePath), EngineConfig(targetOS, mode))
177+
}
178+
179+
func BaseEngineCachePath(cachePath string) string {
180+
return filepath.Join(cachePath, "hover", "engine")
177181
}
178182

179183
// ValidateOrUpdateEngine validates the engine we have cached matches the
@@ -198,10 +202,10 @@ func ValidateOrUpdateEngine(targetOS, cachePath, requiredEngineVersion string, m
198202
}
199203
cachedEngineVersion := string(cachedEngineVersionBytes)
200204
if len(requiredEngineVersion) == 0 {
201-
requiredEngineVersion = flutterversion.FlutterRequiredEngineVersion()
205+
requiredEngineVersion = version.FlutterRequiredEngineVersion()
202206
}
203207

204-
if cachedEngineVersion == requiredEngineVersion {
208+
if cachedEngineVersion == fmt.Sprintf("%s-%s", requiredEngineVersion, version.HoverVersion()) {
205209
log.Printf("Using engine from cache")
206210
return
207211
} else {
@@ -301,7 +305,7 @@ func ValidateOrUpdateEngine(targetOS, cachePath, requiredEngineVersion string, m
301305
}
302306
}
303307

304-
err = ioutil.WriteFile(cachedEngineVersionPath, []byte(requiredEngineVersion), 0664)
308+
err = ioutil.WriteFile(cachedEngineVersionPath, []byte(fmt.Sprintf("%s-%s", requiredEngineVersion, version.HoverVersion())), 0664)
305309
if err != nil {
306310
log.Errorf("Failed to write version file: %v", err)
307311
os.Exit(1)

internal/flutterversion/version.go renamed to internal/version/version.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package flutterversion
1+
package version
22

33
import (
44
"bytes"
55
"encoding/json"
66
"os"
77
"os/exec"
8+
"runtime/debug"
9+
"sync"
810

911
"github.com/go-flutter-desktop/hover/internal/build"
1012
"github.com/go-flutter-desktop/hover/internal/log"
@@ -56,3 +58,20 @@ type flutterVersionResponse struct {
5658
Channel string
5759
EngineRevision string
5860
}
61+
62+
var (
63+
hoverVersionValue string
64+
hoverVersionOnce sync.Once
65+
)
66+
67+
func HoverVersion() string {
68+
hoverVersionOnce.Do(func() {
69+
buildInfo, ok := debug.ReadBuildInfo()
70+
if !ok {
71+
log.Errorf("Cannot obtain version information from hover build. To resolve this, please go-get hover using Go 1.13 or newer.")
72+
os.Exit(1)
73+
}
74+
hoverVersionValue = buildInfo.Main.Version
75+
})
76+
return hoverVersionValue
77+
}

0 commit comments

Comments
 (0)