| // Copyright 2018 The LUCI Authors. |
| // |
| // 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 generate implements 'generate' subcommand. |
| package generate |
| |
| import ( |
| "context" |
| "fmt" |
| "os" |
| "path/filepath" |
| "sort" |
| |
| "github.com/maruel/subcommands" |
| |
| "go.chromium.org/luci/common/cli" |
| "go.chromium.org/luci/starlark/interpreter" |
| |
| "go.chromium.org/luci/lucicfg" |
| "go.chromium.org/luci/lucicfg/cli/base" |
| ) |
| |
| // Cmd is 'generate' subcommand. |
| func Cmd(params base.Parameters) *subcommands.Command { |
| return &subcommands.Command{ |
| UsageLine: "generate <input.star>", |
| ShortDesc: "interprets a high-level config, generating *.cfg files", |
| CommandRun: func() subcommands.CommandRun { |
| c := &generateRun{} |
| c.Init(params, false) |
| return c |
| }, |
| } |
| } |
| |
| type generateRun struct { |
| base.Subcommand |
| } |
| |
| type generateResult struct { |
| // TODO |
| } |
| |
| func (c *generateRun) Run(a subcommands.Application, args []string, env subcommands.Env) int { |
| if !c.CheckArgs(args, 1, 1) { |
| return 1 |
| } |
| ctx := cli.GetContext(a, c, env) |
| return c.Done(c.run(ctx, args[0])) |
| } |
| |
| func (c *generateRun) run(ctx context.Context, inputFile string) (*generateResult, error) { |
| abs, err := filepath.Abs(inputFile) |
| if err != nil { |
| return nil, err |
| } |
| |
| // Make sure the input file exists, to make the error message in this case be |
| // more humane. lucicfg.Generate will formulate this error as "no such module" |
| // which looks confusing. |
| switch _, err := os.Stat(abs); { |
| case os.IsNotExist(err): |
| return nil, fmt.Errorf("no such file: %s", inputFile) |
| case err != nil: |
| return nil, err |
| } |
| |
| // The directory with the input file becomes the root of the main package. |
| root, main := filepath.Split(abs) |
| |
| state, err := lucicfg.Generate(ctx, lucicfg.Inputs{ |
| Code: interpreter.FileSystemLoader(root), |
| Entry: main, |
| TextPBHeader: "# Auto-generated by lucicfg.\n# Do not modify manually.\n\n", |
| }) |
| if err != nil { |
| return nil, err |
| } |
| |
| // TODO: write generated files somewhere. |
| |
| files := make([]string, 0, len(state.Configs)) |
| for f := range state.Configs { |
| files = append(files, f) |
| } |
| sort.Strings(files) |
| |
| for _, f := range files { |
| fmt.Println("--------------------------------------------------") |
| fmt.Println(f) |
| fmt.Println("--------------------------------------------------") |
| fmt.Print(state.Configs[f]) |
| fmt.Println("--------------------------------------------------") |
| } |
| return nil, nil |
| } |