-
Notifications
You must be signed in to change notification settings - Fork 38
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
make embed assets relative to where annotation lives or abs from proj… #30
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package execunit | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/bmatcuk/doublestar/v4" | ||
"github.com/klothoplatform/klotho/pkg/annotation" | ||
"github.com/klothoplatform/klotho/pkg/core" | ||
|
@@ -43,6 +46,12 @@ func (p Assets) Transform(result *core.CompilationResult, deps *core.Dependencie | |
matcher.include, _ = annot.Capability.Directives.StringArray("include") | ||
matcher.exclude, _ = annot.Capability.Directives.StringArray("exclude") | ||
|
||
err := matcher.ModifyPathsForAnnotatedFile(f.Path()) | ||
if err != nil { | ||
errs.Append(err) | ||
break | ||
} | ||
|
||
matchCount := 0 | ||
for _, asset := range input.Files() { | ||
if matcher.Matches(asset.Path()) { | ||
|
@@ -75,6 +84,45 @@ type assetPathMatcher struct { | |
err error | ||
} | ||
|
||
func (m *assetPathMatcher) ModifyPathsForAnnotatedFile(path string) error { | ||
newInclude := []string{} | ||
for _, pattern := range m.include { | ||
absPath, err := filepath.Abs(pattern) | ||
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. Seems odd to convert the pattern to an absolute path, perhaps Also, do we expect these paths to be standard unix or platform-specific? These calls will all be platform specific. If platform specific is intended, the 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. IsAbs is probably better so youre saying we will see the path for our files as If so then why dont we convert everything to an absolute path from the klotho root in readdir so we have a standard path we can follow? Our internal compiler logic shouldnt be dependent on the platform we are running on. |
||
if err != nil { | ||
return err | ||
} | ||
if absPath == pattern { | ||
newInclude = append(newInclude, strings.TrimPrefix(pattern, "/")) | ||
continue | ||
} | ||
relPath, err := filepath.Rel(filepath.Dir("."), filepath.Join(filepath.Dir(path), pattern)) | ||
if err != nil { | ||
return err | ||
} | ||
newInclude = append(newInclude, relPath) | ||
} | ||
m.include = newInclude | ||
|
||
newExclude := []string{} | ||
for _, pattern := range m.exclude { | ||
absPath, err := filepath.Abs(pattern) | ||
if err != nil { | ||
return err | ||
} | ||
if absPath == pattern { | ||
newExclude = append(newExclude, strings.TrimPrefix(pattern, "/")) | ||
continue | ||
} | ||
relPath, err := filepath.Rel(filepath.Dir("."), filepath.Join(filepath.Dir(path), pattern)) | ||
if err != nil { | ||
return err | ||
} | ||
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. nit: Seems like this could be extracted into a reusable function. |
||
newExclude = append(newExclude, relPath) | ||
} | ||
m.exclude = newExclude | ||
return nil | ||
} | ||
|
||
func (m *assetPathMatcher) Matches(p string) bool { | ||
if m.err != nil { | ||
return false | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Having this be a mutator function seems a bit weird to me. An assetPathMatcher isn't valid before calling this function and I'm not sure if it'd behave right if called twice, so callers need to make sure to call it exactly once on construction. Seems like it might be better served in a
NewAssetPathMatcher
constructor function.