Skip to content

Commit 4b248bc

Browse files
committed
added Rescan function
1 parent 7f809ce commit 4b248bc

File tree

5 files changed

+292
-43
lines changed

5 files changed

+292
-43
lines changed

commands/instances.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,80 @@ func UpdateLibrariesIndex(ctx context.Context, lm *librariesmanager.LibrariesMan
181181
//os.Exit(ErrNetwork)
182182
}
183183
}
184+
185+
func Rescan(ctx context.Context, req *rpc.RescanReq) (*rpc.RescanResp, error) {
186+
id := req.Instance.Id
187+
coreInstance, ok := instances[id]
188+
if !ok {
189+
return nil, fmt.Errorf("invalid handle")
190+
}
191+
192+
config := coreInstance.config
193+
if config == nil {
194+
return nil, fmt.Errorf("invalid request")
195+
}
196+
197+
var pm *packagemanager.PackageManager
198+
pm = packagemanager.NewPackageManager(
199+
config.IndexesDir(),
200+
config.PackagesDir(),
201+
config.DownloadsDir(),
202+
config.DataDir.Join("tmp"))
203+
204+
for _, URL := range config.BoardManagerAdditionalUrls {
205+
if err := pm.LoadPackageIndex(URL); err != nil {
206+
return nil, fmt.Errorf("loading "+URL.String()+" package index: %s", err)
207+
}
208+
}
209+
210+
if err := pm.LoadHardware(config); err != nil {
211+
return nil, fmt.Errorf("loading hardware packages: %s", err)
212+
}
213+
214+
// Initialize library manager
215+
// --------------------------
216+
lm := librariesmanager.NewLibraryManager(
217+
config.IndexesDir(),
218+
config.DownloadsDir())
219+
220+
// Add IDE builtin libraries dir
221+
if bundledLibsDir := config.IDEBundledLibrariesDir(); bundledLibsDir != nil {
222+
lm.AddLibrariesDir(bundledLibsDir, libraries.IDEBuiltIn)
223+
}
224+
225+
// Add sketchbook libraries dir
226+
lm.AddLibrariesDir(config.LibrariesDir(), libraries.Sketchbook)
227+
228+
// Add libraries dirs from installed platforms
229+
if pm != nil {
230+
for _, targetPackage := range pm.GetPackages().Packages {
231+
for _, platform := range targetPackage.Platforms {
232+
if platformRelease := pm.GetInstalledPlatformRelease(platform); platformRelease != nil {
233+
lm.AddPlatformReleaseLibrariesDir(platformRelease, libraries.PlatformBuiltIn)
234+
}
235+
}
236+
}
237+
}
238+
239+
// Load index and auto-update it if needed
240+
if err := lm.LoadIndex(); err != nil {
241+
UpdateLibrariesIndex(ctx, lm, func(curr *rpc.DownloadProgress) {
242+
fmt.Printf(">> %+v\n", curr)
243+
})
244+
if err := lm.LoadIndex(); err != nil {
245+
return nil, fmt.Errorf("loading libraries index: %s", err)
246+
}
247+
}
248+
249+
// Scan for libraries
250+
if err := lm.RescanLibraries(); err != nil {
251+
return nil, fmt.Errorf("libraries rescan: %s", err)
252+
}
253+
254+
coreInstance.pm = pm
255+
coreInstance.lm = lm
256+
id = req.Instance.Id
257+
return &rpc.RescanResp{
258+
Instance: &rpc.Instance{Id: id},
259+
}, nil
260+
}

daemon/client/client.go

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,24 @@ func main() {
3434
}
3535
instance := resp.GetInstance()
3636
fmt.Println("Created new server instance:", instance)
37-
37+
UninstalResp, err := client.PlatformUninstall(context.Background(), &rpc.PlatformUninstallReq{
38+
Instance: instance,
39+
PlatformPackage: "arduino",
40+
Architecture: "samd",
41+
Version: "1.6.21",
42+
})
43+
if err != nil {
44+
if UninstalResp != nil {
45+
}
46+
fmt.Printf("uninstall error: %s\n", err)
47+
os.Exit(1)
48+
}
3849
install := func() {
3950
installRespStream, err := client.PlatformInstall(context.Background(), &rpc.PlatformInstallReq{
4051
Instance: instance,
4152
PlatformPackage: "arduino",
4253
Architecture: "samd",
54+
Version: "1.6.19",
4355
})
4456
if err != nil {
4557
fmt.Printf("Error installing platform: %s\n", err)
@@ -58,9 +70,39 @@ func main() {
5870
}
5971
fmt.Println("Installation completed!")
6072
}
61-
62-
install()
6373
install()
74+
respResc, err := client.Rescan(context.Background(), &rpc.RescanReq{
75+
Instance: instance,
76+
})
77+
if err != nil {
78+
fmt.Printf("Error creating server instance: %s\n", err)
79+
os.Exit(1)
80+
}
81+
instance = respResc.GetInstance()
82+
upgrade := func() {
83+
upgradeRespStream, err := client.PlatformUpgrade(context.Background(), &rpc.PlatformUpgradeReq{
84+
Instance: instance,
85+
PlatformPackage: "arduino",
86+
Architecture: "samd",
87+
})
88+
if err != nil {
89+
fmt.Printf("Error Upgrade platform: %s\n", err)
90+
os.Exit(1)
91+
}
92+
for {
93+
upgradeResp, err := upgradeRespStream.Recv()
94+
if err == io.EOF {
95+
break
96+
}
97+
if err != nil {
98+
fmt.Printf("Install error: %s\n", err)
99+
os.Exit(1)
100+
}
101+
fmt.Printf("%s\n", upgradeResp.GetProgress())
102+
}
103+
fmt.Println("Upgrade completed!")
104+
}
105+
upgrade()
64106

65107
details, err := client.BoardDetails(context.Background(), &rpc.BoardDetailsReq{
66108
Instance: instance,

daemon/daemon.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func (s *ArduinoCoreServerImpl) Destroy(ctx context.Context, req *rpc.DestroyReq
4747
return commands.Destroy(ctx, req)
4848
}
4949

50+
func (s *ArduinoCoreServerImpl) Rescan(ctx context.Context, req *rpc.RescanReq) (*rpc.RescanResp, error) {
51+
return commands.Rescan(ctx, req)
52+
}
53+
5054
func (s *ArduinoCoreServerImpl) Init(ctx context.Context, req *rpc.InitReq) (*rpc.InitResp, error) {
5155
return commands.Init(ctx, req)
5256
}

0 commit comments

Comments
 (0)