Skip to content

Commit 9170d1f

Browse files
committed
Added --output/-o flag to 'compile'
1 parent 11f648a commit 9170d1f

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

commands/commands_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,40 @@ func TestCompileCommands(t *testing.T) {
390390
require.Contains(t, string(d), "Sketch created")
391391

392392
// Build sketch for arduino:avr:uno
393-
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:uno", currSketchbookDir.Join("Test1").String())
393+
test1 := currSketchbookDir.Join("Test1").String()
394+
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:uno", test1)
394395
require.Zero(t, exitCode, "exit code")
395396
require.Contains(t, string(d), "Sketch uses")
397+
require.True(t, paths.New(test1).Join("Test1.arduino.avr.uno.hex").Exist())
396398

397399
// Build sketch for arduino:avr:nano (without options)
398-
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", currSketchbookDir.Join("Test1").String())
400+
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", test1)
399401
require.Zero(t, exitCode, "exit code")
400402
require.Contains(t, string(d), "Sketch uses")
403+
require.True(t, paths.New(test1).Join("Test1.arduino.avr.nano.hex").Exist())
404+
405+
// Build sketch with --output path
406+
require.NoError(t, os.Chdir(tmp))
407+
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "test", test1)
408+
require.Zero(t, exitCode, "exit code")
409+
require.Contains(t, string(d), "Sketch uses")
410+
require.True(t, paths.New("test.hex").Exist())
411+
412+
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "test2.hex", test1)
413+
require.Zero(t, exitCode, "exit code")
414+
require.Contains(t, string(d), "Sketch uses")
415+
require.True(t, paths.New("test2.hex").Exist())
416+
require.NoError(t, paths.New(tmp, "anothertest").MkdirAll())
417+
418+
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", "anothertest/test", test1)
419+
require.Zero(t, exitCode, "exit code")
420+
require.Contains(t, string(d), "Sketch uses")
421+
require.True(t, paths.New("anothertest", "test.hex").Exist())
422+
423+
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:nano", "-o", tmp+"/anothertest/test2", test1)
424+
require.Zero(t, exitCode, "exit code")
425+
require.Contains(t, string(d), "Sketch uses")
426+
require.True(t, paths.New("anothertest", "test2.hex").Exist())
401427
}
402428

403429
func TestCoreCommands(t *testing.T) {

commands/compile/compile.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func InitCommand() *cobra.Command {
5959
command.Flags().StringVar(
6060
&flags.buildCachePath, "build-cache-path", "",
6161
"Builds of 'core.a' are saved into this path to be cached and reused.")
62+
command.Flags().StringVarP(
63+
&flags.exportFile, "output", "o", "",
64+
"Filename of the compile output.")
6265
command.Flags().StringVar(
6366
&flags.buildPath, "build-path", "",
6467
"Path where to save compiled files. If omitted, a directory will be created in the default temporary path of your OS.")
@@ -91,6 +94,7 @@ var flags struct {
9194
verbose bool // Turns on verbose mode.
9295
quiet bool // Suppresses almost every output.
9396
vidPid string // VID/PID specific build properties.
97+
exportFile string // The compiled binary is written to this file
9498
}
9599

96100
func run(cmd *cobra.Command, args []string) {
@@ -250,9 +254,22 @@ func run(cmd *cobra.Command, args []string) {
250254
fqbn.Configs = properties.NewMap()
251255
fqbnSuffix := strings.Replace(fqbn.String(), ":", ".", -1)
252256

257+
var exportPath *paths.Path
258+
var exportFile string
259+
if flags.exportFile == "" {
260+
exportPath = paths.New(sketch.FullPath)
261+
exportFile = sketch.Name + "." + fqbnSuffix
262+
} else {
263+
exportPath = paths.New(flags.exportFile).Parent()
264+
exportFile = paths.New(flags.exportFile).Base()
265+
if strings.HasSuffix(exportFile, ext) {
266+
exportFile = exportFile[:len(exportFile)-len(ext)]
267+
}
268+
}
269+
253270
// Copy .hex file to sketch directory
254271
srcHex := paths.New(outputPath)
255-
dstHex := paths.New(sketch.FullPath).Join(sketch.Name + "." + fqbnSuffix + ext)
272+
dstHex := exportPath.Join(exportFile + ext)
256273
logrus.WithField("from", srcHex).WithField("to", dstHex).Print("copying sketch build output")
257274
if err = srcHex.CopyTo(dstHex); err != nil {
258275
formatter.PrintError(err, "Error copying output file.")
@@ -261,7 +278,7 @@ func run(cmd *cobra.Command, args []string) {
261278

262279
// Copy .elf file to sketch directory
263280
srcElf := paths.New(outputPath[:len(outputPath)-3] + "elf")
264-
dstElf := paths.New(sketch.FullPath).Join(sketch.Name + "." + fqbnSuffix + ".elf")
281+
dstElf := exportPath.Join(exportFile + ".elf")
265282
logrus.WithField("from", srcElf).WithField("to", dstElf).Print("copying sketch build output")
266283
if err = srcElf.CopyTo(dstElf); err != nil {
267284
formatter.PrintError(err, "Error copying elf file.")

0 commit comments

Comments
 (0)