Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow generate command to generate a single DBC file #295

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,52 @@ func main() {
It is possible to generate Go code from a `.dbc` file.

```
$ go run go.einride.tech/can/cmd/cantool generate <dbc file root folder> <output folder>
$ go run go.einride.tech/can/cmd/cantool generate <input> <output folder>
```

The INPUT argument can be either a directory containing DBC files, or the path
to a single DBC file.

#### Placement of generated code

OUTPUT FOLDER is treated slightly different dependeing on whether INPUT is a
file or a directory.

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
```

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.

#### Linting

In order to generate Go code that makes sense, we currently perform some
validations when parsing the DBC file so there may need to be some changes on
the DBC file to make it work

#### Using generated code

After generating Go code we can marshal a message to a frame:

```go
Expand Down
14 changes: 9 additions & 5 deletions cmd/cantool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,30 @@ func main() {

func generateCommand(app *kingpin.Application) {
command := app.Command("generate", "generate CAN messages")
inputDir := command.
Arg("input-dir", "input directory").
fileOrDir := command.
Arg("input", "input DBC file, if <input> is a directory all contained DBC files will be generated").
Required().
ExistingDir()
ExistingFileOrDir()
outputDir := command.
Arg("output-dir", "output directory").
Required().
String()
command.Action(func(_ *kingpin.ParseContext) error {
return filepath.Walk(*inputDir, func(p string, i os.FileInfo, err error) error {
return filepath.Walk(*fileOrDir, func(p string, i os.FileInfo, err error) error {
if err != nil {
return err
}
if i.IsDir() || filepath.Ext(p) != ".dbc" {
return nil
}
relPath, err := filepath.Rel(*inputDir, p)
relPath, err := filepath.Rel(*fileOrDir, p)
if err != nil {
return err
}
if relPath == "." {
// happens if fileOrDir points directly to the DBC file
relPath = filepath.Base(*fileOrDir)
}
outputFile := relPath + ".go"
outputPath := filepath.Join(*outputDir, outputFile)
return genGo(p, outputPath)
Expand Down