cleanupOrphanedIncludes removes include files that are no longer used by any workflow
(verbose bool)
| 173 | |
| 174 | // cleanupOrphanedIncludes removes include files that are no longer used by any workflow |
| 175 | func cleanupOrphanedIncludes(verbose bool) error { |
| 176 | removeLog.Print("Cleaning up orphaned include files") |
| 177 | // Get all remaining markdown files |
| 178 | mdFiles, err := getMarkdownWorkflowFiles("") |
| 179 | if err != nil { |
| 180 | // No markdown files means we can clean up all includes |
| 181 | removeLog.Print("No markdown files found, cleaning up all includes") |
| 182 | if verbose { |
| 183 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("No markdown files found, cleaning up all includes")) |
| 184 | } |
| 185 | return cleanupAllIncludes(verbose) |
| 186 | } |
| 187 | |
| 188 | // Collect all include dependencies from remaining workflows |
| 189 | usedIncludes := make(map[string]struct { |
| 190 | }) |
| 191 | |
| 192 | for _, mdFile := range mdFiles { |
| 193 | content, err := os.ReadFile(mdFile) |
| 194 | if err != nil { |
| 195 | if verbose { |
| 196 | fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Could not read %s for include analysis: %v", mdFile, err))) |
| 197 | } |
| 198 | continue |
| 199 | } |
| 200 | |
| 201 | // Find includes used by this workflow |
| 202 | includes, err := findIncludesInContent(string(content)) |
| 203 | if err != nil { |
| 204 | if verbose { |
| 205 | fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Could not analyze includes in %s: %v", mdFile, err))) |
| 206 | } |
| 207 | continue |
| 208 | } |
| 209 | |
| 210 | for _, include := range includes { |
| 211 | usedIncludes[include] = struct { |
| 212 | }{} |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Find all include files in the workflows directory |
| 217 | // Only consider files in subdirectories (like shared/) as potential include files |
| 218 | // Root-level .md files are workflow files, not include files |
| 219 | workflowsDir := constants.GetWorkflowDir() |
| 220 | var allIncludes []string |
| 221 | |
| 222 | err = filepath.Walk(workflowsDir, func(path string, info os.FileInfo, err error) error { |
| 223 | if err != nil { |
| 224 | return err |
| 225 | } |
| 226 | |
| 227 | if !info.IsDir() && strings.HasSuffix(info.Name(), ".md") { |
| 228 | relPath, err := filepath.Rel(workflowsDir, path) |
| 229 | if err != nil { |
| 230 | return err |
| 231 | } |
| 232 |