Skip to content
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

Exclude Path Flag #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 52 additions & 34 deletions rice/embed-go.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

const boxFilename = "rice-box.go"

func operationEmbedGo(pkg *build.Package) {
func operationEmbedGo(pkg *build.Package, excludePaths []string) {

boxMap := findBoxes(pkg)

Expand Down Expand Up @@ -76,44 +76,62 @@ func operationEmbedGo(pkg *build.Package) {
filename = strings.Replace(filename, "\\", "/", -1)
filename = strings.TrimPrefix(filename, "/")
if info.IsDir() {
dirData := &dirDataType{
Identifier: "dir" + nextIdentifier(),
FileName: filename,
ModTime: info.ModTime().Unix(),
ChildFiles: make([]*fileDataType, 0),
ChildDirs: make([]*dirDataType, 0),
includeDir := true
for _, excludePath := range excludePaths {
if strings.Contains(filename, excludePath) {
includeDir = false
break
}
}
verbosef("\tincludes dir: '%s'\n", dirData.FileName)
box.Dirs[dirData.FileName] = dirData

// add tree entry (skip for root, it'll create a recursion)
if dirData.FileName != "" {
pathParts := strings.Split(dirData.FileName, "/")
parentDir := box.Dirs[strings.Join(pathParts[:len(pathParts)-1], "/")]
parentDir.ChildDirs = append(parentDir.ChildDirs, dirData)
if includeDir {
dirData := &dirDataType{
Identifier: "dir" + nextIdentifier(),
FileName: filename,
ModTime: info.ModTime().Unix(),
ChildFiles: make([]*fileDataType, 0),
ChildDirs: make([]*dirDataType, 0),
}
verbosef("\tincludes dir: '%s'\n", dirData.FileName)
box.Dirs[dirData.FileName] = dirData

// add tree entry (skip for root, it'll create a recursion)
if dirData.FileName != "" {
pathParts := strings.Split(dirData.FileName, "/")
parentDir := box.Dirs[strings.Join(pathParts[:len(pathParts)-1], "/")]
parentDir.ChildDirs = append(parentDir.ChildDirs, dirData)
}
}
} else {
fileData := &fileDataType{
Identifier: "file" + nextIdentifier(),
FileName: filename,
ModTime: info.ModTime().Unix(),
includeFile := true
for _, excludePath := range excludePaths {
if strings.Contains(filename, excludePath) {
includeFile = false
break
}
}
verbosef("\tincludes file: '%s'\n", fileData.FileName)
fileData.Content, err = ioutil.ReadFile(path)
if err != nil {
fmt.Printf("error reading file content while walking box: %s\n", err)
os.Exit(1)
}
box.Files = append(box.Files, fileData)

// add tree entry
pathParts := strings.Split(fileData.FileName, "/")
parentDir := box.Dirs[strings.Join(pathParts[:len(pathParts)-1], "/")]
if parentDir == nil {
fmt.Printf("Error: parent of %s is not within the box\n", path)
os.Exit(1)
if includeFile {
fileData := &fileDataType{
Identifier: "file" + nextIdentifier(),
FileName: filename,
ModTime: info.ModTime().Unix(),
}
verbosef("\tincludes file: '%s'\n", fileData.FileName)
fileData.Content, err = ioutil.ReadFile(path)
if err != nil {
fmt.Printf("error reading file content while walking box: %s\n", err)
os.Exit(1)
}
box.Files = append(box.Files, fileData)

// add tree entry
pathParts := strings.Split(fileData.FileName, "/")
parentDir := box.Dirs[strings.Join(pathParts[:len(pathParts)-1], "/")]
if parentDir == nil {
fmt.Printf("Error: parent of %s is not within the box\n", path)
os.Exit(1)
}
parentDir.ChildFiles = append(parentDir.ChildFiles, fileData)
}
parentDir.ChildFiles = append(parentDir.ChildFiles, fileData)
}
return nil
})
Expand Down
4 changes: 2 additions & 2 deletions rice/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func FindBox(s string) {
func LoadBoxes() {
rice := fakerice{}
rice.FindBox("foo")

FindBox("bar")
}
`),
Expand Down Expand Up @@ -232,7 +232,7 @@ func FindBox(s string) {
func LoadBoxes1() {
rice := fakerice{}
rice.FindBox("foo")

FindBox("bar")
}
`),
Expand Down
3 changes: 2 additions & 1 deletion rice/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
// flags
var flags struct {
Verbose bool `long:"verbose" short:"v" description:"Show verbose debug information"`
ImportPaths []string `long:"import-path" short:"i" description:"Import path(s) to use. Using PWD when left empty. Specify multiple times for more import paths to append"`
ImportPaths []string `long:"import-path" short:"i" description:"Import path(s) to use. Using PWD when left empty. Specify multiple times for more import paths to append."`
ExcludePaths []string `long:"exclude-path" short:"e" description:"Exclude path(s). Specify multiple times to exclude paths to append."`

Append struct {
Executable string `long:"exec" description:"Executable to append" required:"true"`
Expand Down
2 changes: 1 addition & 1 deletion rice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
switch flagsParser.Active.Name {
case "embed", "embed-go":
for _, pkg := range pkgs {
operationEmbedGo(pkg)
operationEmbedGo(pkg, flags.ExcludePaths)
}
case "embed-syso":
log.Println("WARNING: embedding .syso is experimental..")
Expand Down
2 changes: 1 addition & 1 deletion rice/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func init() {
{{range .Files}}{{.Identifier}} := &embedded.EmbeddedFile{
Filename: ` + "`" + `{{.FileName}}` + "`" + `,
FileModTime: time.Unix({{.ModTime}}, 0),
Content: string({{.Content | printf "%q"}}),
Content: string({{.Content | printf "%q"}}),
}
{{end}}

Expand Down