示例#1
0
// Generate compiles and runs the generator and returns the generated filenames.
func (m *Generator) Generate() ([]string, error) {
	if codegen.OutputDir == "" {
		return nil, fmt.Errorf("missing output directory specification")
	}

	if codegen.DesignPackagePath == "" {
		return nil, fmt.Errorf("missing design package path specification")
	}

	if os.Getenv("GOPATH") == "" {
		return nil, fmt.Errorf("GOPATH not set")
	}

	// Create output directory
	if err := os.MkdirAll(codegen.OutputDir, 0755); err != nil {
		return nil, err
	}

	// Create temporary workspace used for generation
	w, err := codegen.NewWorkspace("goagen")
	if err != nil {
		if _, ok := err.(*os.PathError); ok {
			err = fmt.Errorf(`invalid output directory path "%s"`, codegen.OutputDir)
		}
		return nil, err
	}
	defer func() {
		if !codegen.Debug {
			w.Delete()
		}
	}()
	if codegen.Debug {
		fmt.Printf("goagen source dir: %s\n", w.Path)
	}

	// Figure out design package name from its path
	path, err := codegen.PackageSourcePath(codegen.DesignPackagePath)
	if err != nil {
		return nil, err
	}
	pkgName, err := codegen.PackageName(path)
	if err != nil {
		return nil, err
	}

	// Generate tool source code.
	p, err := w.NewPackage(pkgName)
	if err != nil {
		return nil, err
	}
	m.generateToolSourceCode(p)

	// Compile and run generated tool.
	genbin, err := p.Compile("goagen")
	if err != nil {
		return nil, err
	}
	return m.spawn(genbin)
}
示例#2
0
	var debug bool
	var outputDir string
	var designPackage *codegen.Package
	var designPackagePath string
	var designPackageSource string

	var m *meta.Generator

	BeforeEach(func() {
		genfunc = ""
		debug = false
		designPackagePath = "design"
		designPackageSource = "package design"
		codegen.DesignPackagePath = designPackagePath
		var err error
		outputWorkspace, err = codegen.NewWorkspace("output")
		p, err := outputWorkspace.NewPackage("testOutput")
		Ω(err).ShouldNot(HaveOccurred())
		outputDir = p.Abs()
		designWorkspace, err = codegen.NewWorkspace("test")
		Ω(err).ShouldNot(HaveOccurred())
		compiledFiles = nil
		compileError = nil
	})

	JustBeforeEach(func() {
		if designPackagePath != "" {
			designPackage, err := designWorkspace.NewPackage(designPackagePath)
			Ω(err).ShouldNot(HaveOccurred())
			if designPackageSource != "" {
				file := designPackage.CreateSourceFile("design.go")
示例#3
0
	"github.com/goadesign/goa/dslengine"
	"github.com/goadesign/goa/goagen/codegen"
	"github.com/goadesign/goa/goagen/gen_app"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Generate", func() {
	var workspace *codegen.Workspace
	var outDir string
	var files []string
	var genErr error

	BeforeEach(func() {
		var err error
		workspace, err = codegen.NewWorkspace("test")
		Ω(err).ShouldNot(HaveOccurred())
		outDir, err = ioutil.TempDir(filepath.Join(workspace.Path, "src"), "")
		Ω(err).ShouldNot(HaveOccurred())
		os.Args = []string{"goagen", "--out=" + outDir, "--design=foo"}
	})

	JustBeforeEach(func() {
		files, genErr = genapp.Generate(design.Design)
	})

	AfterEach(func() {
		workspace.Delete()
	})

	Context("with a dummy API", func() {