mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-02 05:41:10 -08:00
feat(java): add segment
This commit is contained in:
parent
6798da704b
commit
c185ebd09c
45
docs/docs/segment-java.md
Normal file
45
docs/docs/segment-java.md
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
---
|
||||||
|
id: java
|
||||||
|
title: Java
|
||||||
|
sidebar_label: Java
|
||||||
|
---
|
||||||
|
|
||||||
|
## What
|
||||||
|
|
||||||
|
Display the currently active java version.
|
||||||
|
|
||||||
|
## Sample Configuration
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"style": "powerline",
|
||||||
|
"powerline_symbol": "\uE0B0",
|
||||||
|
"foreground": "#ffffff",
|
||||||
|
"background": "#4063D8",
|
||||||
|
"properties": {
|
||||||
|
"prefix": " \uE738 "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Properties
|
||||||
|
|
||||||
|
- display_version: `boolean` - display the java version - defaults to `true`
|
||||||
|
- display_error: `boolean` - show the error context when failing to retrieve the version information - defaults to `true`
|
||||||
|
- missing_command_text: `string` - text to display when the java command is missing - defaults to empty
|
||||||
|
- display_mode: `string` - determines when the segment is displayed
|
||||||
|
- `always`: the segment is always displayed
|
||||||
|
- `files`: the segment is only displayed when one of the following files is present:
|
||||||
|
- `build.gradle.kts`
|
||||||
|
- `build.sbt`
|
||||||
|
- `.java-version`
|
||||||
|
- `.deps.edn`
|
||||||
|
- `project.clj`
|
||||||
|
- `build.boot`
|
||||||
|
- `*.java`
|
||||||
|
- `*.class`
|
||||||
|
- `*.gradle`
|
||||||
|
- `*.jar`
|
||||||
|
- `*.clj`
|
||||||
|
- `*.cljc`
|
|
@ -19,6 +19,7 @@ module.exports = {
|
||||||
"exit",
|
"exit",
|
||||||
"git",
|
"git",
|
||||||
"golang",
|
"golang",
|
||||||
|
"java",
|
||||||
"julia",
|
"julia",
|
||||||
"kubectl",
|
"kubectl",
|
||||||
"node",
|
"node",
|
||||||
|
|
|
@ -94,6 +94,8 @@ const (
|
||||||
Ruby SegmentType = "ruby"
|
Ruby SegmentType = "ruby"
|
||||||
// Aws writes the active aws context
|
// Aws writes the active aws context
|
||||||
Aws SegmentType = "aws"
|
Aws SegmentType = "aws"
|
||||||
|
// Java writes the active java version
|
||||||
|
Java SegmentType = "java"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (segment *Segment) string() string {
|
func (segment *Segment) string() string {
|
||||||
|
@ -216,6 +218,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
|
||||||
ExecutionTime: &executiontime{},
|
ExecutionTime: &executiontime{},
|
||||||
Ruby: &ruby{},
|
Ruby: &ruby{},
|
||||||
Aws: &aws{},
|
Aws: &aws{},
|
||||||
|
Java: &java{},
|
||||||
}
|
}
|
||||||
if writer, ok := functions[segment.Type]; ok {
|
if writer, ok := functions[segment.Type]; ok {
|
||||||
props := &properties{
|
props := &properties{
|
||||||
|
|
57
src/segment_java.go
Normal file
57
src/segment_java.go
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type java struct {
|
||||||
|
language *language
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *java) string() string {
|
||||||
|
return j.language.string()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *java) init(props *properties, env environmentInfo) {
|
||||||
|
javaRegex := `\((?P<version>(?P<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<patch>[0-9]+)).*\).*(?:built|from)`
|
||||||
|
javaCmd := &cmd{
|
||||||
|
executable: "java",
|
||||||
|
args: []string{"-Xinternalversion"},
|
||||||
|
regex: javaRegex,
|
||||||
|
}
|
||||||
|
j.language = &language{
|
||||||
|
env: env,
|
||||||
|
props: props,
|
||||||
|
extensions: []string{
|
||||||
|
"pom.xml",
|
||||||
|
"build.gradle.kts",
|
||||||
|
"build.sbt",
|
||||||
|
".java-version",
|
||||||
|
".deps.edn",
|
||||||
|
"project.clj",
|
||||||
|
"build.boot",
|
||||||
|
"*.java",
|
||||||
|
"*.class",
|
||||||
|
"*.gradle",
|
||||||
|
"*.jar",
|
||||||
|
"*.clj",
|
||||||
|
"*.cljc",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
javaHome := j.language.env.getenv("JAVA_HOME")
|
||||||
|
if len(javaHome) > 0 {
|
||||||
|
java := fmt.Sprintf("%s/bin/java", javaHome)
|
||||||
|
j.language.commands = []*cmd{
|
||||||
|
{
|
||||||
|
executable: java,
|
||||||
|
args: []string{"-Xinternalversion"},
|
||||||
|
regex: javaRegex,
|
||||||
|
},
|
||||||
|
javaCmd,
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
j.language.commands = []*cmd{javaCmd}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *java) enabled() bool {
|
||||||
|
return j.language.enabled()
|
||||||
|
}
|
58
src/segment_java_test.go
Executable file
58
src/segment_java_test.go
Executable file
|
@ -0,0 +1,58 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJava(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Case string
|
||||||
|
ExpectedString string
|
||||||
|
Version string
|
||||||
|
JavaHomeVersion string
|
||||||
|
JavaHomeEnabled bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Case: "OpenJDK macOS",
|
||||||
|
ExpectedString: "1.8.0",
|
||||||
|
Version: "OpenJDK 64-Bit Server VM (25.275-b01) for bsd-amd64 JRE (1.8.0_275-b01), built on Nov 9 2020 12:07:35 by \"jenkins\" with gcc 4.2.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "OpenJDK macOS with JAVA_HOME, no executable",
|
||||||
|
ExpectedString: "1.8.0",
|
||||||
|
Version: "OpenJDK 64-Bit Server VM (25.275-b01) for bsd-amd64 JRE (1.8.0_275-b01), built on Nov 9 2020 12:07:35 by \"jenkins\" with gcc 4.2.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "OpenJDK macOS with JAVA_HOME and executable",
|
||||||
|
ExpectedString: "1.7.0",
|
||||||
|
JavaHomeEnabled: true,
|
||||||
|
JavaHomeVersion: "OpenJDK 64-Bit Server VM (25.275-b01) for bsd-amd64 JRE (1.7.0_275-b01), built on Nov 9 2020 12:07:35 by \"jenkins\" with gcc 4.2.1",
|
||||||
|
Version: "OpenJDK 64-Bit Server VM (25.275-b01) for bsd-amd64 JRE (1.8.0_275-b01), built on Nov 9 2020 12:07:35 by \"jenkins\" with gcc 4.2.1",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
env := new(MockedEnvironment)
|
||||||
|
env.On("hasCommand", "java").Return(true)
|
||||||
|
env.On("runCommand", "java", []string{"-Xinternalversion"}).Return(tc.Version, nil)
|
||||||
|
env.On("hasFiles", "pom.xml").Return(true)
|
||||||
|
if tc.JavaHomeEnabled {
|
||||||
|
env.On("getenv", "JAVA_HOME").Return("/usr/java")
|
||||||
|
env.On("hasCommand", "/usr/java/bin/java").Return(true)
|
||||||
|
env.On("runCommand", "/usr/java/bin/java", []string{"-Xinternalversion"}).Return(tc.JavaHomeVersion, nil)
|
||||||
|
} else {
|
||||||
|
env.On("getenv", "JAVA_HOME").Return("")
|
||||||
|
}
|
||||||
|
props := &properties{
|
||||||
|
values: map[Property]interface{}{
|
||||||
|
DisplayVersion: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
j := &java{}
|
||||||
|
j.init(props, env)
|
||||||
|
assert.True(t, j.enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
|
||||||
|
assert.Equal(t, tc.ExpectedString, j.string(), fmt.Sprintf("Failed in case: %s", tc.Case))
|
||||||
|
}
|
||||||
|
}
|
|
@ -154,7 +154,8 @@
|
||||||
"ruby",
|
"ruby",
|
||||||
"ytm",
|
"ytm",
|
||||||
"executiontime",
|
"executiontime",
|
||||||
"aws"
|
"aws",
|
||||||
|
"java"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"style": {
|
"style": {
|
||||||
|
@ -715,6 +716,32 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"if": {
|
||||||
|
"properties": {
|
||||||
|
"type": { "const": "java" }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"then": {
|
||||||
|
"title": "Julia Segment",
|
||||||
|
"description": "https://ohmyposh.dev/docs/java",
|
||||||
|
"properties": {
|
||||||
|
"properties": {
|
||||||
|
"properties": {
|
||||||
|
"display_version": {
|
||||||
|
"$ref": "#/definitions/display_version"
|
||||||
|
},
|
||||||
|
"display_mode": {
|
||||||
|
"$ref": "#/definitions/display_mode"
|
||||||
|
},
|
||||||
|
"missing_command_text": {
|
||||||
|
"$ref": "#/definitions/missing_command_text"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"if": {
|
"if": {
|
||||||
"properties": {
|
"properties": {
|
||||||
|
|
Loading…
Reference in a new issue