Skip to content

Commit

Permalink
Merge pull request #382 from stateful/refactor-project-layout
Browse files Browse the repository at this point in the history
Refactor project package to allow linkage
  • Loading branch information
pastuxso authored Sep 26, 2023
2 parents eb2a0d2 + 047db90 commit a96ffa4
Show file tree
Hide file tree
Showing 30 changed files with 296 additions and 232 deletions.
221 changes: 12 additions & 209 deletions internal/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@ import (
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/stateful/runme/internal/document"
runnerv1 "github.com/stateful/runme/internal/gen/proto/go/runme/runner/v1"
"github.com/stateful/runme/internal/project"
"github.com/stateful/runme/internal/runner/client"
"github.com/stateful/runme/internal/tui"
"github.com/stateful/runme/internal/tui/prompt"
"github.com/stateful/runme/pkg/project"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -57,6 +54,17 @@ func getProject() (proj project.Project, err error) {
return
}

func newProjectLoader(cmd *cobra.Command) (*project.ProjectLoader, error) {
fd := os.Stdout.Fd()

if int(fd) >= 0 {
loader := project.NewLoader(cmd.OutOrStdout(), cmd.InOrStdin(), isTerminal(fd))
return &loader, nil
}

return nil, fmt.Errorf("invalid file descriptor due to restricted environments, redirected standard output, system configuration issues, or testing/simulation setups")
}

func getCodeBlocks() (document.CodeBlocks, error) {
return project.GetCodeBlocks(
filepath.Join(fChdir, fFileName),
Expand Down Expand Up @@ -315,208 +323,3 @@ func replaceVarValue(ev CommandExportExtractMatch, newValue string) string {
replacedText := fmt.Sprintf("%s=%q", parts[0], newValue)
return replacedText
}

type loadTasksModel struct {
spinner spinner.Model

status string
filename string

clear bool

err error

tasks project.CodeBlocks
files []string

nextTaskMsg tea.Cmd
}

type loadTaskFinished struct{}

func newLoadTasksModel(nextTaskMsg tea.Cmd) loadTasksModel {
return loadTasksModel{
spinner: spinner.New(spinner.WithSpinner(spinner.MiniDot)),
nextTaskMsg: nextTaskMsg,
status: "Initializing...",
tasks: make(project.CodeBlocks, 0),
}
}

func loadFiles(proj project.Project, w io.Writer, r io.Reader) ([]string, error) {
m, err := runTasksModel(proj, true, w, r)
if err != nil {
return nil, err
}

return m.files, nil
}

func loadTasks(proj project.Project, w io.Writer, r io.Reader, filter bool) (project.CodeBlocks, error) {
m, err := runTasksModel(proj, false, w, r)
if err != nil {
return nil, err
}

tasks := m.tasks

if filter {
tasks = project.FilterCodeBlocks[project.CodeBlock](m.tasks, fAllowUnknown, fAllowUnnamed)

if len(tasks) == 0 {
// try again without filtering unnamed
tasks = project.FilterCodeBlocks[project.CodeBlock](m.tasks, fAllowUnknown, true)
}
}

return tasks, nil
}

func runTasksModel(proj project.Project, filesOnly bool, w io.Writer, r io.Reader) (*loadTasksModel, error) {
channel := make(chan interface{})
go proj.LoadTasks(filesOnly, channel)

nextTaskMsg := func() tea.Msg {
msg, ok := <-channel

if !ok {
return loadTaskFinished{}
}

return msg
}

m := newLoadTasksModel(nextTaskMsg)

resultModel := m

if isTerminal(os.Stdout.Fd()) {
p := tea.NewProgram(m, tea.WithOutput(w), tea.WithInput(r))
result, err := p.Run()
if err != nil {
return nil, err
}

resultModel = result.(loadTasksModel)
} else {
if strings.ToLower(os.Getenv("RUNME_VERBOSE")) != "true" {
w = io.Discard
}

_, _ = fmt.Fprintln(w, "Initializing...")

outer:
for {
if resultModel.err != nil {
break
}

switch msg := nextTaskMsg().(type) {
case loadTaskFinished:
_, _ = fmt.Fprintln(w, "")
break outer
case project.LoadTaskStatusSearchingFiles:
_, _ = fmt.Fprintln(w, "Searching for files...")
case project.LoadTaskStatusParsingFiles:
_, _ = fmt.Fprintln(w, "Parsing files...")
default:
if newModel, ok := resultModel.TaskUpdate(msg).(loadTasksModel); ok {
resultModel = newModel
}
}
}
}

if resultModel.err != nil {
return nil, resultModel.err
}

return &resultModel, nil
}

func (m loadTasksModel) Init() tea.Cmd {
return tea.Batch(
func() tea.Msg {
return m.spinner.Tick()
},
m.nextTaskMsg,
)
}

func (m loadTasksModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.err != nil {
return m, tea.Quit
}

switch msg := msg.(type) {
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
case loadTaskFinished:
m.clear = true
return m, tea.Quit
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "crtl+d":
m.err = errors.New("aborted")
return m, tea.Quit
}
}

if m, ok := m.TaskUpdate(msg).(loadTasksModel); ok {
return m, m.nextTaskMsg
}

return m, nil
}

func (m loadTasksModel) TaskUpdate(msg tea.Msg) tea.Model {
switch msg := msg.(type) {

case project.LoadTaskError:
m.err = msg.Err

// status
case project.LoadTaskStatusSearchingFiles:
m.filename = ""
m.status = "Searching for files..."
case project.LoadTaskStatusParsingFiles:
m.filename = ""
m.status = "Parsing files..."

// filename
case project.LoadTaskSearchingFolder:
m.filename = msg.Folder
case project.LoadTaskParsingFile:
m.filename = msg.Filename

// results
case project.LoadTaskFoundFile:
m.files = append(m.files, msg.Filename)
case project.LoadTaskFoundTask:
m.tasks = append(m.tasks, msg.Task)

default:
return nil
}

return m
}

func (m loadTasksModel) View() (s string) {
if m.clear {
return
}

s += m.spinner.View()
s += " "

s += m.status

if m.filename != "" {
s += fmt.Sprintf(" (%s)", m.filename)
}

return
}
9 changes: 7 additions & 2 deletions internal/cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/stateful/runme/internal/project"
"github.com/stateful/runme/pkg/project"
)

func fmtCmd() *cobra.Command {
Expand Down Expand Up @@ -37,7 +37,12 @@ func fmtCmd() *cobra.Command {
files := args

if len(files) == 0 {
projectFiles, err := loadFiles(proj, cmd.OutOrStdout(), cmd.InOrStdin())
loader, err := newProjectLoader(cmd)
if err != nil {
return err
}

projectFiles, err := loader.LoadFiles(proj)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/cli/go-gh/pkg/tableprinter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/stateful/runme/internal/project"
"github.com/stateful/runme/internal/shell"
"github.com/stateful/runme/pkg/project"
)

type row struct {
Expand Down Expand Up @@ -43,7 +43,12 @@ func listCmd() *cobra.Command {
return err
}

allBlocks, err := loadTasks(proj, cmd.OutOrStdout(), cmd.InOrStdin(), true)
loader, err := newProjectLoader(cmd)
if err != nil {
return err
}

allBlocks, err := loader.LoadTasks(proj, fAllowUnknown, fAllowUnnamed, true)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions internal/cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/stateful/runme/internal/project"
"github.com/stateful/runme/pkg/project"
)

func printCmd() *cobra.Command {
Expand All @@ -22,7 +22,12 @@ func printCmd() *cobra.Command {
}

generateBlocks:
blocks, err := loadTasks(proj, cmd.OutOrStdout(), cmd.InOrStdin(), true)
loader, err := newProjectLoader(cmd)
if err != nil {
return err
}

blocks, err := loader.LoadTasks(proj, fAllowUnknown, fAllowUnnamed, true)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"github.com/rwtodd/Go.Sed/sed"
"github.com/spf13/cobra"
"github.com/stateful/runme/internal/document"
"github.com/stateful/runme/internal/project"
"github.com/stateful/runme/internal/runner/client"
"github.com/stateful/runme/internal/tui"
"github.com/stateful/runme/pkg/project"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -75,7 +75,12 @@ func runCmd() *cobra.Command {

{
searchBlocks:
blocks, err := loadTasks(proj, cmd.OutOrStdout(), cmd.InOrStdin(), true)
loader, err := newProjectLoader(cmd)
if err != nil {
return err
}

blocks, err := loader.LoadTasks(proj, fAllowUnknown, fAllowUnnamed, true)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions internal/cmd/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/stateful/runme/internal/project"
"github.com/stateful/runme/internal/tasks"
"github.com/stateful/runme/pkg/project"
)

func tasksCmd() *cobra.Command {
Expand All @@ -22,7 +22,12 @@ func tasksCmd() *cobra.Command {
}

generateBlocks:
blocks, err := loadTasks(proj, cmd.OutOrStdout(), cmd.InOrStdin(), true)
loader, err := newProjectLoader(cmd)
if err != nil {
return err
}

blocks, err := loader.LoadTasks(proj, fAllowUnknown, fAllowUnnamed, true)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions internal/cmd/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/mgutz/ansi"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/stateful/runme/internal/project"
"github.com/stateful/runme/internal/runner"
"github.com/stateful/runme/internal/runner/client"
"github.com/stateful/runme/internal/version"
"github.com/stateful/runme/pkg/project"
"golang.org/x/exp/constraints"
)

Expand All @@ -37,7 +37,12 @@ func tuiCmd() *cobra.Command {
return err
}

blocks, err := loadTasks(proj, cmd.OutOrStdout(), cmd.InOrStdin(), false)
loader, err := newProjectLoader(cmd)
if err != nil {
return err
}

blocks, err := loader.LoadTasks(proj, fAllowUnknown, fAllowUnnamed, false)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit a96ffa4

Please sign in to comment.