In this paper, starting from blog.befovy.com/2019/12/fvm…

The first version of Flutter multi-version management tool, FVM V0.1.0, was completed in Go over the weekend. I have to admit, this version is completely wheel building, and is still exactly the same as Leoafarias FVM.

Why the wheel

In short, there is a need: In developing FijkPlayer (a Flutter media player), I gradually moved from the Android and iOS mobile version to the desktop version of Flutter. In the development of FijkPlayer desktop, I used Go-Flutter to provide desktop support for FLUTTER. Go-flutter defaults to flutter beta. The mobile fijkPlayer uses the Flutter Stable version.

At the beginning, IT was painful for me to change the channel of flutter. A lot of content had to be downloaded to change the channel of flutter. Running the Flutter doctor after the channel switch required a lot of downloads.

Later, I found Leoafarias FVM, but the installation of this tool failed the first time. In fact, I don’t know much about Dart Native, so I didn’t solve the installation problem. I haven’t looked into whether Dart Native is a real binary or not, but if DART is needed to run (like running a JAR requires a JVM), I don’t really like anything with a runtime on the desktop. I just need a little tool.

So why not build your own?

The original hover tool was modified and incorporated into the main branch when go-Flutter was developed. Hover is also a command line small tool, and can be installed directly through go get, hover using library SPF13 / COBRA to achieve command line sub-command and parameter resolution and other “boilerplate code”, engineering structure is very simple, clear.

On the other hand, ALTHOUGH I learned Go early, I have also used it on many small projects (yes, mostly the big assignments I did when I was in school). I used it a lot, but it wasn’t systematic enough. The code I wrote was just a demo. Recently, I plan to improve my GO language level further. One action is to join the Chinese translation team of GCTT-GO, translating some high-quality articles of foreign masters. Meanwhile, I plan to seriously launch a GO project and open source. But to get started, let’s make an FVM.

Yes, the first version, V0.1.0, fully implemented all the logic in Leoafarias FVM and was a complete wheel project. However, the later version was separated from Leoafarias FVM. I wanted to develop FVM with Chinese characteristics.

Cobra creates the GO command line tool

Spf13 / Cobra is a GO library that helps us quickly create go command-line tools by providing a Go tool that generates “sample code”. The big names in the Go world Docker, Kubernetes, Hugo, etc all use COBRA to build commands.

First, install the COBRA tool:

Holds the go get -u github.com/spf13/cobra/cobraCopy the code

Create project FVM using COBRA and use github.com/befovy/fvm as package name:

Cobra init FVM -- PKG-name github.com/befovy/fvmCopy the code

Use the subcommands required by Cobra add:

› cobra add list
› cobra add install
› cobra add remove
› cobra add flutter
› cobra add use
› cobra add config
Copy the code

After completing these steps we have these files:

Holds the tree. /. / ├ ─ ─ LICENSE ├ ─ ─ CMD │ ├ ─ ─ config. Go │ ├ ─ ─ flutter. Go │ ├ ─ ─ the go │ ├ ─ ─ list. Go │ ├ ─ ─ remove. Go │ ├ ─ ─ └. Go │ └─ useCopy the code

Then we initialize the Go Modules, download the dependencies, and compile FVM.

Go Mod init github.com/befovy/fvm › Go Mod Tidy › Go BuildCopy the code

The compiled FVM appears, run it and see what happens.

/ FVM A longer description that spans multiple lines and likely contains examples and usage of using your application.  For example: Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application. Usage: fvm [command] Available Commands: config A brief description of your command flutter A brief description of your command help Help about any command install A brief description of your command list A brief description of your command remove A brief description of your command use A brief description of your command Flags: --config string config file (default is $HOME/.fvm.yaml) -h, --help help for fvm -t, --toggle Help message for toggle Use "fvm [command] --help" for more information about a command.Copy the code

Cobra has built a prototype of FVM and added subcommands. Now it’s time to implement the specific logic of FVM.

FVM function realization

As an SDK version management tool, FVM enables users to install and cache multiple Flutter versions locally at the same time and quickly switch between them.

The core logic is to cache multiple Flutter versions in local folders and create soft links for projects specifying Flutter versions. Or create a specific version of Flutter soft link in the global environment.

Internal implementation of major functions are dependent on the GO language standard library OS /exec package. Using OS /exec, you can create a subprocess to execute commands and manage the input and output of the subprocess.

Each FVM subcommand, basically look up some files, folders, execute the following git command and parse its output.

The specific implementation is in the code, here is not wordy.

The implementation should pay attention to check all kinds of possible errors, output error prompt to the user. If the error affects the continuation of the business logic, exit the program.

In FVM V0.1.0, this convenience is not detailed enough. But that alone made me feel the annoying error in Go. Further refactoring is required to handle errors with more elegant code.

Harvest summary

What do I get by implementing an FVM?

  • I wrote a tool for myself to facilitate me to switch the Fluter version.

  • FVM is just the beginning, and it’s not as difficult as the big assignments I’ve done before. But FVM is open source and published. I happen to have just translated an article, Go Modules: v2 and later, and I can also practice the philosophy behind Go Modules by maintaining FVM with constant updates.

  • This version of FVM is too simple and requires very little knowledge. However, if I encounter any new knowledge or awesome idea in the future, I can also quickly test the waters in FVM. After all, it’s boring to try hello World every time.

Finally, I hope FVM gets attention and feedback.