Skip to content

Bundle merges multiple Go source files into single one

License

Notifications You must be signed in to change notification settings

bbrodriges/bundle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bundle

Bundle merges multiple Go source files into single one.

Install

go get -u github.com/bbrodriges/bundle

Usage

As a separate command:

$ bundle --help
Usage: bundle -p <package_name> [-d] pattern...
Available options:
  -d	Delete source files after bundling
  -p string
    	Package name to be written into result file. Required

$ bundle -d -p main *_gen.go > bundle.go # bundle all autogenerated files in current folder
$ bundle -d -p main -o bundle.go user.msgp.go user.str.go # bundle only specific files

As a go generate flag:

package types

//go:generate msgp -o=user.msgp.go -tests=false -io=false
//go:generate stringer -type=User -output=user.str.go -linecomment
//msgp:shim UserState as:string using:(UserState).String/UserStateFromString

//go:generate bundle -d -p=types -o=user_gen.go user.msgp.go user.str.go

type User struct {
    Username string `msg:"username"`
    Email string `msg:"email"`
    State UserState `msg:"state"`
}

type UserState int

const (
    Unknown UserState = iota -1 // UNKNOWN
    Inactive                    // INACTIVE
    Active                      // ACTIVE
    Banned                      // BANNED
)

func UserStateFromString(s string) UserState {
    switch s {
    case "INACTIVE":
        return Inactive
    case "ACTIVE":
        return Active
    case "BANNED":
        return Banned
    }
    return Unknown
}

The above code upon calling go generate . will:

  1. create files called user.msgp.go and user.str.go with containing autogenerated code
  2. bundle these two files into single user_gen.go and delete source files user.msgp.go and user.str.go

Limitations

  • bundle cannot handle definition conflicts (e.g. structs, functions, constants of the same names)
  • bundle cannot handle import cycle conflicts

All it does is collects and strips out import declarations from every given source files AST and concats them into single file with helping comments.

About

Bundle merges multiple Go source files into single one

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages