示例#1
0
// Ensure a dependency-less target can be parsed.
func TestParser_Parse_NoDependencies(t *testing.T) {
	path := MustTempDir()
	defer MustRemoveAll(path)

	MustWriteFile(filepath.Join(path, "Bakefile.lua"), []byte(`
target("bin/flynn-host", function()
	exec "run1.sh"
	exec "run2.sh"
end)
`))

	// Parse directory.
	p := bake.NewParser()
	if err := p.ParseDir(path); err != nil {
		t.Fatal(err)
	}

	// Parse directory and retrieve target from package.
	target := p.Package.Target("bin/flynn-host")
	if target == nil {
		t.Fatal("expected target")
	} else if len(target.Dependencies) != 0 {
		t.Fatalf("unexpected dependencies: %v", target.Dependencies)
	} else if !reflect.DeepEqual(target.Commands, []bake.Command{
		&bake.ExecCommand{Args: []string{"run1.sh"}},
		&bake.ExecCommand{Args: []string{"run2.sh"}},
	}) {
		t.Fatalf("unexpected commands: %s", spew.Sdump(target.Commands))
	}
}
示例#2
0
// Ensure a target can be parsed with dependencies.
func TestParser_Parse_Dependencies(t *testing.T) {
	path := MustTempDir()
	defer MustRemoveAll(path)

	MustWriteFile(filepath.Join(path, "Bakefile.lua"), []byte(`
target("bin/flynn-host", depends("A", "B"), function() end)
`))

	// Parse directory.
	p := bake.NewParser()
	if err := p.ParseDir(path); err != nil {
		t.Fatal(err)
	}

	// Retrieve target from package.
	target := p.Package.Target("bin/flynn-host")
	if target == nil {
		t.Fatal("expected target")
	} else if !reflect.DeepEqual(target.Dependencies, []string{"A", "B"}) {
		t.Fatalf("unexpected denpendencies: %v", target.Dependencies)
	}
}
示例#3
0
文件: main.go 项目: benbjohnson/bake
// Run executes the program.
func (m *Main) Run() error {
	// Validate arguments.
	if m.Root == "" {
		return errors.New("project root required")
	} else if m.DataDir == "" {
		return errors.New("data directory required")
	}

	// Ensure root is an absolute path.
	root, err := filepath.Abs(m.Root)
	if err != nil {
		return fmt.Errorf("abs path: %s", err)
	}
	m.Root = root

	// Initialize snapshot.
	ss := bake.NewSnapshot(filepath.Join(m.DataDir, m.Root, SnapshotFile), m.Root)

	// Parse build rules.
	parser := bake.NewParser()
	if err := parser.ParseDir(m.Root); err != nil {
		return err
	}
	pkg := parser.Package

	// If no targets are specified then build all targets.
	if len(m.Targets) == 0 {
		m.Targets = pkg.TargetNames()
	}

	// Create planner. Only use snapshot if not force building.
	p := bake.NewPlanner(pkg)
	if !m.Force {
		p.Snapshot = ss
	}

	// Create build plan.
	build, err := p.Plan(m.Targets)
	if err != nil {
		return err
	} else if build == nil {
		fmt.Fprintln(m.Stderr, "nothing to build, exiting")
		return nil
	}
	defer build.Close()

	// Recursively attach all readers to stdout/stderr.
	m.pipeReaders(build, make(map[*bake.Build]struct{}))

	// Create mount directory.
	mountPath, err := ioutil.TempDir("", "bake-")
	if err != nil {
		return err
	}
	defer os.Remove(mountPath)

	// Create file system.
	fs, err := m.openFileSystem(mountPath)
	if err != nil {
		return err
	}
	defer m.closeFileSystem(fs)

	// Execute the build.
	if err := m.build(build, fs, ss); err != nil {
		return err
	}

	return nil
}