-
Notifications
You must be signed in to change notification settings - Fork 1
Remove dead fragmentation state and duplicate source reads #71
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
0eb1796
Remove ASCII verification.
Vladyslav-Kuksiuk 5ae2c7d
Simplify fragmentation path-choosing logic.
Vladyslav-Kuksiuk 65c6fa3
Add `absolutePath` type.
Vladyslav-Kuksiuk 299a00a
Merge remote-tracking branch 'refs/remotes/origin/master' into remove…
Vladyslav-Kuksiuk 4d2b902
Improve grammar.
Vladyslav-Kuksiuk de09cf0
Make `Fragmentation.CodeFile` private.
Vladyslav-Kuksiuk 5f411e2
Extract `unsupportedEncodingError`.
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 |
|---|---|---|
|
|
@@ -37,60 +37,36 @@ package fragmentation | |
|
|
||
| import ( | ||
| "bufio" | ||
| "embed-code/embed-code-go/files" | ||
| _type "embed-code/embed-code-go/type" | ||
| "bytes" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| config "embed-code/embed-code-go/configuration" | ||
| ) | ||
|
|
||
| // NamedPathPrefix the prefix before the named code source. | ||
| const NamedPathPrefix = "$" | ||
|
|
||
| // Fragmentation splits the given file into fragments. | ||
| // | ||
| // Configuration — a configuration for embedding. | ||
| // | ||
| // SourcesRoot — a named source code path. | ||
| // | ||
| // CodeFile — a full path of a file to fragment. | ||
| type Fragmentation struct { | ||
| Configuration config.Configuration | ||
| SourcesRoot _type.NamedPath | ||
| CodeFile string | ||
| // codeFile is the absolute path of the source file being fragmented. | ||
| codeFile string | ||
| // fragmentBuilders collects fragment partitions by name while the source file is scanned. | ||
| fragmentBuilders map[string]*FragmentBuilder | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's document this prop. |
||
| } | ||
|
|
||
| // NewFragmentation builds Fragmentation from given codeFileRelative and config. | ||
| // | ||
| // codeFileRelative — a relative path to a code file to fragment. | ||
| // NewFragmentation builds Fragmentation for the given code file. | ||
| // | ||
| // config — a configuration for embedding. | ||
| func NewFragmentation( | ||
| codeFileRelative string, | ||
| codeRoot _type.NamedPath, | ||
| config config.Configuration, | ||
| ) (Fragmentation, error) { | ||
| fragmentation := Fragmentation{} | ||
|
|
||
| fragmentation.SourcesRoot = codeRoot | ||
| _, err := filepath.Abs(codeRoot.Path) | ||
| // codeFile — a relative or absolute path to a code file to fragment. | ||
| func NewFragmentation(codeFile string) (Fragmentation, error) { | ||
| absoluteCodeFile, err := filepath.Abs(codeFile) | ||
| if err != nil { | ||
| return Fragmentation{}, err | ||
| } | ||
|
|
||
| absoluteCodeFile, err := filepath.Abs(codeFileRelative) | ||
| if err != nil { | ||
| return Fragmentation{}, err | ||
| } | ||
| fragmentation.CodeFile = absoluteCodeFile | ||
|
|
||
| fragmentation.Configuration = config | ||
| fragmentation.fragmentBuilders = make(map[string]*FragmentBuilder) | ||
|
|
||
| return fragmentation, nil | ||
| return Fragmentation{ | ||
| codeFile: absoluteCodeFile, | ||
| fragmentBuilders: make(map[string]*FragmentBuilder), | ||
| }, nil | ||
| } | ||
|
|
||
| // DoFragmentation splits the file into fragments. | ||
|
|
@@ -100,16 +76,15 @@ func NewFragmentation( | |
| func (f Fragmentation) DoFragmentation() ([]string, map[string]Fragment, error) { | ||
| var contentToRender []string | ||
|
|
||
| file, err := os.Open(f.CodeFile) | ||
| content, err := os.ReadFile(f.codeFile) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| if err := validateTextEncoding(content); err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| defer func(file *os.File) { | ||
| err = file.Close() | ||
| }(file) | ||
|
|
||
| scanner := bufio.NewScanner(file) | ||
| scanner := bufio.NewScanner(bytes.NewReader(content)) | ||
| lineNumber := 0 | ||
| for scanner.Scan() { | ||
| lineNumber++ | ||
|
|
@@ -118,11 +93,11 @@ func (f Fragmentation) DoFragmentation() ([]string, map[string]Fragment, error) | |
| if err != nil { | ||
| return nil, nil, fmt.Errorf( | ||
| "failed to do fragmentation on file `file://%s:%d`: %w", | ||
| f.CodeFile, lineNumber, err, | ||
| f.codeFile, lineNumber, err, | ||
| ) | ||
| } | ||
| } | ||
| if err = scanner.Err(); err != nil { | ||
| if err := scanner.Err(); err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
|
|
@@ -135,22 +110,6 @@ func (f Fragmentation) DoFragmentation() ([]string, map[string]Fragment, error) | |
| return contentToRender, fragments, nil | ||
| } | ||
|
|
||
| // shouldDoFragmentation reports whether the file is valid to do fragmentation: | ||
| // - it exists by the given path | ||
| // - it is a file (not a dir) | ||
| // - it is textual-encoded. | ||
| func shouldDoFragmentation(filePath string) (bool, error) { | ||
| exists, err := files.IsFileExist(filePath) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| if exists { | ||
| return IsEncodedAsText(filePath) | ||
| } | ||
|
|
||
| return false, nil | ||
| } | ||
|
|
||
| // Parses a single line of input and performs the following actions: | ||
| // - identifies fragment start and end markers within given line; | ||
| // - updates fragmentBuilders based on the markers; | ||
|
|
@@ -196,7 +155,7 @@ func (f Fragmentation) parseStartDocFragments(docFragments []string, cursor int) | |
| fragment, exists := f.fragmentBuilders[fragmentName] | ||
| if !exists { | ||
| builder := FragmentBuilder{ | ||
| CodeFilePath: f.CodeFile, | ||
| CodeFilePath: f.codeFile, | ||
| Name: fragmentName, | ||
| } | ||
| f.fragmentBuilders[fragmentName] = &builder | ||
|
|
@@ -220,7 +179,7 @@ func (f Fragmentation) parseEndDocFragments(endDocFragments []string, cursor int | |
| } | ||
| } else { | ||
| return fmt.Errorf("cannot end the fragment `%s` of the file `file://%s` as it wasn't started", | ||
| fragmentName, f.CodeFile) | ||
| fragmentName, f.codeFile) | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.