-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jan Steinke
committed
Aug 29, 2021
1 parent
ccb0c1a
commit 38fcc5b
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |