blob: 079045950fb962d469cadc9306ea2358c8226e06 [file] [log] [blame]
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package format registers and implements the "format" apic command.
//
// The format command re-formats an API file to a consistent style.
package main
import (
"bytes"
"flag"
"io/ioutil"
"path/filepath"
"android.googlesource.com/platform/tools/gpu/api/format"
"android.googlesource.com/platform/tools/gpu/api/parser"
"android.googlesource.com/platform/tools/gpu/framework/app"
"android.googlesource.com/platform/tools/gpu/framework/log"
"android.googlesource.com/platform/tools/gpu/framework/parse"
)
func init() {
verb := &app.Verb{
Name: "format",
ShortHelp: "Formats an api file",
Run: doFormat,
}
app.AddVerb(verb)
}
func doFormat(ctx log.Context, flags flag.FlagSet) error {
args := flags.Args()
if len(args) < 1 {
app.Usage(ctx, "Missing api file")
return nil
}
paths := []string{}
for _, path := range args {
files, err := filepath.Glob(path)
if err != nil {
return err
}
paths = append(paths, files...)
}
for _, path := range paths {
ctx := ctx.S("file", path)
f, err := ioutil.ReadFile(path)
if err != nil {
ctx.Fail(err, "Failed to read api file")
continue
}
m := parse.NewCSTMap()
api, errs := parser.Parse(path, string(f), m)
if len(errs) > 0 {
ctx.Error().Log("Errors while parsing api file:")
for i, e := range errs {
ctx.Error().Logf("%d: %v", i, e)
}
continue
}
buf := &bytes.Buffer{}
format.Format(api, m, buf)
if err = ioutil.WriteFile(path, buf.Bytes(), 0777); err != nil {
ctx.Fail(err, "Failed to write formatted api file")
}
}
return nil
}