Skip to content

Commit 9dc2cca

Browse files
committedApr 6, 2018
commands:compile Always copy .hex and .elf in sketch main folder
Also use the io library instead of relying on os.exec to allow compatibility between OSs Signed-off-by: Matteo Suppo <matteo.suppo@gmail.com>
1 parent ef27db0 commit 9dc2cca

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed
 

‎commands/compile/compile.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ package compile
3131

3232
import (
3333
"fmt"
34+
"io"
3435
"os"
35-
"os/exec"
3636
"path/filepath"
3737
"sort"
3838
"strings"
@@ -256,12 +256,41 @@ func run(cmd *cobra.Command, args []string) {
256256
outputPath := ctx.BuildProperties.ExpandPropsInString("{build.path}/{recipe.output.tmp_file}")
257257
ext := filepath.Ext(outputPath)
258258
fqbn = strings.Replace(fqbn, ":", ".", -1)
259-
if err := exec.Command("cp", outputPath, sketch.Name+"."+fqbn+ext).Run(); err != nil {
260-
formatter.PrintError(err, "Error copying output file.")
259+
260+
// Copy .hex file to sketch folder
261+
src := outputPath
262+
dst := filepath.Join(sketchPath, sketch.Name+"."+fqbn+ext)
263+
if err = copyFile(src, dst); err != nil {
264+
formatter.PrintError(err, "Error copying hex file.")
261265
os.Exit(commands.ErrGeneric)
262266
}
263-
if err := exec.Command("cp", outputPath[:len(outputPath)-3]+"elf", sketch.Name+"."+fqbn+".elf").Run(); err != nil {
264-
formatter.PrintError(err, "Error copying output file.")
267+
268+
// Copy .elf file to sketch folder
269+
src = outputPath[:len(outputPath)-3] + "elf"
270+
dst = filepath.Join(sketchPath, sketch.Name+"."+fqbn+".elf")
271+
if err = copyFile(src, dst); err != nil {
272+
formatter.PrintError(err, "Error copying elf file.")
265273
os.Exit(commands.ErrGeneric)
266274
}
267275
}
276+
277+
// copyFile copies the src file to dst. Any existing file will be overwritten and will not copy file attributes.
278+
func copyFile(src, dst string) error {
279+
in, err := os.Open(src)
280+
if err != nil {
281+
return err
282+
}
283+
defer in.Close()
284+
285+
out, err := os.Create(dst)
286+
if err != nil {
287+
return err
288+
}
289+
defer out.Close()
290+
291+
_, err = io.Copy(out, in)
292+
if err != nil {
293+
return err
294+
}
295+
return out.Close()
296+
}

0 commit comments

Comments
 (0)
Please sign in to comment.