Unverified Commit 5159f45e authored by Cristian Maglie's avatar Cristian Maglie Committed by GitHub

Refactored bundle tool initialization (#1349)

Also fixed a bug that prevented tools-reload after auto install in some
rare circumstances (almost impossible to trigger actually).
parent 1b048093
......@@ -22,7 +22,7 @@ import (
semver "go.bug.st/relaxed-semver"
)
func loadBuiltinCtagsMetadata(pm *packagemanager.PackageManager) {
func getBuiltinCtagsTool(pm *packagemanager.PackageManager) *cores.ToolRelease {
builtinPackage := pm.Packages.GetOrCreatePackage("builtin")
ctagsTool := builtinPackage.GetOrCreateTool("ctags")
ctagsRel := ctagsTool.GetOrCreateRelease(semver.ParseRelaxed("5.8-arduino11"))
......@@ -88,11 +88,5 @@ func loadBuiltinCtagsMetadata(pm *packagemanager.PackageManager) {
},
},
}
}
var ctagsVersion = semver.ParseRelaxed("5.8-arduino11")
func getBuiltinCtagsTool(pm *packagemanager.PackageManager) (*cores.ToolRelease, error) {
loadBuiltinCtagsMetadata(pm)
return pm.Package("builtin").Tool("ctags").Release(ctagsVersion).Get()
return ctagsRel
}
......@@ -112,10 +112,7 @@ func ListBoards(pm *packagemanager.PackageManager) ([]*discovery.Port, error) {
defer listBoardMutex.Unlock()
// get the bundled tool
t, err := getBuiltinSerialDiscoveryTool(pm)
if err != nil {
return nil, err
}
t := getBuiltinSerialDiscoveryTool(pm)
// determine if it's installed
if !t.IsInstalled() {
......@@ -146,11 +143,7 @@ func ListBoards(pm *packagemanager.PackageManager) ([]*discovery.Port, error) {
// WatchListBoards returns a channel that receives events from the bundled discovery tool
func WatchListBoards(pm *packagemanager.PackageManager) (<-chan *discovery.Event, error) {
t, err := getBuiltinSerialDiscoveryTool(pm)
if err != nil {
return nil, err
}
t := getBuiltinSerialDiscoveryTool(pm)
if !t.IsInstalled() {
return nil, fmt.Errorf("missing serial-discovery tool")
}
......@@ -175,10 +168,10 @@ func WatchListBoards(pm *packagemanager.PackageManager) (<-chan *discovery.Event
return disc.EventChannel(10), nil
}
func getBuiltinSerialDiscoveryTool(pm *packagemanager.PackageManager) (*cores.ToolRelease, error) {
func getBuiltinSerialDiscoveryTool(pm *packagemanager.PackageManager) *cores.ToolRelease {
builtinPackage := pm.Packages.GetOrCreatePackage("builtin")
serialDiscoveryTool := builtinPackage.GetOrCreateTool("serial-discovery")
serialDiscoveryToolRel := serialDiscoveryTool.GetOrCreateRelease(serialDiscoveryVersion)
serialDiscoveryToolRel.Flavors = serialDiscoveryFlavors
return pm.Package("builtin").Tool("serial-discovery").Release(serialDiscoveryVersion).Get()
return serialDiscoveryToolRel
}
......@@ -181,6 +181,8 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) *sta
// If this is not done the information of the uninstall core is kept in memory,
// even if it should not.
instance.PackageManager.Clear()
ctagsTool := getBuiltinCtagsTool(instance.PackageManager)
serialDiscoveryTool := getBuiltinSerialDiscoveryTool(instance.PackageManager)
// Load Platforms
urls := []string{globals.DefaultIndexURL}
......@@ -254,8 +256,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) *sta
}
// Install tools if necessary
toolHasBeenInstalled := false
ctagsTool, err := getBuiltinCtagsTool(instance.PackageManager)
ctagsHasBeenInstalled, err := instance.installToolIfMissing(ctagsTool, downloadCallback, taskCallback)
if err != nil {
s := status.Newf(codes.Internal, err.Error())
responseCallback(&rpc.InitResponse{
......@@ -263,19 +264,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) *sta
Error: s.Proto(),
},
})
} else {
toolHasBeenInstalled, err = instance.installToolIfMissing(ctagsTool, downloadCallback, taskCallback)
if err != nil {
s := status.Newf(codes.Internal, err.Error())
responseCallback(&rpc.InitResponse{
Message: &rpc.InitResponse_Error{
Error: s.Proto(),
},
})
}
}
serialDiscoveryTool, _ := getBuiltinSerialDiscoveryTool(instance.PackageManager)
serialHasBeenInstalled, err := instance.installToolIfMissing(serialDiscoveryTool, downloadCallback, taskCallback)
if err != nil {
s := status.Newf(codes.Internal, err.Error())
responseCallback(&rpc.InitResponse{
......@@ -283,19 +274,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) *sta
Error: s.Proto(),
},
})
} else {
toolHasBeenInstalled, err = instance.installToolIfMissing(serialDiscoveryTool, downloadCallback, taskCallback)
if err != nil {
s := status.Newf(codes.Internal, err.Error())
responseCallback(&rpc.InitResponse{
Message: &rpc.InitResponse_Error{
Error: s.Proto(),
},
})
}
}
if toolHasBeenInstalled {
if ctagsHasBeenInstalled || serialHasBeenInstalled {
// We installed at least one new tool after loading hardware
// so we must reload again otherwise we would never found them.
for _, err := range instance.PackageManager.LoadHardware() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment