feat(segment): add aurelia

This commit is contained in:
Ernesto Cianciotta 2024-11-05 19:40:31 +01:00 committed by Jan De Dobbeleer
parent 1adf542a77
commit 5ec18f230f
18 changed files with 268 additions and 102 deletions

View file

@ -33,6 +33,8 @@ const (
ANGULAR SegmentType = "angular"
// ARGOCD writes the current argocd context
ARGOCD SegmentType = "argocd"
// AURELIA writes which aurelia version is currently referenced in package.json
AURELIA SegmentType = "aurelia"
// AWS writes the active aws context
AWS SegmentType = "aws"
// AZ writes the Azure subscription info we're currently in
@ -226,6 +228,7 @@ const (
var Segments = map[SegmentType]func() SegmentWriter{
ANGULAR: func() SegmentWriter { return &segments.Angular{} },
ARGOCD: func() SegmentWriter { return &segments.Argocd{} },
AURELIA: func() SegmentWriter { return &segments.Aurelia{} },
AWS: func() SegmentWriter { return &segments.Aws{} },
AZ: func() SegmentWriter { return &segments.Az{} },
AZD: func() SegmentWriter { return &segments.Azd{} },

View file

@ -26,6 +26,5 @@ func (a *Angular) Enabled() bool {
}
func (a *Angular) getVersion() (string, error) {
// tested by nx_test.go
return getNodePackageVersion(a.language.env, filepath.Join("@angular", "core"))
return a.nodePackageVersion(filepath.Join("@angular", "core"))
}

30
src/segments/aurelia.go Normal file
View file

@ -0,0 +1,30 @@
package segments
type Aurelia struct {
language
}
func (a *Aurelia) Template() string {
return languageTemplate
}
func (a *Aurelia) Enabled() bool {
a.extensions = []string{"package.json"}
a.commands = []*cmd{
{
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)(-(?P<prerelease>[a-z]+).(?P<buildmetadata>[0-9]+))?)))`,
getVersion: a.getVersion,
},
}
a.versionURLTemplate = "https://github.com/aurelia/aurelia/releases/tag/v{{ .Full }}"
if !a.hasNodePackage("aurelia") {
return false
}
return a.language.Enabled()
}
func (a *Aurelia) getVersion() (string, error) {
return a.nodePackageVersion("aurelia")
}

View file

@ -1,9 +1,5 @@
package segments
import (
"encoding/json"
)
type Cds struct {
language
HasDependency bool
@ -22,7 +18,6 @@ func (c *Cds) Enabled() bool {
regex: `@sap/cds: (?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
}
//TODO: is this necessary?
c.language.loadContext = c.loadContext
c.language.inContext = c.inContext
c.displayMode = c.props.GetString(DisplayMode, DisplayModeContext)
@ -31,29 +26,11 @@ func (c *Cds) Enabled() bool {
}
func (c *Cds) loadContext() {
if !c.language.env.HasFiles("package.json") {
if !c.hasNodePackage("@sap/cds") {
return
}
content := c.language.env.FileContent("package.json")
objmap := map[string]json.RawMessage{}
if err := json.Unmarshal([]byte(content), &objmap); err != nil {
return
}
dependencies := map[string]json.RawMessage{}
if err := json.Unmarshal(objmap["dependencies"], &dependencies); err != nil {
return
}
for d := range dependencies {
if d == "@sap/cds" {
c.HasDependency = true
break
}
}
c.HasDependency = true
}
func (c *Cds) inContext() bool {

View file

@ -72,12 +72,8 @@ func TestCdsSegment(t *testing.T) {
}
props[DisplayMode] = tc.DisplayMode
if len(tc.PackageJSON) != 0 {
env.On("HasFiles", "package.json").Return(true)
env.On("FileContent", "package.json").Return(tc.PackageJSON)
} else {
env.On("HasFiles", "package.json").Return(false)
}
env.On("HasFiles", "package.json").Return(len(tc.PackageJSON) != 0)
env.On("FileContent", "package.json").Return(tc.PackageJSON)
cds := &Cds{}
cds.Init(props, env)

View file

@ -307,3 +307,42 @@ func (l *language) buildVersionURL() {
l.version.URL = url
}
func (l *language) hasNodePackage(name string) bool {
packageJSON := l.env.FileContent("package.json")
var packageData map[string]interface{}
if err := json.Unmarshal([]byte(packageJSON), &packageData); err != nil {
return false
}
dependencies, ok := packageData["dependencies"].(map[string]interface{})
if !ok {
return false
}
if _, exists := dependencies[name]; !exists {
return false
}
return true
}
func (l *language) nodePackageVersion(name string) (string, error) {
folder := filepath.Join(l.env.Pwd(), "node_modules", name)
const fileName string = "package.json"
if !l.env.HasFilesInDir(folder, fileName) {
return "", fmt.Errorf("%s not found in %s", fileName, folder)
}
content := l.env.FileContent(filepath.Join(folder, fileName))
var data ProjectData
err := json.Unmarshal([]byte(content), &data)
if err != nil {
return "", err
}
return data.Version, nil
}

View file

@ -1,6 +1,7 @@
package segments
import (
"path/filepath"
"testing"
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
@ -559,3 +560,38 @@ func getMockedLanguageEnv(params *mockedLanguageParams) (*mock.Environment, prop
return env, props
}
func TestNodePackageVersion(t *testing.T) {
cases := []struct {
Case string
PackageJSON string
Version string
ShouldFail bool
NoFiles bool
}{
{Case: "14.1.5", Version: "14.1.5", PackageJSON: "{ \"name\": \"nx\",\"version\": \"14.1.5\"}"},
{Case: "14.0.0", Version: "14.0.0", PackageJSON: "{ \"name\": \"nx\",\"version\": \"14.0.0\"}"},
{Case: "no files", NoFiles: true, ShouldFail: true},
{Case: "bad data", ShouldFail: true, PackageJSON: "bad data"},
}
for _, tc := range cases {
var env = new(mock.Environment)
env.On("Pwd").Return("posh")
path := filepath.Join("posh", "node_modules", "nx")
env.On("HasFilesInDir", path, "package.json").Return(!tc.NoFiles)
env.On("FileContent", filepath.Join(path, "package.json")).Return(tc.PackageJSON)
a := &language{}
a.Init(properties.Map{}, env)
got, err := a.nodePackageVersion("nx")
if tc.ShouldFail {
assert.Error(t, err, tc.Case)
return
}
assert.Nil(t, err, tc.Case)
assert.Equal(t, tc.Version, got, tc.Case)
}
}

View file

@ -1,13 +1,5 @@
package segments
import (
"encoding/json"
"fmt"
"path/filepath"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
type Nx struct {
language
}
@ -30,20 +22,5 @@ func (a *Nx) Enabled() bool {
}
func (a *Nx) getVersion() (string, error) {
return getNodePackageVersion(a.language.env, "nx")
}
func getNodePackageVersion(env runtime.Environment, nodePackage string) (string, error) {
const fileName string = "package.json"
folder := filepath.Join(env.Pwd(), "node_modules", nodePackage)
if !env.HasFilesInDir(folder, fileName) {
return "", fmt.Errorf("%s not found in %s", fileName, folder)
}
content := env.FileContent(filepath.Join(folder, fileName))
var data ProjectData
err := json.Unmarshal([]byte(content), &data)
if err != nil {
return "", err
}
return data.Version, nil
return a.nodePackageVersion("nx")
}

View file

@ -1,43 +0,0 @@
package segments
import (
"path/filepath"
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
"github.com/stretchr/testify/assert"
)
func TestGetNodePackageVersion(t *testing.T) {
cases := []struct {
Case string
PackageJSON string
Version string
ShouldFail bool
NoFiles bool
}{
{Case: "14.1.5", Version: "14.1.5", PackageJSON: "{ \"name\": \"nx\",\"version\": \"14.1.5\"}"},
{Case: "14.0.0", Version: "14.0.0", PackageJSON: "{ \"name\": \"nx\",\"version\": \"14.0.0\"}"},
{Case: "no files", NoFiles: true, ShouldFail: true},
{Case: "bad data", ShouldFail: true, PackageJSON: "bad data"},
}
for _, tc := range cases {
var env = new(mock.Environment)
env.On("Pwd").Return("posh")
path := filepath.Join("posh", "node_modules", "nx")
env.On("HasFilesInDir", path, "package.json").Return(!tc.NoFiles)
env.On("FileContent", filepath.Join(path, "package.json")).Return(tc.PackageJSON)
got, err := getNodePackageVersion(env, "nx")
if tc.ShouldFail {
assert.Error(t, err, tc.Case)
return
}
assert.Nil(t, err, tc.Case)
assert.Equal(t, tc.Version, got, tc.Case)
}
}

View file

@ -22,6 +22,5 @@ func (r *React) Enabled() bool {
}
func (r *React) getVersion() (string, error) {
// tested by nx_test.go
return getNodePackageVersion(r.language.env, "react")
return r.nodePackageVersion("react")
}

View file

@ -146,6 +146,15 @@
"trailing_diamond": "\ue0b4 ",
"type": "angular"
},
{
"background": "#ffffff",
"foreground": "#de1f84",
"leading_diamond": " \ue0b6",
"style": "diamond",
"template": "\u03b1 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}",
"trailing_diamond": "\ue0b4 ",
"type": "aurelia"
},
{
"background": "#1e293b",
"foreground": "#ffffff",

View file

@ -80,6 +80,12 @@
"template": "<#ffffff>(</>{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}<#ffffff>)</>",
"type": "angular"
},
{
"foreground": "#de1f84",
"style": "plain",
"template": "<#ffffff>(</>{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}<#ffffff>)</>",
"type": "aurelia"
},
{
"foreground": "#ffffff",
"style": "plain",

View file

@ -51,6 +51,14 @@ blocks:
properties:
fetch_version: true
template: " 󰚲 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} "
- type: aurelia
style: powerline
powerline_symbol:
background: purple
foreground: white
properties:
fetch_version: true
template: " α {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} "
- type: aws
style: powerline
powerline_symbol:
@ -252,7 +260,9 @@ palette:
cyan: "#80FFEA"
green: "#8AFF80"
magenta-purple: "#FF80BF"
purple: "#DE1F84"
red: "#FF9580"
white: "#FFFFFF"
white-cursorColor-foreground: "#F8F8F2"
yellow: "#FFCA80"
version: 2

View file

@ -69,6 +69,14 @@
"foreground": "#ffffff",
"template": " \uE753 {{ if .Error }}<#FE4A49>?</>{{ else }}{{ .Full }}{{ end }} "
},
{
"type": "aurelia",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"background": "#000000",
"foreground": "#ffffff",
"template": " \uE753 {{ if .Error }}<#DE1F84>?</>{{ else }}{{ .Full }}{{ end }} "
},
{
"type": "node",
"style": "powerline",

View file

@ -106,6 +106,15 @@
"trailing_diamond": "<transparent,#ffffff>\ue0b2</>",
"type": "angular"
},
{
"background": "#ffffff",
"foreground": "#de1f84",
"leading_diamond": "\ue0b2",
"style": "diamond",
"template": "\u03b1 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} ",
"trailing_diamond": "<transparent,#ffffff>\ue0b2</>",
"type": "aurelia"
},
{
"background": "#565656",
"foreground": "#faa029",

View file

@ -315,6 +315,7 @@
"azfunc",
"argocd",
"angular",
"aurelia",
"battery",
"bazel",
"brewfather",
@ -3297,6 +3298,54 @@
}
}
},
{
"if": {
"properties": {
"type": {
"const": "aurelia"
}
}
},
"then": {
"title": "Aurelia Segment",
"description": "https://ohmyposh.dev/docs/segments/cli/aurelia",
"properties": {
"properties": {
"properties": {
"home_enabled": {
"$ref": "#/definitions/home_enabled"
},
"fetch_version": {
"$ref": "#/definitions/fetch_version"
},
"missing_command_text": {
"$ref": "#/definitions/missing_command_text"
},
"display_mode": {
"$ref": "#/definitions/display_mode"
},
"version_url_template": {
"$ref": "#/definitions/version_url_template"
},
"extensions": {
"type": "array",
"title": "Extensions",
"description": "The extensions to look for when determining if the current directory is an Aurelia project",
"default": [
"package.json"
],
"items": {
"type": "string"
}
},
"folders": {
"$ref": "#/definitions/folders"
}
}
}
}
}
},
{
"if": {
"properties": {

View file

@ -0,0 +1,61 @@
---
id: aurelia
title: Aurelia
sidebar_label: Aurelia
---
## What
Display the currently active Aurelia version.
## Sample Configuration
import Config from "@site/src/components/Config.js";
<Config
data={{
type: "aurelia",
style: "powerline",
powerline_symbol: "\uE0B0",
foreground: "#ffffff",
background: "#de1f84",
template: " \u03b1 {{ .Full }} ",
}}
/>
## Properties
| Name | Type | Default | Description |
| ---------------------- | :--------: | :------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `home_enabled` | `boolean` | `false` | display the segment in the HOME folder or not |
| `fetch_version` | `boolean` | `true` | fetch the aurelia version |
| `missing_command_text` | `string` | | text to display when the command is missing |
| `display_mode` | `string` | `context` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when file `extensions` listed are present</li><li>`context`: displays the segment when the environment or files is active</li></ul> |
| `version_url_template` | `string` | | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes |
| `extensions` | `[]string` | `package.json` | allows to override the default list of file extensions to validate |
| `folders` | `[]string` | | allows to override the list of folder names to validate |
## Template ([info][templates])
:::note default template
```template
{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}
```
:::
### Properties
| Name | Type | Description |
| -------- | -------- | -------------------------------------------------- |
| `.Full` | `string` | the full version |
| `.Major` | `string` | major number |
| `.Minor` | `string` | minor number |
| `.Patch` | `string` | patch number |
| `.URL` | `string` | URL of the version info / release notes |
| `.Error` | `string` | error encountered when fetching the version string |
[go-text-template]: https://golang.org/pkg/text/template/
[templates]: /docs/configuration/templates
[aurelia]: https://docs.aurelia.io/

View file

@ -62,6 +62,7 @@ module.exports = {
items: [
"segments/cli/angular",
"segments/cli/argocd",
"segments/cli/aurelia",
"segments/cli/bazel",
"segments/cli/buf",
"segments/cli/bun",