|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020-2022 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package packagemanager |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "net/url" |
| 21 | + |
| 22 | + "github.com/arduino/arduino-cli/arduino" |
| 23 | + "github.com/arduino/arduino-cli/arduino/cores" |
| 24 | + "github.com/arduino/arduino-cli/arduino/resources" |
| 25 | + "github.com/arduino/arduino-cli/arduino/sketch" |
| 26 | + "github.com/arduino/arduino-cli/cli/globals" |
| 27 | + "github.com/arduino/arduino-cli/configuration" |
| 28 | + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 29 | + "github.com/arduino/go-paths-helper" |
| 30 | + "github.com/sirupsen/logrus" |
| 31 | +) |
| 32 | + |
| 33 | +// LoadHardwareForProfile load the hardware platforms for the given profile. |
| 34 | +// If installMissing is true then possibly missing tools and platforms will be downloaded and installed. |
| 35 | +func (pm *PackageManager) LoadHardwareForProfile(p *sketch.Profile, installMissing bool, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) []error { |
| 36 | + // Load required platforms |
| 37 | + var merr []error |
| 38 | + var platformReleases []*cores.PlatformRelease |
| 39 | + indexURLs := map[string]*url.URL{} |
| 40 | + for _, platformRef := range p.Platforms { |
| 41 | + if platformRelease, err := pm.loadProfilePlatform(platformRef, installMissing, downloadCB, taskCB); err != nil { |
| 42 | + merr = append(merr, fmt.Errorf("%s: %w", tr("loading required platform %s", platformRef), err)) |
| 43 | + logrus.WithField("platform", platformRef).WithError(err).Debugf("Error loading platform for profile") |
| 44 | + } else { |
| 45 | + platformReleases = append(platformReleases, platformRelease) |
| 46 | + indexURLs[platformRelease.Platform.Name] = platformRef.PlatformIndexURL |
| 47 | + logrus.WithField("platform", platformRef).Debugf("Loaded platform for profile") |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Load tools dependencies for the platforms |
| 52 | + for _, platformRelease := range platformReleases { |
| 53 | + // TODO: pm.FindPlatformReleaseDependencies(platformRelease) |
| 54 | + |
| 55 | + for _, toolDep := range platformRelease.ToolDependencies { |
| 56 | + indexURL := indexURLs[toolDep.ToolPackager] |
| 57 | + if err := pm.loadProfileTool(toolDep, indexURL, installMissing, downloadCB, taskCB); err != nil { |
| 58 | + merr = append(merr, fmt.Errorf("%s: %w", tr("loading required tool %s", toolDep), err)) |
| 59 | + logrus.WithField("tool", toolDep).WithField("index_url", indexURL).WithError(err).Debugf("Error loading tool for profile") |
| 60 | + } else { |
| 61 | + logrus.WithField("tool", toolDep).WithField("index_url", indexURL).Debugf("Loaded tool for profile") |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return merr |
| 67 | +} |
| 68 | + |
| 69 | +func (pm *PackageManager) loadProfilePlatform(platformRef *sketch.ProfilePlatformReference, installMissing bool, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) (*cores.PlatformRelease, error) { |
| 70 | + targetPackage := pm.Packages.GetOrCreatePackage(platformRef.Packager) |
| 71 | + platform := targetPackage.GetOrCreatePlatform(platformRef.Architecture) |
| 72 | + release := platform.GetOrCreateRelease(platformRef.Version) |
| 73 | + |
| 74 | + uid := platformRef.InternalUniqueIdentifier() |
| 75 | + destDir := configuration.ProfilesCacheDir(configuration.Settings).Join(uid) |
| 76 | + if !destDir.IsDir() && installMissing { |
| 77 | + // Try installing the missing platform |
| 78 | + if err := pm.installMissingProfilePlatform(platformRef, destDir, downloadCB, taskCB); err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + } |
| 82 | + return release, pm.loadPlatformRelease(release, destDir) |
| 83 | +} |
| 84 | + |
| 85 | +func (pm *PackageManager) installMissingProfilePlatform(platformRef *sketch.ProfilePlatformReference, destDir *paths.Path, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error { |
| 86 | + // Instantiate a temporary package manager only for platform installation |
| 87 | + _ = pm.TempDir.MkdirAll() |
| 88 | + tmp, err := paths.MkTempDir(pm.TempDir.String(), "") |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("installing missing platform: could not create temp dir %s", err) |
| 91 | + } |
| 92 | + tmpPm := NewPackageManager(tmp, tmp, pm.DownloadDir, tmp, pm.userAgent) |
| 93 | + defer tmp.RemoveAll() |
| 94 | + |
| 95 | + // Download the main index and parse it |
| 96 | + taskCB(&rpc.TaskProgress{Name: tr("Downloading platform %s", platformRef)}) |
| 97 | + defaultIndexURL, _ := url.Parse(globals.DefaultIndexURL) |
| 98 | + indexesToDownload := []*url.URL{defaultIndexURL} |
| 99 | + if platformRef.PlatformIndexURL != nil { |
| 100 | + indexesToDownload = append(indexesToDownload, platformRef.PlatformIndexURL) |
| 101 | + } |
| 102 | + for _, indexURL := range indexesToDownload { |
| 103 | + if err != nil { |
| 104 | + taskCB(&rpc.TaskProgress{Name: tr("Error downloading %s", indexURL)}) |
| 105 | + return &arduino.FailedDownloadError{Message: tr("Error downloading %s", indexURL), Cause: err} |
| 106 | + } |
| 107 | + indexResource := resources.IndexResource{URL: indexURL} |
| 108 | + if err := indexResource.Download(tmpPm.IndexDir, downloadCB); err != nil { |
| 109 | + taskCB(&rpc.TaskProgress{Name: tr("Error downloading %s", indexURL)}) |
| 110 | + return &arduino.FailedDownloadError{Message: tr("Error downloading %s", indexURL), Cause: err} |
| 111 | + } |
| 112 | + if err := tmpPm.LoadPackageIndex(indexURL); err != nil { |
| 113 | + taskCB(&rpc.TaskProgress{Name: tr("Error loading index %s", indexURL)}) |
| 114 | + return &arduino.FailedInstallError{Message: tr("Error loading index %s", indexURL), Cause: err} |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Download the platform |
| 119 | + tmpTargetPackage := tmpPm.Packages.GetOrCreatePackage(platformRef.Packager) |
| 120 | + tmpPlatform := tmpTargetPackage.GetOrCreatePlatform(platformRef.Architecture) |
| 121 | + tmpPlatformRelease := tmpPlatform.GetOrCreateRelease(platformRef.Version) |
| 122 | + if err := tmpPm.DownloadPlatformRelease(tmpPlatformRelease, nil, downloadCB); err != nil { |
| 123 | + taskCB(&rpc.TaskProgress{Name: tr("Error downloading platform %s", tmpPlatformRelease)}) |
| 124 | + return &arduino.FailedInstallError{Message: tr("Error downloading platform %s", tmpPlatformRelease), Cause: err} |
| 125 | + } |
| 126 | + taskCB(&rpc.TaskProgress{Completed: true}) |
| 127 | + |
| 128 | + // Perform install |
| 129 | + taskCB(&rpc.TaskProgress{Name: tr("Installing platform %s", tmpPlatformRelease)}) |
| 130 | + if err := tmpPm.InstallPlatformInDirectory(tmpPlatformRelease, destDir); err != nil { |
| 131 | + taskCB(&rpc.TaskProgress{Name: tr("Error installing platform %s", tmpPlatformRelease)}) |
| 132 | + return &arduino.FailedInstallError{Message: tr("Error installing platform %s", tmpPlatformRelease), Cause: err} |
| 133 | + } |
| 134 | + taskCB(&rpc.TaskProgress{Completed: true}) |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func (pm *PackageManager) loadProfileTool(toolRef *cores.ToolDependency, indexURL *url.URL, installMissing bool, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error { |
| 139 | + targetPackage := pm.Packages.GetOrCreatePackage(toolRef.ToolPackager) |
| 140 | + tool := targetPackage.GetOrCreateTool(toolRef.ToolName) |
| 141 | + |
| 142 | + uid := toolRef.InternalUniqueIdentifier(indexURL) |
| 143 | + destDir := configuration.ProfilesCacheDir(configuration.Settings).Join(uid) |
| 144 | + |
| 145 | + if !destDir.IsDir() && installMissing { |
| 146 | + // Try installing the missing tool |
| 147 | + toolRelease := tool.GetOrCreateRelease(toolRef.ToolVersion) |
| 148 | + if toolRelease == nil { |
| 149 | + return &arduino.InvalidVersionError{Cause: fmt.Errorf(tr("version %s not found", toolRef.ToolVersion))} |
| 150 | + } |
| 151 | + if err := pm.installMissingProfileTool(toolRelease, destDir, downloadCB, taskCB); err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return pm.loadToolReleaseFromDirectory(tool, toolRef.ToolVersion, destDir) |
| 157 | +} |
| 158 | + |
| 159 | +func (pm *PackageManager) installMissingProfileTool(toolRelease *cores.ToolRelease, destDir *paths.Path, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error { |
| 160 | + // Instantiate a temporary package manager only for platform installation |
| 161 | + tmp, err := paths.MkTempDir(destDir.Parent().String(), "") |
| 162 | + if err != nil { |
| 163 | + return fmt.Errorf("installing missing platform: could not create temp dir %s", err) |
| 164 | + } |
| 165 | + defer tmp.RemoveAll() |
| 166 | + |
| 167 | + // Download the tool |
| 168 | + toolResource := toolRelease.GetCompatibleFlavour() |
| 169 | + if toolResource == nil { |
| 170 | + return &arduino.InvalidVersionError{Cause: fmt.Errorf(tr("version %s not available for this operating system", toolRelease))} |
| 171 | + } |
| 172 | + taskCB(&rpc.TaskProgress{Name: tr("Downloading tool %s", toolRelease)}) |
| 173 | + if err := toolResource.Download(pm.DownloadDir, nil, toolRelease.String(), downloadCB); err != nil { |
| 174 | + taskCB(&rpc.TaskProgress{Name: tr("Error downloading tool %s", toolRelease)}) |
| 175 | + return &arduino.FailedInstallError{Message: tr("Error installing tool %s", toolRelease), Cause: err} |
| 176 | + } |
| 177 | + taskCB(&rpc.TaskProgress{Completed: true}) |
| 178 | + |
| 179 | + // Install tool |
| 180 | + taskCB(&rpc.TaskProgress{Name: tr("Installing tool %s", toolRelease)}) |
| 181 | + if err := toolResource.Install(pm.DownloadDir, tmp, destDir); err != nil { |
| 182 | + taskCB(&rpc.TaskProgress{Name: tr("Error installing tool %s", toolRelease)}) |
| 183 | + return &arduino.FailedInstallError{Message: tr("Error installing tool %s", toolRelease), Cause: err} |
| 184 | + } |
| 185 | + taskCB(&rpc.TaskProgress{Completed: true}) |
| 186 | + return nil |
| 187 | +} |
0 commit comments