From d8ef1eda0924b3c50a737ba6df7d6f89b41797d9 Mon Sep 17 00:00:00 2001 From: Sourabh Mehta <73165318+soumeh01@users.noreply.github.com> Date: Thu, 4 Apr 2024 08:30:05 +0200 Subject: [PATCH] Remove '>=' from missing pack version string (#195) --- pkg/builder/csolution/builder.go | 5 +++++ pkg/utils/utils.go | 13 +++++++++++++ pkg/utils/utils_test.go | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/pkg/builder/csolution/builder.go b/pkg/builder/csolution/builder.go index bf4f5cf..40499c8 100644 --- a/pkg/builder/csolution/builder.go +++ b/pkg/builder/csolution/builder.go @@ -102,6 +102,11 @@ func (b CSolutionBuilder) installMissingPacks() (err error) { if pack == "" { continue } + + // This call should be removed once the limitation of 'cpackget' + // to handle '>=' in pack version, is resolved + pack = utils.RemoveVersionRange(pack) + args = []string{"add", pack, "--force-reinstall", "--agree-embedded-license"} cpackgetBin := filepath.Join(b.InstallConfigs.BinPath, "cpackget"+b.InstallConfigs.BinExtn) if _, err := os.Stat(cpackgetBin); os.IsNotExist(err) { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index b51cda3..d380469 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -11,6 +11,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "runtime" "strings" @@ -325,3 +326,15 @@ func ResolveContexts(allContext []string, contextFilters []string) ([]string, er } return selectedContexts, nil } + +func RemoveVersionRange(str string) string { + // This function removes the version range specifier '>=' + // from the pack version. for e.g. "ARM::CMSIS@>=6.0.0" is + // converted into "ARM::CMSIS@6.0.0" + re := regexp.MustCompile(`@([^0-9]*)(\d)`) + match := re.FindStringSubmatch(str) + if len(match) >= 3 { + return strings.Replace(str, match[1], "", 1) + } + return str +} diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 5c1318e..ba799f0 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -352,3 +352,23 @@ func TestParseCsolutionFile(t *testing.T) { assert.Equal(data.Solution.TargetTypes[1].Type, "CM0") }) } + +func TestRemoveVersionRange(t *testing.T) { + assert := assert.New(t) + + testCases := []struct { + inputString string + expectedOutput string + }{ + {"ARM::CMSIS@>=6.0.0", "ARM::CMSIS@6.0.0"}, + {"ARM::CMSIS@>=6.0.0-alpha0", "ARM::CMSIS@6.0.0-alpha0"}, + {"ARM::CMSIS@6.0.0", "ARM::CMSIS@6.0.0"}, + {"ARM::CMSIS", "ARM::CMSIS"}, + {"", ""}, + } + + for _, test := range testCases { + outString := RemoveVersionRange(test.inputString) + assert.Equal(test.expectedOutput, outString) + } +}