Skip to content

Commit 3447c14

Browse files
committed
Converted CTagsRunner into a function
1 parent 5cbb80d commit 3447c14

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
@@ -56,8 +56,14 @@ func PreprocessSketchWithCtags(ctx *types.Context) error {
5656
ctx.SketchSourceAfterCppPreprocessing = bldr.FilterSketchSource(ctx.Sketch, bytes.NewReader(src), false)
5757
}
5858

59-
if err := (&CTagsRunner{Source: &ctx.SketchSourceAfterCppPreprocessing, TargetFileName: "sketch_merged.cpp"}).Run(ctx); err != nil {
60-
return errors.WithStack(err)
59+
var ctagsStderr []byte
60+
_, ctagsStderr, ctx.PrototypesLineWhereToInsert, ctx.Prototypes, err = RunCTags(
61+
ctx.Sketch, ctx.SketchSourceAfterCppPreprocessing, "sketch_merged.cpp", ctx.BuildProperties, ctx.PreprocPath)
62+
if ctx.Verbose {
63+
ctx.WriteStderr(ctagsStderr)
64+
}
65+
if err != nil {
66+
return err
6167
}
6268
ctx.SketchSourceAfterArduinoPreprocessing, ctx.PrototypesSection = PrototypesAdder(ctx.SketchSourceMerged, ctx.PrototypesLineWhereToInsert, ctx.LineOffset, ctx.Prototypes, ctx.DebugPreprocessor)
6369

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
@@ -45,8 +45,8 @@ func TestCTagsRunner(t *testing.T) {
4545
buildPath := SetupBuildPath(t, ctx)
4646
defer buildPath.RemoveAll()
4747

48-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
4948
var _err error
49+
var ctagsOutput []byte
5050
commands := []types.Command{
5151
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
5252
types.BareCommand(func(ctx *types.Context) error {
@@ -56,7 +56,10 @@ func TestCTagsRunner(t *testing.T) {
5656
&builder.ContainerFindIncludes{},
5757
&builder.PrintUsedLibrariesIfVerbose{},
5858
&builder.WarnAboutArchIncompatibleLibraries{},
59-
ctagsRunner,
59+
types.BareCommand(func(ctx *types.Context) error {
60+
ctagsOutput, _, _, _, _err = builder.RunCTags(ctx.Sketch, ctx.SketchSourceMerged, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
61+
return _err
62+
}),
6063
}
6164
for _, command := range commands {
6265
err := command.Run(ctx)
@@ -71,7 +74,7 @@ func TestCTagsRunner(t *testing.T) {
7174
"digitalCommand " + quotedSketchLocation + " /^void digitalCommand(BridgeClient client) {$/;\" kind:function line:82 signature:(BridgeClient client) returntype:void\n" +
7275
"analogCommand " + quotedSketchLocation + " /^void analogCommand(BridgeClient client) {$/;\" kind:function line:109 signature:(BridgeClient client) returntype:void\n" +
7376
"modeCommand " + quotedSketchLocation + " /^void modeCommand(BridgeClient client) {$/;\" kind:function line:149 signature:(BridgeClient client) returntype:void\n"
74-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
77+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
7578
}
7679

7780
func TestCTagsRunnerSketchWithClass(t *testing.T) {
@@ -92,8 +95,8 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) {
9295
buildPath := SetupBuildPath(t, ctx)
9396
defer buildPath.RemoveAll()
9497

95-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
9698
var _err error
99+
var ctagsOutput []byte
97100
commands := []types.Command{
98101
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
99102
types.BareCommand(func(ctx *types.Context) error {
@@ -103,7 +106,10 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) {
103106
&builder.ContainerFindIncludes{},
104107
&builder.PrintUsedLibrariesIfVerbose{},
105108
&builder.WarnAboutArchIncompatibleLibraries{},
106-
ctagsRunner,
109+
types.BareCommand(func(ctx *types.Context) error {
110+
ctagsOutput, _, _, _, _err = builder.RunCTags(ctx.Sketch, ctx.SketchSourceMerged, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
111+
return _err
112+
}),
107113
}
108114
for _, command := range commands {
109115
err := command.Run(ctx)
@@ -116,7 +122,7 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) {
116122
"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" +
117123
"setup\t" + quotedSketchLocation + "\t/^void setup() {$/;\"\tkind:function\tline:13\tsignature:()\treturntype:void\n" +
118124
"loop\t" + quotedSketchLocation + "\t/^void loop() {$/;\"\tkind:function\tline:17\tsignature:()\treturntype:void\n"
119-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
125+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
120126
}
121127

122128
func TestCTagsRunnerSketchWithTypename(t *testing.T) {
@@ -137,8 +143,8 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) {
137143
buildPath := SetupBuildPath(t, ctx)
138144
defer buildPath.RemoveAll()
139145

140-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
141146
var _err error
147+
var ctagsOutput []byte
142148
commands := []types.Command{
143149
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
144150
types.BareCommand(func(ctx *types.Context) error {
@@ -148,7 +154,10 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) {
148154
&builder.ContainerFindIncludes{},
149155
&builder.PrintUsedLibrariesIfVerbose{},
150156
&builder.WarnAboutArchIncompatibleLibraries{},
151-
ctagsRunner,
157+
types.BareCommand(func(ctx *types.Context) error {
158+
ctagsOutput, _, _, _, _err = builder.RunCTags(ctx.Sketch, ctx.SketchSourceMerged, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
159+
return _err
160+
}),
152161
}
153162
for _, command := range commands {
154163
err := command.Run(ctx)
@@ -160,7 +169,7 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) {
160169
"setup\t" + quotedSketchLocation + "\t/^void setup() {$/;\"\tkind:function\tline:6\tsignature:()\treturntype:void\n" +
161170
"loop\t" + quotedSketchLocation + "\t/^void loop() {}$/;\"\tkind:function\tline:10\tsignature:()\treturntype:void\n" +
162171
"func\t" + quotedSketchLocation + "\t/^typename Foo<char>::Bar func(){$/;\"\tkind:function\tline:12\tsignature:()\treturntype:Foo::Bar\n"
163-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
172+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
164173
}
165174

166175
func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
@@ -181,8 +190,8 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
181190
buildPath := SetupBuildPath(t, ctx)
182191
defer buildPath.RemoveAll()
183192

184-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
185193
var _err error
194+
var ctagsOutput []byte
186195
commands := []types.Command{
187196
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
188197
types.BareCommand(func(ctx *types.Context) error {
@@ -192,7 +201,10 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
192201
&builder.ContainerFindIncludes{},
193202
&builder.PrintUsedLibrariesIfVerbose{},
194203
&builder.WarnAboutArchIncompatibleLibraries{},
195-
ctagsRunner,
204+
types.BareCommand(func(ctx *types.Context) error {
205+
ctagsOutput, _, _, _, _err = builder.RunCTags(ctx.Sketch, ctx.SketchSourceMerged, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
206+
return _err
207+
}),
196208
}
197209
for _, command := range commands {
198210
err := command.Run(ctx)
@@ -203,7 +215,7 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
203215
expectedOutput := "value\t" + quotedSketchLocation + "\t/^\tint value() {$/;\"\tkind:function\tline:2\tnamespace:Test\tsignature:()\treturntype:int\n" +
204216
"setup\t" + quotedSketchLocation + "\t/^void setup() {}$/;\"\tkind:function\tline:7\tsignature:()\treturntype:void\n" +
205217
"loop\t" + quotedSketchLocation + "\t/^void loop() {}$/;\"\tkind:function\tline:8\tsignature:()\treturntype:void\n"
206-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
218+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
207219
}
208220

209221
func TestCTagsRunnerSketchWithTemplates(t *testing.T) {
@@ -224,8 +236,8 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) {
224236
buildPath := SetupBuildPath(t, ctx)
225237
defer buildPath.RemoveAll()
226238

227-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
228239
var _err error
240+
var ctagsOutput []byte
229241
commands := []types.Command{
230242
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
231243
types.BareCommand(func(ctx *types.Context) error {
@@ -235,7 +247,10 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) {
235247
&builder.ContainerFindIncludes{},
236248
&builder.PrintUsedLibrariesIfVerbose{},
237249
&builder.WarnAboutArchIncompatibleLibraries{},
238-
ctagsRunner,
250+
types.BareCommand(func(ctx *types.Context) error {
251+
ctagsOutput, _, _, _, _err = builder.RunCTags(ctx.Sketch, ctx.SketchSourceMerged, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
252+
return _err
253+
}),
239254
}
240255
for _, command := range commands {
241256
err := command.Run(ctx)
@@ -247,5 +262,5 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) {
247262
"bVar\t" + quotedSketchLocation + "\t/^c< 8 > bVar;$/;\"\tkind:variable\tline:15\n" +
248263
"aVar\t" + quotedSketchLocation + "\t/^c< 1<<8 > aVar;$/;\"\tkind:variable\tline:16\n" +
249264
"func\t" + quotedSketchLocation + "\t/^template<int X> func( c< 1<<X> & aParam) {$/;\"\tkind:function\tline:18\tsignature:( c< 1<<X> & aParam)\treturntype:template\n"
250-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
265+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
251266
}

0 commit comments

Comments
 (0)