Пример #1
0
func TestGenGoObjcWrappers(t *testing.T) {
	if runtime.GOOS != "darwin" {
		t.Skipf("can only generate objc wrappers on darwin")
	}
	for _, filename := range objcTests {
		var buf bytes.Buffer
		refs := fileRefs(t, filename, "ObjC/")
		types, err := objc.Import(refs)
		if err != nil {
			t.Fatal(err)
		}
		tmpGopath, err := ioutil.TempDir(os.TempDir(), "gomobile-bind-test-")
		if err != nil {
			t.Fatal(err)
		}
		defer os.RemoveAll(tmpGopath)
		cg := genObjcPackages(t, tmpGopath, types, &buf)
		pkg := typeCheck(t, filename, tmpGopath)
		cg.GenGo()
		testGenGo(t, filename, &buf, pkg)
	}
}
Пример #2
0
func GenObjcWrappers(pkgs []*build.Package, srcDir, pkgGen string) ([]*objc.Named, error) {
	refs, err := importers.AnalyzePackages(pkgs, "ObjC/")
	if err != nil {
		return nil, err
	}
	types, err := objc.Import(refs)
	if err != nil {
		return nil, err
	}
	var buf bytes.Buffer
	g := &bind.ObjcWrapper{
		Printer: &bind.Printer{
			IndentEach: []byte("\t"),
			Buf:        &buf,
		},
	}
	g.Init(types)
	for i, name := range g.Packages() {
		pkgDir := filepath.Join(pkgGen, "src", "ObjC", name)
		if err := os.MkdirAll(pkgDir, 0700); err != nil {
			return nil, err
		}
		pkgFile := filepath.Join(pkgDir, "package.go")
		generate := func(w io.Writer) error {
			if buildN {
				return nil
			}
			buf.Reset()
			g.GenPackage(i)
			_, err := io.Copy(w, &buf)
			return err
		}
		if err := writeFile(pkgFile, generate); err != nil {
			return nil, fmt.Errorf("failed to create the ObjC wrapper package %s: %v", name, err)
		}
	}
	generate := func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenGo()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(srcDir, "interfaces.go"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the ObjC wrapper Go file: %v", err)
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenInterfaces()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(pkgGen, "src", "ObjC", "interfaces.go"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the ObjC wrapper Go file: %v", err)
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenH()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(srcDir, "interfaces.h"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the ObjC wrapper header file: %v", err)
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenM()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(srcDir, "interfaces.m"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the Java classes ObjC file: %v", err)
	}
	return types, nil
}