-
Notifications
You must be signed in to change notification settings - Fork 1
Split processor orchestration from per-file processing #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a7fb6c2
Extract orchestration logic from `processor.go`.
Vladyslav-Kuksiuk 7d93b17
Merge remote-tracking branch 'refs/remotes/origin/master' into split-…
Vladyslav-Kuksiuk 409c87b
Improve readability.
Vladyslav-Kuksiuk bfa83d6
Remove unused processor API.
Vladyslav-Kuksiuk 5ff8eff
Improve docs.
Vladyslav-Kuksiuk 83a0334
Improve doc.
Vladyslav-Kuksiuk 839600a
Improve processor behavior.
Vladyslav-Kuksiuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| // Copyright 2026, TeamDev. All rights reserved. | ||
| // | ||
| // Redistribution and use in source and/or binary forms, with or without | ||
| // modification, must retain the above copyright notice and the following | ||
| // disclaimer. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| package embedding | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "embed-code/embed-code-go/configuration" | ||
| "embed-code/embed-code-go/embedding/parsing" | ||
| "embed-code/embed-code-go/logging" | ||
|
|
||
| "github.com/bmatcuk/doublestar/v4" | ||
| ) | ||
|
|
||
| // EmbedAllResult contains the result of an EmbedAll operation. | ||
| // | ||
| // TotalEmbeddings is the total number of embeddings found in the target documentation files. | ||
| // | ||
| // UpdatedTargetFiles is the list of updated target documentation files. | ||
| type EmbedAllResult struct { | ||
| TotalEmbeddings int | ||
| UpdatedTargetFiles []string | ||
| } | ||
|
|
||
| // processorHandler applies one processing mode to a discovered documentation file. | ||
| type processorHandler func(docFilePath string, processor Processor) error | ||
|
|
||
| // EmbedAll processes embedding for multiple documentation files based on provided config. | ||
| // | ||
| // Iterates over patterns in the configuration, finds documentation files matching those patterns, | ||
| // creates a Processor for each file, and embeds code fragments in them. | ||
| // | ||
| // config — a configuration for embedding. | ||
| func EmbedAll(config configuration.Configuration) (EmbedAllResult, error) { | ||
| totalEmbeddings := 0 | ||
| var updatedTargetFiles []string | ||
| requiredDocPaths, embeddingErrors := processRequiredDocs(config, func( | ||
| _ string, | ||
| processor Processor, | ||
| ) error { | ||
| context, err := processor.Embed() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| totalEmbeddings += context.EmbeddingsCount() | ||
| if context.IsContentChanged() { | ||
| updatedTargetFiles = append(updatedTargetFiles, context.MarkdownFilePath) | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| return nil | ||
| }) | ||
| if len(embeddingErrors) > 0 { | ||
| return EmbedAllResult{}, errors.Join(embeddingErrors...) | ||
| } | ||
| if totalEmbeddings > 0 { | ||
| slog.Info( | ||
| fmt.Sprintf( | ||
| "Processed %d documentation file(s) with %d embedding(s) in `%s`%s.", | ||
|
MykytaPimonovTD marked this conversation as resolved.
|
||
| len(requiredDocPaths), totalEmbeddings, | ||
| logging.FileReference(config.DocumentationRoot), | ||
| configNameLabel(config), | ||
| ), | ||
| ) | ||
| } else { | ||
| slog.Warn( | ||
| fmt.Sprintf("No embedding instructions were found in documentation folder `%s`%s.", | ||
| logging.FileReference(config.DocumentationRoot), configNameLabel(config)), | ||
| ) | ||
| } | ||
|
|
||
| return EmbedAllResult{ | ||
| TotalEmbeddings: totalEmbeddings, | ||
| UpdatedTargetFiles: updatedTargetFiles, | ||
| }, nil | ||
| } | ||
|
|
||
| // configNameLabel formats a configuration name for summary log messages. | ||
| // | ||
| // A non-empty label starts with a space so callers can append it directly. | ||
| func configNameLabel(config configuration.Configuration) string { | ||
| if config.Name == "" { | ||
| return "" | ||
| } | ||
|
|
||
| return fmt.Sprintf(" for `%s` embedding setup", config.Name) | ||
| } | ||
|
|
||
| // CheckUpToDate returns documentation files that are not up-to-date with code files. | ||
| // | ||
| // config — a configuration for embedding. | ||
| func CheckUpToDate(config configuration.Configuration) ([]string, error) { | ||
| changedFiles, checkErrors := findChangedFiles(config) | ||
| if len(checkErrors) > 0 { | ||
| return nil, errors.Join(checkErrors...) | ||
| } | ||
|
|
||
| return changedFiles, nil | ||
| } | ||
|
|
||
| // findChangedFiles returns documentation files that are not up-to-date with their code files. | ||
| // | ||
| // config — a configuration for embedding. | ||
| func findChangedFiles(config configuration.Configuration) ([]string, []error) { | ||
| var changedFiles []string | ||
| _, checkErrors := processRequiredDocs(config, func( | ||
| docFilePath string, | ||
| processor Processor, | ||
| ) error { | ||
| upToDate, err := processor.isUpToDate() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !upToDate { | ||
| changedFiles = append(changedFiles, docFilePath) | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| return changedFiles, checkErrors | ||
| } | ||
|
|
||
| // processRequiredDocs applies a processing handler to every documentation file in config. | ||
| func processRequiredDocs( | ||
| config configuration.Configuration, | ||
| handle processorHandler, | ||
| ) ([]string, []error) { | ||
| requiredDocPaths, err := requiredDocs(config) | ||
| if err != nil { | ||
| return nil, []error{err} | ||
| } | ||
|
|
||
| var processingErrors []error | ||
| for _, doc := range requiredDocPaths { | ||
| processor := newProcessor(doc, config, parsing.Transitions, requiredDocPaths) | ||
| if err := handle(doc, processor); err != nil { | ||
| processingErrors = append(processingErrors, err) | ||
| } | ||
| } | ||
|
|
||
| return requiredDocPaths, processingErrors | ||
| } | ||
|
|
||
| // requiredDocs returns documentation files matched by includes minus excludes. | ||
| func requiredDocs(config configuration.Configuration) ([]string, error) { | ||
| documentationRoot := config.DocumentationRoot | ||
| includedPatterns := config.DocIncludes | ||
| excludedPatterns := config.DocExcludes | ||
|
|
||
| includedDocs, err := getFilesByPatterns(documentationRoot, includedPatterns) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| excludedDocs, err := getFilesByPatterns(documentationRoot, excludedPatterns) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(excludedDocs) == 0 { | ||
| slog.Info(fmt.Sprintf( | ||
| "Found %d documentation file(s) from `%s` matching include pattern(s) %s.", | ||
| len(includedDocs), logging.FileReference(documentationRoot), | ||
| patternsLabel(includedPatterns), | ||
| )) | ||
|
|
||
| return includedDocs, nil | ||
| } | ||
|
|
||
| result := removeElements(includedDocs, excludedDocs) | ||
| slog.Info(fmt.Sprintf( | ||
| "Found %d documentation file(s) from `%s` matching include pattern(s) %s "+ | ||
| "and exclude pattern(s) %s.", | ||
| len(result), logging.FileReference(documentationRoot), patternsLabel(includedPatterns), | ||
| patternsLabel(excludedPatterns), | ||
| )) | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| // patternsLabel formats glob patterns for human-readable log messages. | ||
| func patternsLabel(patterns []string) string { | ||
| if len(patterns) == 0 { | ||
| return "nothing" | ||
| } | ||
|
|
||
| return "`" + strings.Join(patterns, "`, `") + "`" | ||
| } | ||
|
|
||
| // getFilesByPatterns expands documentation glob patterns relative to the given root. | ||
| func getFilesByPatterns(root string, patterns []string) ([]string, error) { | ||
| var result []string | ||
| for _, pattern := range patterns { | ||
| globString := filepath.Join(root, filepath.FromSlash(pattern)) | ||
| matches, err := doublestar.FilepathGlob(globString) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for _, match := range matches { | ||
| result = append(result, filepath.ToSlash(match)) | ||
| } | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| // removeElements returns values from first that are not present in second. | ||
| func removeElements(first, second []string) []string { | ||
| secondMap := make(map[string]struct{}) | ||
| for _, value := range second { | ||
| secondMap[value] = struct{}{} | ||
| } | ||
|
|
||
| var result []string | ||
| for _, value := range first { | ||
| if _, exists := secondMap[value]; !exists { | ||
| result = append(result, value) | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.