Skip to content

Commit

Permalink
fix: Handle includes module type like regular module
Browse files Browse the repository at this point in the history
Instead of processing includes modules during the recipe parsing, process them like any other module. This allows users to use the includes module as a nested module of a different module.
  • Loading branch information
axtloss committed Nov 15, 2024
1 parent 442c3f3 commit a383988
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 63 deletions.
52 changes: 51 additions & 1 deletion core/build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -321,6 +322,55 @@ func BuildModules(recipe *api.Recipe, modules []interface{}) ([]ModuleCommand, e
return cmds, nil
}

func buildIncludesModule(moduleInterface interface{}, recipe *api.Recipe) (string, error) {
var include IncludesModule
err := mapstructure.Decode(moduleInterface, &include)
if err != nil {
return "", err
}

if len(include.Includes) == 0 {
return "", errors.New("includes module must have at least one module to include")
}

var commands []string
for _, include := range include.Includes {
var modulePath string

// in case of a remote include, we need to download the
// recipe before including it
if include[:4] == "http" {
fmt.Printf("Downloading recipe from %s\n", include)
modulePath, err = downloadRecipe(include)
if err != nil {
return "", err
}
} else if followsGhPattern(include) {
// if the include follows the github pattern, we need to
// download the recipe from the github repository
fmt.Printf("Downloading recipe from %s\n", include)
modulePath, err = downloadGhRecipe(include)
if err != nil {
return "", err
}
} else {
modulePath = filepath.Join(recipe.ParentPath, include)
}

includeModule, err := GenModule(modulePath)
if err != nil {
return "", err
}

buildModule, err := BuildModule(recipe, includeModule)
if err != nil {
return "", err
}
commands = append(commands, buildModule...)
}
return strings.Join(commands, "\n"), nil
}

// Build a command string for the given module in the recipe
func BuildModule(recipe *api.Recipe, moduleInterface interface{}) ([]string, error) {
var module Module
Expand All @@ -345,7 +395,7 @@ func BuildModule(recipe *api.Recipe, moduleInterface interface{}) ([]string, err

moduleBuilders := map[string]func(interface{}, *api.Recipe) (string, error){
"shell": BuildShellModule,
"includes": func(interface{}, *api.Recipe) (string, error) { return "", nil },
"includes": buildIncludesModule,
}

if moduleBuilder, ok := moduleBuilders[module.Type]; ok {
Expand Down
62 changes: 0 additions & 62 deletions core/loader.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package core

import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/mitchellh/mapstructure"
"github.com/vanilla-os/vib/api"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -120,66 +118,6 @@ func LoadRecipe(path string) (*api.Recipe, error) {
}
}

// here we expand modules of type "includes"
var newRecipeModules []interface{}

for _, moduleInterface := range stage.Modules {

var module Module
err := mapstructure.Decode(moduleInterface, &module)
if err != nil {
return nil, err
}

if module.Type == "includes" {
var include IncludesModule
err := mapstructure.Decode(moduleInterface, &include)
if err != nil {
return nil, err
}

if len(include.Includes) == 0 {
return nil, errors.New("includes module must have at least one module to include")
}

for _, include := range include.Includes {
var modulePath string

// in case of a remote include, we need to download the
// recipe before including it
if include[:4] == "http" {
fmt.Printf("Downloading recipe from %s\n", include)
modulePath, err = downloadRecipe(include)
if err != nil {
return nil, err
}
} else if followsGhPattern(include) {
// if the include follows the github pattern, we need to
// download the recipe from the github repository
fmt.Printf("Downloading recipe from %s\n", include)
modulePath, err = downloadGhRecipe(include)
if err != nil {
return nil, err
}
} else {
modulePath = filepath.Join(recipe.ParentPath, include)
}

includeModule, err := GenModule(modulePath)
if err != nil {
return nil, err
}

newRecipeModules = append(newRecipeModules, includeModule)
}

continue
}

newRecipeModules = append(newRecipeModules, moduleInterface)
}

stage.Modules = newRecipeModules
recipe.Stages[i] = stage
}

Expand Down

0 comments on commit a383988

Please sign in to comment.