|
| 1 | +package arguments |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/arduino/arduino-cli/arduino/cores" |
| 7 | + "github.com/arduino/arduino-cli/cli/instance" |
| 8 | + "github.com/arduino/arduino-cli/commands" |
| 9 | + "github.com/arduino/arduino-cli/commands/board" |
| 10 | + "github.com/arduino/arduino-cli/commands/core" |
| 11 | + "github.com/arduino/arduino-cli/commands/lib" |
| 12 | + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 13 | +) |
| 14 | + |
| 15 | +// GetInstalledBoards is an helper function useful to autocomplete. |
| 16 | +// It returns a list of fqbn |
| 17 | +// it's taken from cli/board/listall.go |
| 18 | +func GetInstalledBoards() []string { |
| 19 | + inst := instance.CreateAndInit() |
| 20 | + |
| 21 | + list, _ := board.ListAll(context.Background(), &rpc.BoardListAllRequest{ |
| 22 | + Instance: inst, |
| 23 | + SearchArgs: nil, |
| 24 | + IncludeHiddenBoards: false, |
| 25 | + }) |
| 26 | + var res []string |
| 27 | + // transform the data structure for the completion |
| 28 | + for _, i := range list.Boards { |
| 29 | + res = append(res, i.Fqbn+"\t"+i.Name) |
| 30 | + } |
| 31 | + return res |
| 32 | +} |
| 33 | + |
| 34 | +// GetInstalledProtocols is an helper function useful to autocomplete. |
| 35 | +// It returns a list of protocols available based on the installed boards |
| 36 | +func GetInstalledProtocols() []string { |
| 37 | + inst := instance.CreateAndInit() |
| 38 | + pm := commands.GetPackageManager(inst.Id) |
| 39 | + boards := pm.InstalledBoards() |
| 40 | + |
| 41 | + installedProtocols := make(map[string]struct{}) |
| 42 | + for _, board := range boards { |
| 43 | + for _, protocol := range board.Properties.SubTree("upload.tool").FirstLevelKeys() { |
| 44 | + if protocol == "default" { |
| 45 | + // default is used as fallback when trying to upload to a board |
| 46 | + // using a protocol not defined for it, it's useless showing it |
| 47 | + // in autocompletion |
| 48 | + continue |
| 49 | + } |
| 50 | + installedProtocols[protocol] = struct{}{} |
| 51 | + } |
| 52 | + } |
| 53 | + res := make([]string, len(installedProtocols)) |
| 54 | + i := 0 |
| 55 | + for k := range installedProtocols { |
| 56 | + res[i] = k |
| 57 | + i++ |
| 58 | + } |
| 59 | + return res |
| 60 | +} |
| 61 | + |
| 62 | +// GetInstalledProgrammers is an helper function useful to autocomplete. |
| 63 | +// It returns a list of programmers available based on the installed boards |
| 64 | +func GetInstalledProgrammers() []string { |
| 65 | + inst := instance.CreateAndInit() |
| 66 | + pm := commands.GetPackageManager(inst.Id) |
| 67 | + |
| 68 | + // we need the list of the available fqbn in order to get the list of the programmers |
| 69 | + list, _ := board.ListAll(context.Background(), &rpc.BoardListAllRequest{ |
| 70 | + Instance: inst, |
| 71 | + SearchArgs: nil, |
| 72 | + IncludeHiddenBoards: false, |
| 73 | + }) |
| 74 | + |
| 75 | + installedProgrammers := make(map[string]string) |
| 76 | + for _, board := range list.Boards { |
| 77 | + fqbn, _ := cores.ParseFQBN(board.Fqbn) |
| 78 | + _, boardPlatform, _, _, _, _ := pm.ResolveFQBN(fqbn) |
| 79 | + for programmerID, programmer := range boardPlatform.Programmers { |
| 80 | + installedProgrammers[programmerID] = programmer.Name |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + res := make([]string, len(installedProgrammers)) |
| 85 | + i := 0 |
| 86 | + for programmerID := range installedProgrammers { |
| 87 | + res[i] = programmerID + "\t" + installedProgrammers[programmerID] |
| 88 | + i++ |
| 89 | + } |
| 90 | + return res |
| 91 | +} |
| 92 | + |
| 93 | +// GetUninstallableCores is an helper function useful to autocomplete. |
| 94 | +// It returns a list of cores which can be uninstalled |
| 95 | +func GetUninstallableCores() []string { |
| 96 | + inst := instance.CreateAndInit() |
| 97 | + |
| 98 | + platforms, _ := core.GetPlatforms(&rpc.PlatformListRequest{ |
| 99 | + Instance: inst, |
| 100 | + UpdatableOnly: false, |
| 101 | + All: false, |
| 102 | + }) |
| 103 | + var res []string |
| 104 | + // transform the data structure for the completion |
| 105 | + for _, i := range platforms { |
| 106 | + res = append(res, i.Id+"\t"+i.Name) |
| 107 | + } |
| 108 | + return res |
| 109 | +} |
| 110 | + |
| 111 | +// GetInstallableCores is an helper function useful to autocomplete. |
| 112 | +// It returns a list of cores which can be installed/downloaded |
| 113 | +func GetInstallableCores() []string { |
| 114 | + inst := instance.CreateAndInit() |
| 115 | + |
| 116 | + platforms, _ := core.PlatformSearch(&rpc.PlatformSearchRequest{ |
| 117 | + Instance: inst, |
| 118 | + SearchArgs: "", |
| 119 | + AllVersions: false, |
| 120 | + }) |
| 121 | + var res []string |
| 122 | + // transform the data structure for the completion |
| 123 | + for _, i := range platforms.SearchOutput { |
| 124 | + res = append(res, i.Id+"\t"+i.Name) |
| 125 | + } |
| 126 | + return res |
| 127 | +} |
| 128 | + |
| 129 | +// GetInstalledLibraries is an helper function useful to autocomplete. |
| 130 | +// It returns a list of libs which are currently installed, including the builtin ones |
| 131 | +func GetInstalledLibraries() []string { |
| 132 | + return getLibraries(true) |
| 133 | +} |
| 134 | + |
| 135 | +// GetUninstallableLibraries is an helper function useful to autocomplete. |
| 136 | +// It returns a list of libs which can be uninstalled |
| 137 | +func GetUninstallableLibraries() []string { |
| 138 | + return getLibraries(false) |
| 139 | +} |
| 140 | + |
| 141 | +func getLibraries(all bool) []string { |
| 142 | + inst := instance.CreateAndInit() |
| 143 | + libs, _ := lib.LibraryList(context.Background(), &rpc.LibraryListRequest{ |
| 144 | + Instance: inst, |
| 145 | + All: all, |
| 146 | + Updatable: false, |
| 147 | + Name: "", |
| 148 | + Fqbn: "", |
| 149 | + }) |
| 150 | + var res []string |
| 151 | + // transform the data structure for the completion |
| 152 | + for _, i := range libs.InstalledLibraries { |
| 153 | + res = append(res, i.Library.Name+"\t"+i.Library.Sentence) |
| 154 | + } |
| 155 | + return res |
| 156 | +} |
| 157 | + |
| 158 | +// GetInstallableLibs is an helper function useful to autocomplete. |
| 159 | +// It returns a list of libs which can be installed/downloaded |
| 160 | +func GetInstallableLibs() []string { |
| 161 | + inst := instance.CreateAndInit() |
| 162 | + |
| 163 | + libs, _ := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{ |
| 164 | + Instance: inst, |
| 165 | + Query: "", // if no query is specified all the libs are returned |
| 166 | + }) |
| 167 | + var res []string |
| 168 | + // transform the data structure for the completion |
| 169 | + for _, i := range libs.Libraries { |
| 170 | + res = append(res, i.Name+"\t"+i.Latest.Sentence) |
| 171 | + } |
| 172 | + return res |
| 173 | +} |
| 174 | + |
| 175 | +// GetConnectedBoards is an helper function useful to autocomplete. |
| 176 | +// It returns a list of boards which are currently connected |
| 177 | +// Obviously it does not suggests network ports because of the timeout |
| 178 | +func GetConnectedBoards() []string { |
| 179 | + inst := instance.CreateAndInit() |
| 180 | + |
| 181 | + list, _ := board.List(&rpc.BoardListRequest{ |
| 182 | + Instance: inst, |
| 183 | + }) |
| 184 | + var res []string |
| 185 | + // transform the data structure for the completion |
| 186 | + for _, i := range list { |
| 187 | + res = append(res, i.Port.Address) |
| 188 | + } |
| 189 | + return res |
| 190 | +} |
0 commit comments