返回 DeepSeek-Reasonix
blank_project.go
根目录 / desktop / blank_project.go
1 package main
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "strings"
8 )
9
10 // PickBlankProjectParent opens a folder chooser defaulting to the active
11 // project's parent, where sibling projects are normally created.
12 func (a *App) PickBlankProjectParent() (string, error) {
13 if a.ctx == nil {
14 return "", nil
15 }
16 cur, _ := os.Getwd()
17 a.mu.RLock()
18 if tab := a.activeTabLocked(); tab != nil && tab.WorkspaceRoot != "" {
19 cur = filepath.Dir(tab.WorkspaceRoot)
20 }
21 a.mu.RUnlock()
22 return a.nativeHost().OpenDirectoryDialog(a.ctx, nativeDialogOptions{
23 Title: "Choose where to create the project",
24 DefaultDirectory: dialogDefaultDirectory(cur),
25 })
26 }
27
28 // CreateBlankProject creates one new directory below parentDir and returns its
29 // absolute path; opening it remains on the existing workspace navigation path.
30 func (a *App) CreateBlankProject(parentDir, projectName string) (string, error) {
31 return createBlankProject(parentDir, projectName)
32 }
33
34 func createBlankProject(parentDir, projectName string) (string, error) {
35 parentDir = strings.TrimSpace(parentDir)
36 if parentDir == "" {
37 return "", fmt.Errorf("parent folder is required")
38 }
39 if abs, err := filepath.Abs(parentDir); err == nil {
40 parentDir = abs
41 } else {
42 return "", fmt.Errorf("resolve parent folder: %w", err)
43 }
44 info, err := os.Stat(parentDir)
45 if err != nil {
46 return "", fmt.Errorf("open parent folder: %w", err)
47 }
48 if !info.IsDir() {
49 return "", fmt.Errorf("parent path is not a directory: %s", parentDir)
50 }
51
52 projectName = strings.TrimSpace(projectName)
53 if projectName == "" {
54 return "", fmt.Errorf("project name is required")
55 }
56 if projectName == "." || projectName == ".." || strings.ContainsAny(projectName, `/\\`) {
57 return "", fmt.Errorf("project name must be a single folder name")
58 }
59 for _, r := range projectName {
60 if r < 0x20 || r == 0x7f {
61 return "", fmt.Errorf("project name cannot contain control characters")
62 }
63 }
64
65 target := filepath.Join(parentDir, projectName)
66 if filepath.Dir(target) != filepath.Clean(parentDir) {
67 return "", fmt.Errorf("project name must be a single folder name")
68 }
69 if err := os.Mkdir(target, 0o755); err != nil {
70 if os.IsExist(err) {
71 return "", fmt.Errorf("project folder already exists: %s", target)
72 }
73 return "", fmt.Errorf("create project folder: %w", err)
74 }
75 return target, nil
76 }
77
77 lines GO