Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
Rewrite gb.Project to be an interface (#614)
Browse files Browse the repository at this point in the history
* Rewrite gb.Project to be an interface

* remove gb.Srcdir
  • Loading branch information
davecheney committed Jun 21, 2016
1 parent fcccb46 commit 93e0c13
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 83 deletions.
2 changes: 1 addition & 1 deletion build_test.go
Expand Up @@ -76,7 +76,7 @@ func TestBuild(t *testing.T) {

proj := testProject(t)
for _, tt := range tests {
ctx, err := proj.NewContext(tt.opts...)
ctx, err := NewContext(proj, tt.opts...)
ctx.Force = true
defer ctx.Destroy()
pkg, err := ctx.ResolvePackage(tt.pkg)
Expand Down
10 changes: 3 additions & 7 deletions cmd/cmd.go
Expand Up @@ -4,7 +4,6 @@ package cmd
import (
"flag"
"os"
"path/filepath"

"github.com/constabulary/gb"
"github.com/constabulary/gb/internal/debug"
Expand Down Expand Up @@ -78,11 +77,8 @@ func NewContext(projectroot string, options ...func(*gb.Context) error) (*gb.Con
if err != nil {
return nil, errors.Wrap(err, "could not locate project root")
}
project := gb.NewProject(root,
gb.SourceDir(filepath.Join(root, "src")),
gb.SourceDir(filepath.Join(root, "vendor", "src")),
)
proj := gb.NewProject(root)

debug.Debugf("project root %q", project.Projectdir())
return project.NewContext(options...)
debug.Debugf("project root %q", proj.Projectdir())
return gb.NewContext(proj, options...)
}
9 changes: 2 additions & 7 deletions cmd/gb-vendor/main.go
Expand Up @@ -53,10 +53,7 @@ func main() {
if err != nil {
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")),
)
project := gb.NewProject(root)

debug.Debugf("project root %q", project.Projectdir())

Expand All @@ -79,9 +76,7 @@ func main() {
args = fs.Args() // reset args to the leftovers from fs.Parse
debug.Debugf("args: %v", args)

ctx, err := project.NewContext(
gb.GcToolchain(),
)
ctx, err := gb.NewContext(project, gb.GcToolchain())
if err != nil {
fatalf("unable to construct context: %v", err)
}
Expand Down
11 changes: 4 additions & 7 deletions context.go
Expand Up @@ -20,7 +20,7 @@ import (

// Context represents an execution of one or more Targets inside a Project.
type Context struct {
*Project
Project

importers []interface {
Import(path string) (*importer.Package, error)
Expand Down Expand Up @@ -110,10 +110,7 @@ func WithRace(c *Context) error {
// NewContext returns a new build context from this project.
// By default this context will use the gc toolchain with the
// host's GOOS and GOARCH values.
func (p *Project) NewContext(opts ...func(*Context) error) (*Context, error) {
if len(p.srcdirs) == 0 {
return nil, errors.New("no source directories supplied")
}
func NewContext(p Project, opts ...func(*Context) error) (*Context, error) {
envOr := func(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
Expand Down Expand Up @@ -236,9 +233,9 @@ func (c *Context) Workdir() string { return c.workdir }
// ResolvePackage resolves the package at path using the current context.
func (c *Context) ResolvePackage(path string) (*Package, error) {
if path == "." {
return nil, errors.Errorf("%q is not a package", filepath.Join(c.rootdir, "src"))
return nil, errors.Errorf("%q is not a package", filepath.Join(c.Projectdir(), "src"))
}
path, err := relImportPath(filepath.Join(c.rootdir, "src"), path)
path, err := relImportPath(filepath.Join(c.Projectdir(), "src"), path)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions context_test.go
Expand Up @@ -57,7 +57,7 @@ func TestContextCtxString(t *testing.T) {

proj := testProject(t)
for _, tt := range tests {
ctx, err := proj.NewContext(tt.opts...)
ctx, err := NewContext(proj, tt.opts...)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestContextLoadPackage(t *testing.T) {

proj := testProject(t)
for _, tt := range tests {
ctx, err := proj.NewContext(tt.opts...)
ctx, err := NewContext(proj, tt.opts...)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestContextImportPackage(t *testing.T) {
}}

for _, tt := range tests {
ctx, err := proj.NewContext()
ctx, err := NewContext(proj)
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 2 additions & 5 deletions example_test.go
Expand Up @@ -15,14 +15,11 @@ func ExampleNewProject() {

// 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
)
proj := gb.NewProject(root)

// 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()
ctx, err := gb.NewContext(proj)
if err != nil {
log.Fatal("Could not create new context:", err)
}
Expand Down
10 changes: 5 additions & 5 deletions install_test.go
Expand Up @@ -33,15 +33,15 @@ func TestStale(t *testing.T) {
root := mktemp(t)
defer os.RemoveAll(root)

proj := Project{
proj := &project{
rootdir: root,
srcdirs: []Srcdir{{
Root: filepath.Join(getwd(t), "testdata", "src"),
}},
srcdirs: []string{
filepath.Join(getwd(t), "testdata", "src"),
},
}

newctx := func() *Context {
ctx, err := proj.NewContext(
ctx, err := NewContext(proj,
GcToolchain(),
)
if err != nil {
Expand Down
20 changes: 11 additions & 9 deletions package_test.go
Expand Up @@ -10,17 +10,19 @@ import (
"github.com/constabulary/gb/internal/importer"
)

func testProject(t *testing.T) *Project {
func testProject(t *testing.T) Project {
cwd := getwd(t)
root := filepath.Join(cwd, "testdata")
return NewProject(root,
SourceDir(filepath.Join(root, "src")),
)
return &project{
rootdir: root,
srcdirs: []string{
filepath.Join(root, "src"),
},
}
}

func testContext(t *testing.T, opts ...func(*Context) error) *Context {
prj := testProject(t)
ctx, err := prj.NewContext(opts...)
ctx, err := NewContext(testProject(t), opts...)
if err != nil {
t.Fatal(err)
}
Expand All @@ -41,7 +43,7 @@ func TestResolvePackage(t *testing.T) {
}}
proj := testProject(t)
for _, tt := range tests {
ctx, err := proj.NewContext(tt.opts...)
ctx, err := NewContext(proj, tt.opts...)
defer ctx.Destroy()
_, err = ctx.ResolvePackage(tt.pkg)
if !reflect.DeepEqual(err, tt.err) {
Expand Down Expand Up @@ -91,7 +93,7 @@ func TestPackageBinfile(t *testing.T) {

proj := testProject(t)
for i, tt := range tests {
ctx, _ := proj.NewContext(tt.opts...)
ctx, _ := NewContext(proj, tt.opts...)
defer ctx.Destroy()
pkg, err := ctx.ResolvePackage(tt.pkg)
if err != nil {
Expand Down Expand Up @@ -206,7 +208,7 @@ func TestNewPackage(t *testing.T) {
}}
proj := testProject(t)
for i, tt := range tests {
ctx, _ := proj.NewContext()
ctx, _ := NewContext(proj)
defer ctx.Destroy()

got, err := ctx.NewPackage(&tt.pkg)
Expand Down
50 changes: 27 additions & 23 deletions project.go
Expand Up @@ -11,52 +11,56 @@ import (
// $PROJECT/ - the project root
// $PROJECT/src/ - base directory for the source of packages
// $PROJECT/bin/ - base directory for the compiled binaries
type Project struct {
rootdir string
srcdirs []Srcdir
type Project interface {

// Projectdir returns the path root of this project.
Projectdir() string

// Pkgdir returns the path to precompiled packages.
Pkgdir() string

// Bindir returns the path for compiled programs.
Bindir() string

// Srcdirs returns the path to the source directories.
Srcdirs() []string
}

func SourceDir(root string) func(*Project) {
return func(p *Project) {
p.srcdirs = append(p.srcdirs, Srcdir{Root: root})
}
type project struct {
rootdir string
srcdirs []string
}

func NewProject(root string, options ...func(*Project)) *Project {
proj := Project{
func NewProject(root string) Project {
proj := project{
rootdir: root,
srcdirs: []string{
filepath.Join(root, "src"),
filepath.Join(root, "vendor", "src"),
},
}

for _, opt := range options {
opt(&proj)
}

return &proj
}

// Pkgdir returns the path to precompiled packages.
func (p *Project) Pkgdir() string {
func (p *project) Pkgdir() string {
return filepath.Join(p.rootdir, "pkg")
}

// Projectdir returns the path root of this project.
func (p *Project) Projectdir() string {
func (p *project) Projectdir() string {
return p.rootdir
}

// Srcdirs returns the path to the source directories.
// The first source directory will always be
// filepath.Join(Projectdir(), "src")
// but there may be additional directories.
func (p *Project) Srcdirs() []string {
var dirs []string
for _, s := range p.srcdirs {
dirs = append(dirs, s.Root)
}
return dirs
func (p *project) Srcdirs() []string {
return p.srcdirs
}

// Bindir returns the path for compiled programs.
func (p *Project) Bindir() string {
func (p *project) Bindir() string {
return filepath.Join(p.rootdir, "bin")
}
10 changes: 0 additions & 10 deletions src.go

This file was deleted.

9 changes: 3 additions & 6 deletions test/test_test.go
Expand Up @@ -223,18 +223,15 @@ func getwd(t *testing.T) string {
return cwd
}

func testProject(t *testing.T) *gb.Project {
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")),
)
return gb.NewProject(root)
}

func testContext(t *testing.T, opts ...func(*gb.Context) error) *gb.Context {
prj := testProject(t)
opts = append([]func(*gb.Context) error{gb.GcToolchain()}, opts...)
ctx, err := prj.NewContext(opts...)
ctx, err := gb.NewContext(testProject(t), opts...)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 93e0c13

Please sign in to comment.