Skip to content

Commit 100dd75

Browse files
committed
Fix {serial.port} being resolved too early
1 parent 2db8873 commit 100dd75

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

conn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func uploadHandler(c *gin.Context) {
130130

131131
go func() {
132132
// Resolve commandline
133-
commandline, err := upload.Resolve(data.Port, data.Board, filePath, data.Commandline, data.Extra, &Tools)
133+
commandline, err := upload.PartiallyResolve(data.Board, filePath, data.Commandline, data.Extra, &Tools)
134134
if err != nil {
135135
send(map[string]string{uploadStatusStr: "Error", "Msg": err.Error()})
136136
return

tools/download.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ func (t *Tools) DownloadPackageIndex(index_file, signature_file string) error {
126126
return nil
127127
}
128128

129+
func pathExists(path string) bool {
130+
_, err := os.Stat(path)
131+
if err == nil {
132+
return true
133+
}
134+
if os.IsNotExist(err) {
135+
return false
136+
}
137+
return true
138+
}
139+
129140
// Download will parse the index at the indexURL for the tool to download.
130141
// It will extract it in a folder in .arduino-create, and it will update the
131142
// Installed map.
@@ -181,7 +192,8 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
181192

182193
// Check if it already exists
183194
if behaviour == "keep" {
184-
if _, ok := t.installed[key]; ok {
195+
location, ok := t.installed[key]
196+
if ok && pathExists(location) {
185197
t.Logger.Println("The tool is already present on the system")
186198
return nil
187199
}

upload/upload.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ type Extra struct {
4343
ParamsQuiet string `json:"params_quiet"`
4444
}
4545

46-
// Resolve replaces some symbols in the commandline with the appropriate values
46+
// PartiallyResolve replaces some symbols in the commandline with the appropriate values
4747
// it can return an error when looking a variable in the Locater
48-
func Resolve(port, board, file, commandline string, extra Extra, t Locater) (string, error) {
48+
func PartiallyResolve(board, file, commandline string, extra Extra, t Locater) (string, error) {
4949
commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(file)), -1)
5050
commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file))), -1)
51-
commandline = strings.Replace(commandline, "{serial.port}", port, -1)
52-
commandline = strings.Replace(commandline, "{serial.port.file}", filepath.Base(port), -1)
5351

5452
if extra.Verbose == true {
5553
commandline = strings.Replace(commandline, "{upload.verbose}", extra.ParamsVerbose, -1)
@@ -67,12 +65,20 @@ func Resolve(port, board, file, commandline string, extra Extra, t Locater) (str
6765
if err != nil {
6866
return "", errors.Wrapf(err, "get location of %s", element)
6967
}
70-
commandline = strings.Replace(commandline, element, location, 1)
68+
if location != "" {
69+
commandline = strings.Replace(commandline, element, location, 1)
70+
}
7171
}
7272

7373
return commandline, nil
7474
}
7575

76+
func fixupPort(port, commandline string) string {
77+
commandline = strings.Replace(commandline, "{serial.port}", port, -1)
78+
commandline = strings.Replace(commandline, "{serial.port.file}", filepath.Base(port), -1)
79+
return commandline
80+
}
81+
7682
// Network performs a network upload
7783
func Network(port, board, file, commandline string, auth Auth, l Logger) error {
7884
Busy = true
@@ -85,6 +91,8 @@ func Network(port, board, file, commandline string, auth Auth, l Logger) error {
8591
auth.Password = "arduino"
8692
}
8793

94+
commandline = fixupPort(port, commandline)
95+
8896
// try with a form
8997
err := form(port, board, file, auth, l)
9098
if err != nil {
@@ -110,6 +118,8 @@ func Serial(port, commandline string, extra Extra, l Logger) error {
110118
}
111119
}
112120

121+
commandline = fixupPort(port, commandline)
122+
113123
z, err := shellwords.Parse(commandline)
114124
if err != nil {
115125
return errors.Wrapf(err, "Parse commandline")

0 commit comments

Comments
 (0)