feat: use asdf to determine elixir version

In order to speed up version checking for the elixir segment, this change makes
it so that we first ask asdf for the elixir version, which is much faster.

If asdf is not installed or configured, the previous behavior of running
`elixir --version` will be used.

This also makes a minor fix to the elixir documentation, replacing 'flutter' with 'elixir'.
This commit is contained in:
Jen Spinney 2023-07-09 15:16:08 -07:00 committed by Jan De Dobbeleer
parent 69a95bfb38
commit cca3e053ad
3 changed files with 40 additions and 8 deletions

View file

@ -19,6 +19,11 @@ func (e *Elixir) Init(props properties.Properties, env platform.Environment) {
props: props, props: props,
extensions: []string{"*.ex", "*.exs"}, extensions: []string{"*.ex", "*.exs"},
commands: []*cmd{ commands: []*cmd{
{
executable: "asdf",
args: []string{"current", "elixir"},
regex: `elixir\s+(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))[^\s]*\s+`,
},
{ {
executable: "elixir", executable: "elixir",
args: []string{"--version"}, args: []string{"--version"},

View file

@ -4,29 +4,56 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestElixir(t *testing.T) { func TestElixir(t *testing.T) {
cases := []struct { cases := []struct {
Case string Case string
ExpectedString string ExpectedString string
Version string ElixirVersionOutput string
AsdfVersionOutput string
HasAsdf bool
AsdfExitCode int
}{ }{
{ {
Case: "elixir 1.14.2", Case: "Version without asdf",
ExpectedString: "1.14.2", ExpectedString: "1.14.2",
Version: "Erlang/OTP 25 [erts-13.1.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit] [dtrace]\n\nElixir 1.14.2 (compiled with Erlang/OTP 25)", ElixirVersionOutput: "Erlang/OTP 25 [erts-13.1.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit] [dtrace]\n\nElixir 1.14.2 (compiled with Erlang/OTP 25)",
},
{
Case: "Version with asdf",
ExpectedString: "1.14.2",
HasAsdf: true,
AsdfVersionOutput: "elixir 1.14.2-otp-25 /path/to/.tool-versions",
ElixirVersionOutput: "Should not be used",
},
{
Case: "Version with asdf not set: should fall back to elixir --version",
ExpectedString: "1.14.2",
HasAsdf: true,
AsdfVersionOutput: "elixir ______ No version is set. Run \"asdf <global|shell|local> elixir <version>\"",
AsdfExitCode: 126,
ElixirVersionOutput: "Erlang/OTP 25 [erts-13.1.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit] [dtrace]\n\nElixir 1.14.2 (compiled with Erlang/OTP 25)",
}, },
} }
for _, tc := range cases { for _, tc := range cases {
params := &mockedLanguageParams{ params := &mockedLanguageParams{
cmd: "elixir", cmd: "elixir",
versionParam: "--version", versionParam: "--version",
versionOutput: tc.Version, versionOutput: tc.ElixirVersionOutput,
extension: "*.ex", extension: "*.ex",
} }
env, props := getMockedLanguageEnv(params) env, props := getMockedLanguageEnv(params)
env.On("HasCommand", "asdf").Return(tc.HasAsdf)
var asdfErr error
if tc.AsdfExitCode != 0 {
asdfErr = &platform.CommandError{ExitCode: tc.AsdfExitCode}
}
env.On("RunCommand", "asdf", []string{"current", "elixir"}).Return(tc.AsdfVersionOutput, asdfErr)
r := &Elixir{} r := &Elixir{}
r.Init(props, env) r.Init(props, env)
assert.True(t, r.Enabled(), fmt.Sprintf("Failed in case: %s", tc.Case)) assert.True(t, r.Enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))

View file

@ -26,7 +26,7 @@ import Config from '@site/src/components/Config.js';
| Name | Type | Description | | Name | Type | Description |
| ---------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ---------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `home_enabled` | `boolean` | display the segment in the HOME folder or not - defaults to `false` | | `home_enabled` | `boolean` | display the segment in the HOME folder or not - defaults to `false` |
| `fetch_version` | `boolean` | fetch the flutter version - defaults to `true` | | `fetch_version` | `boolean` | fetch the elixir version - defaults to `true` |
| `missing_command_text` | `string` | text to display when the command is missing - defaults to empty | | `missing_command_text` | `string` | text to display when the command is missing - defaults to empty |
| `display_mode` | `string` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when `*.ex` or `*.exs` files are present (**default**)</li></ul> | | `display_mode` | `string` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when `*.ex` or `*.exs` files are present (**default**)</li></ul> |
| `version_url_template` | `string` | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes | | `version_url_template` | `string` | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes |