diff --git a/src/config/segment_types.go b/src/config/segment_types.go index 218fccf4..6d4193b8 100644 --- a/src/config/segment_types.go +++ b/src/config/segment_types.go @@ -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{} }, diff --git a/src/segments/angular.go b/src/segments/angular.go index f7617a78..c56d709b 100644 --- a/src/segments/angular.go +++ b/src/segments/angular.go @@ -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")) } diff --git a/src/segments/aurelia.go b/src/segments/aurelia.go new file mode 100644 index 00000000..49063eb1 --- /dev/null +++ b/src/segments/aurelia.go @@ -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((?P[0-9]+).(?P[0-9]+).(?P[0-9]+)(-(?P[a-z]+).(?P[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") +} diff --git a/src/segments/cds.go b/src/segments/cds.go index afa3d243..df96b79a 100644 --- a/src/segments/cds.go +++ b/src/segments/cds.go @@ -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((?P[0-9]+).(?P[0-9]+).(?P[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 { diff --git a/src/segments/cds_test.go b/src/segments/cds_test.go index 035e35bc..8bc493c5 100644 --- a/src/segments/cds_test.go +++ b/src/segments/cds_test.go @@ -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) diff --git a/src/segments/language.go b/src/segments/language.go index 11327a91..e5b3855b 100644 --- a/src/segments/language.go +++ b/src/segments/language.go @@ -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 +} diff --git a/src/segments/language_test.go b/src/segments/language_test.go index 08e55f7d..b34b16e0 100644 --- a/src/segments/language_test.go +++ b/src/segments/language_test.go @@ -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) + } +} diff --git a/src/segments/nx.go b/src/segments/nx.go index 2e538cab..5c29f20f 100644 --- a/src/segments/nx.go +++ b/src/segments/nx.go @@ -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") } diff --git a/src/segments/nx_test.go b/src/segments/nx_test.go deleted file mode 100644 index 9ffb1845..00000000 --- a/src/segments/nx_test.go +++ /dev/null @@ -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) - } -} diff --git a/src/segments/react.go b/src/segments/react.go index bd40b99a..19f6ca04 100644 --- a/src/segments/react.go +++ b/src/segments/react.go @@ -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") } diff --git a/themes/atomic.omp.json b/themes/atomic.omp.json index 1c810631..118e6c25 100644 --- a/themes/atomic.omp.json +++ b/themes/atomic.omp.json @@ -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", diff --git a/themes/atomicBit.omp.json b/themes/atomicBit.omp.json index 893de3d2..73a5b768 100644 --- a/themes/atomicBit.omp.json +++ b/themes/atomicBit.omp.json @@ -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", diff --git a/themes/devious-diamonds.omp.yaml b/themes/devious-diamonds.omp.yaml index 33d4db11..714e2b5e 100644 --- a/themes/devious-diamonds.omp.yaml +++ b/themes/devious-diamonds.omp.yaml @@ -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 diff --git a/themes/froczh.omp.json b/themes/froczh.omp.json index 0c14d807..41ae9be8 100644 --- a/themes/froczh.omp.json +++ b/themes/froczh.omp.json @@ -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", diff --git a/themes/night-owl.omp.json b/themes/night-owl.omp.json index 6189b69d..722d4490 100644 --- a/themes/night-owl.omp.json +++ b/themes/night-owl.omp.json @@ -106,6 +106,15 @@ "trailing_diamond": "\ue0b2", "type": "angular" }, + { + "background": "#ffffff", + "foreground": "#de1f84", + "leading_diamond": "\ue0b2", + "style": "diamond", + "template": "\u03b1 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} ", + "trailing_diamond": "\ue0b2", + "type": "aurelia" + }, { "background": "#565656", "foreground": "#faa029", diff --git a/themes/schema.json b/themes/schema.json index 36b17016..11609d5c 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -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": { diff --git a/website/docs/segments/cli/aurelia.mdx b/website/docs/segments/cli/aurelia.mdx new file mode 100644 index 00000000..77637895 --- /dev/null +++ b/website/docs/segments/cli/aurelia.mdx @@ -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"; + + + +## 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` |
  • `always`: the segment is always displayed
  • `files`: the segment is only displayed when file `extensions` listed are present
  • `context`: displays the segment when the environment or files is active
| +| `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/ diff --git a/website/sidebars.js b/website/sidebars.js index 315994be..35557dde 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -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",