|
| 1 | +package run |
| 2 | + |
| 3 | +import ( |
| 4 | + "encr.dev/pkg/watcher" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +var embeddedFiles = make(map[string][]string) |
| 12 | + |
| 13 | +// ignoreEventEmbedded checks whether the event is related to an embedded file |
| 14 | +func ignoreEventEmbedded(event watcher.Event) (bool, error) { |
| 15 | + switch event.EventType { |
| 16 | + case watcher.CREATED: |
| 17 | + return true, handleCreatedFile(event.Path) |
| 18 | + case watcher.DELETED: |
| 19 | + return true, handleDeletedFile(event.Path) |
| 20 | + case watcher.MODIFIED: |
| 21 | + return handleModifiedFile(event.Path) |
| 22 | + default: |
| 23 | + return true, nil |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func handleModifiedFile(path string) (bool, error) { |
| 28 | + if strings.HasSuffix(path, ".go") { |
| 29 | + return true, updateEmbeddedFiles(path) |
| 30 | + } |
| 31 | + |
| 32 | + embedded, err := isFileEmbedded(path) |
| 33 | + if err != nil { |
| 34 | + return true, err |
| 35 | + } |
| 36 | + |
| 37 | + return !embedded, nil |
| 38 | +} |
| 39 | + |
| 40 | +func initializeEmbeddedFilesTracker(root string) error { |
| 41 | + if len(embeddedFiles) > 0 { |
| 42 | + return nil |
| 43 | + } |
| 44 | + |
| 45 | + return filepath.Walk(root, func(path string, info os.FileInfo, err error) error { |
| 46 | + if err != nil || info.IsDir() || filepath.Ext(path) != ".go" { |
| 47 | + return err |
| 48 | + } |
| 49 | + return updateEmbeddedFiles(path) |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +func handleCreatedFile(path string) error { |
| 54 | + if strings.HasSuffix(path, ".go") { |
| 55 | + return updateEmbeddedFiles(path) |
| 56 | + } |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +func handleDeletedFile(path string) error { |
| 61 | + delete(embeddedFiles, path) |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func isFileEmbedded(fpath string) (bool, error) { |
| 66 | + for _, files := range embeddedFiles { |
| 67 | + for _, file := range files { |
| 68 | + if file == fpath { |
| 69 | + return true, nil |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + return false, nil |
| 74 | +} |
| 75 | + |
| 76 | +func updateEmbeddedFiles(path string) error { |
| 77 | + embeds, err := parseEmbeddedFiles(path) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("failed to parse embedded files: %w", err) |
| 80 | + } |
| 81 | + embeddedFiles[path] = embeds |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// parseEmbeddedFiles returns all the embedded files for a given source file |
| 86 | +func parseEmbeddedFiles(sourceFile string) ([]string, error) { |
| 87 | + data, err := os.ReadFile(sourceFile) |
| 88 | + if err != nil { |
| 89 | + return nil, fmt.Errorf("failed to read source file: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + var embeddedPaths []string |
| 93 | + sourceDir := filepath.Dir(sourceFile) |
| 94 | + lines := strings.Split(string(data), "\n") |
| 95 | + |
| 96 | + for _, line := range lines { |
| 97 | + line = strings.TrimSpace(line) |
| 98 | + if strings.HasPrefix(line, "//go:embed") { |
| 99 | + parts := strings.Fields(line) |
| 100 | + if len(parts) > 1 { |
| 101 | + dir := parts[1] |
| 102 | + filepaths, err := getFilePathsFromDir(filepath.Join(sourceDir, dir)) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + embeddedPaths = append(embeddedPaths, filepaths...) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return embeddedPaths, nil |
| 111 | +} |
| 112 | + |
| 113 | +// getFilePathsFromDir retrieves all file paths from a directory recursively |
| 114 | +func getFilePathsFromDir(dir string) ([]string, error) { |
| 115 | + var filePaths []string |
| 116 | + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 117 | + if err != nil || info.IsDir() { |
| 118 | + return err |
| 119 | + } |
| 120 | + filePaths = append(filePaths, path) |
| 121 | + return nil |
| 122 | + }) |
| 123 | + if err != nil { |
| 124 | + return nil, fmt.Errorf("failed to walk through directory %s: %w", dir, err) |
| 125 | + } |
| 126 | + return filePaths, nil |
| 127 | +} |
0 commit comments