pulumi/pkg/compiler/compiler.go
joeduffy e75f06bb2b Sketch a mu build command and its scaffolding
This adds a bunch of general scaffolding and the beginning of a `build` command.

The general engineering scaffolding includes:

* Glide for dependency management.
* A Makefile that runs govet and golint during builds.
* Google's Glog library for logging.
* Cobra for command line functionality.

The Mu-specific scaffolding includes some packages:

* mu/pkg/diag: A package for compiler-like diagnostics.  It's fairly barebones
  at the moment, however we can embellish this over time.
* mu/pkg/errors: A package containing Mu's predefined set of errors.
* mu/pkg/workspace: A package containing workspace-related convenience helpers.

in addition to a main entrypoint that simply wires up and invokes the CLI.  From
there, the mu/cmd package takes over, with the Cobra-defined CLI commands.

Finally, the mu/pkg/compiler package actually implements the compiler behavior.
Or, it will.  For now, it simply parses a JSON or YAML Mufile into the core
mu/pkg/api types, and prints out the result.
2016-11-15 14:30:34 -05:00

59 lines
1.5 KiB
Go

// Copyright 2016 Marapongo, Inc. All rights reserved.
package compiler
import (
"fmt"
"github.com/golang/glog"
"github.com/marapongo/mu/pkg/diag"
"github.com/marapongo/mu/pkg/errors"
"github.com/marapongo/mu/pkg/workspace"
)
// Compiler provides an interface into the many phases of the Mu compilation process.
type Compiler interface {
// Diags fetches the diagnostics sink used by this compiler instance.
Diags() diag.Sink
// Build detects and compiles inputs from the given location, storing build artifacts in the given destination.
Build(inp string, outp string)
}
// compiler is the canonical implementation of the Mu compiler.
type compiler struct {
opts Options
}
// NewCompiler creates a new instance of the Mu compiler, with the given initialization settings.
func NewCompiler(opts Options) Compiler {
return &compiler{opts}
}
func (c *compiler) Diags() diag.Sink {
return c.opts.Diags
}
func (c *compiler) Build(inp string, outp string) {
glog.Infof("Building target directory '%v' to '%v'", inp, outp)
// First find the root of the current package based on the location of its Mufile.
mufile, err := workspace.DetectMufile(inp)
if err != nil {
c.Diags().Errorf(errors.MissingMufile, inp)
return
}
// To build the Mu package, first parse the input file.
p := NewParser(c)
stack := p.Parse(mufile)
// If any errors happened during parsing, we cannot proceed; exit now.
if c.Diags().Errors() > 0 {
return
}
fmt.Printf("PARSED: %v\n", stack)
}