Skip to content

Commit

Permalink
Sets proper flutter path, and fvm worspace
Browse files Browse the repository at this point in the history
  • Loading branch information
leoafarias committed Mar 1, 2019
1 parent d9914a5 commit b44bff3
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 70 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ Removes a specific version

$ fvm remove <version>

Removes all versions except the current activated one
Removes all Flutter versions, and channels

$ fvm shake
$ fvm clean


8 changes: 4 additions & 4 deletions cmd/shake.go → cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ import (
)

// pruneCmd represents the prune command
var shakeCmd = &cobra.Command{
Use: "shake",
Short: "Remove all versions except the current version",
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "Remove all flutter versions",

Run: func(cmd *cobra.Command, args []string) {
lib.RemoveVersions()
},
}

func init() {
rootCmd.AddCommand(shakeCmd)
rootCmd.AddCommand(cleanCmd)

// Here you will define your flags and configuration settings.

Expand Down
5 changes: 3 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ var listCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
fmt.Println(len(vs))

if len(vs) == 0 {
fmt.Println("No Flutter versions installed")
fmt.Println("No Flutter versions installed/n")
os.Exit(1)
}

Expand All @@ -59,6 +59,7 @@ var listCmd = &cobra.Command{
}

lib.LoadVersion(vs[i].Name)

},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// releaseCmd represents the release command
var releaseCmd = &cobra.Command{
Use: "release",
Short: "Gets release by version",
Short: "Gets release by version number",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("What version would like to install?")
Expand Down
40 changes: 36 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/leoafarias/fvm/lib"
"github.com/manifoldco/promptui"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -28,12 +31,41 @@ var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "fvm",
Short: "A version management tool for Flutter",
Short: "Lists all the installed versions of Flutter",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println(args)
// },
Run: func(cmd *cobra.Command, args []string) {
// var options []string
vs, err := lib.ListVersions()
if err != nil {
log.Fatal(err)
}

if len(vs) == 0 {
fmt.Println("No Flutter versions installed")
os.Exit(0)
}

templates := promptui.SelectTemplates{
Active: `👉 {{ .Name | cyan | bold }}`,
Inactive: ` {{ .Name | cyan }}`,
Selected: `{{ "✔" | green | bold }} {{ "Channel" | bold }}: {{ .Name | cyan }}`,
}

list := promptui.Select{
Label: "Choose Installed Versions",
Items: vs,
Templates: &templates,
}

i, _, err := list.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}

lib.LoadVersion(vs[i].Name)
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
69 changes: 24 additions & 45 deletions lib/fluttertools/fluttertools.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/briandowns/spinner"
homedir "github.com/mitchellh/go-homedir"
"github.com/ttacon/chalk"
)

Expand Down Expand Up @@ -87,6 +88,29 @@ func runGit(execPath string, args ...string) (string, error) {

}

// GetFlutterHome - Returns the home path to the flutter directory
func GetFlutterHome() string {

var flutterHome string
envVar := os.Getenv("PATH")
v := strings.Split(envVar, ":")
for _, v := range v {
if strings.Contains(v, "flutter") {
flutterHome = strings.TrimSuffix(v, "/bin")
}
}

if len(flutterHome) == 0 {
homeDir, _ := homedir.Dir()
flutterHome = path.Join(homeDir, "flutter")
}

os.MkdirAll(flutterHome, os.ModePerm)

return flutterHome

}

// RunDoctor - runs 'flutter doctor' command
func RunDoctor() {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
Expand All @@ -102,48 +126,3 @@ func RunDoctor() {
s.Stop()
fmt.Println(chalk.Cyan.Color("[✓]"), "Flutter is setup")
}

// RunDoctor - runs 'flutter doctor' command
// func RunDoctor() {
// s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
// s.Color("cyan", "bold")

// s.Start()

// cmd := exec.Command("flutter", "doctor")

// c := make(chan struct{})
// wg.Add(1)

// go func(cmd *exec.Cmd, c chan struct{}) {
// s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
// s.Prefix = "Setting up Flutter"
// s.Color("cyan", "bold")
// s.Start()

// defer wg.Done()
// stdout, err := cmd.StdoutPipe()
// if err != nil {
// panic(err)
// }
// <-c
// scanner := bufio.NewScanner(stdout)
// for scanner.Scan() {
// m := scanner.Text()
// // fmt.Println(m)
// if !strings.HasPrefix(m, " ") {
// s.Suffix = m
// }
// }

// fmt.Println("[✓] " + "Setup Complete")

// }(cmd, c)

// c <- struct{}{}
// cmd.Start()

// cmd.Wait()
// s.Stop()
// fmt.Println(chalk.Cyan.Color("[✓]"), "Flutter is setup")
// }
28 changes: 16 additions & 12 deletions lib/fvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ import (
)

var (
homeDir, _ = homedir.Dir()
workspaceHome = path.Join(homeDir, "fvm")
flutterHome = path.Join(homeDir, "flutter")
versionsDir = path.Join(workspaceHome, "versions")
flutterHome = fluttertools.GetFlutterHome()
workspaceHome = getWorkspaceHome()
)

// Versions - slice of versions
Expand Down Expand Up @@ -81,7 +79,7 @@ func setup(v Version) (Version, error) {

// If directory doesnt exists get the channel
if v.dir == false {
fluttertools.GetChannel(versionsDir, v.Name)
fluttertools.GetChannel(workspaceHome, v.Name)
v.dir = true
}

Expand Down Expand Up @@ -113,7 +111,7 @@ func setup(v Version) (Version, error) {
// ListVersions - lists all the current versions
func ListVersions() (Versions, error) {
var vs Versions
files, err := ioutil.ReadDir(versionsDir)
files, err := ioutil.ReadDir(workspaceHome)
if err != nil {
return Versions{}, err
}
Expand All @@ -125,7 +123,7 @@ func ListVersions() (Versions, error) {
}
dir := f.Name()

versionNumber, _ := fluttertools.GetVersionNumber(path.Join(versionsDir, dir))
versionNumber, _ := fluttertools.GetVersionNumber(path.Join(workspaceHome, dir))

vs = append(vs, Version{
Name: dir,
Expand Down Expand Up @@ -156,7 +154,7 @@ func ListVersions() (Versions, error) {

// RemoveVersions - Remove all the files in the versionPath provided
func RemoveVersions() error {
folders, err := filepath.Glob(filepath.Join(versionsDir, "*"))
folders, err := filepath.Glob(filepath.Join(workspaceHome, "*"))
if err != nil {
return err
}
Expand All @@ -181,7 +179,7 @@ func RemoveVersion(version string) error {
if v.Name == version && v.Active {
dirPath = flutterHome
} else {
dirPath = path.Join(versionsDir, version)
dirPath = path.Join(workspaceHome, version)

}
}
Expand All @@ -203,8 +201,7 @@ func toggleActive(fromActive bool, branch string) error {
s.Start()

if fromActive {
// toPath = path.Join(versionsDir, branch)
// fromPath = flutterHome

s.Suffix = "Deactivating version [" + branch + "]"
if err := os.Remove(flutterHome); err != nil {
log.Fatal(err)
Expand All @@ -213,7 +210,7 @@ func toggleActive(fromActive bool, branch string) error {

s.Suffix = "Deactivating version [" + branch + "]"
os.Remove(flutterHome)
toPath = path.Join(versionsDir, branch)
toPath = path.Join(workspaceHome, branch)
s.Suffix = "Activating version [" + branch + "]"
err := os.Symlink(toPath, flutterHome)
if err != nil {
Expand All @@ -226,3 +223,10 @@ func toggleActive(fromActive bool, branch string) error {
fmt.Println(chalk.Cyan.Color("[✓]"), s.Suffix)
return nil
}

func getWorkspaceHome() string {
homeDir, _ := homedir.Dir()
workspaceHome := path.Join(homeDir, "fvm")
os.MkdirAll(workspaceHome, os.ModePerm)
return workspaceHome
}

0 comments on commit b44bff3

Please sign in to comment.