|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2023 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package preprocessor |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/executils" |
| 24 | + "github.com/arduino/arduino-cli/i18n" |
| 25 | + "github.com/arduino/go-paths-helper" |
| 26 | + "github.com/arduino/go-properties-orderedmap" |
| 27 | + "github.com/pkg/errors" |
| 28 | +) |
| 29 | + |
| 30 | +var tr = i18n.Tr |
| 31 | + |
| 32 | +// RunCTags performs a run of ctags on the given source file. Returns the ctags output and the stderr contents. |
| 33 | +func RunCTags(sourceFile *paths.Path, buildProperties *properties.Map) ([]byte, []byte, error) { |
| 34 | + ctagsBuildProperties := properties.NewMap() |
| 35 | + ctagsBuildProperties.Set("tools.ctags.path", "{runtime.tools.ctags.path}") |
| 36 | + ctagsBuildProperties.Set("tools.ctags.cmd.path", "{path}/ctags") |
| 37 | + ctagsBuildProperties.Set("tools.ctags.pattern", `"{cmd.path}" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "{source_file}"`) |
| 38 | + ctagsBuildProperties.Merge(buildProperties) |
| 39 | + ctagsBuildProperties.Merge(ctagsBuildProperties.SubTree("tools").SubTree("ctags")) |
| 40 | + ctagsBuildProperties.SetPath("source_file", sourceFile) |
| 41 | + |
| 42 | + pattern := ctagsBuildProperties.Get("pattern") |
| 43 | + if pattern == "" { |
| 44 | + return nil, nil, errors.Errorf(tr("%s pattern is missing"), "ctags") |
| 45 | + } |
| 46 | + |
| 47 | + commandLine := ctagsBuildProperties.ExpandPropsInString(pattern) |
| 48 | + parts, err := properties.SplitQuotedString(commandLine, `"'`, false) |
| 49 | + if err != nil { |
| 50 | + return nil, nil, err |
| 51 | + } |
| 52 | + proc, err := executils.NewProcess(nil, parts...) |
| 53 | + if err != nil { |
| 54 | + return nil, nil, err |
| 55 | + } |
| 56 | + stdout, stderr, err := proc.RunAndCaptureOutput(context.Background()) |
| 57 | + |
| 58 | + // Append ctags arguments to stderr |
| 59 | + args := fmt.Sprintln(strings.Join(parts, " ")) |
| 60 | + stderr = append([]byte(args), stderr...) |
| 61 | + return stdout, stderr, err |
| 62 | +} |
0 commit comments