Skip to content

Commit

Permalink
Purge more and more. Deduplicate and unify the final steps of the cod…
Browse files Browse the repository at this point in the history
…egen.
  • Loading branch information
Alexandre Bourget committed Sep 12, 2024
1 parent ada8f16 commit 180b93f
Show file tree
Hide file tree
Showing 23 changed files with 72 additions and 376 deletions.
44 changes: 42 additions & 2 deletions convo.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package codegen

import (
"maps"
"slices"

"github.com/streamingfast/substreams-codegen/loop"
)

type Conversation[X any] struct {
State X

factory *MsgWrapFactory
updateFunc func(Conversation[X], loop.Msg) loop.Cmd
factory *MsgWrapFactory
}

func (c *Conversation[X]) SetFactory(f *MsgWrapFactory) {
Expand Down Expand Up @@ -42,3 +44,41 @@ func (c *Conversation[X]) CmdAskProjectName() loop.Cmd {
Validation("^([a-z][a-z0-9_]{0,63})$", "The project name must be a valid identifier with only lowercase letters, numbers and underscores, up to 64 characters.").
Cmd()
}

func (c *Conversation[X]) CmdDownloadFiles(msg ReturnGenerate) loop.Cmd {
if msg.Err != nil {
return loop.Seq(
c.Msg().Messagef("Code generation failed with error: %s", msg.Err).Cmd(),
loop.Quit(msg.Err),
)
}

downloadCmd := c.Action(InputSourceDownloaded{}).DownloadFiles()

for _, fileName := range slices.Sorted(maps.Keys(msg.ProjectFiles)) {
fileDescription := ""
if _, ok := FileDescriptions[fileName]; ok {
fileDescription = FileDescriptions[fileName]
}
downloadCmd.AddFile(fileName, msg.ProjectFiles[fileName], "text/plain", fileDescription)
}

return loop.Seq(
c.Msg().Messagef(`Your Substreams project is ready! Start streaming with:
`+"```"+`bash
substreams build
substreams auth
substreams gui # Get streaming!
`+"```"+`
Build Subgraphs and other sinks with:
`+"```"+`bash
substreams codegen subgraph
substreams codegen sql
`+"```"+`
`).Cmd(), downloadCmd.Cmd(),
loop.Quit(nil),
)
}
30 changes: 2 additions & 28 deletions evm-events-calls/convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,7 @@ func (c *Convo) NextStep() (out loop.Cmd) {
return cmd(AskAddContract{})
}

if !p.generatedCodeCompleted {
return cmd(codegen.RunGenerate{})
}

return loop.Quit(nil)
return cmd(codegen.RunGenerate{})
}

func (c *Convo) Update(msg loop.Msg) loop.Cmd {
Expand Down Expand Up @@ -828,30 +824,8 @@ message {{.Proto.MessageName}} {{.Proto.OutputModuleFieldName}} {
return c.CmdGenerate(c.State.Generate)

case codegen.ReturnGenerate:
if msg.Err != nil {
return loop.Seq(
c.Msg().Messagef("Code generation failed with error: %s", msg.Err).Cmd(),
loop.Quit(msg.Err),
)
}

c.State.generatedCodeCompleted = true
return c.CmdDownloadFiles(msg)

downloadCmd := c.Action(codegen.InputSourceDownloaded{}).DownloadFiles()

for fileName, fileContent := range msg.ProjectFiles {
fileDescription := ""
if _, ok := codegen.FileDescriptions[fileName]; ok {
fileDescription = codegen.FileDescriptions[fileName]
}

downloadCmd.AddFile(fileName, fileContent, "text/plain", fileDescription)
}

return loop.Seq(c.Msg().Messagef("Code generation complete!").Cmd(), downloadCmd.Cmd())

case codegen.InputSourceDownloaded:
return c.NextStep()
}

return loop.Quit(fmt.Errorf("invalid loop message: %T", msg))
Expand Down
2 changes: 1 addition & 1 deletion evm-events-calls/convo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestConvoUpdate(t *testing.T) {

//cmds := next()
msg1 := seq[0]().(*pbconvo.SystemOutput)
assert.Equal(t, msg1.GetMessage().Markdown, "Code generation complete!")
assert.Contains(t, msg1.GetMessage().Markdown, "substreams build\nsubstreams auth\nsubstreams gui")
//msg2 := seq[1]().(*pbconvo.SystemOutput)
//assert.NotNil(t, msg2.GetDownloadFiles())
//
Expand Down
12 changes: 1 addition & 11 deletions evm-events-calls/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"math"
"regexp"
"strings"
"time"

"github.com/codemodus/kace"
"github.com/golang-cz/textcase"
Expand All @@ -25,16 +24,7 @@ type Project struct {
Download bool `json:"download,omitempty"`
ConfirmEnoughContracts bool `json:"confirm_enough_contracts,omitempty"`

// Remote build part removed for the moment
// confirmDownloadOnly bool
// confirmDoCompile bool

currentContractIdx int
compilingBuild bool
generatedCodeCompleted bool

buildStarted time.Time
forceGeneration bool
currentContractIdx int
}

func dynamicContractNames(contracts []*DynamicContract) (out []string) {
Expand Down
31 changes: 2 additions & 29 deletions evm-minimal/convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ func (c *Convo) NextStep() (out loop.Cmd) {
return loop.Seq(cmd(codegen.MsgInvalidChainName{}), cmd(codegen.AskChainName{}))
}

if !p.generatedCodeCompleted {
return cmd(codegen.RunGenerate{})
}

return loop.Quit(nil)
return cmd(codegen.RunGenerate{})
}

func isValidChainName(input string) bool {
Expand Down Expand Up @@ -110,30 +106,7 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
return c.CmdGenerate(c.State.Generate)

case codegen.ReturnGenerate:
if msg.Err != nil {
return loop.Seq(
c.Msg().Messagef("Code generation failed with error: %s", msg.Err).Cmd(),
loop.Quit(msg.Err),
)
}

c.State.generatedCodeCompleted = true

downloadCmd := c.Action(codegen.InputSourceDownloaded{}).DownloadFiles()

for fileName, fileContent := range msg.ProjectFiles {
fileDescription := ""
if _, ok := codegen.FileDescriptions[fileName]; ok {
fileDescription = codegen.FileDescriptions[fileName]
}

downloadCmd.AddFile(fileName, fileContent, "text/plain", fileDescription)
}

return loop.Seq(c.Msg().Messagef("Code generation complete!").Cmd(), downloadCmd.Cmd())

case codegen.InputSourceDownloaded:
return c.NextStep()
return c.CmdDownloadFiles(msg)
}

return loop.Quit(fmt.Errorf("invalid loop message: %T", msg))
Expand Down
2 changes: 0 additions & 2 deletions evm-minimal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ type Project struct {
ChainName string `json:"chainName"`
Compile bool `json:"compile,omitempty"` // optional field to write in state and automatically compile with no confirmation.
Download bool `json:"download,omitempty"`

generatedCodeCompleted bool
}

func (p *Project) ChainConfig() *ChainConfig { return ChainConfigByID[p.ChainName] }
Expand Down
32 changes: 2 additions & 30 deletions injective-events/convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ func (c *Convo) NextStep() (out loop.Cmd) {
return cmd(AskAnotherEventType{})
}

if !p.generatedCodeCompleted {
return cmd(codegen.RunGenerate{})
}

return loop.Quit(nil)
return cmd(codegen.RunGenerate{})
}

func (c *Convo) Update(msg loop.Msg) loop.Cmd {
Expand Down Expand Up @@ -291,31 +287,7 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
return c.CmdGenerate(c.State.Generate)

case codegen.ReturnGenerate:
if msg.Err != nil {
return loop.Seq(
c.Msg().Message("Build failed!").Cmd(),
c.Msg().Messagef("The build failed with error: %s", msg.Err).Cmd(),
loop.Quit(msg.Err),
)
}

c.State.generatedCodeCompleted = true

downloadCmd := c.Action(codegen.InputSourceDownloaded{}).DownloadFiles()

for fileName, fileContent := range msg.ProjectFiles {
fileDescription := ""
if _, ok := codegen.FileDescriptions[fileName]; ok {
fileDescription = codegen.FileDescriptions[fileName]
}

downloadCmd.AddFile(fileName, fileContent, "text/plain", fileDescription)
}

return loop.Seq(c.Msg().Messagef("Code generation complete!").Cmd(), downloadCmd.Cmd())

case codegen.InputSourceDownloaded:
return c.NextStep()
return c.CmdDownloadFiles(msg)
}

return loop.Quit(fmt.Errorf("invalid loop message: %T", msg))
Expand Down
2 changes: 0 additions & 2 deletions injective-events/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ type Project struct {
EventDescs []*eventDesc `json:"messageTypes,omitempty"`
currentEventIdx int
EventsComplete bool `json:"eventsComplete,omitempty"`

generatedCodeCompleted bool
}

func (p *Project) ChainConfig() *ChainConfig { return ChainConfigByID[p.ChainName] }
Expand Down
36 changes: 2 additions & 34 deletions injective-minimal/convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,7 @@ func (c *Convo) NextStep() loop.Cmd {
return cmd(codegen.AskInitialStartBlockType{})
}

if !p.generatedCodeCompleted {
return cmd(codegen.RunGenerate{})
}

// Remote build part removed for the moment
// if !p.confirmDoCompile && !p.confirmDownloadOnly {
// return cmd(codegen.AskConfirmCompile{})
// }

return loop.Quit(nil)
return cmd(codegen.RunGenerate{})
}

func (c *Convo) Update(msg loop.Msg) loop.Cmd {
Expand Down Expand Up @@ -140,30 +131,7 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
return c.CmdGenerate(c.State.Generate)

case codegen.ReturnGenerate:
if msg.Err != nil {
return loop.Seq(
c.Msg().Messagef("Code generation failed with error: %s", msg.Err).Cmd(),
loop.Quit(msg.Err),
)
}

c.State.generatedCodeCompleted = true

downloadCmd := c.Action(codegen.InputSourceDownloaded{}).DownloadFiles()

for fileName, fileContent := range msg.ProjectFiles {
fileDescription := ""
if _, ok := codegen.FileDescriptions[fileName]; ok {
fileDescription = codegen.FileDescriptions[fileName]
}

downloadCmd.AddFile(fileName, fileContent, "text/plain", fileDescription)
}

return loop.Seq(c.Msg().Messagef("Code generation complete!").Cmd(), downloadCmd.Cmd())

case codegen.InputSourceDownloaded:
return c.NextStep()
return c.CmdDownloadFiles(msg)
}

return loop.Quit(fmt.Errorf("invalid loop message: %T", msg))
Expand Down
2 changes: 0 additions & 2 deletions injective-minimal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ type Project struct {
Download bool `json:"download,omitempty"`
InitialBlock uint64 `json:"initialBlock,omitempty"`
InitialBlockSet bool `json:"initialBlockSet,omitempty"`

generatedCodeCompleted bool
}

func (p *Project) ModuleName() string { return strings.ReplaceAll(p.Name, "-", "_") }
Expand Down
31 changes: 2 additions & 29 deletions sol-minimal/convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ func (c *Convo) NextStep() loop.Cmd {
return cmd(codegen.AskProjectName{})
}

if !p.generatedCodeCompleted {
return cmd(codegen.RunGenerate{})
}

return loop.Quit(nil)
return cmd(codegen.RunGenerate{})
}

func (c *Convo) Update(msg loop.Msg) loop.Cmd {
Expand Down Expand Up @@ -67,30 +63,7 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
return c.CmdGenerate(c.State.Generate)

case codegen.ReturnGenerate:
if msg.Err != nil {
return loop.Seq(
c.Msg().Messagef("Code generation failed with error: %s", msg.Err).Cmd(),
loop.Quit(msg.Err),
)
}

c.State.generatedCodeCompleted = true

downloadCmd := c.Action(codegen.InputSourceDownloaded{}).DownloadFiles()

for fileName, fileContent := range msg.ProjectFiles {
fileDescription := ""
if _, ok := codegen.FileDescriptions[fileName]; ok {
fileDescription = codegen.FileDescriptions[fileName]
}

downloadCmd.AddFile(fileName, fileContent, "text/plain", fileDescription)
}

return loop.Seq(c.Msg().Messagef("Code generation complete!").Cmd(), downloadCmd.Cmd())

case codegen.InputSourceDownloaded:
return c.NextStep()
return c.CmdDownloadFiles(msg)
}

return loop.Quit(fmt.Errorf("invalid loop message: %T", msg))
Expand Down
2 changes: 0 additions & 2 deletions sol-minimal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ type Project struct {
ChainName string `json:"chainName"`
Compile bool `json:"compile,omitempty"` // optional field to write in state and automatically compile with no confirmation.
Download bool `json:"download,omitempty"`

generatedCodeCompleted bool
}

func (p *Project) ModuleName() string { return strings.ReplaceAll(p.Name, "-", "_") }
Expand Down
Loading

0 comments on commit 180b93f

Please sign in to comment.