diff --git a/src/config/segment_types.go b/src/config/segment_types.go index 782b6671e5ef..89bd6473f085 100644 --- a/src/config/segment_types.go +++ b/src/config/segment_types.go @@ -133,6 +133,7 @@ func init() { gob.Register(&segments.Todoist{}) gob.Register(&segments.UI5Tooling{}) gob.Register(&segments.Umbraco{}) + gob.Register(&segments.Uno{}) gob.Register(&segments.Unity{}) gob.Register(&segments.Upgrade{}) gob.Register(&segments.UpgradeCache{}) @@ -349,6 +350,8 @@ const ( UI5TOOLING SegmentType = "ui5tooling" // UMBRACO writes the Umbraco version if Umbraco is present UMBRACO SegmentType = "umbraco" + // UNO writes the Uno.Sdk version from global.json + UNO SegmentType = "uno" // UNITY writes which Unity version is currently active UNITY SegmentType = "unity" // UPGRADE lets you know if you can upgrade Oh My Posh @@ -473,6 +476,7 @@ var Segments = map[SegmentType]func() SegmentWriter{ TODOIST: func() SegmentWriter { return &segments.Todoist{} }, UI5TOOLING: func() SegmentWriter { return &segments.UI5Tooling{} }, UMBRACO: func() SegmentWriter { return &segments.Umbraco{} }, + UNO: func() SegmentWriter { return &segments.Uno{} }, UNITY: func() SegmentWriter { return &segments.Unity{} }, UPGRADE: func() SegmentWriter { return &segments.Upgrade{} }, V: func() SegmentWriter { return &segments.V{} }, diff --git a/src/segments/uno.go b/src/segments/uno.go new file mode 100644 index 000000000000..8752841487c6 --- /dev/null +++ b/src/segments/uno.go @@ -0,0 +1,39 @@ +package segments + +import ( + "encoding/json" + + "github.com/gookit/goutil/jsonutil" +) + +type unoGlobalJSON struct { + MSBuildSDKs map[string]string `json:"msbuild-sdks"` +} + +type Uno struct { + Base + + Version string +} + +func (u *Uno) Template() string { + return " {{ .Version }} " +} + +func (u *Uno) Enabled() bool { + file, err := u.env.HasParentFilePath("global.json", false) + if err != nil { + return false + } + + content := jsonutil.StripComments(u.env.FileContent(file.Path)) + + var globalJSON unoGlobalJSON + err = json.Unmarshal([]byte(content), &globalJSON) + if err != nil { + return false + } + + u.Version = globalJSON.MSBuildSDKs["Uno.Sdk"] + return len(u.Version) > 0 +} diff --git a/src/segments/uno_test.go b/src/segments/uno_test.go new file mode 100644 index 000000000000..959507b87ea4 --- /dev/null +++ b/src/segments/uno_test.go @@ -0,0 +1,85 @@ +package segments + +import ( + "errors" + "testing" + + "github.com/jandedobbeleer/oh-my-posh/src/runtime" + "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" + "github.com/jandedobbeleer/oh-my-posh/src/segments/options" + "github.com/stretchr/testify/assert" +) + +func TestUnoSegment(t *testing.T) { + cases := []struct { + Case string + GlobalJSON string + ExpectedVersion string + ExpectedEnabled bool + HasGlobalJSON bool + }{ + { + Case: "no global.json found", + ExpectedEnabled: false, + }, + { + Case: "invalid global.json", + HasGlobalJSON: true, + GlobalJSON: `invalid json`, + ExpectedEnabled: false, + }, + { + Case: "global.json without Uno.Sdk", + HasGlobalJSON: true, + GlobalJSON: `{"msbuild-sdks":{"Another.Sdk":"1.0.0"}}`, + ExpectedEnabled: false, + }, + { + Case: "valid Uno.Sdk in global.json", + HasGlobalJSON: true, + GlobalJSON: `{"msbuild-sdks":{"Uno.Sdk":"6.5.31"}}`, + ExpectedVersion: "6.5.31", + ExpectedEnabled: true, + }, + { + Case: "valid dotnet sdk global.json without Uno.Sdk", + HasGlobalJSON: true, + GlobalJSON: `{"sdk":{ "version": "10.0.103" }}`, + ExpectedEnabled: false, + }, + { + Case: "valid Uno.Sdk in commented global.json", + HasGlobalJSON: true, + GlobalJSON: `{ + // Uno SDK version + "msbuild-sdks": { + "Uno.Sdk": "6.5.31" + } +}`, + ExpectedVersion: "6.5.31", + ExpectedEnabled: true, + }, + } + + for _, tc := range cases { + env := new(mock.Environment) + + if tc.HasGlobalJSON { + file := &runtime.FileInfo{Path: "/test/global.json"} + env.On("HasParentFilePath", "global.json", false).Return(file, nil) + env.On("FileContent", "/test/global.json").Return(tc.GlobalJSON) + } else { + env.On("HasParentFilePath", "global.json", false).Return(&runtime.FileInfo{}, errors.New("file not found")) + } + + uno := &Uno{} + uno.Init(options.Map{}, env) + + assert.Equal(t, tc.ExpectedEnabled, uno.Enabled(), tc.Case) + assert.Equal(t, tc.ExpectedVersion, uno.Version, tc.Case) + + if tc.ExpectedEnabled { + assert.Equal(t, tc.ExpectedVersion, renderTemplate(env, "{{ .Version }}", uno), tc.Case) + } + } +} diff --git a/themes/schema.json b/themes/schema.json index e4cc3b1740b0..53dea801a027 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -460,6 +460,7 @@ "todoist", "ui5tooling", "umbraco", + "uno", "unity", "upgrade", "v", @@ -4547,6 +4548,19 @@ "description": "https://ohmyposh.dev/docs/segments/cli/umbraco" } }, + { + "if": { + "properties": { + "type": { + "const": "uno" + } + } + }, + "then": { + "title": "Uno Platform Segment", + "description": "https://ohmyposh.dev/docs/segments/cli/uno" + } + }, { "if": { "properties": { diff --git a/website/docs/segments/cli/uno.mdx b/website/docs/segments/cli/uno.mdx new file mode 100644 index 000000000000..531ddb010890 --- /dev/null +++ b/website/docs/segments/cli/uno.mdx @@ -0,0 +1,43 @@ +--- +id: uno +title: Uno Platform +sidebar_label: Uno Platform +--- + +## What + +Display the currently active [Uno Platform][uno] SDK version. + +## Sample Configuration + +import Config from "@site/src/components/Config.js"; + + + +## Template ([info][templates]) + +:::note default template + +```template + {{ .Version }} +``` + +::: + +### Properties + +| Name | Type | Description | +| ---------- | -------- | ------------------------- | +| `.Version` | `string` | the Uno Platform SDK version | + +[uno]: https://platform.uno/ +[templates]: /docs/configuration/templates diff --git a/website/sidebars.js b/website/sidebars.js index 9d29a0ac3ca0..0ce3c459b7c8 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -83,6 +83,7 @@ export default { "segments/cli/terraform", "segments/cli/ui5tooling", "segments/cli/umbraco", + "segments/cli/uno", "segments/cli/unity", "segments/cli/xmake", "segments/cli/yarn",