Skip to content

Commit 1a4758f

Browse files
committed
handle all strange cases (files with spaces etc)
works on almost all platforms ("program" doesn't work in darwin) Former-commit-id: 138c91fa96a41391d4ddd8e0469e447c36a5af1b [formerly 32b12d6f24a6161ac42b4b66362c334110f3772f] Former-commit-id: 751484155442bdbd5fcc050ae25d1eccf4aa502c
1 parent 016ab8d commit 1a4758f

File tree

3 files changed

+53
-35
lines changed

3 files changed

+53
-35
lines changed

hub.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ func checkCmd(m []byte) {
173173

174174
args := strings.Split(s, " ")
175175
if len(args) > 3 {
176-
go spProgram(args[1], args[2], args[3])
176+
var slice []string = args[3:len(args)]
177+
go spProgram(args[1], args[2], strings.Join(slice, " "))
177178
} else {
178179
go spErr("You did not specify a port, a board to program and a filename")
179180
}

serial.go

+35-17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
//"github.com/kballard/go-shellquote"
1111
//"github.com/johnlauer/goserial"
1212
//"github.com/mikepb/go-serial"
13+
"github.com/kardianos/osext"
1314
"go.bug.st/serial"
1415
"log"
1516
"os"
@@ -673,22 +674,24 @@ func formatCmdline(cmdline string, boardOptions map[string]string) (string, bool
673674
return cmdline, true
674675
}
675676

676-
func assembleCompilerCommand(boardname string, portname string, filePath string) (bool, string, string) {
677+
func assembleCompilerCommand(boardname string, portname string, filePath string) (bool, string, []string) {
677678
// walk across the local filesystem, find boards.txt files, search for the board in it
678679

680+
execPath, _ := osext.Executable()
681+
679682
boardFields := strings.Split(boardname, ":")
680683
if len(boardFields) != 3 {
681684
h.broadcastSys <- []byte("Board need to be specified in core:architecture:name format")
682-
return false, "", ""
685+
return false, "", nil
683686
}
684-
file, err := os.Open(boardFields[0] + "/hardware/" + boardFields[1] + "/boards.txt")
687+
tempPath := (filepath.Dir(execPath) + "/" + boardFields[0] + "/hardware/" + boardFields[1] + "/boards.txt")
688+
file, err := os.Open(tempPath)
685689
if err != nil {
686690
h.broadcastSys <- []byte("Could not find board: " + boardname)
687691
fmt.Println("Error:", err)
688-
return false, "", ""
692+
return false, "", nil
689693
}
690694
scanner := bufio.NewScanner(file)
691-
cmdline := ""
692695
//ide_tools_dir := "./" + boardFields[0] + "/tools"
693696

694697
boardOptions := make(map[string]string)
@@ -704,16 +707,20 @@ func assembleCompilerCommand(boardname string, portname string, filePath string)
704707
}
705708

706709
boardOptions["serial.port"] = portname
707-
boardOptions["build.project_name"] = strings.Trim(filePath, "\n")
710+
711+
filePath = strings.Trim(filePath, "\n")
712+
boardOptions["build.path"] = filepath.Dir(filePath)
713+
boardOptions["build.project_name"] = filepath.Base(filePath)
708714

709715
//fmt.Printf("boardOptions %v %T", boardOptions, boardOptions)
710716

711717
file.Close()
712-
file, err = os.Open(boardFields[0] + "/hardware/" + boardFields[1] + "/platform.txt")
718+
tempPath = (filepath.Dir(execPath) + "/" + boardFields[0] + "/hardware/" + boardFields[1] + "/platform.txt")
719+
file, err = os.Open(tempPath)
713720
if err != nil {
714721
h.broadcastSys <- []byte("Could not find board: " + boardname)
715722
fmt.Println("Error:", err)
716-
return false, "", ""
723+
return false, "", nil
717724
}
718725
scanner = bufio.NewScanner(file)
719726

@@ -732,27 +739,30 @@ func assembleCompilerCommand(boardname string, portname string, filePath string)
732739
file.Close()
733740

734741
version := uploadOptions["runtime.tools."+tool+".version"]
735-
path, err := filepath.Abs(boardFields[0] + "/tools/" + tool + "/" + version)
742+
path := (filepath.Dir(execPath) + "/" + boardFields[0] + "/tools/" + tool + "/" + version)
736743
if err != nil {
737744
h.broadcastSys <- []byte("Could not find board: " + boardname)
738745
fmt.Println("Error:", err)
739-
return false, "", ""
746+
return false, "", nil
740747
}
741748

742749
boardOptions["runtime.tools.avrdude.path"] = path
743750

744751
//boardOptions["config.path"] = uploadOptions["tools."+tool+".config.path"]
745752
//boardOptions["path"] = uploadOptions["tools."+tool+".path"]
746753

747-
var winded = true
748-
749-
cmdline = uploadOptions["tools."+tool+".upload.pattern"]
754+
cmdline := uploadOptions["tools."+tool+".upload.pattern"]
750755
// remove cmd.path as it is handles differently
751756
cmdline = strings.Replace(cmdline, "\"{cmd.path}\"", " ", 1)
752757
cmdline = strings.Replace(cmdline, "\"", "", -1)
753758

754-
for winded != false {
755-
cmdline, winded = formatCmdline(cmdline, boardOptions)
759+
cmdlineSlice := strings.Split(cmdline, " ")
760+
var winded = true
761+
for index, _ := range cmdlineSlice {
762+
winded = true
763+
for winded != false {
764+
cmdlineSlice[index], winded = formatCmdline(cmdlineSlice[index], boardOptions)
765+
}
756766
}
757767

758768
// cmdline := "-C" + ide_tools_dir + "etc/avrdude.conf" +
@@ -774,14 +784,22 @@ func assembleCompilerCommand(boardname string, portname string, filePath string)
774784
port, err := serial.OpenPort(portname, mode)
775785
if err != nil {
776786
log.Println(err)
777-
return false, "", ""
787+
return false, "", nil
778788
}
779789
time.Sleep(time.Second / 2)
780790
port.Close()
781791
time.Sleep(time.Second * 2)
782792
}
783793

784-
return (tool != ""), tool, cmdline
794+
tool = (filepath.Dir(execPath) + "/" + boardFields[0] + "/tools/" + tool + "/bin/" + tool)
795+
// the file doesn't exist, we are on windows
796+
if _, err := os.Stat(tool); err != nil {
797+
tool = tool + ".exe"
798+
// convert all "/" to "\"
799+
tool = strings.Replace(tool, "/", "\\", -1)
800+
}
801+
802+
return (tool != ""), tool, cmdlineSlice
785803
}
786804

787805
func findPortByName(portname string) (*serport, bool) {

serialport.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"log"
1010
"os/exec"
11+
"runtime"
1112
"strconv"
1213
"strings"
1314
)
@@ -413,31 +414,29 @@ func spHandlerClose(p *serport) {
413414
h.broadcastSys <- []byte("Closing serial port " + p.portConf.Name)
414415
}
415416

416-
func spHandlerProgram(flasher string, cmdString string) {
417+
func spHandlerProgram(flasher string, cmdString []string) {
417418

418-
s := strings.Split(cmdString, " ")
419-
oscmd := exec.Command(flasher, s...)
419+
var oscmd *exec.Cmd
420+
if runtime.GOOS == "darwin" {
421+
sh, _ := exec.LookPath("sh")
422+
oscmd = exec.Command(sh+" "+flasher, cmdString...)
423+
} else {
424+
oscmd = exec.Command(flasher, cmdString...)
425+
}
420426

421427
// Stdout buffer
422-
var cmdOutput bytes.Buffer
423-
// Attach buffer to command
424-
oscmd.Stdout = &cmdOutput
425-
426-
h.broadcastSys <- []byte("Start flashing with command " + cmdString)
428+
//var cmdOutput []byte
427429

428-
err := oscmd.Start()
429-
if err != nil {
430-
log.Fatal(err)
431-
}
432-
log.Printf("Waiting for command to finish... %v", oscmd)
430+
//h.broadcastSys <- []byte("Start flashing with command " + cmdString)
431+
log.Printf("Flashing with command:" + strings.Join(cmdString, " "))
433432

434-
err = oscmd.Wait()
433+
cmdOutput, err := oscmd.CombinedOutput()
435434

436435
if err != nil {
437-
log.Printf("Command finished with error: %v", err)
438-
h.broadcastSys <- []byte("Could not program the board: " + cmdOutput.String())
436+
log.Printf("Command finished with error: %v "+string(cmdOutput), err)
437+
h.broadcastSys <- []byte("Could not program the board")
439438
} else {
440-
log.Printf("Finished without error. Good stuff. stdout:%v", cmdOutput.String())
439+
log.Printf("Finished without error. Good stuff. stdout: " + string(cmdOutput))
441440
h.broadcastSys <- []byte("Flash OK!")
442441
// analyze stdin
443442

0 commit comments

Comments
 (0)