mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
perf(unity): implement faster C# version lookups
This commit is contained in:
parent
64417b2255
commit
a97100f936
|
@ -66,7 +66,12 @@ func (u *Unity) GetUnityVersion() (version string, err error) {
|
|||
unityVersion := firstLine[versionStartIndex:]
|
||||
unityVersion = strings.TrimSpace(unityVersion)
|
||||
|
||||
return strings.TrimSuffix(unityVersion, "f1"), nil
|
||||
fIndex := strings.Index(unityVersion, "f")
|
||||
if versionPrefixIndex > -1 {
|
||||
unityVersion = unityVersion[:fIndex]
|
||||
}
|
||||
|
||||
return unityVersion, nil
|
||||
}
|
||||
|
||||
func (u *Unity) GetCSharpVersion() (version string, err error) {
|
||||
|
@ -76,8 +81,41 @@ func (u *Unity) GetCSharpVersion() (version string, err error) {
|
|||
}
|
||||
shortUnityVersion := u.UnityVersion[0:lastDotIndex]
|
||||
|
||||
if val, found := u.env.Cache().Get(shortUnityVersion); found {
|
||||
csharpVersion := strings.TrimSuffix(val, ".0")
|
||||
var csharpVersionsByUnityVersion = map[string]string{
|
||||
"2017.1": "C# 6",
|
||||
"2017.2": "C# 6",
|
||||
"2017.3": "C# 6",
|
||||
"2017.4": "C# 6",
|
||||
"2018.1": "C# 6",
|
||||
"2018.2": "C# 6",
|
||||
"2018.3": "C# 7.3",
|
||||
"2018.4": "C# 7.3",
|
||||
"2019.1": "C# 7.3",
|
||||
"2019.2": "C# 7.3",
|
||||
"2019.3": "C# 7.3",
|
||||
"2019.4": "C# 7.3",
|
||||
"2020.1": "C# 7.3",
|
||||
"2020.2": "C# 8",
|
||||
"2020.3": "C# 8",
|
||||
"2021.1": "C# 8",
|
||||
"2021.2": "C# 9",
|
||||
"2021.3": "C# 9",
|
||||
"2022.1": "C# 9",
|
||||
"2022.2": "C# 9",
|
||||
"2023.1": "C# 9",
|
||||
}
|
||||
|
||||
csharpVersion, found := csharpVersionsByUnityVersion[shortUnityVersion]
|
||||
if found {
|
||||
return csharpVersion, nil
|
||||
}
|
||||
|
||||
u.env.Debug(fmt.Sprintf("Unity version %s doesn't exist in the map", shortUnityVersion))
|
||||
return u.GetCSharpVersionFromWeb(shortUnityVersion)
|
||||
}
|
||||
|
||||
func (u *Unity) GetCSharpVersionFromWeb(shortUnityVersion string) (version string, err error) {
|
||||
if csharpVersion, found := u.env.Cache().Get(shortUnityVersion); found {
|
||||
return csharpVersion, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -36,99 +36,15 @@ func TestUnitySegment(t *testing.T) {
|
|||
Case string
|
||||
ExpectedOutput string
|
||||
VersionFileText string
|
||||
CacheGet CacheGet
|
||||
CacheSet CacheSet
|
||||
ExpectedToBeEnabled bool
|
||||
VersionFileExists bool
|
||||
HTTPResponse HTTPResponse
|
||||
}{
|
||||
{
|
||||
Case: "C# version cached",
|
||||
Case: "Unity version exists in C# map",
|
||||
ExpectedOutput: "\ue721 2021.3.16 C# 9",
|
||||
ExpectedToBeEnabled: true,
|
||||
VersionFileExists: true,
|
||||
VersionFileText: "m_EditorVersion: 2021.3.16f1\nm_EditorVersionWithRevision: 2021.3.16f1 (4016570cf34f)",
|
||||
CacheGet: CacheGet{
|
||||
key: "2021.3",
|
||||
val: "C# 9",
|
||||
found: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
Case: "C# version not cached",
|
||||
ExpectedOutput: "\ue721 2021.3.16 C# 9",
|
||||
ExpectedToBeEnabled: true,
|
||||
VersionFileExists: true,
|
||||
VersionFileText: "m_EditorVersion: 2021.3.16f1\nm_EditorVersionWithRevision: 2021.3.16f1 (4016570cf34f)",
|
||||
CacheGet: CacheGet{
|
||||
key: "2021.3",
|
||||
val: "",
|
||||
found: false,
|
||||
},
|
||||
CacheSet: CacheSet{
|
||||
key: "2021.3",
|
||||
val: "C# 9",
|
||||
},
|
||||
HTTPResponse: HTTPResponse{
|
||||
body: `<a href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9">C# 9.0</a>`,
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
Case: "C# version has a minor version",
|
||||
ExpectedOutput: "\ue721 2021.3.16 C# 7.3",
|
||||
ExpectedToBeEnabled: true,
|
||||
VersionFileExists: true,
|
||||
VersionFileText: "m_EditorVersion: 2021.3.16f1\nm_EditorVersionWithRevision: 2021.3.16f1 (4016570cf34f)",
|
||||
CacheGet: CacheGet{
|
||||
key: "2021.3",
|
||||
val: "",
|
||||
found: false,
|
||||
},
|
||||
CacheSet: CacheSet{
|
||||
key: "2021.3",
|
||||
val: "C# 7.3",
|
||||
},
|
||||
HTTPResponse: HTTPResponse{
|
||||
body: `<a href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7-3">C# 7.3</a>`,
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
Case: "C# version not found in webpage",
|
||||
ExpectedOutput: "\ue721 2021.3.16",
|
||||
ExpectedToBeEnabled: true,
|
||||
VersionFileExists: true,
|
||||
VersionFileText: "m_EditorVersion: 2021.3.16f1\nm_EditorVersionWithRevision: 2021.3.16f1 (4016570cf34f)",
|
||||
CacheGet: CacheGet{
|
||||
key: "2021.3",
|
||||
val: "",
|
||||
found: false,
|
||||
},
|
||||
CacheSet: CacheSet{
|
||||
key: "2021.3",
|
||||
val: "",
|
||||
},
|
||||
HTTPResponse: HTTPResponse{
|
||||
body: `<h1>Sorry... that page seems to be missing!</h1>`,
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
Case: "http request fails",
|
||||
ExpectedOutput: "\ue721 2021.3.16",
|
||||
ExpectedToBeEnabled: true,
|
||||
VersionFileExists: true,
|
||||
VersionFileText: "m_EditorVersion: 2021.3.16f1\nm_EditorVersionWithRevision: 2021.3.16f1 (4016570cf34f)",
|
||||
CacheGet: CacheGet{
|
||||
key: "2021.3",
|
||||
val: "",
|
||||
found: false,
|
||||
},
|
||||
HTTPResponse: HTTPResponse{
|
||||
body: "",
|
||||
err: errors.New("FAIL"),
|
||||
},
|
||||
},
|
||||
{
|
||||
Case: "ProjectSettings/ProjectVersion.txt doesn't exist",
|
||||
|
@ -153,12 +69,142 @@ func TestUnitySegment(t *testing.T) {
|
|||
ExpectedToBeEnabled: true,
|
||||
VersionFileExists: true,
|
||||
VersionFileText: "m_EditorVersion: 2021.3.16f1\r\nm_EditorVersionWithRevision: 2021.3.16f1 (4016570cf34f)\r\n",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On("Error", mock2.Anything).Return()
|
||||
env.On("Debug", mock2.Anything)
|
||||
|
||||
err := errors.New("no match at root level")
|
||||
var projectDir *platform.FileInfo
|
||||
if tc.VersionFileExists {
|
||||
err = nil
|
||||
projectDir = &platform.FileInfo{
|
||||
ParentFolder: "UnityProjectRoot",
|
||||
Path: "UnityProjectRoot/ProjectSettings",
|
||||
IsDir: true,
|
||||
}
|
||||
env.On("HasFilesInDir", projectDir.Path, "ProjectVersion.txt").Return(tc.VersionFileExists)
|
||||
versionFilePath := filepath.Join(projectDir.Path, "ProjectVersion.txt")
|
||||
env.On("FileContent", versionFilePath).Return(tc.VersionFileText)
|
||||
}
|
||||
env.On("HasParentFilePath", "ProjectSettings").Return(projectDir, err)
|
||||
|
||||
props := properties.Map{}
|
||||
unity := &Unity{}
|
||||
unity.Init(props, env)
|
||||
assert.Equal(t, tc.ExpectedToBeEnabled, unity.Enabled())
|
||||
if tc.ExpectedToBeEnabled {
|
||||
assert.Equal(t, tc.ExpectedOutput, renderTemplate(env, unity.Template(), unity), tc.Case)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2021.9.20f1 is used in the test cases below as a fake Unity version.
|
||||
// As such, it doesn't exist in the predfined map in unity.go. This
|
||||
// allows us to test the web request portion of the code, which is the
|
||||
// fallback for obtaining a C# version.
|
||||
func TestUnitySegmentCSharpWebRequest(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
ExpectedOutput string
|
||||
VersionFileText string
|
||||
CacheGet CacheGet
|
||||
CacheSet CacheSet
|
||||
ExpectedToBeEnabled bool
|
||||
VersionFileExists bool
|
||||
HTTPResponse HTTPResponse
|
||||
}{
|
||||
{
|
||||
Case: "C# version cached",
|
||||
ExpectedOutput: "\ue721 2021.9.20 C# 10",
|
||||
ExpectedToBeEnabled: true,
|
||||
VersionFileExists: true,
|
||||
VersionFileText: "m_EditorVersion: 2021.9.20f1\nm_EditorVersionWithRevision: 2021.9.20f1 (4016570cf34f)",
|
||||
CacheGet: CacheGet{
|
||||
key: "2021.3",
|
||||
val: "C# 9",
|
||||
key: "2021.9",
|
||||
val: "C# 10",
|
||||
found: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
Case: "C# version not cached",
|
||||
ExpectedOutput: "\ue721 2021.9.20 C# 10",
|
||||
ExpectedToBeEnabled: true,
|
||||
VersionFileExists: true,
|
||||
VersionFileText: "m_EditorVersion: 2021.9.20f1\nm_EditorVersionWithRevision: 2021.9.20f1 (4016570cf34f)",
|
||||
CacheGet: CacheGet{
|
||||
key: "2021.9",
|
||||
val: "",
|
||||
found: false,
|
||||
},
|
||||
CacheSet: CacheSet{
|
||||
key: "2021.9",
|
||||
val: "C# 10",
|
||||
},
|
||||
HTTPResponse: HTTPResponse{
|
||||
body: `<a href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10">C# 10.0</a>`,
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
Case: "C# version has a minor version",
|
||||
ExpectedOutput: "\ue721 2021.9.20 C# 10.1",
|
||||
ExpectedToBeEnabled: true,
|
||||
VersionFileExists: true,
|
||||
VersionFileText: "m_EditorVersion: 2021.9.20f1\nm_EditorVersionWithRevision: 2021.9.20f1 (4016570cf34f)",
|
||||
CacheGet: CacheGet{
|
||||
key: "2021.9",
|
||||
val: "",
|
||||
found: false,
|
||||
},
|
||||
CacheSet: CacheSet{
|
||||
key: "2021.9",
|
||||
val: "C# 10.1",
|
||||
},
|
||||
HTTPResponse: HTTPResponse{
|
||||
body: `<a href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10-1">C# 10.1</a>`,
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
Case: "C# version not found in webpage",
|
||||
ExpectedOutput: "\ue721 2021.9.20",
|
||||
ExpectedToBeEnabled: true,
|
||||
VersionFileExists: true,
|
||||
VersionFileText: "m_EditorVersion: 2021.9.20f1\nm_EditorVersionWithRevision: 2021.9.20f1 (4016570cf34f)",
|
||||
CacheGet: CacheGet{
|
||||
key: "2021.9",
|
||||
val: "",
|
||||
found: false,
|
||||
},
|
||||
CacheSet: CacheSet{
|
||||
key: "2021.9",
|
||||
val: "",
|
||||
},
|
||||
HTTPResponse: HTTPResponse{
|
||||
body: `<h1>Sorry... that page seems to be missing!</h1>`,
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
Case: "http request fails",
|
||||
ExpectedOutput: "\ue721 2021.9.20",
|
||||
ExpectedToBeEnabled: true,
|
||||
VersionFileExists: true,
|
||||
VersionFileText: "m_EditorVersion: 2021.9.20f1\nm_EditorVersionWithRevision: 2021.9.20f1 (4016570cf34f)",
|
||||
CacheGet: CacheGet{
|
||||
key: "2021.9",
|
||||
val: "",
|
||||
found: false,
|
||||
},
|
||||
HTTPResponse: HTTPResponse{
|
||||
body: "",
|
||||
err: errors.New("FAIL"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
|
|
|
@ -8,13 +8,16 @@ sidebar_label: Unity
|
|||
|
||||
Display the currently active [Unity][unity] and C# versions.
|
||||
|
||||
The Unity version is displayed regardless of whether or not the C# version fetching succeeds.
|
||||
The Unity version is displayed regardless of whether or not the corrsponding C# version can be found.
|
||||
The C# version is determined by first checking a static table.
|
||||
If the Unity version isn't found, a web request is made to [the Unity docs][unity-csharp-page] to
|
||||
try extracting it from there. A web request only occurs the first time a given `major.minor`
|
||||
Unity version is encountered. Subsequent invocations return the cached C# version.
|
||||
|
||||
The C# version is fetched from [the docs][unity-csharp-page], but the page only exists as far back as Unity 2018.3.
|
||||
Earlier versions will not display a C# version.
|
||||
C# version display is only supported from Unity 2017.1.
|
||||
|
||||
A web request only occurs the first time a given `major.minor` Unity version is encountered.
|
||||
Subsequent invocations return the cached C# version.
|
||||
Unity 2017.1 - 2019.1 support two C# versions, depending on which scripting runtime is selected in Player Settings.
|
||||
This segment always chooses the higher version.
|
||||
|
||||
## Sample Configuration
|
||||
|
||||
|
|
Loading…
Reference in a new issue