Skip to content

Commit

Permalink
Attempt to disambiguate dependency installation Vs. downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLich committed Jan 2, 2022
1 parent 84895c8 commit 5b26dad
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 50 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func main() {
Usage: "Installs dependencies into the .regolith folder.",
Action: func(c *cli.Context) error {
initRegolith(debug)
return regolith.InstallDependencies(regolith.StringArrayContains(c.FlagNames(), "force"), regolith.StringArrayContains(c.FlagNames(), "update"))
return regolith.InstallFilters(regolith.StringArrayContains(c.FlagNames(), "force"), regolith.StringArrayContains(c.FlagNames(), "update"))
},
Flags: []cli.Flag{
&cli.BoolFlag{
Expand Down
8 changes: 4 additions & 4 deletions regolith/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

type filterDefinition struct {
filter func(filter Filter, settings map[string]interface{}, absoluteLocation string) error
install func(filter Filter, path string) error
check func() error
validateDefinition func(filter Filter) error
filter func(filter Filter, settings map[string]interface{}, absoluteLocation string) error
installDependencies func(filter Filter, path string) error
check func() error
validateDefinition func(filter Filter) error
}

var FilterTypes = map[string]filterDefinition{}
Expand Down
2 changes: 1 addition & 1 deletion regolith/java_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const javaFilterName = "java"
func RegisterJavaFilter(filters map[string]filterDefinition) {
filters[javaFilterName] = filterDefinition{
filter: runJavaFilter,
install: func(filter Filter, path string) error {
installDependencies: func(filter Filter, path string) error {
return nil
},
check: checkJavaRequirements,
Expand Down
6 changes: 3 additions & 3 deletions regolith/nim_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const nimFilterName = "nim"

func RegisterNimFilter(filters map[string]filterDefinition) {
filters[nimFilterName] = filterDefinition{
filter: runNimFilter,
install: installNimFilter,
check: checkNimRequirements,
filter: runNimFilter,
installDependencies: installNimFilter,
check: checkNimRequirements,
}
}

Expand Down
6 changes: 3 additions & 3 deletions regolith/nodejs_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const nodeJSFilterName = "nodejs"

func RegisterNodeJSFilter(filters map[string]filterDefinition) {
filters[nodeJSFilterName] = filterDefinition{
filter: runNodeJSFilter,
install: installNodeJSFilter,
check: checkNodeJSRequirements,
filter: runNodeJSFilter,
installDependencies: installNodeJSFilter,
check: checkNodeJSRequirements,
}
}

Expand Down
54 changes: 30 additions & 24 deletions regolith/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func LoadFilterJsonProfile(

// Installs all dependencies of the profile
func (profile *Profile) Install(isForced bool, profilePath string) error {
for filter := range profile.Filters {
filter := &profile.Filters[filter] // Using pointer is faster than creating copies in the loop and gives more options
for filterName, filter := range profile.Filters {
Logger.Infof(" - installing filter %s...", filterName)

downloadPath, err := filter.Download(isForced, profilePath)
// TODO - we could use type switch to handle different kinds of errors
Expand All @@ -131,6 +131,9 @@ func (profile *Profile) Install(isForced bool, profilePath string) error {
continue
}

// Install dependencies
err = filter.DownloadDependencies(isForced, downloadPath)

// Move filters 'data' folder contents into 'data'
filterName := filter.GetIdName()
localDataPath := path.Join(profile.DataPath, filterName)
Expand Down Expand Up @@ -167,7 +170,7 @@ func (profile *Profile) Install(isForced bool, profilePath string) error {

// Create profile from filter.json file
remoteProfile, err := LoadFilterJsonProfile(
filepath.Join(downloadPath, "filter.json"), *filter, *profile)
filepath.Join(downloadPath, "filter.json"), filter, *profile)
if err != nil {
return err // TODO - I don't think this should break the entire install. Just remove the files and continue.
}
Expand Down Expand Up @@ -241,29 +244,32 @@ func (filter *Filter) GetFriendlyName() string {
// owns the filter (the directory of either the config.json or filter.json
// file). The profileDir combined with Script property of the filter gives
// the absolute path to the script.
func (filter *Filter) Download(isForced bool, profileDirectory string) (string, error) {
url := filter.GetDownloadUrl()
if url == "" {
// Not a remote filter, download the dependencies
if filterDefinition, ok := FilterTypes[filter.RunWith]; ok {
scriptPath, err := filepath.Abs(filepath.Join(profileDirectory, filter.Script))
if err != nil {
return "", wrapError(fmt.Sprintf(
"Unable to resolve path of %s script",
filter.GetFriendlyName()), err)
}
err = filterDefinition.install(*filter, filepath.Dir(scriptPath))
if err != nil {
return "", wrapError(fmt.Sprintf(
"Couldn't install filter dependencies %s",
filter.GetFriendlyName()), err)
}
} else {
Logger.Warnf(
"Filter type '%s' not supported", filter.RunWith)

// Installs all dependencies of the filter
func (filter *Filter) DownloadDependencies(isForced bool, profileDirectory string) error {
if filterDefinition, ok := FilterTypes[filter.RunWith]; ok {
scriptPath, err := filepath.Abs(filepath.Join(profileDirectory, filter.Script))
if err != nil {
return wrapError(fmt.Sprintf(
"Unable to resolve path of %s script",
filter.GetFriendlyName()), err)
}
err = filterDefinition.installDependencies(*filter, filepath.Dir(scriptPath))
if err != nil {
return wrapError(fmt.Sprintf(
"Couldn't install filter dependencies %s",
filter.GetFriendlyName()), err)
}
return "", nil // The filter is not downloaded (just dependencies)
} else {
Logger.Warnf(
"Filter type '%s' not supported", filter.RunWith)
}
return nil
}

// Downloads the filter into its own directory and returns the download path.
func (filter *Filter) Download(isForced bool, profileDirectory string) (string, error) {
url := filter.GetDownloadUrl()

// Download the filter into the cache folder
downloadPath := UrlToPath(url)
Expand Down
6 changes: 3 additions & 3 deletions regolith/python_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const pythonFilterName = "python"

func RegisterPythonFilter(filters map[string]filterDefinition) {
filters[pythonFilterName] = filterDefinition{
filter: runPythonFilter,
install: installPythonFilter,
check: checkPythonRequirements,
filter: runPythonFilter,
installDependencies: installPythonFilter,
check: checkPythonRequirements,
}
}

Expand Down
11 changes: 5 additions & 6 deletions regolith/remote_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ func IsRemoteFilterCached(url string) bool {
// Recursively install dependencies for the entire config.
// - Force mode will overwrite existing dependencies.
// - Non-force mode will only install dependencies that are not already installed.
func InstallDependencies(isForced bool, updateFilters bool) error {
Logger.Infof("Installing dependencies...")

func InstallFilters(isForced bool, updateFilters bool) error {
project, err := LoadConfig()
if err != nil {
return wrapError("Failed to load project config", err)
Expand All @@ -49,13 +47,14 @@ func InstallDependencies(isForced bool, updateFilters bool) error {
if err != nil {
return wrapError("Could not get working directory", err)
}
for _, profile := range project.Profiles {
for profileName, profile := range project.Profiles {
Logger.Infof("Installing profile %s...", profileName)
err := profile.Install(isForced, wd)
if err != nil {
return wrapError("Could not install dependency", err)
return wrapError("Could not install profile!", err)
}
}

Logger.Infof("Dependencies installed.")
Logger.Infof("Profile installation complete.")
return nil
}
6 changes: 3 additions & 3 deletions regolith/shell_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ var shells = [][]string{{"powershell", "-command"}, {"cmd", "/k"}, {"bash", "-c"

func RegisterShellFilter(filters map[string]filterDefinition) {
filters[shellFilterName] = filterDefinition{
filter: runShellFilter,
install: func(filter Filter, path string) error { return nil },
check: checkShellRequirements,
filter: runShellFilter,
installDependencies: func(filter Filter, path string) error { return nil },
check: checkShellRequirements,
validateDefinition: func(filter Filter) error {
if filter.Command == "" {
return errors.New("Missing 'command' field in filter definition")
Expand Down
2 changes: 1 addition & 1 deletion test/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestLocalRequirementsInstallAndRun(t *testing.T) {
regolith.InitLogging(true)
regolith.RegisterFilters()
t.Log("Installing the dependencies")
if err := regolith.InstallDependencies(true, false); err != nil {
if err := regolith.InstallFilters(true, false); err != nil {
t.Fatal("'regolith install' failed:", err)
}
if err := regolith.Unlock(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/remote_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestDoubleRemoteFilter(t *testing.T) {
// Run InstallDependencies
regolith.InitLogging(true)
regolith.RegisterFilters()
regolith.InstallDependencies(false, false)
regolith.InstallFilters(false, false)
// Load created paths for comparison with expected output
createdPaths, err := listPaths(".", ".")
if err != nil {
Expand Down

0 comments on commit 5b26dad

Please sign in to comment.