Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cbuild] Add list environment command #62

Merged
merged 4 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/cbuild/commands/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ func init() {
_ = command.Flags().MarkHidden("toolchain")
command.Parent().HelpFunc()(command, strings)
})
ListCmd.AddCommand(ListConfigurationsCmd, ListContextsCmd, ListToolchainsCmd)
ListEnvironmentCmd.SetHelpFunc(func(command *cobra.Command, strings []string) {
_ = command.Flags().MarkHidden("schema")
_ = command.Flags().MarkHidden("toolchain")
command.Parent().HelpFunc()(command, strings)
})
ListCmd.AddCommand(ListConfigurationsCmd, ListContextsCmd, ListToolchainsCmd, ListEnvironmentCmd)
}
34 changes: 34 additions & 0 deletions cmd/cbuild/commands/list/list_environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2023 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
package list

import (
"cbuild/pkg/builder"
"cbuild/pkg/builder/csolution"
"cbuild/pkg/utils"

"github.com/spf13/cobra"
)

var ListEnvironmentCmd = &cobra.Command{
Use: "environment",
Short: "Print list of environment configurations",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
configs, err := utils.GetInstallConfigs()
if err != nil {
return err
}

p := csolution.CSolutionBuilder{
BuilderParams: builder.BuilderParams{
Runner: utils.Runner{},
InstallConfigs: configs,
},
}
return p.ListEnvironment()
},
}
39 changes: 39 additions & 0 deletions cmd/cbuild/commands/list/list_environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2023 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

package list_test

import (
"cbuild/cmd/cbuild/commands"
"testing"

"github.com/stretchr/testify/assert"
)

func TestListEnvironmentCommand(t *testing.T) {
assert := assert.New(t)

t.Run("invalid args", func(t *testing.T) {
cmd := commands.NewRootCmd()
cmd.SetArgs([]string{"list", "environment", "--invalid"})
err := cmd.Execute()
assert.Error(err)
})

t.Run("test list environment", func(t *testing.T) {
cmd := commands.NewRootCmd()
cmd.SetArgs([]string{"list", "environment"})
err := cmd.Execute()
assert.Error(err)
})

t.Run("test help", func(t *testing.T) {
cmd := commands.NewRootCmd()
cmd.SetArgs([]string{"list", "environment", "-h"})
err := cmd.Execute()
assert.Nil(err)
})
}
65 changes: 65 additions & 0 deletions pkg/builder/csolution/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -293,6 +294,59 @@ func (b CSolutionBuilder) listToolchains(quiet bool) (toolchains []string, err e
return toolchains, nil
}

func (b CSolutionBuilder) listEnvironment(quiet bool) (envConfigs []string, err error) {
// get installed exe path and version number
getInstalledExeInfo := func(name string) string {
path, err := utils.GetInstalledExePath(name)
if err != nil || path == "" {
return "<Not Found>"
}

// run "exe --version" command
versionStr, err := b.Runner.ExecuteCommand(path, true, "--version")
if err != nil {
versionStr = ""
}

// get version
var version string
if name == "cmake" {
regex := "version\\s(.*?)\\s"
re, err := regexp.Compile(regex)
if err == nil {
match := re.FindAllStringSubmatch(versionStr, 1)
for index := range match {
version = match[index][1]
break
}
}
} else {
version = versionStr
}
info := path
if version != "" {
info += ", version " + version
}
return info
}

// step1: call csolution list environment
args := []string{"list", "environment"}
output, err := b.runCSolution(args, quiet)
if err != nil {
return
}
if output != "" {
envConfigs = strings.Split(strings.ReplaceAll(strings.TrimSpace(output), "\r\n", "\n"), "\n")
}

// step2: add other environment info
envConfigs = append(envConfigs, "cmake="+getInstalledExeInfo("cmake"))
envConfigs = append(envConfigs, "ninja="+getInstalledExeInfo("ninja"))

return envConfigs, nil
}

func (b CSolutionBuilder) ListConfigurations() error {
configurations, err := b.listConfigurations()
if err != nil {
Expand All @@ -312,6 +366,17 @@ func (b CSolutionBuilder) ListToolchains() error {
return err
}

func (b CSolutionBuilder) ListEnvironment() error {
envConfigs, err := b.listEnvironment(true)
if err != nil {
return err
}
for _, config := range envConfigs {
fmt.Println(config)
}
return nil
}

func (b CSolutionBuilder) Build() (err error) {
_ = utils.UpdateEnvVars(b.InstallConfigs.BinPath, b.InstallConfigs.EtcPath)

Expand Down
12 changes: 5 additions & 7 deletions pkg/utils/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
package utils

import (
"errors"
"os"
"path/filepath"
"runtime"

log "github.com/sirupsen/logrus"
)

type Configurations struct {
Expand All @@ -28,8 +27,7 @@ func GetInstallConfigs() (configs Configurations, err error) {
if binPath == "" {
binPath, err = GetExecutablePath()
if err != nil {
log.Error("executable path was not found")
return configs, err
return Configurations{}, err
}
}
if binPath != "" {
Expand All @@ -38,9 +36,9 @@ func GetInstallConfigs() (configs Configurations, err error) {

configs.BinPath = binPath
etcPath := filepath.Clean(binPath + "/../etc")
if _, err := os.Stat(etcPath); os.IsNotExist(err) {
log.Error("etc directory was not found")
return configs, err
if _, err = os.Stat(etcPath); os.IsNotExist(err) {
err = errors.New(etcPath + " path was not found")
return Configurations{}, err
}
if etcPath != "" {
etcPath, _ = filepath.Abs(etcPath)
Expand Down
9 changes: 9 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package utils
import (
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -336,3 +337,11 @@ func Contains[T comparable](slice []T, elem T) bool {
}
return false
}

func GetInstalledExePath(exeName string) (path string, err error) {
path, err = exec.LookPath(exeName)
if strings.Contains(path, "\\") {
path = strings.ReplaceAll(path, "\\", "/")
}
return
}
9 changes: 9 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,12 @@ func TestContains(t *testing.T) {
assert.Equal(output, test.expectedResult)
}
}

func TestGetInstalledExePath(t *testing.T) {
assert := assert.New(t)
t.Run("test to get invalid executable path", func(t *testing.T) {
path, err := GetInstalledExePath("testunknown")
assert.Equal(path, "")
assert.Error(err)
})
}