blob: 128d1b67d3e2e679bdd0d1f3038fc54ec509b598 [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package generators
import (
"errors"
"fmt"
"io"
"io/ioutil"
"strings"
recipes "fuchsia.googlesource.com/infra/infra/fxicfg/starlark/protos/recipes"
"fuchsia.googlesource.com/infra/infra/starlark/starlarkgen"
"github.com/golang/protobuf/proto"
)
// FuchsiaPySpecGenerator produces Starlark code that generates a spec for the fuchsia.py
// recipe.
type FuchsiaPySpecGenerator struct {
w io.Writer
}
func NewFuchsiaPySpecGenerator(output io.Writer) *FuchsiaPySpecGenerator {
return &FuchsiaPySpecGenerator{output}
}
func (g *FuchsiaPySpecGenerator) Generate(in io.Reader) error {
spec, err := g.parseTextproto(in)
if err != nil {
return fmt.Errorf("failed to parse input: %v", err)
}
output := starlarkgen.Generate(spec)
fmt.Fprintf(g.w, output)
return nil
}
// parse parses a spec for the Fuchsia recipe from the given io.Reader.
func (g *FuchsiaPySpecGenerator) parseTextproto(r io.Reader) (*recipes.Fuchsia, error) {
bytes, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
textproto := strings.TrimSpace(string(bytes))
if textproto == "" {
return nil, errors.New("input is empty")
}
spec := new(recipes.Fuchsia)
if err := proto.UnmarshalText(string(textproto), spec); err != nil {
return nil, err
}
return spec, nil
}