Skip to content

Commit 4408123

Browse files
committed
Use rpc fields to run Compile command
1 parent 51dd95f commit 4408123

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

commands/compile/compile.go

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ import (
1818
"github.com/arduino/arduino-cli/rpc"
1919
paths "github.com/arduino/go-paths-helper"
2020
properties "github.com/arduino/go-properties-orderedmap"
21-
flags "github.com/jessevdk/go-flags"
2221
"github.com/sirupsen/logrus"
2322
)
2423

2524
func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error) {
2625
logrus.Info("Executing `arduino compile`")
2726
var sketchPath *paths.Path
28-
if len(args) > 0 {
29-
sketchPath = paths.New(args[0])
27+
if req.GetSketchPath() != "" {
28+
sketchPath = paths.New(req.GetSketchPath())
3029
}
3130
sketch, err := cli.InitSketch(sketchPath)
3231
if err != nil {
3332
formatter.PrintError(err, "Error opening sketch.")
3433
os.Exit(cli.ErrGeneric)
3534
}
3635

37-
if flags.fqbn == "" && sketch != nil && sketch.Metadata != nil {
38-
flags.fqbn = sketch.Metadata.CPU.Fqbn
36+
fqbnIn := req.GetFqbn()
37+
if fqbnIn == "" && sketch != nil && sketch.Metadata != nil {
38+
fqbnIn = sketch.Metadata.CPU.Fqbn
3939
}
40-
if flags.fqbn == "" {
40+
if fqbnIn == "" {
4141
formatter.PrintErrorMessage("No Fully Qualified Board Name provided.")
4242
os.Exit(cli.ErrGeneric)
4343
}
44-
fqbn, err := cores.ParseFQBN(flags.fqbn)
44+
fqbn, err := cores.ParseFQBN(fqbnIn)
4545
if err != nil {
4646
formatter.PrintErrorMessage("Fully Qualified Board Name has incorrect format.")
4747
os.Exit(cli.ErrBadArgument)
@@ -80,64 +80,64 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
8080
os.Exit(cli.ErrCoreConfig)
8181
}
8282

83-
ctx := &types.Context{}
84-
ctx.PackageManager = pm
85-
ctx.FQBN = fqbn
86-
ctx.SketchLocation = sketch.FullPath
83+
builderCtx := &types.Context{}
84+
builderCtx.PackageManager = pm
85+
builderCtx.FQBN = fqbn
86+
builderCtx.SketchLocation = sketch.FullPath
8787

8888
// FIXME: This will be redundant when arduino-builder will be part of the cli
8989
if packagesDir, err := cli.Config.HardwareDirectories(); err == nil {
90-
ctx.HardwareDirs = packagesDir
90+
builderCtx.HardwareDirs = packagesDir
9191
} else {
9292
formatter.PrintError(err, "Cannot get hardware directories.")
9393
os.Exit(cli.ErrCoreConfig)
9494
}
9595

9696
if toolsDir, err := cli.Config.BundleToolsDirectories(); err == nil {
97-
ctx.ToolsDirs = toolsDir
97+
builderCtx.ToolsDirs = toolsDir
9898
} else {
9999
formatter.PrintError(err, "Cannot get bundled tools directories.")
100100
os.Exit(cli.ErrCoreConfig)
101101
}
102102

103-
ctx.OtherLibrariesDirs = paths.NewPathList()
104-
ctx.OtherLibrariesDirs.Add(cli.Config.LibrariesDir())
103+
builderCtx.OtherLibrariesDirs = paths.NewPathList()
104+
builderCtx.OtherLibrariesDirs.Add(cli.Config.LibrariesDir())
105105

106-
if flags.buildPath != "" {
107-
ctx.BuildPath = paths.New(flags.buildPath)
108-
err = ctx.BuildPath.MkdirAll()
106+
if req.GetBuildPath() != "" {
107+
builderCtx.BuildPath = paths.New(req.GetBuildPath())
108+
err = builderCtx.BuildPath.MkdirAll()
109109
if err != nil {
110110
formatter.PrintError(err, "Cannot create the build directory.")
111111
os.Exit(cli.ErrBadCall)
112112
}
113113
}
114114

115-
ctx.Verbose = flags.verbose
115+
builderCtx.Verbose = req.GetVerbose()
116116

117-
ctx.CoreBuildCachePath = paths.TempDir().Join("arduino-core-cache")
117+
builderCtx.CoreBuildCachePath = paths.TempDir().Join("arduino-core-cache")
118118

119-
ctx.USBVidPid = flags.vidPid
120-
ctx.WarningsLevel = flags.warnings
119+
builderCtx.USBVidPid = req.GetVidPid()
120+
builderCtx.WarningsLevel = req.GetWarnings()
121121

122122
if cli.GlobalFlags.Debug {
123-
ctx.DebugLevel = 100
123+
builderCtx.DebugLevel = 100
124124
} else {
125-
ctx.DebugLevel = 5
125+
builderCtx.DebugLevel = 5
126126
}
127127

128-
ctx.CustomBuildProperties = append(flags.buildProperties, "build.warn_data_percentage=75")
128+
builderCtx.CustomBuildProperties = append(req.GetBuildProperties(), "build.warn_data_percentage=75")
129129

130-
if flags.buildCachePath != "" {
131-
ctx.BuildCachePath = paths.New(flags.buildCachePath)
132-
err = ctx.BuildCachePath.MkdirAll()
130+
if req.GetBuildCachePath() != "" {
131+
builderCtx.BuildCachePath = paths.New(req.GetBuildCachePath())
132+
err = builderCtx.BuildCachePath.MkdirAll()
133133
if err != nil {
134134
formatter.PrintError(err, "Cannot create the build cache directory.")
135135
os.Exit(cli.ErrBadCall)
136136
}
137137
}
138138

139139
// Will be deprecated.
140-
ctx.ArduinoAPIVersion = "10607"
140+
builderCtx.ArduinoAPIVersion = "10607"
141141

142142
// Check if Arduino IDE is installed and get it's libraries location.
143143
preferencesTxt := cli.Config.DataDir.Join("preferences.txt")
@@ -154,15 +154,15 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
154154
sort.Strings(pathVariants)
155155
ideHardwarePath := lastIdeSubProperties.Get(pathVariants[len(pathVariants)-1])
156156
ideLibrariesPath := filepath.Join(filepath.Dir(ideHardwarePath), "libraries")
157-
ctx.BuiltInLibrariesDirs = paths.NewPathList(ideLibrariesPath)
157+
builderCtx.BuiltInLibrariesDirs = paths.NewPathList(ideLibrariesPath)
158158
}
159159

160-
if flags.showProperties {
161-
err = builder.RunParseHardwareAndDumpBuildProperties(ctx)
162-
} else if flags.preprocess {
163-
err = builder.RunPreprocess(ctx)
160+
if req.GetShowProperties() {
161+
err = builder.RunParseHardwareAndDumpBuildProperties(builderCtx)
162+
} else if req.GetPreprocess() {
163+
err = builder.RunPreprocess(builderCtx)
164164
} else {
165-
err = builder.RunBuilder(ctx)
165+
err = builder.RunBuilder(builderCtx)
166166
}
167167

168168
if err != nil {
@@ -171,7 +171,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
171171
}
172172

173173
// FIXME: Make a function to obtain these info...
174-
outputPath := ctx.BuildProperties.ExpandPropsInString("{build.path}/{recipe.output.tmp_file}")
174+
outputPath := builderCtx.BuildProperties.ExpandPropsInString("{build.path}/{recipe.output.tmp_file}")
175175
ext := filepath.Ext(outputPath)
176176

177177
// FIXME: Make a function to produce a better name...
@@ -181,12 +181,12 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
181181

182182
var exportPath *paths.Path
183183
var exportFile string
184-
if flags.exportFile == "" {
184+
if req.GetExportFile() == "" {
185185
exportPath = sketch.FullPath
186186
exportFile = sketch.Name + "." + fqbnSuffix
187187
} else {
188-
exportPath = paths.New(flags.exportFile).Parent()
189-
exportFile = paths.New(flags.exportFile).Base()
188+
exportPath = paths.New(req.GetExportFile()).Parent()
189+
exportFile = paths.New(req.GetExportFile()).Base()
190190
if strings.HasSuffix(exportFile, ext) {
191191
exportFile = exportFile[:len(exportFile)-len(ext)]
192192
}

0 commit comments

Comments
 (0)