blob: 58904112ccaf67a93978ed2f6ddfdb73bb89fb09 [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
Serge Bazanski4b1e37c2021-09-28 12:49:15 +020017package fietsje
Serge Bazanskif369cfa2020-05-22 18:36:42 +020018
19import (
20 "fmt"
21 "io"
22 "sort"
23)
24
Serge Bazanski216fe7b2021-05-21 18:36:16 +020025// render writes a gazelle-compatible starlark file based on the enabled
26// dependencies in this planner.
Serge Bazanskif369cfa2020-05-22 18:36:42 +020027func (p *planner) render(w io.Writer) error {
28 fmt.Fprintln(w, `load("@bazel_gazelle//:deps.bzl", "go_repository")`)
29 fmt.Fprintln(w, ``)
30 fmt.Fprintln(w, `def go_repositories():`)
31
32 // Get and sort all enabled importpaths.
33 var enabled []string
34 for importpath, _ := range p.enabled {
35 enabled = append(enabled, importpath)
36 }
37 sort.Slice(enabled, func(i, j int) bool { return enabled[i] < enabled[j] })
38
39 // Render all importpaths.
40 for _, importpath := range enabled {
41 d := p.available[importpath]
42 if err := d.lock(); err != nil {
43 return fmt.Errorf("could not lock %q: %v", importpath, err)
44 }
45
46 fmt.Fprintf(w, " go_repository(\n")
47 fmt.Fprintf(w, " name = %q,\n", d.locked.bazelName)
48 fmt.Fprintf(w, " importpath = %q,\n", d.importpath)
49 fmt.Fprintf(w, " version = %q,\n", d.locked.semver)
50 fmt.Fprintf(w, " sum = %q,\n", d.locked.sum)
Serge Bazanski14cf7502020-05-28 14:29:56 +020051 if d.replace != "" {
52 fmt.Fprintf(w, " replace = %q,\n", d.replace)
53 }
Serge Bazanskif369cfa2020-05-22 18:36:42 +020054 if d.disableProtoBuild {
55 fmt.Fprintf(w, " build_file_proto_mode = %q,\n", "disable")
56 }
Serge Bazanski14cf7502020-05-28 14:29:56 +020057 if d.forceBazelGeneration {
58 fmt.Fprintf(w, " build_file_generation = %q,\n", "on")
59 }
Serge Bazanskif369cfa2020-05-22 18:36:42 +020060 if d.buildTags != nil {
61 fmt.Fprintf(w, " build_tags = [\n")
62 for _, tag := range d.buildTags {
63 fmt.Fprintf(w, " %q,\n", tag)
64 }
65 fmt.Fprintf(w, " ],\n")
66 }
67 if d.patches != nil {
68 fmt.Fprintf(w, " patches = [\n")
69 for _, patch := range d.patches {
70 fmt.Fprintf(w, " %q,\n", "//third_party/go/patches:"+patch)
71 }
72 fmt.Fprintf(w, " ],\n")
Lorenz Brunefb028f2020-07-28 17:04:49 +020073 }
74 if d.prePatches != nil {
75 fmt.Fprintf(w, " pre_patches = [\n")
76 for _, patch := range d.prePatches {
77 fmt.Fprintf(w, " %q,\n", "//third_party/go/patches:"+patch)
78 }
79 fmt.Fprintf(w, " ],\n")
80 }
81 if d.patches != nil || d.prePatches != nil {
Serge Bazanskif369cfa2020-05-22 18:36:42 +020082 fmt.Fprintf(w, " patch_args = [%q],\n", "-p1")
83 }
Serge Bazanskif12bedf2021-01-15 16:58:50 +010084 fmt.Fprintf(w, " build_extra_args = [\n")
85 fmt.Fprintf(w, " %q,\n", "-go_naming_convention=go_default_library")
86 fmt.Fprintf(w, " %q,\n", "-go_naming_convention_external=go_default_library")
87 for _, arg := range d.buildExtraArgs {
88 fmt.Fprintf(w, " %q,\n", arg)
Serge Bazanski14cf7502020-05-28 14:29:56 +020089 }
Serge Bazanskif12bedf2021-01-15 16:58:50 +010090 fmt.Fprintf(w, " ],\n")
Serge Bazanskif369cfa2020-05-22 18:36:42 +020091 fmt.Fprintf(w, " )\n")
92 }
93 return nil
94}