Skip to content

Commit 10b70eb

Browse files
committed
[WIP] Here's the new PackageManager
1 parent 0088cd3 commit 10b70eb

File tree

14 files changed

+191
-10
lines changed

14 files changed

+191
-10
lines changed

commands/board/attach.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
8484

8585
time.Sleep(duration)
8686

87+
// FIXME: Replace with the PackageManager
8788
homeFolder, err := configs.ArduinoHomeFolder.Get()
8889
if err != nil {
8990
formatter.PrintError(err, "Cannot Parse Board Index file.")
@@ -145,6 +146,7 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
145146
formatter.PrintResult("BOARD ATTACHED.")
146147
}
147148

149+
// FIXME: Those should probably go in a "BoardManager" pkg or something
148150
// findSerialConnectedBoard find the board which is connected to the specified URI via serial port, using a monitor and a set of Boards
149151
// for the matching.
150152
func findSerialConnectedBoard(bs boards.Boards, monitor *discovery.Monitor, deviceURI *url.URL) *boards.Board {

commands/board/list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var listCommand = &cobra.Command{
3939
// runListCommand detects and lists the connected arduino boards
4040
// (either via serial or network ports).
4141
func runListCommand(cmd *cobra.Command, args []string) {
42+
// FIXME: Replace with the PackageManager
4243
packageFolder, err := configs.PackagesFolder.Get()
4344
if err != nil {
4445
formatter.PrintError(err, "Cannot Parse Board Index file.")

commands/compile/compile.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func run(cmd *cobra.Command, args []string) {
117117
}
118118
}
119119

120+
// FIXME: Replace with the PackageManager
120121
isCtagsInstalled, err := cores.IsToolInstalled(packageName, "ctags")
121122
if err != nil {
122123
formatter.PrintError(err, "Cannot check ctags installation.")
@@ -195,6 +196,7 @@ func run(cmd *cobra.Command, args []string) {
195196
formatter.PrintError(err, "Cannot get the core version.")
196197
os.Exit(commands.ErrBadCall)
197198
}
199+
// FIXME: this is building the CommandLine to run the builder; we should move it to a "cores.compute" pkg
198200
// Add dependency tools paths to build properties with versions corresponding to specific core version.
199201
var packageIndex packageindex.Index
200202
packageindex.LoadIndex(&packageIndex)

commands/config/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func runInitCommand(cmd *cobra.Command, args []string) {
8585
logrus.Info("Done")
8686
}
8787

88+
// FIXME: is this any useful?
8889
// ConfigsFromQuestions asks some questions to the user to properly initialize configs.
8990
// It does not have much sense to use it in JSON formatting, though.
9091
func configsFromQuestions() {

commands/core/args.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func parsePlatformReferenceArgs(args []string) []platformReference {
7878
return ret
7979
}
8080

81+
// FIXME: Replace move to the PackageManager
8182
// findItemsToDownload takes a set of platformReference and returns a set of items to download and
8283
// a set of outputs for non existing platforms.
8384
func findItemsToDownload(sc *cores.Packages, items []platformReference) ([]*cores.PlatformRelease, []*cores.ToolRelease, []output.ProcessResult) {

commands/core/core.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ var command = &cobra.Command{
5555
Example: "arduino core update-index # to update the package index file.",
5656
}
5757

58+
// FIXME: Move to the PackageManager
59+
5860
// getInstalledCores gets the installed cores and puts them in the output struct.
5961
func getInstalledCores(packageName string, cores *[]output.InstalledStuff) {
6062
getInstalledStuff(cores, configs.CoresFolder(packageName))
@@ -118,6 +120,5 @@ func getPackagesStatusContext() (*cores.Packages, error) {
118120
return &status, nil
119121
}
120122

121-
status := index.CreateStatusContext()
122-
return &status, nil
123+
return index.CreateStatusContext(), nil
123124
}

commands/core/download.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/bcmi-labs/arduino-cli/cores"
4141
"github.com/sirupsen/logrus"
4242
"github.com/spf13/cobra"
43+
"github.com/bcmi-labs/arduino-cli/cores/packagemanager"
4344
)
4445

4546
func init() {
@@ -75,6 +76,22 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
7576
Tools: []output.ProcessResult{},
7677
}
7778

79+
// FIXME: that's just a PoC; please have mercy...
80+
fmt.Printf("TESTING THE FLUENT PKGMGR\n")
81+
82+
_, err = packagemanager.PackageManager().AddDefaultPackageIndex()
83+
fmt.Printf("Loading default package index, error: %v\n", err)
84+
85+
tool, err := packagemanager.PackageManager().Package("not-found").Tool("avrdude").IsInstalled()
86+
fmt.Printf("Tool status: %s, %v\n", tool, err)
87+
88+
tool, err = packagemanager.PackageManager().Package("arduino").Tool("not-found").IsInstalled()
89+
fmt.Printf("Tool status: %s, %v\n", tool, err)
90+
91+
tool, err = packagemanager.PackageManager().Package("arduino").Tool("avrdude").IsInstalled()
92+
fmt.Printf("Tool status: %s, %v\n", tool, err)
93+
94+
7895
downloadToolsArchives(toolsToDownload, &outputResults)
7996

8097
downloadPlatformArchives(coresToDownload, &outputResults)
@@ -83,6 +100,9 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
83100
logrus.Info("Done")
84101
}
85102

103+
// FIXME: Move to the PackageManager and look for a way to uncouple of the printing logic
104+
// All this stuff is pkgmgr responsibility for sure
105+
86106
func downloadToolsArchives(tools []*cores.ToolRelease, results *output.CoreProcessResults) {
87107
downloads := map[string]*releases.DownloadResource{}
88108
for _, tool := range tools {

commands/core/install.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
6969

7070
logrus.Info("Preparing download")
7171

72+
// FIXME: Isn't this the same code as in core/download.go? Should be refactored
7273
coresToDownload, toolsToDownload, failOutputs := findItemsToDownload(status, parsePlatformReferenceArgs(args))
7374
failOutputsCount := len(failOutputs)
7475
outputResults := output.CoreProcessResults{

commands/core/list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
7575
os.Exit(commands.ErrCoreConfig)
7676
}
7777

78+
// FIXME: Use the PackageManager instead
7879
pkgs := output.InstalledPackageList{
7980
InstalledPackages: make([]output.InstalledPackage, 0, 10),
8081
}

common/formatter/pretty_print/pretty_print_core.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ import (
3434
"github.com/bcmi-labs/arduino-cli/cores"
3535
"github.com/bcmi-labs/arduino-cli/cores/packageindex"
3636
"github.com/bcmi-labs/arduino-cli/task"
37+
"github.com/bcmi-labs/arduino-cli/cores/packagemanager"
3738
)
3839

3940
// DownloadCoreFileIndex shows info regarding the download of a missing (or corrupted) file index of core packages.
4041
func DownloadCoreFileIndex() task.Task {
41-
return DownloadFileIndex(packageindex.DownloadPackagesFile)
42+
return DownloadFileIndex(packagemanager.PackageManager().DownloadPackagesFile)
4243
}
4344

4445
// CorruptedCoreIndexFix pretty prints messages regarding corrupted index fixes of cores.

cores/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/bcmi-labs/arduino-cli/configs"
3939
)
4040

41+
// FIXME: Move all of these to PackageManager().Tools().isInstalled() or something like that
4142
// IsCoreInstalled detects if a core has been installed.
4243
func IsCoreInstalled(packageName string, name string) (bool, error) {
4344
location, err := configs.CoresFolder(packageName).Get()

cores/packageindex/index.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,12 @@ type indexHelp struct {
110110
}
111111

112112
// CreateStatusContext creates a status context from index data.
113-
func (index Index) CreateStatusContext() cores.Packages {
114-
res := cores.Packages{
115-
Packages: map[string]*cores.Package{},
116-
}
113+
func (index Index) CreateStatusContext() *cores.Packages {
114+
res := cores.NewPackages()
115+
117116
for _, p := range index.Packages {
118117
res.Packages[p.Name] = p.extractPackage()
119-
res.Packages[p.Name].Packages = &res
118+
res.Packages[p.Name].Packages = res
120119
}
121120
return res
122121
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package packagemanager
2+
3+
import (
4+
"sync"
5+
6+
"github.com/bcmi-labs/arduino-cli/cores/packageindex"
7+
"fmt"
8+
"github.com/bcmi-labs/arduino-cli/cores"
9+
"github.com/bcmi-labs/arduino-cli/configs"
10+
"os"
11+
"path/filepath"
12+
"github.com/juju/errors"
13+
)
14+
15+
var packageManagerInstance *packageManager
16+
var once sync.Once
17+
18+
// PackageManager defines the superior oracle which understands all about
19+
// Arduino Packages, how to parse them, download, and so on.
20+
//
21+
// The manager also keeps track of the status of the Packages (their Platform Releases, actually)
22+
// installed in the system.
23+
type packageManager struct {
24+
/* FIXME
25+
26+
What's the typical flow?
27+
28+
You start by downloading AN PackageIndex (remember: multiple repositories are allowed??)
29+
*/
30+
31+
packages *cores.Packages
32+
}
33+
34+
// PackageManager return the instance of the PackageManager
35+
// yeah, that's a singleton by the way...
36+
func PackageManager() *packageManager {
37+
once.Do(func() {
38+
// TODO: why not just use the Go pkg init()?
39+
packageManagerInstance = &packageManager{}
40+
// TODO: perhaps directly use the loading from PackagesIndex file?
41+
packageManagerInstance.packages = cores.NewPackages()
42+
})
43+
44+
fmt.Println("==> Cool! You're using my new singleton PackageManager.")
45+
46+
return packageManagerInstance
47+
}
48+
49+
// FIXME add an handler to be invoked on each verbose operation, in order to let commands display results through the formatter
50+
// as for the progress bars during download
51+
52+
// FIXME this is currently hard-coded with the default PackageIndex and won't merge or check existing Packages!!
53+
func (pm *packageManager) AddDefaultPackageIndex() (*packageManager, error) {
54+
var index packageindex.Index
55+
err := packageindex.LoadIndex(&index)
56+
if err != nil {
57+
//TODO: The original version would automatically fix corrupted index?
58+
/*status, err := prettyPrints.CorruptedCoreIndexFix(index)
59+
if err != nil {
60+
return pm, err
61+
}
62+
pm.packages = &status
63+
return pm, nil*/
64+
65+
return pm, errors.Annotate(err, fmt.Sprintf("failed to load the package index, is probably corrupted" ))
66+
}
67+
68+
// TODO: if this really is a singleton, a lock is needed :(
69+
pm.packages = index.CreateStatusContext()
70+
return pm, nil
71+
}
72+
73+
// TODO: implement the generic version (with merge)
74+
/*func (pm *packageManager) AddPackageIndex() *packageManager {
75+
76+
}*/
77+
78+
// DownloadPackagesFile downloads the core packages index file from Arduino repository.
79+
func (pm *packageManager) DownloadPackagesFile() error {
80+
return packageindex.DownloadPackagesFile()
81+
}
82+
83+
func (pm *packageManager) Package(name string) *packageActions {
84+
//TODO: perhaps these 2 structure should be merged? cores.Packages vs pkgmgr??
85+
var err error
86+
thePackage := pm.packages.Packages[name]
87+
if thePackage == nil {
88+
err = fmt.Errorf("package '%s' not found", name)
89+
}
90+
return &packageActions{
91+
aPackage: thePackage,
92+
forwardError: err,
93+
}
94+
}
95+
96+
type packageActions struct {
97+
aPackage *cores.Package
98+
forwardError error
99+
}
100+
101+
func (pa *packageActions) Tool(name string) *toolActions {
102+
var tool *cores.Tool
103+
err := pa.forwardError
104+
if err == nil {
105+
tool = pa.aPackage.Tools[name]
106+
107+
if tool == nil {
108+
err = fmt.Errorf("tool '%s' not found in package '%s'", name, pa.aPackage.Name)
109+
}
110+
}
111+
return &toolActions{
112+
tool: tool,
113+
forwardError: err,
114+
}
115+
}
116+
117+
type toolActions struct{
118+
tool *cores.Tool
119+
forwardError error
120+
}
121+
122+
func (ta *toolActions) Get() (*cores.Tool, error) {
123+
err := ta.forwardError
124+
if err == nil {
125+
return ta.tool, nil
126+
}
127+
return nil, err
128+
}
129+
130+
func (ta *toolActions) IsInstalled() (bool, error) {
131+
err := ta.forwardError
132+
if err == nil {
133+
location, err := configs.ToolsFolder(ta.tool.Package.Name).Get()
134+
if err != nil {
135+
return false, err
136+
}
137+
_, err = os.Stat(filepath.Join(location, ta.tool.Name))
138+
if !os.IsNotExist(err) {
139+
return true, nil
140+
}
141+
return false, nil
142+
}
143+
return false, err
144+
}

cores/status.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"errors"
3434
"fmt"
3535

36-
properties "github.com/arduino/go-properties-map"
36+
"github.com/arduino/go-properties-map"
3737
"github.com/pmylund/sortutil"
3838
)
3939

@@ -44,6 +44,12 @@ type Packages struct {
4444
Properties properties.Map `json:"-"` // TODO: used to add "ctags" properties, TO BE REMOVED
4545
}
4646

47+
func NewPackages() *Packages {
48+
return &Packages{
49+
Packages: map[string]*Package{},
50+
}
51+
}
52+
4753
// Package represents a package in the system.
4854
type Package struct {
4955
Name string // Name of the package.
@@ -52,7 +58,7 @@ type Package struct {
5258
Email string // Email of maintainer.
5359
Platforms map[string]*Platform // The platforms in the system.
5460
Tools map[string]*Tool // The tools in the system.
55-
Packages *Packages `json:"-"`
61+
Packages *Packages `json:"-"`
5662
}
5763

5864
// Names returns the array containing the name of the packages.

0 commit comments

Comments
 (0)