Skip to content

Commit b4ade07

Browse files
committed
feat: allow generate command to generate a single DBC file
This commit allows a user to run cantool generate INPUT OUTPUT-DIRECTORY where INPUT is either a path to a DBC file. Previously it was only possible to provide INPUT as a directory containing DBC files. N.B. When INPUT is a directory containing DBC files the generated files are created in OUTPUT-DIRECTORY relative to where they are found in INPUT. As an example, given the following directory structure: dbc/ powertrain/pt.dbc steering/steer.dbc brake/brake.dbc A `cantool generate dbc/ gen/go` call will generate the following structure: gen/go/ powertrain/pt.dbc.go steering/steer.dbc.go brake/brake.dbc.go However, when INPUT is a single DBC file it is generated directly in OUTPUT-DIRECTORY with its basename as the stem. `cantool generate dbc/steering/steer.dbc gen/go/steering` will therefore generate the gen/go/steering/steer.dbc.go.
1 parent 56c0075 commit b4ade07

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,52 @@ func main() {
7575
It is possible to generate Go code from a `.dbc` file.
7676

7777
```
78-
$ go run go.einride.tech/can/cmd/cantool generate <dbc file root folder> <output folder>
78+
$ go run go.einride.tech/can/cmd/cantool generate <input> <output folder>
7979
```
8080

81+
The INPUT argument can be either a directory containing DBC files, or the path
82+
to a single DBC file.
83+
84+
#### Placement of generated code
85+
86+
OUTPUT FOLDER is treated slightly different dependeing on whether INPUT is a
87+
file or a directory.
88+
89+
When INPUT is a directory containing DBC files the generated files are created
90+
in OUTPUT-DIRECTORY relative to where they are found in INPUT.
91+
92+
As an example, given the following directory structure:
93+
94+
```
95+
dbc/
96+
powertrain/pt.dbc
97+
steering/steer.dbc
98+
brake/brake.dbc
99+
```
100+
101+
A `cantool generate dbc/ gen/go` call will generate the following structure:
102+
103+
```
104+
gen/go/
105+
powertrain/pt.dbc.go
106+
steering/steer.dbc.go
107+
brake/brake.dbc.go
108+
```
109+
110+
When INPUT is a single DBC file it is generated directly in OUTPUT-DIRECTORY
111+
with its basename as the stem.
112+
113+
`cantool generate dbc/steering/steer.dbc gen/go/steering` will therefore
114+
generate the gen/go/steering/steer.dbc.go.
115+
116+
#### Linting
117+
81118
In order to generate Go code that makes sense, we currently perform some
82119
validations when parsing the DBC file so there may need to be some changes on
83120
the DBC file to make it work
84121

122+
#### Using generated code
123+
85124
After generating Go code we can marshal a message to a frame:
86125

87126
```go

cmd/cantool/main.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,30 @@ func main() {
4444

4545
func generateCommand(app *kingpin.Application) {
4646
command := app.Command("generate", "generate CAN messages")
47-
inputDir := command.
48-
Arg("input-dir", "input directory").
47+
fileOrDir := command.
48+
Arg("input", "input DBC file, if <input> is a directory all contained DBC files will be generated").
4949
Required().
50-
ExistingDir()
50+
ExistingFileOrDir()
5151
outputDir := command.
5252
Arg("output-dir", "output directory").
5353
Required().
5454
String()
5555
command.Action(func(_ *kingpin.ParseContext) error {
56-
return filepath.Walk(*inputDir, func(p string, i os.FileInfo, err error) error {
56+
return filepath.Walk(*fileOrDir, func(p string, i os.FileInfo, err error) error {
5757
if err != nil {
5858
return err
5959
}
6060
if i.IsDir() || filepath.Ext(p) != ".dbc" {
6161
return nil
6262
}
63-
relPath, err := filepath.Rel(*inputDir, p)
63+
relPath, err := filepath.Rel(*fileOrDir, p)
6464
if err != nil {
6565
return err
6666
}
67+
if relPath == "." {
68+
// happens if fileOrDir points directly to the DBC file
69+
relPath = filepath.Base(*fileOrDir)
70+
}
6771
outputFile := relPath + ".go"
6872
outputPath := filepath.Join(*outputDir, outputFile)
6973
return genGo(p, outputPath)

0 commit comments

Comments
 (0)