blob: d06e5306322bdeb76f9db53de3196e71c7b39a0f [file] [log] [blame]
Serge Bazanskif369cfa2020-05-22 18:36:42 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package main
18
19import (
20 "fmt"
21 "io"
22 "sort"
23)
24
25// render writes a gazelle-compatible starlark file based on the enabled dependencies in this planner.
26func (p *planner) render(w io.Writer) error {
27 fmt.Fprintln(w, `load("@bazel_gazelle//:deps.bzl", "go_repository")`)
28 fmt.Fprintln(w, ``)
29 fmt.Fprintln(w, `def go_repositories():`)
30
31 // Get and sort all enabled importpaths.
32 var enabled []string
33 for importpath, _ := range p.enabled {
34 enabled = append(enabled, importpath)
35 }
36 sort.Slice(enabled, func(i, j int) bool { return enabled[i] < enabled[j] })
37
38 // Render all importpaths.
39 for _, importpath := range enabled {
40 d := p.available[importpath]
41 if err := d.lock(); err != nil {
42 return fmt.Errorf("could not lock %q: %v", importpath, err)
43 }
44
45 fmt.Fprintf(w, " go_repository(\n")
46 fmt.Fprintf(w, " name = %q,\n", d.locked.bazelName)
47 fmt.Fprintf(w, " importpath = %q,\n", d.importpath)
48 fmt.Fprintf(w, " version = %q,\n", d.locked.semver)
49 fmt.Fprintf(w, " sum = %q,\n", d.locked.sum)
50 if d.disableProtoBuild {
51 fmt.Fprintf(w, " build_file_proto_mode = %q,\n", "disable")
52 }
53 if d.buildTags != nil {
54 fmt.Fprintf(w, " build_tags = [\n")
55 for _, tag := range d.buildTags {
56 fmt.Fprintf(w, " %q,\n", tag)
57 }
58 fmt.Fprintf(w, " ],\n")
59 }
60 if d.patches != nil {
61 fmt.Fprintf(w, " patches = [\n")
62 for _, patch := range d.patches {
63 fmt.Fprintf(w, " %q,\n", "//third_party/go/patches:"+patch)
64 }
65 fmt.Fprintf(w, " ],\n")
66 fmt.Fprintf(w, " patch_args = [%q],\n", "-p1")
67 }
68
69 fmt.Fprintf(w, " )\n")
70 }
71 return nil
72}