-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes: #9 Adds support for `.slugignore` files. `prepare` will now only copy files which do not match any entry in the `.slugignore` file for a given `source-dir`. The logic is intended to follow logic similar to [this](https://github.com/heroku/slug-compiler/blob/68b63a30907f171e60f8398463d6bbd13b4ed178/lib/slug_compiler.rb#L131-L152): ```ruby lines = File.read(slugignore_path).split total = lines.inject(0) do |total, line| line = (line.split(/#/).first || "").strip if line.empty? total else globs = if line =~ /\// [File.join(build_dir, line)] else # 1.8.7 and 1.9.2 handle expanding ** differently, # where in 1.9.2 ** doesn't match the empty case. So # try empty ** explicitly ["", "**"].map { |g| File.join(build_dir, g, line) } end to_delete = Dir[*globs].uniq.map { |p| File.expand_path(p) } to_delete = to_delete.select { |p| p.match(/^#{build_dir}/) } to_delete.each { |p| FileUtils.rm_rf(p) } total + to_delete.size end end ``` Which as far as I can tell has not changes since Heroku closed-source their compiler, which can be verified by extracting the builder source from Heroku via a custom buildpack.
- Loading branch information
Showing
6 changed files
with
182 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,113 @@ | ||
// package slugignore implements Heroku-like .slugignore functionality for a | ||
// given directory. | ||
// | ||
// Heroku's .slugignore format treats all non-empty and non comment lines | ||
// (comment lines are those beginning with a # characted) as Ruby Dir globs, | ||
package slugignore | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/bmatcuk/doublestar/v4" | ||
) | ||
|
||
// SlugIgnore is the interface for a parsed .slugignore file | ||
// | ||
// A given SlugIgnore is only applicable to the directory which contains the | ||
// .slugignore file, at the time at which it was parsed. | ||
type SlugIgnore interface { | ||
IsIgnored(path string) bool | ||
} | ||
|
||
// ForDirectory parses the .slugignore for a given directory. | ||
// | ||
// If there is no .slugignore file found, it returns a SlugIgnore which always | ||
// returns false when IsIgnored is called. | ||
func ForDirectory(dir string) (SlugIgnore, error) { | ||
f, err := os.Open(filepath.Join(dir, ".slugignore")) | ||
if err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
return &nullSlugIgnore{}, nil | ||
} | ||
|
||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
s := bufio.NewScanner(bufio.NewReader(f)) | ||
globs := []string{} | ||
|
||
for s.Scan() { | ||
line := s.Text() | ||
if strings.HasPrefix(line, "#") { | ||
continue | ||
} | ||
|
||
if strings.TrimSpace(line) == "" { | ||
continue | ||
} | ||
|
||
trimmed := strings.TrimPrefix(line, "/") | ||
if strings.HasPrefix(line, "/") { | ||
globs = append(globs, trimmed) | ||
} else { | ||
globs = append( | ||
globs, | ||
trimmed, | ||
filepath.Join("**", trimmed), | ||
) | ||
} | ||
} | ||
if err := s.Err(); err != nil { | ||
return nil, fmt.Errorf("error parsing .slugignore: %w", err) | ||
} | ||
|
||
ignored := map[string]struct{}{} | ||
for _, glob := range globs { | ||
if !doublestar.ValidatePattern(glob) { | ||
return nil, fmt.Errorf("slugignore pattern is malformed: %v", glob) | ||
} | ||
|
||
matches, err := doublestar.Glob(os.DirFS(dir), glob) | ||
if err != nil { | ||
return nil, fmt.Errorf("error expanding glob %v: %w", glob, err) | ||
} | ||
|
||
for _, match := range matches { | ||
ignored[match] = struct{}{} | ||
} | ||
} | ||
|
||
return cache(ignored), nil | ||
} | ||
|
||
type nullSlugIgnore struct{} | ||
|
||
func (*nullSlugIgnore) IsIgnored(path string) bool { | ||
return false | ||
} | ||
|
||
type cache map[string]struct{} | ||
|
||
func (c cache) IsIgnored(path string) bool { | ||
path = strings.TrimPrefix(path, "/") | ||
|
||
for { | ||
if _, ok := c[path]; ok { | ||
return true | ||
} | ||
|
||
path = filepath.Join(path, "..") | ||
|
||
if path == "." { | ||
break | ||
} | ||
} | ||
|
||
return false | ||
} |