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 | |
Serge Bazanski | 4b1e37c | 2021-09-28 12:49:15 +0200 | [diff] [blame] | 17 | package fietsje |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "sort" |
| 23 | ) |
| 24 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 25 | // render writes a gazelle-compatible starlark file based on the enabled |
| 26 | // dependencies in this planner. |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 27 | func (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 Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 51 | if d.replace != "" { |
| 52 | fmt.Fprintf(w, " replace = %q,\n", d.replace) |
| 53 | } |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 54 | if d.disableProtoBuild { |
| 55 | fmt.Fprintf(w, " build_file_proto_mode = %q,\n", "disable") |
| 56 | } |
Serge Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 57 | if d.forceBazelGeneration { |
| 58 | fmt.Fprintf(w, " build_file_generation = %q,\n", "on") |
| 59 | } |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 60 | 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 Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 73 | } |
| 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 Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 82 | fmt.Fprintf(w, " patch_args = [%q],\n", "-p1") |
| 83 | } |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 84 | fmt.Fprintf(w, " build_extra_args = [\n") |
Lorenz Brun | c2e3b1b | 2021-11-11 11:06:41 +0100 | [diff] [blame^] | 85 | if d.useImportAliasNaming { |
| 86 | fmt.Fprintf(w, " %q,\n", "-go_naming_convention=import_alias") |
| 87 | fmt.Fprintf(w, " %q,\n", "-go_naming_convention_external=import_alias") |
| 88 | } else { |
| 89 | fmt.Fprintf(w, " %q,\n", "-go_naming_convention=go_default_library") |
| 90 | fmt.Fprintf(w, " %q,\n", "-go_naming_convention_external=go_default_library") |
| 91 | } |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 92 | for _, arg := range d.buildExtraArgs { |
| 93 | fmt.Fprintf(w, " %q,\n", arg) |
Serge Bazanski | 14cf750 | 2020-05-28 14:29:56 +0200 | [diff] [blame] | 94 | } |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 95 | fmt.Fprintf(w, " ],\n") |
Serge Bazanski | f369cfa | 2020-05-22 18:36:42 +0200 | [diff] [blame] | 96 | fmt.Fprintf(w, " )\n") |
| 97 | } |
| 98 | return nil |
| 99 | } |