MCPcopy Index your code
hub / github.com/dagger/dagger / Generate

Function Generate

cmd/codegen/codegen.go:41–100  ·  view source on GitHub ↗
(ctx context.Context, cfg generator.Config, genFunc GenFunc)

Source from the content-addressed store, hash-verified

39}
40
41func Generate(ctx context.Context, cfg generator.Config, genFunc GenFunc) (err error) {
42 var introspectionSchema *introspection.Schema
43 var introspectionSchemaVersion string
44 if cfg.IntrospectionJSON != "" {
45 var resp introspection.Response
46 if err := json.Unmarshal([]byte(cfg.IntrospectionJSON), &resp); err != nil {
47 return fmt.Errorf("unmarshal introspection json: %w", err)
48 }
49 introspectionSchema = resp.Schema
50 introspectionSchemaVersion = resp.SchemaVersion
51 } else {
52 introspectionSchema, introspectionSchemaVersion, err = introspection.Introspect(ctx, cfg.Dag)
53 if err != nil {
54 return err
55 }
56 }
57
58 // Set the parent schema
59 generator.SetSchemaParents(introspectionSchema)
60
61 for ctx.Err() == nil {
62 generated, err := genFunc(ctx, introspectionSchema, introspectionSchemaVersion)
63 if err != nil {
64 return err
65 }
66
67 if err := generator.Overlay(ctx, generated.Overlay, cfg.OutputDir); err != nil {
68 return fmt.Errorf("failed to overlay generated code: %w", err)
69 }
70
71 for _, cmd := range generated.PostCommands {
72 // Only set cmd.Dir if the command hasn't already specified one.
73 // This allows generators to target a specific sub-directory (e.g.
74 // the client module directory) without being overridden here.
75 if cmd.Dir == "" {
76 cmd.Dir = cfg.OutputDir
77 if cfg.ModuleConfig != nil && cfg.ModuleConfig.ModuleName != "" {
78 cmd.Dir = filepath.Join(cfg.OutputDir, cfg.ModuleConfig.ModuleSourcePath)
79 }
80 }
81 cmd.Stdout = os.Stdout
82 cmd.Stderr = os.Stderr
83 slog.Info("running post-command:", "args", strings.Join(cmd.Args, " "))
84 err := cmd.Run()
85 if err != nil {
86 slog.Error("post-command failed", "error", err)
87 return err
88 }
89 }
90
91 if !generated.NeedRegenerate {
92 slog.Info("done!")
93 break
94 }
95
96 slog.Info("needs another pass...")
97 }
98

Callers 3

GenerateLibraryFunction · 0.85
GenerateModuleFunction · 0.85
GenerateClientFunction · 0.85

Calls 8

IntrospectFunction · 0.92
SetSchemaParentsFunction · 0.92
OverlayFunction · 0.92
UnmarshalMethod · 0.65
ErrMethod · 0.65
InfoMethod · 0.65
RunMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected