feat(dotnet): template + semver regex

This commit is contained in:
Laurent Nullens 2021-11-07 19:55:22 +01:00 committed by GitHub
parent d04dff5538
commit 93afdca084
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 18 deletions

View file

@ -36,3 +36,18 @@ Display the currently active .NET SDK version.
or `*.fsproj` files are present (default) or `*.fsproj` files are present (default)
- unsupported_version_icon: `string` - text/icon that is displayed when the active .NET SDK version (e.g., one specified - unsupported_version_icon: `string` - text/icon that is displayed when the active .NET SDK version (e.g., one specified
by `global.json`) is not installed/supported - defaults to `\uf071` (X in a rectangle box) by `global.json`) is not installed/supported - defaults to `\uf071` (X in a rectangle box)
- template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
properties below. Defaults does nothing(backward compatibility).
- version_url_template: `string` - A go [text/template][go-text-template] template extended
with [sprig][sprig] utilizing the properties below. Defaults does nothing(backward compatibility).
### Template Properties
- `.Major`: `string` - is the major version
- `.Minor`: `string` - is the minor version
- `.Patch`: `string` - is the patch version
- `.Prerelease`: `string` - is the prerelease version
- `.BuildMetadata`: `string` - is the build metadata
[go-text-template]: https://golang.org/pkg/text/template/
[sprig]: https://masterminds.github.io/sprig/

View file

@ -29,6 +29,8 @@ const (
AlwaysEnabled Property = "always_enabled" AlwaysEnabled Property = "always_enabled"
// SegmentTemplate is the template to use to render the information // SegmentTemplate is the template to use to render the information
SegmentTemplate Property = "template" SegmentTemplate Property = "template"
// VersionURLTemplate is the template to use when building language segment hyperlink
VersionURLTemplate Property = "version_url_template"
// DisplayError to display when an error occurs or not // DisplayError to display when an error occurs or not
DisplayError Property = "display_error" DisplayError Property = "display_error"
// DisplayDefault hides or shows the default // DisplayDefault hides or shows the default

View file

@ -30,7 +30,8 @@ func (d *dotnet) init(props *properties, env environmentInfo) {
{ {
executable: "dotnet", executable: "dotnet",
args: []string{"--version"}, args: []string{"--version"},
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?:\d{2})(?P<patch>[0-9]{1}))))`, regex: `(?P<version>((?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)` +
`(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?))`,
}, },
}, },
versionURLTemplate: "[%1s](https://github.com/dotnet/core/blob/master/release-notes/%[2]s.%[3]s/%[2]s.%[3]s.%[4]s/%[2]s.%[3]s.%[4]s.md)", versionURLTemplate: "[%1s](https://github.com/dotnet/core/blob/master/release-notes/%[2]s.%[3]s/%[2]s.%[3]s.%[4]s/%[2]s.%[3]s.%[4]s.md)",

View file

@ -13,10 +13,12 @@ type inContext func() bool
type matchesVersionFile func() bool type matchesVersionFile func() bool
type version struct { type version struct {
full string Full string
major string Major string
minor string Minor string
patch string Patch string
Prerelease string
BuildMetadata string
} }
type cmd struct { type cmd struct {
@ -32,16 +34,18 @@ func (c *cmd) parse(versionInfo string) error {
return errors.New("cannot parse version string") return errors.New("cannot parse version string")
} }
c.version = &version{} c.version = &version{}
c.version.full = values["version"] c.version.Full = values["version"]
c.version.major = values["major"] c.version.Major = values["major"]
c.version.minor = values["minor"] c.version.Minor = values["minor"]
c.version.patch = values["patch"] c.version.Patch = values["patch"]
c.version.Prerelease = values["prerelease"]
c.version.BuildMetadata = values["buildmetadata"]
return nil return nil
} }
func (c *cmd) buildVersionURL(template string) string { func (c *cmd) buildVersionURL(text, template string) string {
if template == "" { if template == "" {
return c.version.full return text
} }
truncatingSprintf := func(str string, args ...interface{}) (string, error) { truncatingSprintf := func(str string, args ...interface{}) (string, error) {
n := strings.Count(str, "%s") n := strings.Count(str, "%s")
@ -53,9 +57,9 @@ func (c *cmd) buildVersionURL(template string) string {
} }
return fmt.Sprintf(str, args[:n]...), nil return fmt.Sprintf(str, args[:n]...), nil
} }
version, err := truncatingSprintf(template, c.version.full, c.version.major, c.version.minor, c.version.patch) version, err := truncatingSprintf(template, text, c.version.Major, c.version.Minor, c.version.Patch)
if err != nil { if err != nil {
return c.version.full return text
} }
return version return version
} }
@ -110,13 +114,40 @@ func (l *language) string() string {
return "" return ""
} }
if l.props.getBool(EnableHyperlink, false) { segmentTemplate := l.props.getString(SegmentTemplate, "{{.Full}}")
return l.activeCommand.buildVersionURL(l.versionURLTemplate) template := &textTemplate{
Template: segmentTemplate,
Context: l.activeCommand.version,
Env: l.env,
} }
text, err := template.render()
if err != nil {
return err.Error()
}
if l.props.getBool(EnableHyperlink, false) {
versionURLTemplate := l.props.getString(VersionURLTemplate, "")
// backward compatibility
if versionURLTemplate == "" {
text = l.activeCommand.buildVersionURL(text, l.versionURLTemplate)
} else {
template := &textTemplate{
Template: versionURLTemplate,
Context: l.activeCommand.version,
Env: l.env,
}
url, err := template.render()
if err != nil {
return err.Error()
}
text = url
}
}
if l.props.getBool(EnableVersionMismatch, false) { if l.props.getBool(EnableVersionMismatch, false) {
l.setVersionFileMismatch() l.setVersionFileMismatch()
} }
return l.activeCommand.version.full return text
} }
func (l *language) enabled() bool { func (l *language) enabled() bool {

View file

@ -61,5 +61,5 @@ func (n *node) matchesVersionFile() bool {
if len(fileVersion) == 0 { if len(fileVersion) == 0 {
return true return true
} }
return fileVersion == n.language.activeCommand.version.full return fileVersion == n.language.activeCommand.version.Full
} }

View file

@ -26,7 +26,7 @@ func TestNodeMatchesVersionFile(t *testing.T) {
env: env, env: env,
activeCommand: &cmd{ activeCommand: &cmd{
version: &version{ version: &version{
full: tc.Version, Full: tc.Version,
}, },
}, },
}, },