feat(language): parameter for version detection

applied to angular segment to read version from package.json
This commit is contained in:
lnu 2021-12-07 06:27:28 +00:00 committed by Jan De Dobbeleer
parent adb49d1cd0
commit d3c3dea1d7
4 changed files with 58 additions and 17 deletions

2
.vscode/tasks.json vendored
View file

@ -30,7 +30,7 @@
"type": "shell", "type": "shell",
"command": "go", "command": "go",
"label": "devcontainer: rebuild oh-my-posh", "label": "devcontainer: rebuild oh-my-posh",
"detail": "Build oh-my-posh for all shells when inside the devcaontainer", "detail": "Build oh-my-posh for all shells when inside the devcontainer",
"options": { "options": {
"cwd": "${workspaceRoot}/src", "cwd": "${workspaceRoot}/src",
"shell": { "shell": {

View file

@ -1,5 +1,10 @@
package main package main
import (
"encoding/json"
"fmt"
)
type angular struct { type angular struct {
language language
} }
@ -15,9 +20,28 @@ func (a *angular) init(props properties, env environmentInfo) {
extensions: []string{"angular.json"}, extensions: []string{"angular.json"},
commands: []*cmd{ commands: []*cmd{
{ {
executable: "ng", regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
args: []string{"--version"}, getVersion: func() (string, error) {
regex: `Angular CLI: (?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`, const fileName string = "package.json"
const fileFolder string = "/node_modules/@angular/core"
angularFilePath := a.language.env.getcwd() + fileFolder
if !a.language.env.hasFilesInDir(angularFilePath, fileName) {
return "", fmt.Errorf("%s not found in %s", fileName, angularFilePath)
}
// parse file
objmap := map[string]json.RawMessage{}
content := a.language.env.getFileContent(a.language.env.getcwd() + fileFolder + "/" + fileName)
err := json.Unmarshal([]byte(content), &objmap)
if err != nil {
return "", err
}
var str string
err = json.Unmarshal(objmap["version"], &str)
if err != nil {
return "", err
}
return str, nil
},
}, },
}, },
versionURLTemplate: "[%s](https://github.com/angular/angular/releases/tag/%s.%s.%s)", versionURLTemplate: "[%s](https://github.com/angular/angular/releases/tag/%s.%s.%s)",

View file

@ -13,18 +13,24 @@ func TestAngularCliVersionDisplayed(t *testing.T) {
ExpectedString string ExpectedString string
Version string Version string
}{ }{
{Case: "Angular 12.2.9", ExpectedString: "12.2.9", Version: "Angular CLI: 12.2.9"}, {Case: "Angular 13.0.3", ExpectedString: "13.0.3", Version: "{ \"name\": \"@angular/core\",\"version\": \"13.0.3\"}"},
{Case: "Angular 11.0.1", ExpectedString: "11.0.1", Version: "{ \"name\": \"@angular/core\",\"version\": \"11.0.1\"}"},
} }
for _, ta := range cases { for _, ta := range cases {
params := &mockedLanguageParams{ params := &mockedLanguageParams{
cmd: "ng",
versionParam: "--version",
versionOutput: ta.Version,
extension: "angular.json", extension: "angular.json",
} }
env, props := getMockedLanguageEnv(params) var env = new(MockedEnvironment)
// mock getVersion methods
env.On("getcwd", nil).Return("/usr/home/dev/my-app")
env.On("homeDir", nil).Return("/usr/home")
env.On("hasFiles", params.extension).Return(true)
env.On("hasFilesInDir", "/usr/home/dev/my-app/node_modules/@angular/core", "package.json").Return(true)
env.On("getFileContent", "/usr/home/dev/my-app/node_modules/@angular/core/package.json").Return(ta.Version)
var props properties = map[Property]interface{}{}
angular := &angular{} angular := &angular{}
angular.init(props, env) angular.init(props, env)
assert.True(t, angular.enabled(), fmt.Sprintf("Failed in case: %s", ta.Case)) assert.True(t, angular.enabled(), fmt.Sprintf("Failed in case: %s", ta.Case))

View file

@ -10,6 +10,7 @@ type loadContext func()
type inContext func() bool type inContext func() bool
type getVersion func() (string, error)
type matchesVersionFile func() bool type matchesVersionFile func() bool
type version struct { type version struct {
@ -25,6 +26,7 @@ type cmd struct {
executable string executable string
args []string args []string
regex string regex string
getVersion getVersion
} }
func (c *cmd) parse(versionInfo string) (*version, error) { func (c *cmd) parse(versionInfo string) (*version, error) {
@ -166,14 +168,23 @@ func (l *language) hasLanguageFiles() bool {
// setVersion parses the version string returned by the command // setVersion parses the version string returned by the command
func (l *language) setVersion() error { func (l *language) setVersion() error {
for _, command := range l.commands { for _, command := range l.commands {
var versionStr string
var err error
if command.getVersion == nil {
if !l.env.hasCommand(command.executable) { if !l.env.hasCommand(command.executable) {
continue continue
} }
versionStr, err := l.env.runCommand(command.executable, command.args...) versionStr, err = l.env.runCommand(command.executable, command.args...)
if exitErr, ok := err.(*commandError); ok { if exitErr, ok := err.(*commandError); ok {
l.exitCode = exitErr.exitCode l.exitCode = exitErr.exitCode
return fmt.Errorf("err executing %s with %s", command.executable, command.args) return fmt.Errorf("err executing %s with %s", command.executable, command.args)
} }
} else {
versionStr, err = command.getVersion()
if err != nil {
return err
}
}
if versionStr == "" { if versionStr == "" {
continue continue
} }