Skip to content

Commit

Permalink
feat: fvm install from repo
Browse files Browse the repository at this point in the history
  • Loading branch information
befovy committed Jun 25, 2020
1 parent 5c2c33f commit 153dea3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ FVM gives you the ability to install many Flutter **releases** or **channels**.

Use `master` to install the Master channel and `v1.8.0` to install the release.

Use `--repo` to install flutter from unoffical repo
```bash
> fvm install master --repo https://github.com/flutter/flutter.git
```

### Import Flutter from previous installation

FVM has subcommand `import` which can import your previous installed flutter into fvm.
Expand Down
2 changes: 1 addition & 1 deletion cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var importCommand = &cobra.Command{
"Or you can use flags --path to special the path of flutter",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("need to provide a channel or a version or other name as import name, you can use `master` `beta` or `alibaba` `baidu` etc.")
return errors.New("need to provide a channel or a version or other name as import name, you can use `master` `beta` or `alibaba` `baidu` etc")
}
if len(args) > 1 {
return errors.New("allows only one argument, the name to be imported as")
Expand Down
8 changes: 7 additions & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import (
"github.com/spf13/cobra"
)

var repo string

func init() {
installCommand.Flags().StringVar(&repo, "repo", "", "install flutter from unoffical git repo")
rootCmd.AddCommand(installCommand)
}

Expand Down Expand Up @@ -72,7 +75,10 @@ var installCommand = &cobra.Command{
err := fvmgo.CheckIfGitExists()
if err == nil {
version := args[0]
if fvmgo.IsValidFlutterChannel(version) {
if len(repo) > 0 {
fvmgo.Infof("Install flutter <%s> from repo %s", version, repo)
err = fvmgo.FlutterRepoClone(version, repo)
} else if fvmgo.IsValidFlutterChannel(version) {
err = fvmgo.FlutterChannelClone(version)
} else if !maybeVersion(version) {
fvmgo.Errorf("It seems that you want install a Flutter channel but have a invalid channel")
Expand Down
41 changes: 31 additions & 10 deletions fvmgo/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ProcessRunner(cmd string, dir string, arg ...string) error {
if len(dir) == 0 {
cwd, err := os.Getwd()
if err != nil {
return errors.New(fmt.Sprintf("Cannot get work directory: %v", err))
return fmt.Errorf("Cannot get work directory: %v", err)
}
runner.Dir = cwd
} else {
Expand All @@ -67,7 +67,7 @@ func ProcessRunner(cmd string, dir string, arg ...string) error {

err := runner.Run()
if err != nil {
return errors.New(fmt.Sprintf("Command '%s' exited with error: %v", cmd, err))
return fmt.Errorf("Command '%s' exited with error: %v", cmd, err)
}
return nil
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func CurrentVersion() (string, error) {
}
dst, err := os.Readlink(link)
if err != nil {
return "", errors.New(fmt.Sprintf("Cannot read link target: %v", err))
return "", fmt.Errorf("Cannot read link target: %v", err)
}
return filepath.Base(dst), nil
}
Expand Down Expand Up @@ -148,9 +148,30 @@ func checkInstalledCorrectly(version string) bool {
return true
}

func FlutterRepoClone(version string, repo string) error {
if checkInstalledCorrectly(version) {
Warnf("Flutter version %s is already installed", version)
return nil
}

versionDir := filepath.Join(VersionsDir(), version)
Verbosef("Installing Flutter sdk %s to cache directory %s", version, versionDir)

err := os.MkdirAll(versionDir, 0755)
if err != nil {
return fmt.Errorf("Cannot creat directory for version %s: %v", version, err)
}
err = ProcessRunner("git", versionDir, "clone", "-b", version, repo, versionDir)
if err != nil {
return err
}
Infof("Successfully installed flutter %s from %s", version, repo)
return nil
}

func FlutterChannelClone(channel string) error {
if !IsValidFlutterChannel(channel) {
return errors.New(fmt.Sprintf("%s is not a valid flutter channel", channel))
return fmt.Errorf("%s is not a valid flutter channel", channel)
}

Verbosef("%s is a valid flutter channel", channel)
Expand All @@ -162,9 +183,9 @@ func FlutterChannelClone(channel string) error {
Verbosef("Installing Flutter sdk %s to cache directory %s", channel, channelDir)
err := os.MkdirAll(channelDir, 0755)
if err != nil {
return errors.New(fmt.Sprintf("Cannot create directory for channel %s: %v", channel, err))
return fmt.Errorf("Cannot create directory for channel %s: %v", channel, err)
}
err = ProcessRunner("git", channelDir, "clone", "-b", channel, flutterRepo, ".")
err = ProcessRunner("git", channelDir, "clone", "-b", channel, flutterRepo, channelDir)
if err != nil {
return err
}
Expand All @@ -174,7 +195,7 @@ func FlutterChannelClone(channel string) error {

func FlutterVersionClone(version string) error {
if !IsValidFlutterVersion(version) {
return errors.New(fmt.Sprintf("%s is not a valid version", version))
return fmt.Errorf("%s is not a valid version", version)
}
Verbosef("%s is a valid flutter version", version)
if checkInstalledCorrectly(version) {
Expand All @@ -187,9 +208,9 @@ func FlutterVersionClone(version string) error {

err := os.MkdirAll(versionDir, 0755)
if err != nil {
return errors.New(fmt.Sprintf("Cannot creat directory for version %s: %v", version, err))
return fmt.Errorf("Cannot creat directory for version %s: %v", version, err)
}
err = ProcessRunner("git", versionDir, "clone", "-b", version, flutterRepo, ".")
err = ProcessRunner("git", versionDir, "clone", "-b", version, flutterRepo, versionDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -318,7 +339,7 @@ func projectFlutterLink(dir string, depth int) string {
return ""
}

depth -= 1
depth--
return projectFlutterLink(filepath.Dir(dir), depth)
}

Expand Down

0 comments on commit 153dea3

Please sign in to comment.