Example #1
0
// NewSnapshot returns a new instance of Snapshot backed by a temporary path.
func NewSnapshot() *Snapshot {
	path, err := ioutil.TempDir("", "bake-snapshot-path-")
	if err != nil {
		panic(err)
	}
	root, err := ioutil.TempDir("", "bake-snapshot-root-")
	if err != nil {
		panic(err)
	}
	return &Snapshot{Snapshot: bake.NewSnapshot(path, root)}
}
Example #2
0
// 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
}