feat: nbgv segment

resolves #777
This commit is contained in:
Jan De Dobbeleer 2021-06-04 19:27:39 +02:00 committed by Jan De Dobbeleer
parent a4d1c770ae
commit 930f9de114
5 changed files with 186 additions and 0 deletions

View file

@ -0,0 +1,47 @@
---
id: nbgv
title: Nerdbank.GitVersioning
sidebar_label: Nbgv
---
## What
Display the [Nerdbank.GitVersioning][nbgv] version.
:::warning
The Nerdbank.GitVersioning CLI can be a bit slow causing the prompt to feel slow.
:::
## Sample Configuration
```json
{
"type": "nbgv",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#3a579a",
"properties": {
"prefix": " \uF1D2 "
}
}
```
## Properties
- template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
properties below. Defaults to `{{ .Version }}`
## Template Properties
- `.Version`: `string` - the current version
- `.AssemblyVersion`: `string` - the current assembly version
- `.AssemblyInformationalVersion`: `string` - the current assembly informational version
- `.NuGetPackageVersion`: `string` - the current nuget package version
- `.ChocolateyPackageVersion`: `string` - the current chocolatey package version
- `.NpmPackageVersion`: `string` - the current npm package version
- `.SimpleVersion`: `string` - the current simple version
[nbgv]: https://github.com/dotnet/Nerdbank.GitVersioning
[go-text-template]: https://golang.org/pkg/text/template/
[sprig]: https://masterminds.github.io/sprig/

View file

@ -44,6 +44,7 @@ module.exports = {
"java",
"julia",
"kubectl",
"nbgv",
"node",
"os",
"path",

View file

@ -116,6 +116,8 @@ const (
Crystal SegmentType = "crystal"
// Dart writes the active dart version
Dart SegmentType = "dart"
// Nbgv writes the nbgv version information
Nbgv SegmentType = "nbgv"
)
func (segment *Segment) string() string {
@ -245,6 +247,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
AZFunc: &azfunc{},
Crystal: &crystal{},
Dart: &dart{},
Nbgv: &nbgv{},
}
if writer, ok := functions[segment.Type]; ok {
props := &properties{

58
src/segment_nbgv.go Normal file
View file

@ -0,0 +1,58 @@
package main
import (
"encoding/json"
)
type nbgv struct {
props *properties
env environmentInfo
nbgv *versionInfo
}
type versionInfo struct {
VersionFileFound bool `json:"VersionFileFound"`
Version string `json:"Version"`
AssemblyVersion string `json:"AssemblyVersion"`
AssemblyInformationalVersion string `json:"AssemblyInformationalVersion"`
NuGetPackageVersion string `json:"NuGetPackageVersion"`
ChocolateyPackageVersion string `json:"ChocolateyPackageVersion"`
NpmPackageVersion string `json:"NpmPackageVersion"`
SimpleVersion string `json:"SimpleVersion"`
}
func (n *nbgv) enabled() bool {
nbgv := "nbgv"
if !n.env.hasCommand(nbgv) {
return false
}
response, err := n.env.runCommand(nbgv, "get-version", "--format=json")
if err != nil {
return false
}
n.nbgv = new(versionInfo)
err = json.Unmarshal([]byte(response), n.nbgv)
if err != nil {
return false
}
return n.nbgv.VersionFileFound
}
func (n *nbgv) string() string {
segmentTemplate := n.props.getString(SegmentTemplate, "{{ .Version }}")
template := &textTemplate{
Template: segmentTemplate,
Context: n.nbgv,
Env: n.env,
}
text, err := template.render()
if err != nil {
return err.Error()
}
return text
}
func (n *nbgv) init(props *properties, env environmentInfo) {
n.props = props
n.env = env
}

77
src/segment_nbgv_test.go Normal file
View file

@ -0,0 +1,77 @@
package main
import (
"errors"
"testing"
"github.com/alecthomas/assert"
)
func TestNbgv(t *testing.T) {
cases := []struct {
Case string
ExpectedEnabled bool
ExpectedString string
Response string
HasNbgv bool
SegmentTemplate string
Error error
}{
{Case: "nbgv not installed"},
{Case: "nbgv installed, no version file", HasNbgv: true, Response: "{ \"VersionFileFound\": false }"},
{Case: "nbgv installed with version file", ExpectedEnabled: true, HasNbgv: true, Response: "{ \"VersionFileFound\": true }"},
{
Case: "invalid template",
ExpectedEnabled: true,
ExpectedString: "invalid template text",
HasNbgv: true,
Response: "{ \"VersionFileFound\": true }",
SegmentTemplate: "{{ err }}",
},
{
Case: "command error",
HasNbgv: true,
Error: errors.New("oh noes"),
},
{
Case: "invalid json",
HasNbgv: true,
Response: "><<<>>>",
},
{
Case: "Version",
ExpectedEnabled: true,
ExpectedString: "bump",
HasNbgv: true,
Response: "{ \"VersionFileFound\": true, \"Version\": \"bump\" }",
SegmentTemplate: "{{ .Version }}",
},
{
Case: "AssemblyVersion",
ExpectedEnabled: true,
ExpectedString: "bump",
HasNbgv: true,
Response: "{ \"VersionFileFound\": true, \"AssemblyVersion\": \"bump\" }",
SegmentTemplate: "{{ .AssemblyVersion }}",
},
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("hasCommand", "nbgv").Return(tc.HasNbgv)
env.On("runCommand", "nbgv", []string{"get-version", "--format=json"}).Return(tc.Response, tc.Error)
nbgv := &nbgv{
env: env,
props: &properties{
values: map[Property]interface{}{
SegmentTemplate: tc.SegmentTemplate,
},
},
}
enabled := nbgv.enabled()
assert.Equal(t, tc.ExpectedEnabled, enabled, tc.Case)
if enabled {
assert.Equal(t, tc.ExpectedString, nbgv.string(), tc.Case)
}
}
}