@@ -18,6 +18,7 @@ package bricks
1818import (
1919 "errors"
2020 "fmt"
21+ "log/slog"
2122 "maps"
2223 "slices"
2324
@@ -26,6 +27,7 @@ import (
2627
2728 "github.com/arduino/arduino-app-cli/internal/orchestrator/app"
2829 "github.com/arduino/arduino-app-cli/internal/orchestrator/bricksindex"
30+ "github.com/arduino/arduino-app-cli/internal/orchestrator/config"
2931 "github.com/arduino/arduino-app-cli/internal/orchestrator/modelsindex"
3032 "github.com/arduino/arduino-app-cli/internal/store"
3133)
@@ -125,7 +127,8 @@ func (s *Service) AppBrickInstanceDetails(a *app.ArduinoApp, brickID string) (Br
125127 }, nil
126128}
127129
128- func (s * Service ) BricksDetails (id string ) (BrickDetailsResult , error ) {
130+ func (s * Service ) BricksDetails (id string , idProvider * app.IDProvider ,
131+ cfg config.Configuration ) (BrickDetailsResult , error ) {
129132 brick , found := s .bricksIndex .FindBrickByID (id )
130133 if ! found {
131134 return BrickDetailsResult {}, ErrBrickNotFound
@@ -160,6 +163,20 @@ func (s *Service) BricksDetails(id string) (BrickDetailsResult, error) {
160163 }
161164 })
162165
166+ /*qui mi serve una funzione per calcolare questo. devo iterare su ogni app, di esempio o no.
167+ ho già la funzione appList che mi può aiutare.
168+ e per ogni elemento se ho il bircikc id corrente
169+ mi creo un AppReference*/
170+ appList , err := getAppList (cfg )
171+ if err != nil {
172+ slog .Error ("unable to get app list" , slog .String ("error" , err .Error ()))
173+ return BrickDetailsResult {}, fmt .Errorf ("unable to get app list: %w" , err )
174+ }
175+ usedByApps , err := getUsedByApps (appList , brick .ID , idProvider )
176+ if err != nil {
177+ slog .Error ("unable to get used by apps" , slog .String ("error" , err .Error ()))
178+ return BrickDetailsResult {}, fmt .Errorf ("unable to get used by apps: %w" , err )
179+ }
163180 return BrickDetailsResult {
164181 ID : id ,
165182 Name : brick .Name ,
@@ -171,9 +188,75 @@ func (s *Service) BricksDetails(id string) (BrickDetailsResult, error) {
171188 Readme : readme ,
172189 ApiDocsPath : apiDocsPath ,
173190 CodeExamples : codeExamples ,
191+ UsedByApps : usedByApps ,
174192 }, nil
175193}
176194
195+ func getAppList (
196+ cfg config.Configuration ,
197+ ) ([]app.ArduinoApp , error ) {
198+ var (
199+ pathsToExplore paths.PathList
200+ appPaths paths.PathList
201+ )
202+ pathsToExplore .Add (cfg .ExamplesDir ())
203+ pathsToExplore .Add (cfg .AppsDir ())
204+ arduinoApps := []app.ArduinoApp {}
205+
206+ for _ , p := range pathsToExplore {
207+ res , err := p .ReadDirRecursiveFiltered (func (file * paths.Path ) bool {
208+ if file .Base () == ".cache" {
209+ return false
210+ }
211+ if file .Join ("app.yaml" ).NotExist () && file .Join ("app.yml" ).NotExist () {
212+ return true
213+ }
214+ return false
215+ }, paths .FilterDirectories (), paths .FilterOutNames ("python" , "sketch" , ".cache" ))
216+
217+ if err != nil {
218+ slog .Error ("unable to list apps" , slog .String ("error" , err .Error ()))
219+ return arduinoApps , err
220+ }
221+ appPaths .AddAllMissing (res )
222+ }
223+
224+ for _ , file := range appPaths {
225+ app , err := app .Load (file .String ())
226+ if err != nil {
227+ /* result.BrokenApps = append(result.BrokenApps, orchestrator.BrokenAppInfo{
228+ Name: file.Base(),
229+ Error: fmt.Sprintf("unable to parse the app.yaml: %s", err.Error()),
230+ })*/
231+ continue
232+ }
233+
234+ arduinoApps = append (arduinoApps , app )
235+ }
236+ return arduinoApps , nil
237+ }
238+
239+ func getUsedByApps (apps []app.ArduinoApp , brickId string , idProvider * app.IDProvider ) ([]AppReference , error ) {
240+ usedByApps := []AppReference {}
241+ for _ , app := range apps {
242+ for _ , b := range app .Descriptor .Bricks {
243+ if b .ID == brickId {
244+ id , err := idProvider .IDFromPath (app .FullPath )
245+ if err != nil {
246+ return usedByApps , fmt .Errorf ("failed to get app ID for %s: %w" , app .Name , err )
247+ }
248+ usedByApps = append (usedByApps , AppReference {
249+ Name : app .Name ,
250+ ID : id .String (),
251+ Icon : app .Descriptor .Icon ,
252+ })
253+ break
254+ }
255+ }
256+ }
257+ return usedByApps , nil
258+ }
259+
177260type BrickCreateUpdateRequest struct {
178261 ID string `json:"-"`
179262 Model * string `json:"model"`
0 commit comments