Skip to content

Commit

Permalink
Merge pull request #2 from 11wizards/feat/multipackage-aliases
Browse files Browse the repository at this point in the history
feat: additional imports
  • Loading branch information
diegofrata authored Aug 15, 2023
2 parents 8081f80 + e9441b1 commit e0b1bd8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
29 changes: 25 additions & 4 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ import (
//go:embed dart/timestamp_converter.dart
var timestampConverterSrc string

func generateHeader(pkg *packages.Package, wr io.Writer, mode options.Mode) {
func generateHeader(pkg *packages.Package, wr io.Writer, mode options.Mode, imports []string) {
if mode == options.Firestore {
fmt.Fprint(wr, "import 'package:cloud_firestore/cloud_firestore.dart';\n")
}

fmt.Fprint(wr, "import 'package:json_annotation/json_annotation.dart';\n\n")
fmt.Fprintf(wr, "part '%s.go.g.dart';\n\n", pkg.Name)
fmt.Fprint(wr, "import 'package:json_annotation/json_annotation.dart';\n")
for _, imp := range imports {
fmt.Fprintf(wr, "import '%s';\n", imp)
}

fmt.Fprintf(wr, "\npart '%s.go.g.dart';\n\n", pkg.Name)

if mode == options.Firestore {
fmt.Fprint(wr, timestampConverterSrc)
Expand Down Expand Up @@ -122,7 +126,14 @@ func writeOut(output, outputDartFile string, wr *bytes.Buffer) {
}

func Run(options options.Options) {
if abs, err := filepath.Abs(options.Input); err == nil {
options.Input = abs
} else {
panic(err)
}

pkgs, err := packages.Load(&packages.Config{
Dir: options.Input,
Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo,
}, options.Input)

Expand All @@ -131,10 +142,20 @@ func Run(options options.Options) {
os.Exit(1)
}

for _, pkg := range pkgs {
if len(pkg.Errors) > 0 {
for _, err := range pkg.Errors {
fmt.Println(err)
}

os.Exit(1)
}
}

for _, pkg := range pkgs {
var buf []byte
wr := bytes.NewBuffer(buf)
generateHeader(pkg, wr, options.Mode)
generateHeader(pkg, wr, options.Mode, options.Imports)
generateClasses(pkg, wr, options.Mode)
writeOut(options.Output, fmt.Sprintf("%s.go.dart", pkg.Name), wr)
}
Expand Down
7 changes: 4 additions & 3 deletions generator/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const (
)

type Options struct {
Input string
Output string
Mode Mode
Input string
Output string
Imports []string
Mode Mode
}
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ package main

import (
"fmt"
"os"

"github.com/11wizards/go-to-dart/generator"
"github.com/11wizards/go-to-dart/generator/options"
"github.com/spf13/cobra"
"os"
)

var input, output, mode string
var imports []string

var rootCmd = &cobra.Command{
Use: "go-to-dart",
Short: "Go-to-Dart is a tool to generate Dart classes from Go structs",
Run: func(cmd *cobra.Command, args []string) {
o := options.Options{
Input: input,
Output: output,
Mode: options.Mode(mode),
Input: input,
Output: output,
Imports: imports,
Mode: options.Mode(mode),
}

if o.Mode != options.JSON && o.Mode != options.Firestore {
Expand All @@ -32,6 +35,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&input, "input", "i", "", "Input directory to read from")
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "Output directory to write to")
rootCmd.PersistentFlags().StringVarP(&mode, "mode", "m", "json", "Mode to run in: json or firestore")
rootCmd.PersistentFlags().StringSliceVarP(&imports, "imports", "p", []string{}, "Additional imports to add to the generated file")

if err := rootCmd.MarkPersistentFlagRequired("input"); err != nil {
panic(err)
Expand Down

0 comments on commit e0b1bd8

Please sign in to comment.