Skip to content

Commit

Permalink
feat: link flutter dir to fvmhome/current
Browse files Browse the repository at this point in the history
  • Loading branch information
befovy committed Apr 24, 2020
1 parent 9085e25 commit 98b9df8
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions fvmgo/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func FlutterBin() string {
if len(projectBin) > 0 {
return projectBin
} else {
return path.Join(FvmHome(), "fvmbin", "flutter")
return path.Join(FvmHome(), "current")
}
}

Expand Down Expand Up @@ -308,7 +308,7 @@ func projectFlutterLink(dir string, depth int) string {
if len(dir) == 0 {
dir = WorkingDir()
}
link = path.Join(dir, ".fvmbin", "flutter")
link = path.Join(dir, ".fvmbin", "current")

if IsSymlink(link) {
return link
Expand All @@ -320,7 +320,7 @@ func projectFlutterLink(dir string, depth int) string {
return projectFlutterLink(path.Dir(dir), depth)
}

func linkFlutter(linkDir, version string) {
func linkFlutterBin(linkDir, version string) {
if !IsDirectory(linkDir) && !IsNotFound(linkDir) {
Errorf("The path fvm used to make link exists but is not a directory")
os.Exit(1)
Expand Down Expand Up @@ -352,6 +352,24 @@ func linkFlutter(linkDir, version string) {
}
}

func linkFlutterDir(linkDir, version string) {
versionDir := path.Join(VersionsDir(), version)

if !IsNotFound(linkDir) {
err := os.RemoveAll(linkDir)
if err != nil {
Errorf("Cannot remove link file: %v", err)
os.Exit(1)
}
}

err := os.Symlink(versionDir, linkDir)
if err != nil {
Errorf("Cannot link flutter to dest %s: %v", versionDir, err)
os.Exit(1)
}
}

func envPaths() []string {
osPath := os.Getenv("PATH")
var paths []string
Expand Down Expand Up @@ -397,16 +415,18 @@ func FlutterOutOfFvm(install string) []string {

func LinkGlobalFlutter(version string) {
linkPath := path.Join(FvmHome(), "fvmbin")
linkFlutter(linkPath, version)
linkFlutterBin(linkPath, version)

currentPath := path.Join(FvmHome(), "current")
linkFlutterDir(currentPath, version)
paths := envPaths()

if !stringSliceContains(paths, linkPath) {
if !stringSliceContains(paths, currentPath) {
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
cmd := YellowV(" export PATH=\"%s:$PATH\"", linkPath)
Infof("Add %s to path to make sure you can use flutter from terminal\n%v", linkPath, cmd)
cmd := YellowV(" export PATH=\"%s:$PATH\"", currentPath)
Infof("Add %s to path to make sure you can use flutter from terminal\n%v", currentPath, cmd)
} else {
Warnf("Add %s to path to make sure you can use flutter from terminal", linkPath)
Warnf("Add %s to path to make sure you can use flutter from terminal", currentPath)
}
} else {
Infof("linkpath: %v", linkPath)
Expand All @@ -415,5 +435,8 @@ func LinkGlobalFlutter(version string) {

func LinkProjectFlutter(version string) {
linkPath := path.Join(WorkingDir(), ".fvmbin")
linkFlutter(linkPath, version)
linkFlutterBin(linkPath, version)

currentPath := path.Join(WorkingDir(), ".fvmbin", "current")
linkFlutterDir(currentPath, version)
}

0 comments on commit 98b9df8

Please sign in to comment.