blob: bc5d732c35479ce5df0c069f47ce08df326a3935 [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)
Serge Bazanski14cf7502020-05-28 14:29:56 +020050 if d.replace != "" {
51 fmt.Fprintf(w, " replace = %q,\n", d.replace)
52 }
Serge Bazanskif369cfa2020-05-22 18:36:42 +020053 if d.disableProtoBuild {
54 fmt.Fprintf(w, " build_file_proto_mode = %q,\n", "disable")
55 }
Serge Bazanski14cf7502020-05-28 14:29:56 +020056 if d.forceBazelGeneration {
57 fmt.Fprintf(w, " build_file_generation = %q,\n", "on")
58 }
Serge Bazanskif369cfa2020-05-22 18:36:42 +020059 if d.buildTags != nil {
60 fmt.Fprintf(w, " build_tags = [\n")
61 for _, tag := range d.buildTags {
62 fmt.Fprintf(w, " %q,\n", tag)
63 }
64 fmt.Fprintf(w, " ],\n")
65 }
66 if d.patches != nil {
67 fmt.Fprintf(w, " patches = [\n")
68 for _, patch := range d.patches {
69 fmt.Fprintf(w, " %q,\n", "//third_party/go/patches:"+patch)
70 }
71 fmt.Fprintf(w, " ],\n")
72 fmt.Fprintf(w, " patch_args = [%q],\n", "-p1")
73 }
Serge Bazanski14cf7502020-05-28 14:29:56 +020074 if d.buildExtraArgs != nil {
75 fmt.Fprintf(w, " build_extra_args = [\n")
76 for _, arg := range d.buildExtraArgs {
77 fmt.Fprintf(w, " %q,\n", arg)
78 }
79 fmt.Fprintf(w, " ],\n")
80 }
Serge Bazanskif369cfa2020-05-22 18:36:42 +020081
82 fmt.Fprintf(w, " )\n")
83 }
84 return nil
85}