func testProject(t *testing.T) *gb.Project { cwd := getwd(t) root := filepath.Join(cwd, "..", "testdata") return gb.NewProject(root, gb.SourceDir(filepath.Join(root, "src")), ) }
// RunCommand detects the project root, parses flags and runs the Command. func RunCommand(fs *flag.FlagSet, cmd *Command, projectroot, goroot string, args []string) error { if cmd.AddFlags != nil { cmd.AddFlags(fs) } if err := fs.Parse(args); err != nil { fs.Usage() os.Exit(1) } args = fs.Args() // reset to the remaining arguments if projectroot == "" { return fmt.Errorf("project root is blank") } root, err := FindProjectroot(projectroot) if err != nil { return fmt.Errorf("could not locate project root: %v", err) } project := gb.NewProject(root) gb.Debugf("project root %q", project.Projectdir()) ctx, err := project.NewContext( gb.GcToolchain(), ) if err != nil { return fmt.Errorf("unable to construct context: %v", err) } gb.Debugf("args: %v", args) return cmd.Run(ctx, args) }
func main() { args := os.Args[1:] switch { case len(args) < 1, args[0] == "-h", args[0] == "-help": fs.Usage() os.Exit(1) case args[0] == "help": help(args[1:]) return case projectroot == "": log.Fatalf("don't run this binary directly, it is meant to be run as 'gb vendor ...'") default: } root, err := cmd.FindProjectroot(projectroot) if err != nil { log.Fatalf("could not locate project root: %v", err) } project := gb.NewProject(root, gb.SourceDir(filepath.Join(root, "src")), gb.SourceDir(filepath.Join(root, "vendor", "src"))) log.Debugf("project root %q", project.Projectdir()) for _, command := range commands { if command.Name == args[0] && command.Runnable() { // add extra flags if necessary if command.AddFlags != nil { command.AddFlags(fs) } if command.FlagParse != nil { err = command.FlagParse(fs, args) } else { err = fs.Parse(args[1:]) } if err != nil { log.Fatalf("could not parse flags: %v", err) } args = fs.Args() // reset args to the leftovers from fs.Parse log.Debugf("args: %v", args) ctx, err := project.NewContext( gb.GcToolchain(), ) if err != nil { log.Fatalf("unable to construct context: %v", err) } defer ctx.Destroy() if err := command.Run(ctx, args); err != nil { log.Fatalf("command %q failed: %v", command.Name, err) } return } } log.Fatalf("unknown command %q ", args[0]) }
func main() { root, err := cmd.FindProjectroot(projectroot) if err != nil { gb.Fatalf("could not locate project root: %v", err) } project := gb.NewProject(root) gb.Debugf("project root %q", project.Projectdir()) args := os.Args[1:] if len(args) < 1 || args[0] == "-h" { fs.Usage() os.Exit(1) } if args[0] == "help" { help(args[1:]) return } for _, command := range commands { if command.Name == args[0] && command.Runnable() { // add extra flags if necessary if command.AddFlags != nil { command.AddFlags(fs) } if command.FlagParse != nil { err = command.FlagParse(fs, args) } else { err = fs.Parse(args[1:]) } if err != nil { gb.Fatalf("could not parse flags: %v", err) } args = fs.Args() // reset args to the leftovers from fs.Parse gb.Debugf("args: %v", args) ctx, err := project.NewContext( gb.GcToolchain(), ) if err != nil { gb.Fatalf("unable to construct context: %v", err) } if err := command.Run(ctx, args); err != nil { gb.Fatalf("command %q failed: %v", command.Name, err) } return } } gb.Fatalf("unknown command %q ", args[0]) }
// NewContext creates a gb.Context for the project root. func NewContext(projectroot string, options ...func(*gb.Context) error) (*gb.Context, error) { if projectroot == "" { return nil, fmt.Errorf("project root is blank") } root, err := FindProjectroot(projectroot) if err != nil { return nil, fmt.Errorf("could not locate project root: %v", err) } project := gb.NewProject(root, gb.SourceDir(filepath.Join(root, "src")), gb.SourceDir(filepath.Join(root, "vendor", "src")), ) debug.Debugf("project root %q", project.Projectdir()) return project.NewContext(options...) }
func ExampleNewProject() { // Every project begins with a project root. // Normally you'd check this out of source control. root := filepath.Join("home", "dfc", "devel", "demo") // Create a new Project passing in the source directories // under this project's root. proj := gb.NewProject(root, gb.SourceDir(filepath.Join(root, "src")), // $PROJECT/src gb.SourceDir(filepath.Join(root, "vendor", "src")), // $PROJECT/vendor/src ) // Create a new Context from the Project. A Context holds // the state of a specific compilation or test within the Project. ctx, err := proj.NewContext() if err != nil { log.Fatal("Could not create new context:", err) } // Always remember to clean up your Context ctx.Destroy() }
func testProject(t *testing.T) *gb.Project { cwd := getwd(t) root := filepath.Join(cwd, "..", "testdata") return gb.NewProject(root) }
func main() { args := os.Args if len(args) < 2 || args[1] == "-h" { fs.Usage() os.Exit(1) } name := args[1] if name == "help" { help(args[2:]) return } command, ok := commands[name] if (command != nil && !command.Runnable()) || !ok { if _, err := lookupPlugin(name); err != nil { gb.Errorf("unknown command %q", name) fs.Usage() os.Exit(1) } command = commands["plugin"] args = append([]string{"plugin"}, args...) } // add extra flags if necessary if command.AddFlags != nil { command.AddFlags(fs) } var err error if command.FlagParse != nil { err = command.FlagParse(fs, args) } else { err = fs.Parse(args[2:]) } if err != nil { gb.Fatalf("could not parse flags: %v", err) } args = fs.Args() // reset args to the leftovers from fs.Parse cwd, err := filepath.Abs(cwd) // if cwd was passed in via -R, make sure it is absolute if err != nil { gb.Fatalf("could not make project root absolute: %v", err) } root, err := cmd.FindProjectroot(cwd) if err != nil { gb.Fatalf("could not locate project root: %v", err) } project := gb.NewProject(root) gb.Debugf("project root %q", project.Projectdir()) ctx, err := project.NewContext( gb.GcToolchain(), gb.Ldflags(ldflags), ) if err != nil { gb.Fatalf("unable to construct context: %v", err) } if command.ParseArgs != nil { args = command.ParseArgs(ctx, root, args) } else { args = cmd.ImportPaths(ctx, root, args) } gb.Debugf("args: %v", args) if err := command.Run(ctx, args); err != nil { gb.Fatalf("command %q failed: %v", name, err) } }