Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 1 | // 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 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "sort" |
| 23 | ) |
| 24 | |
| 25 | // render writes a gazelle-compatible starlark file based on the enabled dependencies in this planner. |
| 26 | func (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 Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 50 | if d.replace != "" { |
| 51 | fmt.Fprintf(w, " replace = %q,\n", d.replace) |
| 52 | } |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 53 | if d.disableProtoBuild { |
| 54 | fmt.Fprintf(w, " build_file_proto_mode = %q,\n", "disable") |
| 55 | } |
Serge Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 56 | if d.forceBazelGeneration { |
| 57 | fmt.Fprintf(w, " build_file_generation = %q,\n", "on") |
| 58 | } |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 59 | 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") |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 72 | } |
| 73 | if d.prePatches != nil { |
| 74 | fmt.Fprintf(w, " pre_patches = [\n") |
| 75 | for _, patch := range d.prePatches { |
| 76 | fmt.Fprintf(w, " %q,\n", "//third_party/go/patches:"+patch) |
| 77 | } |
| 78 | fmt.Fprintf(w, " ],\n") |
| 79 | } |
| 80 | if d.patches != nil || d.prePatches != nil { |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 81 | fmt.Fprintf(w, " patch_args = [%q],\n", "-p1") |
| 82 | } |
Serge Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 83 | if d.buildExtraArgs != nil { |
| 84 | fmt.Fprintf(w, " build_extra_args = [\n") |
| 85 | for _, arg := range d.buildExtraArgs { |
| 86 | fmt.Fprintf(w, " %q,\n", arg) |
| 87 | } |
| 88 | fmt.Fprintf(w, " ],\n") |
| 89 | } |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 90 | |
| 91 | fmt.Fprintf(w, " )\n") |
| 92 | } |
| 93 | return nil |
| 94 | } |