Skip to content

Commit

Permalink
add Julia module support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Steinke committed Aug 29, 2021
1 parent ccb0c1a commit 38fcc5b
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ bottles = [
"docker",
]

[julia]
modules =[
"LanguageServer",
"Pluto"
]

[python]
packages = [
"autopep8",
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func initConfig() {
taps := viper.GetStringSlice("homebrew.taps")
vscodeExt := viper.GetStringSlice("vscode.extensions")
npmPackages := viper.GetStringSlice("npm.packages")
juliaModules := viper.GetStringSlice("julia.modules")
var goModules []dotfiles.GoModule
viper.UnmarshalKey("go.modules", &goModules)
var symlinks []dotfiles.Link
Expand All @@ -99,5 +100,6 @@ func initConfig() {
dotfiles.AppStore{Apps: apps, Profile: appStoreProfile, Commander: execCommander},
dotfiles.SystemPreferences{Preferences: preferences, Commander: execCommander},
dotfiles.Symlink{Links: symlinks, Commander: execCommander},
dotfiles.Julia{Modules: juliaModules, Commander: execCommander},
}
}
70 changes: 70 additions & 0 deletions internal/julia.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dotfiles

import (
"fmt"
"strings"

"github.com/sirupsen/logrus"
)

var juliaExe = "julia"

// NPM holds the information for all needed NPM packages.
type Julia struct {
Modules []string
Commander Commander
}

// GetMissingPackages returns a list of packages which are configured but not installed.
func (b Julia) GetMissingPackages() ([]string, error) {
missingBottles := []string{}
stdout, err := b.Commander(juliaExe, "-e", "import Pkg;Pkg.status()")
if err != nil {
return nil, err
}
logrus.WithField("output", string(stdout)).Debug("julia stdout")
installedFormulae := strings.Split(strings.Trim(string(stdout), "\n"), "\n")[1:]
logrus.WithField("output", installedFormulae).Debug("julia installed formulae")
installedMap := map[string]bool{}
for _, p := range installedFormulae {
logrus.WithField("output", p).Error("julia installed formulae")
words := strings.Fields(p)
installedMap[words[1]] = true
}

for _, bottle := range b.Modules {
if ok := installedMap[bottle]; !ok {
missingBottles = append(missingBottles, bottle)
}
}

return missingBottles, nil
}

// InstallPackages takes a list of packages for installation.
func (b Julia) InstallPackages(packages []string) error {
if len(packages) == 0 {
logrus.Info("no julia modules to install")
return nil
}
logrus.Info("Installing julia modules:", packages)

pkgCommand := fmt.Sprintf("import Pkg;Pkg.add.([\"%s\"])", strings.Join(packages[:], "\",\""))

_, err := b.Commander(juliaExe, "-e", pkgCommand)
if err != nil {
logrus.Error("Failed installing julia modules:", err)
}
return nil
}

// UpdatePackages is currently not implemented.
func (b Julia) UpdatePackages() error {
logrus.Info("Upgrading julia modules")
_, err := b.Commander(juliaExe, "-e", "import Pkg;Pkg.update()")
if err != nil {
logrus.Error("Failed update npm packages:", err)
return err
}
return nil
}
62 changes: 62 additions & 0 deletions internal/julia_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package dotfiles

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetMissingJuliaModules(t *testing.T) {
commander := mockCommander{}
defer commander.AssertExpectations(t)
commander.ExpectOutput(
"julia",
[]string{"-e", "import Pkg;Pkg.status()"},
[]byte("Status `~/.julia/environments/v1.6/Project.toml`\n [2b0e0bc5] bar v4.1.0\n [c3e4b0f8] Pluto v0.15.1Pkg\n"),
nil,
)
b := Julia{
Modules: []string{"bar", "foo"},
Commander: commander.Output,
}
missingPackages, err := b.GetMissingPackages()
assert.NoError(t, err)
assert.Equal(t, []string{"foo"}, missingPackages)
}

func TestInstallingJuliaModules(t *testing.T) {
commander := mockCommander{}
defer commander.AssertExpectations(t)
commander.ExpectOutput(
"julia",
[]string{"-e", "import Pkg;Pkg.add.([\"bar\",\"foo\"])"},
nil,
nil,
)
b := Julia{
Commander: commander.Output,
}
err := b.InstallPackages([]string{"bar", "foo"})
assert.NoError(t, err)
}

func TestTryingToInstallJuliaModulesWithEmptyListDoesNotCallCode(t *testing.T) {
commander := mockCommander{}
defer commander.AssertExpectations(t)
b := Julia{
Commander: commander.Output,
}
err := b.InstallPackages([]string{})
assert.NoError(t, err)
}

func TestUpdatingJulaModules(t *testing.T) {
commander := mockCommander{}
defer commander.AssertExpectations(t)
commander.ExpectOutput("julia", []string{"-e", "import Pkg;Pkg.update()"}, nil, nil)
b := Julia{
Commander: commander.Output,
}
err := b.UpdatePackages()
assert.NoError(t, err)
}

0 comments on commit 38fcc5b

Please sign in to comment.