Skip to content

Commit

Permalink
feat: release notes access for all languages
Browse files Browse the repository at this point in the history
If version enabled and hyperlink enabled(global level), an hyperlink to
the release notes of the current version is generated.
The regex and the url template can be modified in the json template.
  • Loading branch information
lnu committed Jan 16, 2021
1 parent 9f022d7 commit 7a3cf32
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/docs/segment-dotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ Display the currently active .NET SDK version.
- missing_command_text: `string` - text to display when the command is missing - defaults to empty
- 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)
- version_regex: `string` - the regex used to parse the version - defaults to `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?:\d{2})(?P<patch>[0-9]{1}))))`
- url_template: `string` - the template used to generate the url to the version - defaults to `[%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)`
2 changes: 2 additions & 0 deletions docs/docs/segment-golang.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ Display the currently active golang version when a folder contains `.go` files.
- display_mode: `string` - determines when the segment is displayed
- `always`: The segment is always displayed
- `files`: The segment is only displayed when `*.go` or `go.mod` files are present (default)
- version_regex: `string` - the regex used to parse the version - defaults to `(?:go(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`
- url_template: `string` - the template used to generate the url to the version - defaults to `[%s](https://golang.org/doc/go%s.%s)`
3 changes: 3 additions & 0 deletions docs/docs/segment-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ Display the currently active node version when a folder contains `.js` or `.ts`
- display_mode: `string` - determines when the segment is displayed
- `always`: The segment is always displayed
- `files`: The segment is only displayed when `*.js`, `*.ts` or package.json files are present (default)
- version_regex: `string` - the regex used to parse the version - defaults to `(?:v(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`
- url_template: `string` - the template used to generate the url to the version - defaults to
`[%[1]s](https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V%[2]s.md#%[1]s)`
2 changes: 2 additions & 0 deletions docs/docs/segment-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ Supports conda, virtualenv and pyenv.
- `files`: The segment is only displayed when `*.py` or `*.ipynb` files are present (default)
- `environment`: The segment is only displayed when a virtual env is present
- `context`: The segment is only displayed when either `environment` or `files` is active
- version_regex: `string` - the regex used to parse the version - defaults to `(?:Python (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`
- url_template: `string` - the template used to generate the url to the version - defaults to `[%s](https://www.python.org/downloads/release/python-%s%s%s/)`
3 changes: 2 additions & 1 deletion src/segment_dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func (d *dotnet) init(props *properties, env environmentInfo) {
commands: []string{"dotnet"},
versionParam: "--version",
extensions: []string{"*.cs", "*.vb", "*.sln", "*.csproj", "*.vbproj"},
versionRegex: `(?P<version>[0-9]+.[0-9]+.[0-9]+)`,
versionRegex: props.getString(Property(VersionRegex), `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?:\d{2})(?P<patch>[0-9]{1}))))`),
urlTemplate: props.getString(Property(URLTemplate), "[%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)"),
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/segment_golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func (g *golang) init(props *properties, env environmentInfo) {
commands: []string{"go"},
versionParam: "version",
extensions: []string{"*.go", "go.mod"},
versionRegex: `go(?P<version>[0-9]+.[0-9]+.[0-9]+)`,
versionRegex: props.getString(Property(VersionRegex), `(?:go(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`),
urlTemplate: props.getString(Property(URLTemplate), "[%s](https://golang.org/doc/go%s.%s)"),
}
}

Expand Down
44 changes: 43 additions & 1 deletion src/segment_language.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package main

import (
"errors"
"fmt"
"strings"
)

type loadContext func()

type inContext func() bool
Expand All @@ -13,9 +19,13 @@ type language struct {
versionParam string
versionRegex string
version string
major string
minor string
patch string
exitCode int
loadContext loadContext
inContext inContext
urlTemplate string
}

const (
Expand All @@ -33,6 +43,10 @@ const (
MissingCommandTextProperty Property = "missing_command_text"
// MissingCommandText displays empty string by default
MissingCommandText string = ""
// VersionRegex sets the regex used to detect the language version
VersionRegex string = "version_regex"
// URLTemplate sets the url template used to generate the hyperlink
URLTemplate string = "url_template"
)

func (l *language) string() string {
Expand All @@ -43,6 +57,20 @@ func (l *language) string() string {
return l.props.getString(MissingCommandTextProperty, MissingCommandText)
}
l.setVersion()

// if regex found no version
if l.version == "" {
return ""
}

// build release notes hyperlink
if l.props.getBool(EnableHyperlink, false) {
version, err := TruncatingSprintf(l.urlTemplate, l.version, l.major, l.minor, l.patch)
if err != nil {
return l.version
}
return version
}
return l.version
}

Expand Down Expand Up @@ -84,9 +112,12 @@ func (l *language) setVersion() {
l.exitCode = exitErr.exitCode
return
}

values := findNamedRegexMatch(l.versionRegex, versionInfo)
l.exitCode = 0
l.version = values["version"]
l.major = values["major"]
l.minor = values["minor"]
l.patch = values["patch"]
}

// hasCommand checks if one of the commands exists and sets it as executable
Expand Down Expand Up @@ -116,3 +147,14 @@ func (l *language) inLanguageContext() bool {
}
return l.inContext()
}

func TruncatingSprintf(str string, args ...interface{}) (string, error) {
n := strings.Count(str, "%s")
if n > len(args) {
return "", errors.New("Too many parameters")
}
if n == 0 {
return fmt.Sprintf(str, args...), nil
}
return fmt.Sprintf(str, args[:n]...), nil
}
82 changes: 79 additions & 3 deletions src/segment_language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

const (
universion = "1.3.3.7"
universion = "1.3.307"
uni = "*.uni"
corn = "*.corn"
)
Expand All @@ -23,6 +23,8 @@ type languageArgs struct {
versionParam string
versionRegex string
missingCommandText string
urlTemplate string
enableHyperlink bool
}

func (l *languageArgs) hasvalue(value string, list []string) bool {
Expand All @@ -45,8 +47,9 @@ func bootStrapLanguageTest(args *languageArgs) *language {
}
props := &properties{
values: map[Property]interface{}{
DisplayVersion: args.displayVersion,
DisplayMode: args.displayMode,
DisplayVersion: args.displayVersion,
DisplayMode: args.displayMode,
EnableHyperlink: args.enableHyperlink,
},
}
if args.missingCommandText != "" {
Expand All @@ -59,6 +62,7 @@ func bootStrapLanguageTest(args *languageArgs) *language {
commands: args.commands,
versionParam: args.versionParam,
versionRegex: args.versionRegex,
urlTemplate: args.urlTemplate,
}
return l
}
Expand Down Expand Up @@ -222,3 +226,75 @@ func TestLanguageEnabledMissingCommandCustomText(t *testing.T) {
assert.True(t, lang.enabled())
assert.Equal(t, args.missingCommandText, lang.string(), "unicorn is available and uni and corn files are found")
}

func TestLanguageHyperlinkEnabled(t *testing.T) {
args := &languageArgs{
versionParam: "--version",
commands: []string{"uni", "corn"},
enabledCommands: []string{"corn"},
extensions: []string{uni, corn},
versionRegex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
urlTemplate: "[%s](https://unicor.org/doc/%s.%s.%s)",
version: universion,
enabledExtensions: []string{corn},
displayVersion: true,
enableHyperlink: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, "[1.3.307](https://unicor.org/doc/1.3.307)", lang.string())
}

func TestLanguageHyperlinkEnabledWrongRegex(t *testing.T) {
args := &languageArgs{
versionParam: "--version",
commands: []string{"uni", "corn"},
enabledCommands: []string{"corn"},
extensions: []string{uni, corn},
versionRegex: `wrong`,
urlTemplate: "[%s](https://unicor.org/doc/%s.%s.%s)",
version: universion,
enabledExtensions: []string{corn},
displayVersion: true,
enableHyperlink: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, "", lang.string())
}

func TestLanguageHyperlinkEnabledLessParamInTemplate(t *testing.T) {
args := &languageArgs{
versionParam: "--version",
commands: []string{"uni", "corn"},
enabledCommands: []string{"corn"},
extensions: []string{uni, corn},
versionRegex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
urlTemplate: "[%s](https://unicor.org/doc/%s)",
version: universion,
enabledExtensions: []string{corn},
displayVersion: true,
enableHyperlink: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, "[1.3.307](https://unicor.org/doc/1)", lang.string())
}

func TestLanguageHyperlinkEnabledMoreParamInTemplate(t *testing.T) {
args := &languageArgs{
versionParam: "--version",
commands: []string{"uni", "corn"},
enabledCommands: []string{"corn"},
extensions: []string{uni, corn},
versionRegex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
urlTemplate: "[%s](https://unicor.org/doc/%s.%s.%s.%s)",
version: universion,
enabledExtensions: []string{corn},
displayVersion: true,
enableHyperlink: true,
}
lang := bootStrapLanguageTest(args)
assert.True(t, lang.enabled())
assert.Equal(t, "1.3.307", lang.string())
}
3 changes: 2 additions & 1 deletion src/segment_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func (n *node) init(props *properties, env environmentInfo) {
commands: []string{"node"},
versionParam: "--version",
extensions: []string{"*.js", "*.ts", "package.json"},
versionRegex: `(?P<version>[0-9]+.[0-9]+.[0-9]+)`,
versionRegex: props.getString(Property(VersionRegex), `(?:v(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`),
urlTemplate: props.getString(Property(URLTemplate), "[%[1]s](https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V%[2]s.md#%[1]s)"),
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/segment_python.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ func (p *python) init(props *properties, env environmentInfo) {
commands: []string{"python", "python3"},
versionParam: "--version",
extensions: []string{"*.py", "*.ipynb", "pyproject.toml", "venv.bak", "venv", ".venv"},
versionRegex: `Python (?P<version>[0-9]+.[0-9]+.[0-9]+)`,
loadContext: p.loadContext,
inContext: p.inContext,
versionRegex: props.getString(Property(VersionRegex), `(?:Python (?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`),
urlTemplate: props.getString(Property(URLTemplate), "[%s](https://www.python.org/downloads/release/python-%s%s%s/)"),
}
}

Expand Down

0 comments on commit 7a3cf32

Please sign in to comment.