Skip to content

Commit fca3a9c

Browse files
committed
move enginecache func to new versioncheck package
1 parent 3193bae commit fca3a9c

File tree

5 files changed

+127
-116
lines changed

5 files changed

+127
-116
lines changed

cmd/build.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/spf13/cobra"
1515

1616
"github.com/go-flutter-desktop/hover/internal/enginecache"
17+
"github.com/go-flutter-desktop/hover/internal/versioncheck"
1718
)
1819

1920
var dotSlash = string([]byte{'.', filepath.Separator})
@@ -209,7 +210,7 @@ func build(projectName string, targetOS string, vmArguments []string) {
209210

210211
if buildBranch == "" {
211212

212-
currentTag, err := enginecache.CurrentGoFlutterTag(filepath.Join(wd, buildPath))
213+
currentTag, err := versioncheck.CurrentGoFlutterTag(filepath.Join(wd, buildPath))
213214
if err != nil {
214215
fmt.Printf("hover: %v\n", err)
215216
os.Exit(1)
@@ -233,7 +234,7 @@ func build(projectName string, targetOS string, vmArguments []string) {
233234
} else {
234235
// when the buildBranch is empty and the currentTag is a release.
235236
// Check if the 'go-flutter' needs updates.
236-
enginecache.CheckFoGoFlutterUpdate(filepath.Join(wd, buildPath), currentTag)
237+
versioncheck.CheckFoGoFlutterUpdate(filepath.Join(wd, buildPath), currentTag)
237238
}
238239

239240
} else {

cmd/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func hoverMigration() bool {
115115

116116
// askForConfirmation asks the user for confirmation.
117117
func askForConfirmation() bool {
118-
fmt.Printf("[Y/n]: ")
118+
fmt.Printf("[y/N]: ")
119119
in := bufio.NewReader(os.Stdin)
120120
s, err := in.ReadString('\n')
121121
if err != nil {
@@ -125,7 +125,7 @@ func askForConfirmation() bool {
125125
s = strings.TrimSpace(s)
126126
s = strings.ToLower(s)
127127

128-
if s == "y" || s == "yes" || s == "" {
128+
if s == "y" || s == "yes" {
129129
return true
130130
}
131131
return false

cmd/upgrade.go

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

1010
"github.com/go-flutter-desktop/hover/internal/enginecache"
11+
"github.com/go-flutter-desktop/hover/internal/versioncheck"
12+
1113
"github.com/spf13/cobra"
1214
)
1315

@@ -95,7 +97,7 @@ func upgradeGoFlutter(targetOS string, engineCachePath string) (err error) {
9597
return
9698
}
9799

98-
currentTag, err := enginecache.CurrentGoFlutterTag(filepath.Join(wd, buildPath))
100+
currentTag, err := versioncheck.CurrentGoFlutterTag(filepath.Join(wd, buildPath))
99101
if err != nil {
100102
fmt.Printf("hover: %v\n", err)
101103
os.Exit(1)

internal/enginecache/version.go

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@ package enginecache
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"os/exec"
8-
"path/filepath"
97
"regexp"
10-
"strconv"
11-
"strings"
12-
"time"
13-
14-
"github.com/pkg/errors"
15-
16-
latest "github.com/tcnksm/go-latest"
178
)
189

1910
func flutterRequiredEngineVersion() string {
@@ -32,105 +23,3 @@ func flutterRequiredEngineVersion() string {
3223

3324
return versionMatch[1]
3425
}
35-
36-
// CheckFoGoFlutterUpdate check the last 'go-flutter' timestamp we have cached
37-
// for the current project. If the last update comes back to more than X days,
38-
// fetch the last Github release semver. If the Github semver is more recent
39-
// than the current one, display the update notice.
40-
func CheckFoGoFlutterUpdate(goDirectoryPath string, currentTag string) {
41-
cachedGoFlutterCheckPath := filepath.Join(goDirectoryPath, ".last_goflutter_check")
42-
cachedGoFlutterCheckBytes, err := ioutil.ReadFile(cachedGoFlutterCheckPath)
43-
if err != nil && !os.IsNotExist(err) {
44-
fmt.Printf("hover: Failed to read the go-flutter last update check: %v\n", err)
45-
return
46-
}
47-
48-
cachedGoFlutterCheck := string(cachedGoFlutterCheckBytes)
49-
cachedGoFlutterCheck = strings.TrimSuffix(cachedGoFlutterCheck, "\n")
50-
51-
now := time.Now()
52-
nowString := strconv.FormatInt(now.Unix(), 10)
53-
54-
if cachedGoFlutterCheck == "" {
55-
err = ioutil.WriteFile(cachedGoFlutterCheckPath, []byte(nowString), 0664)
56-
if err != nil {
57-
fmt.Printf("hover: Failed to write the update timestamp to file: %v\n", err)
58-
}
59-
return
60-
}
61-
62-
i, err := strconv.ParseInt(cachedGoFlutterCheck, 10, 64)
63-
if err != nil {
64-
fmt.Printf("hover: Failed to parse the last update of go-flutter: %v\n", err)
65-
return
66-
}
67-
lastUpdateTimeStamp := time.Unix(i, 0)
68-
69-
checkRate := 1.0
70-
71-
newCheck := now.Sub(lastUpdateTimeStamp).Hours() > checkRate ||
72-
(now.Sub(lastUpdateTimeStamp).Minutes() < 1.0 && // keep the notice for X Minutes
73-
now.Sub(lastUpdateTimeStamp).Minutes() > 0.0)
74-
75-
checkUpdateOptOut := os.Getenv("HOVER_IGNORE_CHECK_NEW_RELEASE")
76-
if newCheck && checkUpdateOptOut != "true" {
77-
fmt.Printf("hover: Checking available release on Github\n")
78-
79-
// fecth the last githubTag
80-
githubTag := &latest.GithubTag{
81-
Owner: "go-flutter-desktop",
82-
Repository: "go-flutter",
83-
FixVersionStrFunc: latest.DeleteFrontV(),
84-
}
85-
86-
res, err := latest.Check(githubTag, currentTag)
87-
if err != nil {
88-
fmt.Printf("hover: Failed to check the latest release of 'go-flutter': %v\n", err)
89-
90-
// update the timestamp
91-
// don't spam people who don't have access to internet
92-
now := time.Now().Add(time.Duration(checkRate) * time.Hour)
93-
nowString := strconv.FormatInt(now.Unix(), 10)
94-
95-
err = ioutil.WriteFile(cachedGoFlutterCheckPath, []byte(nowString), 0664)
96-
if err != nil {
97-
fmt.Printf("hover: Failed to write the update timestamp to file: %v\n", err)
98-
}
99-
100-
return
101-
}
102-
if res.Outdated {
103-
fmt.Printf("hover: The core library 'go-flutter' has an update available. (%s -> %s)\n", currentTag, res.Current)
104-
fmt.Printf(" To update 'go-flutter' in this project run: $ hover upgrade\n")
105-
}
106-
107-
if now.Sub(lastUpdateTimeStamp).Hours() > checkRate {
108-
// update the timestamp
109-
err = ioutil.WriteFile(cachedGoFlutterCheckPath, []byte(nowString), 0664)
110-
if err != nil {
111-
fmt.Printf("hover: Failed to write the update timestamp to file: %v\n", err)
112-
}
113-
}
114-
}
115-
116-
}
117-
118-
// CurrentGoFlutterTag retrieve the semver of go-flutter in 'go.mod'
119-
func CurrentGoFlutterTag(goDirectoryPath string) (currentTag string, err error) {
120-
goModPath := filepath.Join(goDirectoryPath, "go.mod")
121-
goModBytes, err := ioutil.ReadFile(goModPath)
122-
if err != nil && !os.IsNotExist(err) {
123-
err = errors.Wrap(err, "Failed to read the 'go.mod' file: %v")
124-
return
125-
}
126-
127-
re := regexp.MustCompile(`\sgithub.com/go-flutter-desktop/go-flutter\s(\S*)`)
128-
129-
match := re.FindStringSubmatch(string(goModBytes))
130-
if len(match) < 2 {
131-
err = errors.New("Failed to parse the 'go-flutter' version in go.mod")
132-
return
133-
}
134-
currentTag = match[1]
135-
return
136-
}

internal/versioncheck/version.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package versioncheck
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"regexp"
9+
"strconv"
10+
"strings"
11+
"time"
12+
13+
"github.com/pkg/errors"
14+
latest "github.com/tcnksm/go-latest"
15+
)
16+
17+
// CheckFoGoFlutterUpdate check the last 'go-flutter' timestamp we have cached
18+
// for the current project. If the last update comes back to more than X days,
19+
// fetch the last Github release semver. If the Github semver is more recent
20+
// than the current one, display the update notice.
21+
func CheckFoGoFlutterUpdate(goDirectoryPath string, currentTag string) {
22+
cachedGoFlutterCheckPath := filepath.Join(goDirectoryPath, ".last_goflutter_check")
23+
cachedGoFlutterCheckBytes, err := ioutil.ReadFile(cachedGoFlutterCheckPath)
24+
if err != nil && !os.IsNotExist(err) {
25+
fmt.Printf("hover: Failed to read the go-flutter last update check: %v\n", err)
26+
return
27+
}
28+
29+
cachedGoFlutterCheck := string(cachedGoFlutterCheckBytes)
30+
cachedGoFlutterCheck = strings.TrimSuffix(cachedGoFlutterCheck, "\n")
31+
32+
now := time.Now()
33+
nowString := strconv.FormatInt(now.Unix(), 10)
34+
35+
if cachedGoFlutterCheck == "" {
36+
err = ioutil.WriteFile(cachedGoFlutterCheckPath, []byte(nowString), 0664)
37+
if err != nil {
38+
fmt.Printf("hover: Failed to write the update timestamp to file: %v\n", err)
39+
}
40+
return
41+
}
42+
43+
i, err := strconv.ParseInt(cachedGoFlutterCheck, 10, 64)
44+
if err != nil {
45+
fmt.Printf("hover: Failed to parse the last update of go-flutter: %v\n", err)
46+
return
47+
}
48+
lastUpdateTimeStamp := time.Unix(i, 0)
49+
50+
checkRate := 1.0
51+
52+
newCheck := now.Sub(lastUpdateTimeStamp).Hours() > checkRate ||
53+
(now.Sub(lastUpdateTimeStamp).Minutes() < 1.0 && // keep the notice for X Minutes
54+
now.Sub(lastUpdateTimeStamp).Minutes() > 0.0)
55+
56+
checkUpdateOptOut := os.Getenv("HOVER_IGNORE_CHECK_NEW_RELEASE")
57+
if newCheck && checkUpdateOptOut != "true" {
58+
fmt.Printf("hover: Checking available release on Github\n")
59+
60+
// fecth the last githubTag
61+
githubTag := &latest.GithubTag{
62+
Owner: "go-flutter-desktop",
63+
Repository: "go-flutter",
64+
FixVersionStrFunc: latest.DeleteFrontV(),
65+
}
66+
67+
res, err := latest.Check(githubTag, currentTag)
68+
if err != nil {
69+
fmt.Printf("hover: Failed to check the latest release of 'go-flutter': %v\n", err)
70+
71+
// update the timestamp
72+
// don't spam people who don't have access to internet
73+
now := time.Now().Add(time.Duration(checkRate) * time.Hour)
74+
nowString := strconv.FormatInt(now.Unix(), 10)
75+
76+
// TODO: add a ".last_goflutter_check" entry to "go/.gitignore" if the
77+
// hover project was init before hover#12
78+
err = ioutil.WriteFile(cachedGoFlutterCheckPath, []byte(nowString), 0664)
79+
if err != nil {
80+
fmt.Printf("hover: Failed to write the update timestamp to file: %v\n", err)
81+
}
82+
83+
return
84+
}
85+
if res.Outdated {
86+
fmt.Printf("hover: The core library 'go-flutter' has an update available. (%s -> %s)\n", currentTag, res.Current)
87+
fmt.Printf(" To update 'go-flutter' in this project run: $ hover upgrade\n")
88+
}
89+
90+
if now.Sub(lastUpdateTimeStamp).Hours() > checkRate {
91+
// update the timestamp
92+
err = ioutil.WriteFile(cachedGoFlutterCheckPath, []byte(nowString), 0664)
93+
if err != nil {
94+
fmt.Printf("hover: Failed to write the update timestamp to file: %v\n", err)
95+
}
96+
}
97+
}
98+
99+
}
100+
101+
// CurrentGoFlutterTag retrieve the semver of go-flutter in 'go.mod'
102+
func CurrentGoFlutterTag(goDirectoryPath string) (currentTag string, err error) {
103+
goModPath := filepath.Join(goDirectoryPath, "go.mod")
104+
goModBytes, err := ioutil.ReadFile(goModPath)
105+
if err != nil && !os.IsNotExist(err) {
106+
err = errors.Wrap(err, "Failed to read the 'go.mod' file: %v")
107+
return
108+
}
109+
110+
re := regexp.MustCompile(`\sgithub.com/go-flutter-desktop/go-flutter\s(\S*)`)
111+
112+
match := re.FindStringSubmatch(string(goModBytes))
113+
if len(match) < 2 {
114+
err = errors.New("Failed to parse the 'go-flutter' version in go.mod")
115+
return
116+
}
117+
currentTag = match[1]
118+
return
119+
}

0 commit comments

Comments
 (0)