예제 #1
0
func main() {
	// Configure logging.
	log.SetFlags(0)
	log.SetPrefix("protoc-gen-json: ")

	// Read input from the protoc compiler.
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		log.Fatal(err, ": failed to read input")
	}

	// Unmarshal the protoc generation request.
	request := &plugin.CodeGeneratorRequest{}
	if err := proto.Unmarshal(data, request); err != nil {
		log.Fatal(err, ": failed to parse input proto")
	}
	if len(request.FileToGenerate) == 0 {
		log.Fatal(err, ": no input files")
	}

	// Parse the command-line parameters.
	params := util.ParseParams(request)

	// Determine the correct output file name.
	name := "out.json"
	if v, ok := params["out"]; ok {
		name = v
	}

	// Determine the JSON indention.
	indent := "  "
	if v, ok := params["indent"]; ok {
		indent = v
	}

	// Perform generation.
	response := &plugin.CodeGeneratorResponse{}
	data, err = json.MarshalIndent(request, "", indent)
	if err != nil {
		response.Error = proto.String(err.Error())
	} else {
		response.File = []*plugin.CodeGeneratorResponse_File{
			&plugin.CodeGeneratorResponse_File{
				Name:    proto.String(name),
				Content: proto.String(string(data)),
			},
		}
	}

	// Marshal the results and write back to the protoc compiler.
	data, err = proto.Marshal(response)
	if err != nil {
		log.Fatal(err, ": failed to marshal output proto")
	}
	_, err = io.Copy(os.Stdout, bytes.NewReader(data))
	if err != nil {
		log.Fatal(err, ": failed to write output proto")
	}
}
예제 #2
0
func main() {
	// Configure logging.
	log.SetFlags(0)
	log.SetPrefix("protoc-gen-doc: ")

	// Create a template generator.
	g := tmpl.New()

	// Read input from the protoc compiler.
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		log.Fatal(err, ": failed to read input")
	}

	// Unmarshal the protoc generation request.
	request := &plugin.CodeGeneratorRequest{}
	if err := proto.Unmarshal(data, request); err != nil {
		log.Fatal(err, ": failed to parse input proto")
	}
	if err := g.SetRequest(request); err != nil {
		log.Fatal(err, ": failed to set request")
	}
	if len(request.FileToGenerate) == 0 {
		log.Fatal(err, ": no input files")
	}

	// Verify the command-line parameters.
	params := util.ParseParams(request)

	// Handle configuration files.
	if conf, ok := params["conf"]; ok {
		confData, err := ioutil.ReadFile(conf)
		if err != nil {
			log.Fatal(err, ": could not read conf file")
		}
		request.Parameter = proto.String(string(confData))
		params = extendParams(params, util.ParseParams(request))
	}

	paramTemplate, haveTemplate := params["template"]
	paramFileMap, haveFileMap := params["filemap"]
	if haveTemplate && haveFileMap {
		log.Fatal("expected either template or filemap argument, not both")
	}

	// Build the filemap based on the command-line parameters.
	var fileMapDir, fileMapData string
	if haveTemplate {
		// Use the specified template file once on each input proto file.
		fileMapData = fmt.Sprintf(basicFileMap, paramTemplate)
	} else if haveFileMap {
		// Load the filemap template.
		data, err := ioutil.ReadFile(paramFileMap)
		if err != nil {
			log.Fatal(err, ": failed to read file map")
		}
		fileMapData = string(data)
		fileMapDir = filepath.Dir(paramFileMap)
	} else {
		// Use the default filemap template once on each input proto file.
		def := PathDir("src/sourcegraph.com/sourcegraph/prototools/templates/tmpl.html")
		fileMapData = fmt.Sprintf(basicFileMap, def)
		fileMapDir = filepath.Dir(def)
	}

	// Parse the file map template.
	if err = g.ParseFileMap(fileMapDir, fileMapData); err != nil {
		log.Fatal(err, ": failed to parse file map")
	}

	// Dump the execute filemap template, if desired.
	if v, ok := params["dump-filemap"]; ok {
		f, err := os.Create(v)
		if err != nil {
			log.Fatal(err, ": failed to crate dump file")
		}
		dump, err := xml.MarshalIndent(g.FileMap, "", "    ")
		if err != nil {
			log.Fatal(err, ": failed to marshal filemap")
		}
		_, err = io.Copy(f, bytes.NewReader(dump))
		if err != nil {
			log.Fatal(err, ": failed to write dump file")
		}
	}

	// Determine the root directory.
	if v, ok := params["root"]; ok {
		g.RootDir = v
	} else {
		g.RootDir, err = os.Getwd()
		if err != nil {
			log.Fatal(err)
		}
	}

	// Map the API host, if any.
	if v, ok := params["apihost"]; ok {
		g.APIHost = v
	}

	// Perform generation.
	response, err := g.Generate()
	if err != nil {
		log.Fatal(err, ": failed to generate")
	}

	// Marshal the results and write back to the protoc compiler.
	data, err = proto.Marshal(response)
	if err != nil {
		log.Fatal(err, ": failed to marshal output proto")
	}
	_, err = io.Copy(os.Stdout, bytes.NewReader(data))
	if err != nil {
		log.Fatal(err, ": failed to write output proto")
	}
}