|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | +) |
| 10 | + |
| 11 | +// DepConfig defines the source and destination directories for a dependency |
| 12 | +type DepConfig struct { |
| 13 | + Name string // Name of the dependency (for logging) |
| 14 | + SrcDir string // Source directory (relative to project root) |
| 15 | + DstDir string // Destination directory (relative to project root) |
| 16 | +} |
| 17 | + |
| 18 | +// Dependencies to copy. Add new entries here to copy additional dependencies. |
| 19 | +var deps = []DepConfig{ |
| 20 | + { |
| 21 | + Name: "MaaFramework", |
| 22 | + SrcDir: "deps/MaaFramework/bin", |
| 23 | + DstDir: "build/bin/lib", |
| 24 | + }, |
| 25 | + { |
| 26 | + Name: "MaaAgentBinary", |
| 27 | + SrcDir: "deps/MaaFramework/share/MaaAgentBinary", |
| 28 | + DstDir: "build/bin/share/MaaAgentBinary", |
| 29 | + }, |
| 30 | + // Add more dependencies here, e.g.: |
| 31 | + // { |
| 32 | + // Name: "AnotherDep", |
| 33 | + // SrcDir: "deps/AnotherDep/lib", |
| 34 | + // DstDir: "build/bin/plugins", |
| 35 | + // }, |
| 36 | +} |
| 37 | + |
| 38 | +var ( |
| 39 | + forceFlag = flag.Bool("f", false, "Force copy all files even if they exist") |
| 40 | + dirFlag = flag.String("C", "", "Change to directory before running (project root)") |
| 41 | +) |
| 42 | + |
| 43 | +func main() { |
| 44 | + flag.Parse() |
| 45 | + |
| 46 | + // Change to specified directory if provided |
| 47 | + if *dirFlag != "" { |
| 48 | + if err := os.Chdir(*dirFlag); err != nil { |
| 49 | + fmt.Fprintf(os.Stderr, "Failed to change directory to %s: %v\n", *dirFlag, err) |
| 50 | + os.Exit(1) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Get current working directory |
| 55 | + execPath, err := os.Getwd() |
| 56 | + if err != nil { |
| 57 | + fmt.Fprintf(os.Stderr, "Failed to get current directory: %v\n", err) |
| 58 | + os.Exit(1) |
| 59 | + } |
| 60 | + |
| 61 | + totalStats := ©Stats{} |
| 62 | + |
| 63 | + // Process each dependency |
| 64 | + for _, dep := range deps { |
| 65 | + fmt.Printf("Processing dependency: %s\n", dep.Name) |
| 66 | + |
| 67 | + srcDir := filepath.Join(execPath, dep.SrcDir) |
| 68 | + dstDir := filepath.Join(execPath, dep.DstDir) |
| 69 | + |
| 70 | + // Check if source directory exists |
| 71 | + if _, err := os.Stat(srcDir); os.IsNotExist(err) { |
| 72 | + fmt.Fprintf(os.Stderr, "Warning: source directory does not exist for %s: %s (skipped)\n", dep.Name, srcDir) |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + // Ensure destination directory exists |
| 77 | + if err := os.MkdirAll(dstDir, 0755); err != nil { |
| 78 | + fmt.Fprintf(os.Stderr, "Failed to create destination directory for %s: %v\n", dep.Name, err) |
| 79 | + os.Exit(1) |
| 80 | + } |
| 81 | + |
| 82 | + // Copy directory contents |
| 83 | + if err := copyDir(srcDir, dstDir, totalStats); err != nil { |
| 84 | + fmt.Fprintf(os.Stderr, "Copy failed for %s: %v\n", dep.Name, err) |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + fmt.Printf("Done: %d copied, %d skipped (exists)\n", totalStats.copied, totalStats.skipped) |
| 90 | +} |
| 91 | + |
| 92 | +type copyStats struct { |
| 93 | + copied int |
| 94 | + skipped int |
| 95 | +} |
| 96 | + |
| 97 | +// copyDir recursively copies directory contents |
| 98 | +func copyDir(src, dst string, stats *copyStats) error { |
| 99 | + entries, err := os.ReadDir(src) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("failed to read directory %s: %w", src, err) |
| 102 | + } |
| 103 | + |
| 104 | + for _, entry := range entries { |
| 105 | + srcPath := filepath.Join(src, entry.Name()) |
| 106 | + dstPath := filepath.Join(dst, entry.Name()) |
| 107 | + |
| 108 | + if entry.IsDir() { |
| 109 | + // Create subdirectory |
| 110 | + if err := os.MkdirAll(dstPath, 0755); err != nil { |
| 111 | + return fmt.Errorf("failed to create directory %s: %w", dstPath, err) |
| 112 | + } |
| 113 | + // Recursively copy subdirectory |
| 114 | + if err := copyDir(srcPath, dstPath, stats); err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + } else { |
| 118 | + // Skip if file exists and not force mode |
| 119 | + if !*forceFlag { |
| 120 | + if _, err := os.Stat(dstPath); err == nil { |
| 121 | + fmt.Printf("Skipped: %s (exists)\n", entry.Name()) |
| 122 | + stats.skipped++ |
| 123 | + continue |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if err := copyFile(srcPath, dstPath); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + fmt.Printf("Copied: %s\n", entry.Name()) |
| 131 | + stats.copied++ |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +// copyFile copies a single file |
| 139 | +func copyFile(src, dst string) error { |
| 140 | + srcFile, err := os.Open(src) |
| 141 | + if err != nil { |
| 142 | + return fmt.Errorf("failed to open source file %s: %w", src, err) |
| 143 | + } |
| 144 | + defer srcFile.Close() |
| 145 | + |
| 146 | + // Get source file info to preserve permissions |
| 147 | + srcInfo, err := srcFile.Stat() |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("failed to get file info %s: %w", src, err) |
| 150 | + } |
| 151 | + |
| 152 | + dstFile, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, srcInfo.Mode()) |
| 153 | + if err != nil { |
| 154 | + return fmt.Errorf("failed to create destination file %s: %w", dst, err) |
| 155 | + } |
| 156 | + defer dstFile.Close() |
| 157 | + |
| 158 | + if _, err := io.Copy(dstFile, srcFile); err != nil { |
| 159 | + return fmt.Errorf("failed to copy file content %s -> %s: %w", src, dst, err) |
| 160 | + } |
| 161 | + |
| 162 | + return nil |
| 163 | +} |
0 commit comments