Skip to content

Commit f0190df

Browse files
committed
Converted CTagsRunner into a function
1 parent 41bd2e4 commit f0190df

File tree

3 files changed

+81
-62
lines changed

3 files changed

+81
-62
lines changed

legacy/builder/container_add_prototypes.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ func PreprocessSketchWithCtags(ctx *types.Context) error {
6262
ctx.SketchSourceAfterCppPreprocessing = filterSketchSource(ctx.Sketch, bytes.NewReader(src), false)
6363
}
6464

65-
if err := (&CTagsRunner{Source: &ctx.SketchSourceAfterCppPreprocessing, TargetFileName: "sketch_merged.cpp"}).Run(ctx); err != nil {
66-
return errors.WithStack(err)
65+
var ctagsStderr []byte
66+
_, ctagsStderr, ctx.PrototypesLineWhereToInsert, ctx.Prototypes, err = RunCTags(
67+
ctx.Sketch, ctx.SketchSourceAfterCppPreprocessing, "sketch_merged.cpp", ctx.BuildProperties, ctx.PreprocPath)
68+
if ctx.Verbose {
69+
ctx.WriteStderr(ctagsStderr)
70+
}
71+
if err != nil {
72+
return err
6773
}
6874
ctx.SketchSourceAfterArduinoPreprocessing, ctx.PrototypesSection = PrototypesAdder(ctx.SketchSourceMerged, ctx.PrototypesLineWhereToInsert, ctx.LineOffset, ctx.Prototypes, ctx.DebugPreprocessor)
6975

legacy/builder/ctags_runner.go

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,74 +16,72 @@
1616
package builder
1717

1818
import (
19-
"os"
20-
"os/exec"
19+
"bytes"
20+
"strings"
2121

22+
"github.com/arduino/arduino-cli/arduino/sketch"
23+
"github.com/arduino/arduino-cli/executils"
2224
"github.com/arduino/arduino-cli/legacy/builder/ctags"
23-
"github.com/arduino/arduino-cli/legacy/builder/types"
24-
"github.com/arduino/arduino-cli/legacy/builder/utils"
25+
"github.com/arduino/go-paths-helper"
2526
properties "github.com/arduino/go-properties-orderedmap"
2627
"github.com/pkg/errors"
2728
)
2829

29-
type CTagsRunner struct {
30-
Source *string
31-
TargetFileName string
32-
33-
// Needed for unit-testing
34-
CtagsOutput []byte
35-
}
36-
37-
func (r *CTagsRunner) Run(ctx *types.Context) error {
38-
source := *r.Source
39-
40-
preprocPath := ctx.PreprocPath
41-
if err := preprocPath.MkdirAll(); err != nil {
42-
return errors.WithStack(err)
30+
func RunCTags(sketch *sketch.Sketch, source string, targetFileName string, buildProperties *properties.Map, preprocPath *paths.Path,
31+
) (ctagsStdout, ctagsStderr []byte, prototypesLineWhereToInsert int, prototypes []*ctags.Prototype, err error) {
32+
if err = preprocPath.MkdirAll(); err != nil {
33+
return
4334
}
4435

45-
ctagsTargetFilePath := preprocPath.Join(r.TargetFileName)
46-
if err := ctagsTargetFilePath.WriteFile([]byte(source)); err != nil {
47-
return errors.WithStack(err)
36+
ctagsTargetFilePath := preprocPath.Join(targetFileName)
37+
if err = ctagsTargetFilePath.WriteFile([]byte(source)); err != nil {
38+
return
4839
}
4940

50-
buildProperties := properties.NewMap()
51-
buildProperties.Set("tools.ctags.path", "{runtime.tools.ctags.path}")
52-
buildProperties.Set("tools.ctags.cmd.path", "{path}/ctags")
53-
buildProperties.Set("tools.ctags.pattern", `"{cmd.path}" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "{source_file}"`)
54-
buildProperties.Merge(ctx.BuildProperties)
55-
buildProperties.Merge(buildProperties.SubTree("tools").SubTree("ctags"))
56-
buildProperties.SetPath("source_file", ctagsTargetFilePath)
41+
ctagsBuildProperties := properties.NewMap()
42+
ctagsBuildProperties.Set("tools.ctags.path", "{runtime.tools.ctags.path}")
43+
ctagsBuildProperties.Set("tools.ctags.cmd.path", "{path}/ctags")
44+
ctagsBuildProperties.Set("tools.ctags.pattern", `"{cmd.path}" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "{source_file}"`)
45+
ctagsBuildProperties.Merge(buildProperties)
46+
ctagsBuildProperties.Merge(ctagsBuildProperties.SubTree("tools").SubTree("ctags"))
47+
ctagsBuildProperties.SetPath("source_file", ctagsTargetFilePath)
5748

58-
pattern := buildProperties.Get("pattern")
49+
pattern := ctagsBuildProperties.Get("pattern")
5950
if pattern == "" {
60-
return errors.Errorf(tr("%s pattern is missing"), "ctags")
51+
err = errors.Errorf(tr("%s pattern is missing"), "ctags")
52+
return
6153
}
6254

63-
commandLine := buildProperties.ExpandPropsInString(pattern)
55+
commandLine := ctagsBuildProperties.ExpandPropsInString(pattern)
6456
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
6557
if err != nil {
66-
return errors.WithStack(err)
58+
return
6759
}
68-
command := exec.Command(parts[0], parts[1:]...)
69-
command.Env = append(os.Environ(), ctx.PackageManager.GetEnvVarsForSpawnedProcess()...)
70-
71-
ctagsOutput, _, err := utils.ExecCommand(ctx, command, utils.Capture /* stdout */, utils.ShowIfVerbose /* stderr */)
60+
proc, err := executils.NewProcess(nil, parts...)
7261
if err != nil {
73-
return errors.WithStack(err)
62+
return
63+
}
64+
stdout := &bytes.Buffer{}
65+
stderr := &bytes.Buffer{}
66+
proc.RedirectStdoutTo(stdout)
67+
proc.RedirectStderrTo(stderr)
68+
if err = proc.Run(); err != nil {
69+
return
70+
}
71+
stderr.WriteString(strings.Join(parts, " "))
72+
ctagsStdout = stdout.Bytes()
73+
ctagsStderr = stderr.Bytes()
74+
if err != nil {
75+
return
7476
}
7577

7678
parser := &ctags.CTagsParser{}
77-
parser.Parse(ctagsOutput, ctx.Sketch.MainFile)
79+
parser.Parse(ctagsStdout, sketch.MainFile)
7880
parser.FixCLinkageTagsDeclarations()
7981

80-
protos, line := parser.GeneratePrototypes()
82+
prototypes, line := parser.GeneratePrototypes()
8183
if line != -1 {
82-
ctx.PrototypesLineWhereToInsert = line
84+
prototypesLineWhereToInsert = line
8385
}
84-
ctx.Prototypes = protos
85-
86-
// Needed for unit-testing
87-
r.CtagsOutput = ctagsOutput
88-
return nil
86+
return
8987
}

legacy/builder/test/ctags_runner_test.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func TestCTagsRunner(t *testing.T) {
3232
defer cleanUpBuilderTestContext(t, ctx)
3333
ctx.Verbose = true
3434

35-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
3635
var _err error
36+
var ctagsOutput []byte
3737
commands := []types.Command{
3838
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
3939
types.BareCommand(func(ctx *types.Context) error {
@@ -43,7 +43,10 @@ func TestCTagsRunner(t *testing.T) {
4343
&builder.ContainerFindIncludes{},
4444
&builder.PrintUsedLibrariesIfVerbose{},
4545
&builder.WarnAboutArchIncompatibleLibraries{},
46-
ctagsRunner,
46+
types.BareCommand(func(ctx *types.Context) error {
47+
ctagsOutput, _, _, _, _err = builder.RunCTags(ctx.Sketch, ctx.SketchSourceMerged, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
48+
return _err
49+
}),
4750
}
4851
for _, command := range commands {
4952
err := command.Run(ctx)
@@ -58,7 +61,7 @@ func TestCTagsRunner(t *testing.T) {
5861
"digitalCommand " + quotedSketchLocation + " /^void digitalCommand(BridgeClient client) {$/;\" kind:function line:82 signature:(BridgeClient client) returntype:void\n" +
5962
"analogCommand " + quotedSketchLocation + " /^void analogCommand(BridgeClient client) {$/;\" kind:function line:109 signature:(BridgeClient client) returntype:void\n" +
6063
"modeCommand " + quotedSketchLocation + " /^void modeCommand(BridgeClient client) {$/;\" kind:function line:149 signature:(BridgeClient client) returntype:void\n"
61-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
64+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
6265
}
6366

6467
func TestCTagsRunnerSketchWithClass(t *testing.T) {
@@ -67,8 +70,8 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) {
6770
defer cleanUpBuilderTestContext(t, ctx)
6871
ctx.Verbose = true
6972

70-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
7173
var _err error
74+
var ctagsOutput []byte
7275
commands := []types.Command{
7376
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
7477
types.BareCommand(func(ctx *types.Context) error {
@@ -78,7 +81,10 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) {
7881
&builder.ContainerFindIncludes{},
7982
&builder.PrintUsedLibrariesIfVerbose{},
8083
&builder.WarnAboutArchIncompatibleLibraries{},
81-
ctagsRunner,
84+
types.BareCommand(func(ctx *types.Context) error {
85+
ctagsOutput, _, _, _, _err = builder.RunCTags(ctx.Sketch, ctx.SketchSourceMerged, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
86+
return _err
87+
}),
8288
}
8389
for _, command := range commands {
8490
err := command.Run(ctx)
@@ -91,7 +97,7 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) {
9197
"set_values\t" + quotedSketchLocation + "\t/^void Rectangle::set_values (int x, int y) {$/;\"\tkind:function\tline:8\tclass:Rectangle\tsignature:(int x, int y)\treturntype:void\n" +
9298
"setup\t" + quotedSketchLocation + "\t/^void setup() {$/;\"\tkind:function\tline:13\tsignature:()\treturntype:void\n" +
9399
"loop\t" + quotedSketchLocation + "\t/^void loop() {$/;\"\tkind:function\tline:17\tsignature:()\treturntype:void\n"
94-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
100+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
95101
}
96102

97103
func TestCTagsRunnerSketchWithTypename(t *testing.T) {
@@ -100,8 +106,8 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) {
100106
defer cleanUpBuilderTestContext(t, ctx)
101107
ctx.Verbose = true
102108

103-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
104109
var _err error
110+
var ctagsOutput []byte
105111
commands := []types.Command{
106112
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
107113
types.BareCommand(func(ctx *types.Context) error {
@@ -111,7 +117,10 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) {
111117
&builder.ContainerFindIncludes{},
112118
&builder.PrintUsedLibrariesIfVerbose{},
113119
&builder.WarnAboutArchIncompatibleLibraries{},
114-
ctagsRunner,
120+
types.BareCommand(func(ctx *types.Context) error {
121+
ctagsOutput, _, _, _, _err = builder.RunCTags(ctx.Sketch, ctx.SketchSourceMerged, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
122+
return _err
123+
}),
115124
}
116125
for _, command := range commands {
117126
err := command.Run(ctx)
@@ -123,7 +132,7 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) {
123132
"setup\t" + quotedSketchLocation + "\t/^void setup() {$/;\"\tkind:function\tline:6\tsignature:()\treturntype:void\n" +
124133
"loop\t" + quotedSketchLocation + "\t/^void loop() {}$/;\"\tkind:function\tline:10\tsignature:()\treturntype:void\n" +
125134
"func\t" + quotedSketchLocation + "\t/^typename Foo<char>::Bar func(){$/;\"\tkind:function\tline:12\tsignature:()\treturntype:Foo::Bar\n"
126-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
135+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
127136
}
128137

129138
func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
@@ -132,8 +141,8 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
132141
defer cleanUpBuilderTestContext(t, ctx)
133142
ctx.Verbose = true
134143

135-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
136144
var _err error
145+
var ctagsOutput []byte
137146
commands := []types.Command{
138147
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
139148
types.BareCommand(func(ctx *types.Context) error {
@@ -143,7 +152,10 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
143152
&builder.ContainerFindIncludes{},
144153
&builder.PrintUsedLibrariesIfVerbose{},
145154
&builder.WarnAboutArchIncompatibleLibraries{},
146-
ctagsRunner,
155+
types.BareCommand(func(ctx *types.Context) error {
156+
ctagsOutput, _, _, _, _err = builder.RunCTags(ctx.Sketch, ctx.SketchSourceMerged, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
157+
return _err
158+
}),
147159
}
148160
for _, command := range commands {
149161
err := command.Run(ctx)
@@ -154,7 +166,7 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
154166
expectedOutput := "value\t" + quotedSketchLocation + "\t/^\tint value() {$/;\"\tkind:function\tline:2\tnamespace:Test\tsignature:()\treturntype:int\n" +
155167
"setup\t" + quotedSketchLocation + "\t/^void setup() {}$/;\"\tkind:function\tline:7\tsignature:()\treturntype:void\n" +
156168
"loop\t" + quotedSketchLocation + "\t/^void loop() {}$/;\"\tkind:function\tline:8\tsignature:()\treturntype:void\n"
157-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
169+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
158170
}
159171

160172
func TestCTagsRunnerSketchWithTemplates(t *testing.T) {
@@ -163,8 +175,8 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) {
163175
defer cleanUpBuilderTestContext(t, ctx)
164176
ctx.Verbose = true
165177

166-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
167178
var _err error
179+
var ctagsOutput []byte
168180
commands := []types.Command{
169181
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
170182
types.BareCommand(func(ctx *types.Context) error {
@@ -174,7 +186,10 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) {
174186
&builder.ContainerFindIncludes{},
175187
&builder.PrintUsedLibrariesIfVerbose{},
176188
&builder.WarnAboutArchIncompatibleLibraries{},
177-
ctagsRunner,
189+
types.BareCommand(func(ctx *types.Context) error {
190+
ctagsOutput, _, _, _, _err = builder.RunCTags(ctx.Sketch, ctx.SketchSourceMerged, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
191+
return _err
192+
}),
178193
}
179194
for _, command := range commands {
180195
err := command.Run(ctx)
@@ -186,5 +201,5 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) {
186201
"bVar\t" + quotedSketchLocation + "\t/^c< 8 > bVar;$/;\"\tkind:variable\tline:15\n" +
187202
"aVar\t" + quotedSketchLocation + "\t/^c< 1<<8 > aVar;$/;\"\tkind:variable\tline:16\n" +
188203
"func\t" + quotedSketchLocation + "\t/^template<int X> func( c< 1<<X> & aParam) {$/;\"\tkind:function\tline:18\tsignature:( c< 1<<X> & aParam)\treturntype:template\n"
189-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
204+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
190205
}

0 commit comments

Comments
 (0)