예제 #1
0
파일: generator.go 프로젝트: bketelsen/goa
func (m *Generator) generateToolSourceCode(pkg *codegen.Package) {
	file := pkg.CreateSourceFile("main.go")
	imports := append(m.Imports,
		codegen.SimpleImport("fmt"),
		codegen.SimpleImport("os"),
		codegen.SimpleImport("strings"),
		codegen.NewImport(".", "github.com/raphael/goa/design"),
		codegen.NewImport(".", "github.com/raphael/goa/design/dsl"),
		codegen.NewImport("_", filepath.ToSlash(codegen.DesignPackagePath)),
	)
	file.WriteHeader("Code Generator", "main", imports)
	tmpl, err := template.New("generator").Parse(mainTmpl)
	if err != nil {
		panic(err) // bug
	}
	pkgName, err := codegen.PackageName(pkg.Abs())
	if err != nil {
		panic(err)
	}
	context := map[string]string{
		"Genfunc":       m.Genfunc,
		"DesignPackage": codegen.DesignPackagePath,
		"PkgName":       pkgName,
	}
	err = tmpl.Execute(file, context)
	if err != nil {
		panic(err) // bug
	}
	if codegen.Debug {
		src, _ := ioutil.ReadFile(file.Abs())
		fmt.Printf("goagen source:\n%s\n", src)
	}
}
예제 #2
0
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/raphael/goa/goagen/codegen"
	"github.com/raphael/goa/goagen/meta"
)

var _ = Describe("Run", func() {
	var compiledFiles []string
	var compileError error
	var outputWorkspace *codegen.Workspace
	var designWorkspace *codegen.Workspace

	var genfunc string
	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())
예제 #3
0
		It("instantiates a generator even if Design is not initialized", func() {
			dsl.InitDesign()
			var err error
			gen, err = genschema.NewGenerator()
			Ω(err).ShouldNot(HaveOccurred())
			Ω(gen).ShouldNot(BeNil())
		})
	})
})

var _ = Describe("Generate", func() {
	var gen *genschema.Generator
	var files []string
	var genErr error
	var workspace *codegen.Workspace
	var testPkg *codegen.Package

	BeforeEach(func() {
		var err error
		workspace, err = codegen.NewWorkspace("test")
		Ω(err).ShouldNot(HaveOccurred())
		testPkg, err = workspace.NewPackage("schematest")
		Ω(err).ShouldNot(HaveOccurred())
		os.Args = []string{"codegen", "--out=" + testPkg.Abs(), "--design=foo"}
	})

	JustBeforeEach(func() {
		var err error
		gen, err = genschema.NewGenerator()
		Ω(err).ShouldNot(HaveOccurred())
		files, genErr = gen.Generate(design.Design)