Skip to content

Commit

Permalink
🐛 QD-9666 Rewrite reading product-info.json. Determine EAP state by a…
Browse files Browse the repository at this point in the history
…ppearance of -Dqodana.eap=true parameter
  • Loading branch information
avafanasiev authored and tiulpin committed Aug 1, 2024
1 parent 4ef8808 commit 5ae43ee
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 55 deletions.
26 changes: 0 additions & 26 deletions core/ide.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package core

import (
"encoding/json"
"errors"
"fmt"
"github.com/JetBrains/qodana-cli/v2024/cloud"
"github.com/JetBrains/qodana-cli/v2024/platform"
Expand Down Expand Up @@ -316,30 +314,6 @@ func findIde(dir string) string {
return ""
}

// readIdeProductInfo returns IDE info from the given path.
func readIdeProductInfo(ideDir string) map[string]interface{} {
if //goland:noinspection ALL
runtime.GOOS == "darwin" {
ideDir = filepath.Join(ideDir, "Resources")
}
productInfo := filepath.Join(ideDir, "product-info.json")
if _, err := os.Stat(productInfo); errors.Is(err, os.ErrNotExist) {
return nil
}
productInfoFile, err := os.ReadFile(productInfo)
if err != nil {
log.Printf("Problem loading product-info.json: %v ", err)
return nil
}
var productInfoMap map[string]interface{}
err = json.Unmarshal(productInfoFile, &productInfoMap)
if err != nil {
log.Printf("Not a valid product-info.json: %v ", err)
return nil
}
return productInfoMap
}

func prepareLocalIdeSettings(opts *QodanaOptions) {
guessProduct(opts)
if Prod.BaseScriptName == "" {
Expand Down
4 changes: 2 additions & 2 deletions core/installers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ func DownloadAndInstallIDE(ideName string, t *testing.T) {
runtime.GOOS == "darwin" {
ide = filepath.Join(ide, "Contents")
}
prod := readIdeProductInfo(ide)
prod, err := readIdeProductInfo(ide)
defer func(path string) {
err := os.RemoveAll(path)
if err != nil {
platform.ErrorMessage("Cannot clean up temp dir: %s", err)
}
}(ide) // clean up
if prod["productCode"] == "" {
if prod.ProductCode == "" {
t.Fail()
}
}
93 changes: 66 additions & 27 deletions core/product_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package core

import (
"encoding/json"
"fmt"
"github.com/JetBrains/qodana-cli/v2024/platform"
"os"
Expand All @@ -39,6 +40,21 @@ type product struct {
EAP bool
}

type Launch struct {
CustomCommands []struct {
Commands []string
AdditionalJvmArguments []string
} `json:"customCommands"`
}

type ProductInfoJson struct {
Version string `json:"version"`
BuildNumber string `json:"buildNumber"`
ProductCode string `json:"productCode"`
VersionSuffix string `json:"versionSuffix"`
Launch []Launch `json:"launch"`
}

func (p *product) IdeBin() string {
return filepath.Join(p.Home, "bin")
}
Expand Down Expand Up @@ -249,38 +265,61 @@ func guessProduct(opts *QodanaOptions) {
Prod.IdeScript = filepath.Join(Prod.IdeBin(), fmt.Sprintf("%s%s", Prod.BaseScriptName, getScriptSuffix()))
}
}
productInfo, err := readIdeProductInfo(Prod.Home)
if err != nil {
log.Fatalf("Can't read product-info.json: %v ", err)
}
Prod.Version = productInfo.Version
Prod.Code = toQodanaCode(productInfo.ProductCode)
Prod.Name = Prod.getProductNameFromCode()
Prod.Build = productInfo.BuildNumber
Prod.EAP = isEap(*productInfo)

log.Debug(Prod)
platform.SetEnv(platform.QodanaDistEnv, Prod.Home)
}

func isEap(info ProductInfoJson) bool {
treatAsRelease := os.Getenv(platform.QodanaTreatAsRelease)
if productInfo := readIdeProductInfo(Prod.Home); productInfo != nil {
if v, ok := productInfo["version"]; ok {
Prod.Version = v.(string)
} else {
Prod.Version = platform.Version
}
if treatAsRelease == "true" {
return true
}

if v, ok := productInfo["buildNumber"]; ok {
Prod.Build = v.(string)
} else {
Prod.Build = platform.Version
for _, launch := range info.Launch {
for _, command := range launch.CustomCommands {
for _, cmd := range command.Commands {
if cmd == "qodana" {
for _, arg := range command.AdditionalJvmArguments {
if arg == "-Dqodana.eap=true" {
return true
}
}
}
}
}
}

if v, ok := productInfo["productCode"]; ok {
Prod.Code = toQodanaCode(v.(string))
Prod.Name = Prod.getProductNameFromCode()
} else {
Prod.Code = scriptToProductCode(Prod.BaseScriptName)
}
return false
}

if v, ok := productInfo["versionSuffix"]; ok {
Prod.EAP = strings.HasPrefix(v.(string), "EAP")
} else {
Prod.EAP = false
}
if treatAsRelease == "true" {
Prod.EAP = true
}
// readIdeProductInfo returns IDE info from the given path.
func readIdeProductInfo(ideDir string) (*ProductInfoJson, error) {
if //goland:noinspection ALL
runtime.GOOS == "darwin" {
ideDir = filepath.Join(ideDir, "Resources")
}

log.Debug(Prod)
platform.SetEnv(platform.QodanaDistEnv, Prod.Home)
productInfo := filepath.Join(ideDir, "product-info.json")
if _, err := os.Stat(productInfo); err != nil {
return nil, err
}
productInfoFile, err := os.ReadFile(productInfo)
if err != nil {
return nil, err
}
var productInfoJson ProductInfoJson
err = json.Unmarshal(productInfoFile, &productInfoJson)
if err != nil {
return nil, err
}
return &productInfoJson, nil
}

0 comments on commit 5ae43ee

Please sign in to comment.