From 2b21e934e703b7b2cbfc037537fef251251ec353 Mon Sep 17 00:00:00 2001 From: chengsiyuan Date: Wed, 20 Nov 2024 17:52:51 +0800 Subject: [PATCH] [feature] add gen-ide-env cmd. --- README.md | 14 ++++++++-- cmd/gen_ide_env.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 4 +++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 cmd/gen_ide_env.go diff --git a/README.md b/README.md index 9c4af90..8fc2d30 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,15 @@ SylixOS Uploader is a command-line tool designed for uploading SylixOS projects - Generate structured YAML configuration files for SylixOS projects, ensuring easier management and deployment. -3. Versioning: +3. Generate-Ide-Env-Example: + + - Generate environment variables on linux from IDE projects. + +4. Versioning: - Injects Git version information into the build, including the latest tag, commit hash, and dirty state (if there are uncommitted changes). -4. Flexible Build Options: +5. Flexible Build Options: - Default: Build only for the local platform. - Optionally: Cross-compile for multiple platforms (Linux, Windows, macOS, SylixOS). @@ -39,6 +43,12 @@ sylixos-uploader upload --path ~/sylixos-workspace/base-project --device 10.13.1 sylixos-uploader gen-yaml --path ~/sylixos-workspace/base-project ``` +### Generate IDE ENV Example + +```bash +source <(sylixos-uploader gen-ide-env --workspace ~/sylixos-workspace) +``` + ### YAML File Example ```yaml diff --git a/cmd/gen_ide_env.go b/cmd/gen_ide_env.go new file mode 100644 index 0000000..4146c96 --- /dev/null +++ b/cmd/gen_ide_env.go @@ -0,0 +1,67 @@ +package cmd + +import ( + "fmt" + "os" + "path/filepath" + "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 )", + Long: `usage: source <(sylixos-uploader gen-ide-env --workspace ) + +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() { + // 输出结果 + fmt.Printf("export WORKSPACE_%s=%s\n", envVarName, path) + if strings.Contains(relativePath, "-") { + fmt.Printf("echo \"you have to change \\$(WORKSPACE_%s) to \\$(WORKSPACE_%s) in all config.mk\"\n", envVarNameOrgin, envVarName) + } + } + + // 阻止深入子目录 + return filepath.SkipDir + } + return nil + }) + + if err != nil { + fmt.Printf("Error: %v\n", err) + } + }, +} + +func init() { + RootCmd.AddCommand(genIdeEnvCmd) + + // Adding flags with long names + genIdeEnvCmd.Flags().StringVar(&WorkSpacePath, "workspace", "", "Workspace path") +} diff --git a/cmd/root.go b/cmd/root.go index 6a7fcb7..1f2f4fb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -28,6 +28,7 @@ Konwn-Behavior: 2. On Remote device, ELF executable or shared object file will be chmod to 0755. 3. On Remote device, konwn executable script (e.g. .sh, .py, .lua, etc) will be chmod to 0755. 4. Generate upload yaml file from real evo project. + 5. Generate environment variables on linux from IDE projects. Upload-Example: sylixos-uploader upload --path ~/sylixos-workspace/base-project --device 10.13.16.250 --method ftp @@ -35,6 +36,9 @@ Upload-Example: Generate-Yaml-Example: sylixos-uploader gen-yaml --path ~/sylixos-workspace/base-project +Generate-Ide-Env-Example: + source <(sylixos-uploader gen-ide-env --workspace ~/sylixos-workspace) + Yaml-file-Demo: " base-settings: