sylixos-uploader/cmd/gen_ide_env.go

87 lines
2.3 KiB
Go

package cmd
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sylixos-uploader/detector"
"github.com/spf13/cobra"
)
var genIdeEnvCmd = &cobra.Command{
Use: "gen-ide-env",
Short: "usage: source <(sylixos-uploader gen-ide-env --workspace <path>)",
Long: `usage: source <(sylixos-uploader gen-ide-env --workspace <path>)
Generate environment variables from IDE projects.`,
Run: func(cmd *cobra.Command, args []string) {
// 遍历根目录下的第一级子目录
if WorkSpacePath == "" {
WorkSpacePath = "."
}
err := filepath.Walk(WorkSpacePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// 只处理第一级子目录
if info.IsDir() && path != WorkSpacePath {
relativePath, err := filepath.Rel(WorkSpacePath, path)
if err != nil {
return err
}
// 替换路径中的特殊字符,确保变量名合法
envVarNameOrgin := strings.ReplaceAll(relativePath, string(os.PathSeparator), "_")
envVarName := strings.ReplaceAll(envVarNameOrgin, "-", "_")
s := detector.NewSylixOSPrj(path)
if s.IsSylixOSPrj() {
// 输出结果
absPath, err := filepath.Abs(relativePath)
if err != nil {
return err
}
fmt.Printf("export WORKSPACE_%s=%s\n", envVarName, absPath)
if strings.Contains(relativePath, "-") {
fmt.Printf("echo \"you have to change \\$(WORKSPACE_%s) to \\$(WORKSPACE_%s) in all config.mk\"\n", envVarNameOrgin, envVarName)
}
}
}
return nil
})
if err != nil {
fmt.Printf("Error: %v\n", err)
}
},
}
var fakeGenIdeEnvCmd = &cobra.Command{
Use: "gen-ide-env",
Short: "usage: source <(sylixos-uploader gen-ide-env --workspace <path>)",
Long: `usage: source <(sylixos-uploader gen-ide-env --workspace <path>)`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("This command is not supported on %s platform.\n", runtime.GOOS)
},
}
func init() {
switch runtime.GOOS {
case "linux", "darwin":
RootCmd.AddCommand(genIdeEnvCmd)
// Adding flags with long names
genIdeEnvCmd.Flags().StringVar(&WorkSpacePath, "workspace", "", "Workspace path")
default:
RootCmd.AddCommand(fakeGenIdeEnvCmd)
// Adding flags with long names
fakeGenIdeEnvCmd.Flags().StringVar(&WorkSpacePath, "workspace", "", "Workspace path")
}
}