Skip to content

Commit

Permalink
fix: align dotnet segment with other languages
Browse files Browse the repository at this point in the history
  • Loading branch information
lnu committed Dec 29, 2020
1 parent 4959bdb commit e9e7ffd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 50 deletions.
53 changes: 17 additions & 36 deletions src/segment_dotnet.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package main

import (
"errors"
)

type dotnet struct {
props *properties
env environmentInfo
activeVersion string
unsupportedVersion bool
language *language
}

const (
Expand All @@ -18,41 +11,29 @@ const (
)

func (d *dotnet) string() string {
if d.unsupportedVersion {
return d.props.getString(UnsupportedDotnetVersionIcon, "\u2327")
}
version := d.language.string()

if d.props.getBool(DisplayVersion, true) {
return d.activeVersion
// Exit code 145 is a special indicator that dotnet
// ran, but the current project config settings specify
// use of an SDK that isn't installed.
if d.language.exitCode == 145 {
return d.language.props.getString(UnsupportedDotnetVersionIcon, "\u2327")
}

return ""
return version
}

func (d *dotnet) init(props *properties, env environmentInfo) {
d.props = props
d.env = env
d.language = &language{
env: env,
props: props,
commands: []string{"dotnet"},
versionParam: "--version",
extensions: []string{"*.cs", "*.vb", "*.sln", "*.csproj", "*.vbproj"},
versionRegex: `(?P<version>[0-9]+.[0-9]+.[0-9]+)`,
}
}

func (d *dotnet) enabled() bool {
if !d.env.hasCommand("dotnet") {
return false
}

output, err := d.env.runCommand("dotnet", "--version")
if err == nil {
d.activeVersion = output
return true
}

// Exit code 145 is a special indicator that dotnet
// ran, but the current project config settings specify
// use of an SDK that isn't installed.
var exerr *commandError
if errors.As(err, &exerr) && exerr.exitCode == 145 {
d.unsupportedVersion = true
return true
}

return false
return d.language.enabled()
}
11 changes: 6 additions & 5 deletions src/segment_dotnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ func bootStrapDotnetTest(args *dotnetArgs) *dotnet {
} else {
env.On("runCommand", "dotnet", []string{"--version"}).Return(args.version, nil)
}

env.On("hasFiles", "*.cs").Return(true)
env.On("getPathSeperator", nil).Return("")
props := &properties{
values: map[Property]interface{}{
DisplayVersion: args.displayVersion,
UnsupportedDotnetVersionIcon: args.unsupportedIcon,
},
}
a := &dotnet{
env: env,
props: props,
}
return a
dotnet := &dotnet{}
dotnet.init(props, env)
return dotnet
}

func TestEnabledDotnetNotFound(t *testing.T) {
Expand Down
35 changes: 29 additions & 6 deletions src/segment_language.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package main

import "errors"

type language struct {
props *properties
env environmentInfo
extensions []string
commands []string
executable string
versionParam string
versionRegex string
version string
exitCode int
}

const (
Expand All @@ -22,13 +26,19 @@ const (
)

func (l *language) string() string {
// call getVersion if displayVersion set in config
if l.props.getBool(DisplayVersion, true) && l.getVersion() {
return l.version
}
return ""
}

func (l *language) enabled() bool {
// check if one of the defined commands exists in the system
if !l.hasCommand() {
return false
}

displayMode := l.props.getString(DisplayModeProperty, DisplayModeContext)

switch displayMode {
Expand All @@ -43,6 +53,7 @@ func (l *language) enabled() bool {
}
}

// isInContext will return true at least one file matching the extensions is found
func (l *language) isInContext() bool {
for i, extension := range l.extensions {
if l.env.hasFiles(extension) {
Expand All @@ -56,20 +67,32 @@ func (l *language) isInContext() bool {
return true
}

// getVersion returns the version an exit code returned by the exexutable
func (l *language) getVersion() bool {
var executable string
versionInfo, err := l.env.runCommand(l.executable, l.versionParam)
var exerr *commandError
if err == nil {
values := findNamedRegexMatch(l.versionRegex, versionInfo)
l.exitCode = 0
l.version = values["version"]
} else {
errors.As(err, &exerr)
l.exitCode = exerr.exitCode
l.version = ""
}
return true
}

// hasCommand checks if one of the commands exists and set it as executablr
func (l *language) hasCommand() bool {
for i, command := range l.commands {
if l.env.hasCommand(command) {
executable = command
l.executable = command
break
}
if i == len(l.commands)-1 {
return false
}
}
versionInfo, _ := l.env.runCommand(executable, l.versionParam)
values := findNamedRegexMatch(l.versionRegex, versionInfo)
l.version = values["version"]

return true
}
5 changes: 2 additions & 3 deletions src/segment_language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func TestLanguageFilesFoundButNoCommandAndVersion(t *testing.T) {
displayVersion: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, "", lang.string())
assert.False(t, lang.enabled(), "unicorn is not available")
}

func TestLanguageFilesFoundButNoCommandAndNoVersion(t *testing.T) {
Expand All @@ -80,7 +79,7 @@ func TestLanguageFilesFoundButNoCommandAndNoVersion(t *testing.T) {
enabledExtensions: []string{uni},
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled(), "unicorn is not available")
assert.False(t, lang.enabled(), "unicorn is not available")
}

func TestLanguageDisabledNoFiles(t *testing.T) {
Expand Down

0 comments on commit e9e7ffd

Please sign in to comment.