示例#1
0
文件: gen.go 项目: crozz-chen/apimeta
func GenFiles(out, project, tplStr string, bs []byte) error {
	models, apis, err := meta.Load(bs)
	if nil != err {
		return err
	}

	if !strings.HasSuffix(out, "/") {
		out += "/"
	}
	path_file := Convert(project, models, apis)
	for path, file := range path_file {
		if len(file.Models) == 0 && len(file.Apis) == 0 {
			continue
		}

		path = out + path
		if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
			return err
		}

		buf := bytes.NewBuffer(*new([]byte))
		GenFileContent(tplStr, file, buf)
		//		bs := wr.Bytes()
		bs, err := format.Source(buf.Bytes())
		if err != nil {
			return err
		}

		if err := ioutil.WriteFile(path, bs, os.ModePerm); err != nil {
			return err
		}
	}
	return nil
}
示例#2
0
文件: gen.go 项目: crozz-chen/apimeta
func GenZip(out, project, tplStr string, bs []byte) error {
	models, apis, err := meta.Load(bs)
	if nil != err {
		return err
	}

	if !strings.HasSuffix(out, "/") {
		out += "/"
	}
	if err := os.MkdirAll(out, os.ModePerm); err != nil {
		return err
	}
	f, err := os.Create(out + project + ".zip")
	defer f.Close()
	if err != nil {
		return err
	}
	ar := zip.NewWriter(f)
	defer ar.Close()

	path_file := Convert(project, models, apis)
	for path, file := range path_file {
		if len(file.Models) == 0 && len(file.Apis) == 0 {
			continue
		}

		buf := bytes.NewBuffer(*new([]byte))
		GenFileContent(tplStr, file, buf)
		//		bs := wr.Bytes()
		bs, err := format.Source(buf.Bytes())
		if err != nil {
			return err
		}

		item, err := ar.Create(path)
		if err != nil {
			return err
		}
		buf = bytes.NewBuffer(bs)
		_, err = buf.WriteTo(item)
		if err != nil {
			return err
		}
	}
	return nil
}