Skip to content

Commit 53491bf

Browse files
refactor: consolidate license generation in internal/sidekick/license and internal/yaml/copyright (#3715)
Consolidate license generation into a shared internal package by merging duplicated logic between `internal/sidekick/license` and `internal/yaml/copyright`. Key changes: - Moved `internal/sidekick/license` to `internal/license`. - Removed `internal/yaml/copyright.go` and updated `internal/yaml` to use the new `internal/license` package for generating license headers. This is part of a larger refactoring effort to delete `internal/sidekick/sidekick` the CLI and associated configs Fixes #3716 --------- Signed-off-by: James Wu <jameslynnwu@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent f39c370 commit 53491bf

File tree

9 files changed

+43
-37
lines changed

9 files changed

+43
-37
lines changed

internal/sidekick/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"os"
2323
"path"
2424

25-
"github.com/googleapis/librarian/internal/sidekick/license"
25+
"github.com/googleapis/librarian/internal/license"
2626
"github.com/pelletier/go-toml/v2"
2727
)
2828

internal/sidekick/dart/annotate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323
"strconv"
2424
"strings"
2525

26+
"github.com/googleapis/librarian/internal/license"
2627
"github.com/googleapis/librarian/internal/sidekick/api"
2728
"github.com/googleapis/librarian/internal/sidekick/language"
28-
"github.com/googleapis/librarian/internal/sidekick/license"
2929
"github.com/iancoleman/strcase"
3030
)
3131

internal/sidekick/rust/annotate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"slices"
2222
"strings"
2323

24+
"github.com/googleapis/librarian/internal/license"
2425
"github.com/googleapis/librarian/internal/sidekick/api"
2526
"github.com/googleapis/librarian/internal/sidekick/language"
26-
"github.com/googleapis/librarian/internal/sidekick/license"
2727
"github.com/iancoleman/strcase"
2828
)
2929

internal/sidekick/rust_prost/annotate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ package rust_prost
1818
import (
1919
"strings"
2020

21+
"github.com/googleapis/librarian/internal/license"
2122
"github.com/googleapis/librarian/internal/sidekick/api"
2223
"github.com/googleapis/librarian/internal/sidekick/config"
23-
"github.com/googleapis/librarian/internal/sidekick/license"
2424
"github.com/googleapis/librarian/internal/sidekick/protobuf"
2525
"github.com/googleapis/librarian/internal/sidekick/rust"
2626
)

internal/yaml/copyright.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

internal/yaml/yaml.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ package yaml
1818
import (
1919
"bytes"
2020
"os"
21+
"strconv"
22+
"strings"
23+
"time"
2124

2225
"github.com/google/yamlfmt/formatters/basic"
26+
"github.com/googleapis/librarian/internal/license"
2327
"gopkg.in/yaml.v3"
2428
)
2529

@@ -79,7 +83,19 @@ func Write(path string, v any) error {
7983
if err != nil {
8084
return err
8185
}
82-
data = append([]byte(copyright), data...)
86+
87+
// Add # comment prefix to each line of the license header.
88+
var b strings.Builder
89+
year := time.Now().Year()
90+
for _, line := range license.LicenseHeader(strconv.Itoa(year)) {
91+
b.WriteString("#")
92+
b.WriteString(line)
93+
b.WriteString("\n")
94+
}
95+
b.WriteString("\n")
96+
header := b.String()
97+
98+
data = append([]byte(header), data...)
8399
return os.WriteFile(path, data, 0644)
84100
}
85101

internal/yaml/yaml_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
package yaml
1616

1717
import (
18+
"fmt"
1819
"os"
1920
"path/filepath"
21+
"strconv"
2022
"testing"
23+
"time"
2124

2225
"github.com/google/go-cmp/cmp"
2326
)
@@ -75,8 +78,26 @@ func TestReadWrite(t *testing.T) {
7578
}
7679
}
7780

81+
const copyright = `# Copyright %s Google LLC
82+
#
83+
# Licensed under the Apache License, Version 2.0 (the "License");
84+
# you may not use this file except in compliance with the License.
85+
# You may obtain a copy of the License at
86+
#
87+
# https://www.apache.org/licenses/LICENSE-2.0
88+
#
89+
# Unless required by applicable law or agreed to in writing, software
90+
# distributed under the License is distributed on an "AS IS" BASIS,
91+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
92+
# See the License for the specific language governing permissions and
93+
# limitations under the License.
94+
95+
`
96+
7897
func TestWrite(t *testing.T) {
79-
want := copyright + `name: test
98+
// Construct expected header
99+
header := fmt.Sprintf(copyright, strconv.Itoa(time.Now().Year()))
100+
want := header + `name: test
80101
version: v1.0.0
81102
`
82103
path := filepath.Join(t.TempDir(), "test.yaml")

0 commit comments

Comments
 (0)