Skip to content

Commit 5880d01

Browse files
committed
legacy: moving path-relativization code out of PrepareCommand
1 parent 382d77c commit 5880d01

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

legacy/builder/builder_utils/utils.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -507,19 +507,30 @@ func PrepareCommandForRecipe(ctx *types.Context, buildProperties *properties.Map
507507
return nil, i18n.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, recipe)
508508
}
509509

510-
var err error
511510
commandLine := buildProperties.ExpandPropsInString(pattern)
512511
if removeUnsetProperties {
513512
commandLine = properties.DeleteUnexpandedPropsFromString(commandLine)
514513
}
515514

516-
relativePath := ""
515+
command, err := utils.PrepareCommand(commandLine, logger)
517516

517+
// if the overall commandline is too long for the platform
518+
// try reducing the length by making the filenames relative
519+
// and changing working directory to build.path
518520
if len(commandLine) > COMMANDLINE_LIMIT {
519-
relativePath = buildProperties.Get("build.path")
521+
relativePath := buildProperties.Get("build.path")
522+
for i, arg := range command.Args {
523+
if _, err := os.Stat(arg); os.IsNotExist(err) {
524+
continue
525+
}
526+
rel, err := filepath.Rel(relativePath, arg)
527+
if err == nil && !strings.Contains(rel, "..") && len(rel) < len(arg) {
528+
command.Args[i] = rel
529+
}
530+
}
531+
command.Dir = relativePath
520532
}
521533

522-
command, err := utils.PrepareCommand(commandLine, logger, relativePath)
523534
if err != nil {
524535
return nil, errors.WithStack(err)
525536
}

legacy/builder/ctags_runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (s *CTagsRunner) Run(ctx *types.Context) error {
4141
}
4242

4343
commandLine := properties.ExpandPropsInString(pattern)
44-
command, err := utils.PrepareCommand(commandLine, logger, "")
44+
command, err := utils.PrepareCommand(commandLine, logger)
4545
if err != nil {
4646
return errors.WithStack(err)
4747
}

legacy/builder/preprocess_sketch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (s *ArduinoPreprocessorRunner) Run(ctx *types.Context) error {
106106
}
107107

108108
commandLine := properties.ExpandPropsInString(pattern)
109-
command, err := utils.PrepareCommand(commandLine, logger, "")
109+
command, err := utils.PrepareCommand(commandLine, logger)
110110
if err != nil {
111111
return errors.WithStack(err)
112112
}

legacy/builder/utils/utils.go

+1-17
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func TrimSpace(value string) string {
189189
return strings.TrimSpace(value)
190190
}
191191

192-
func PrepareCommand(pattern string, logger i18n.Logger, relativePath string) (*exec.Cmd, error) {
192+
func PrepareCommand(pattern string, logger i18n.Logger) (*exec.Cmd, error) {
193193
parts, err := ParseCommandLine(pattern, logger)
194194
if err != nil {
195195
return nil, errors.WithStack(err)
@@ -201,26 +201,10 @@ func PrepareCommand(pattern string, logger i18n.Logger, relativePath string) (*e
201201
if part == "" {
202202
continue
203203
}
204-
// if relativePath is specified, the overall commandline is too long for the platform
205-
// try reducing the length by making the filenames relative
206-
// and changing working directory to build.path
207-
if relativePath != "" {
208-
if _, err := os.Stat(part); !os.IsNotExist(err) {
209-
tmp, err := filepath.Rel(relativePath, part)
210-
if err == nil && !strings.Contains(tmp, "..") && len(tmp) < len(part) {
211-
part = tmp
212-
}
213-
}
214-
}
215204
args = append(args, part)
216205
}
217206

218207
cmd := exec.Command(command, args...)
219-
220-
if relativePath != "" {
221-
cmd.Dir = relativePath
222-
}
223-
224208
return cmd, nil
225209
}
226210

0 commit comments

Comments
 (0)