sylixos-uploader/cmd/gen_ide_env.go

87 lines
2.3 KiB
Go
Raw Normal View History

2024-11-20 17:52:51 +08:00
package cmd
import (
"fmt"
"os"
"path/filepath"
2024-11-20 18:38:59 +08:00
"runtime"
2024-11-20 17:52:51 +08:00
"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() {
// 输出结果
2024-11-20 18:38:59 +08:00
absPath, err := filepath.Abs(relativePath)
if err != nil {
return err
}
fmt.Printf("export WORKSPACE_%s=%s\n", envVarName, absPath)
2024-11-20 17:52:51 +08:00
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)
}
},
}
2024-11-20 18:38:59 +08:00
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)
},
}
2024-11-20 17:52:51 +08:00
func init() {
2024-11-20 18:38:59 +08:00
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)
2024-11-20 17:52:51 +08:00
2024-11-20 18:38:59 +08:00
// Adding flags with long names
fakeGenIdeEnvCmd.Flags().StringVar(&WorkSpacePath, "workspace", "", "Workspace path")
}
2024-11-20 17:52:51 +08:00
}