mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-21 02:55:37 -08:00
feat(language): parameter for version detection
applied to angular segment to read version from package.json
This commit is contained in:
parent
adb49d1cd0
commit
d3c3dea1d7
2
.vscode/tasks.json
vendored
2
.vscode/tasks.json
vendored
|
@ -30,7 +30,7 @@
|
|||
"type": "shell",
|
||||
"command": "go",
|
||||
"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": {
|
||||
"cwd": "${workspaceRoot}/src",
|
||||
"shell": {
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type angular struct {
|
||||
language
|
||||
}
|
||||
|
@ -15,9 +20,28 @@ func (a *angular) init(props properties, env environmentInfo) {
|
|||
extensions: []string{"angular.json"},
|
||||
commands: []*cmd{
|
||||
{
|
||||
executable: "ng",
|
||||
args: []string{"--version"},
|
||||
regex: `Angular CLI: (?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
|
||||
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
|
||||
getVersion: func() (string, error) {
|
||||
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)",
|
||||
|
|
|
@ -13,18 +13,24 @@ func TestAngularCliVersionDisplayed(t *testing.T) {
|
|||
ExpectedString 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 {
|
||||
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.init(props, env)
|
||||
assert.True(t, angular.enabled(), fmt.Sprintf("Failed in case: %s", ta.Case))
|
||||
|
|
|
@ -10,6 +10,7 @@ type loadContext func()
|
|||
|
||||
type inContext func() bool
|
||||
|
||||
type getVersion func() (string, error)
|
||||
type matchesVersionFile func() bool
|
||||
|
||||
type version struct {
|
||||
|
@ -25,6 +26,7 @@ type cmd struct {
|
|||
executable string
|
||||
args []string
|
||||
regex string
|
||||
getVersion getVersion
|
||||
}
|
||||
|
||||
func (c *cmd) parse(versionInfo string) (*version, error) {
|
||||
|
@ -166,13 +168,22 @@ func (l *language) hasLanguageFiles() bool {
|
|||
// setVersion parses the version string returned by the command
|
||||
func (l *language) setVersion() error {
|
||||
for _, command := range l.commands {
|
||||
if !l.env.hasCommand(command.executable) {
|
||||
continue
|
||||
}
|
||||
versionStr, err := l.env.runCommand(command.executable, command.args...)
|
||||
if exitErr, ok := err.(*commandError); ok {
|
||||
l.exitCode = exitErr.exitCode
|
||||
return fmt.Errorf("err executing %s with %s", command.executable, command.args)
|
||||
var versionStr string
|
||||
var err error
|
||||
if command.getVersion == nil {
|
||||
if !l.env.hasCommand(command.executable) {
|
||||
continue
|
||||
}
|
||||
versionStr, err = l.env.runCommand(command.executable, command.args...)
|
||||
if exitErr, ok := err.(*commandError); ok {
|
||||
l.exitCode = exitErr.exitCode
|
||||
return fmt.Errorf("err executing %s with %s", command.executable, command.args)
|
||||
}
|
||||
} else {
|
||||
versionStr, err = command.getVersion()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if versionStr == "" {
|
||||
continue
|
||||
|
|
Loading…
Reference in a new issue