-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
182 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
id: nbgv | ||
title: Nerdbank.GitVersioning | ||
sidebar_label: Nbgv | ||
--- | ||
|
||
## What | ||
|
||
Display the [Nerdbank.GitVersioning][nbgv] version. | ||
|
||
## 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/ |
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 |
---|---|---|
|
@@ -44,6 +44,7 @@ module.exports = { | |
"java", | ||
"julia", | ||
"kubectl", | ||
"nbgv", | ||
"node", | ||
"os", | ||
"path", | ||
|
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,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 | ||
} |
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,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) | ||
} | ||
} | ||
} |