From 99c301310af60df9569920aaf923b0f79950adad Mon Sep 17 00:00:00 2001 From: Xuehai Pan Date: Wed, 4 Jun 2025 20:20:55 +0800 Subject: [PATCH] Add a separator after the license header if the template has no bottom --- testdata/expected/Gemfile | 1 + testdata/expected/file.bzl | 1 + testdata/expected/file.cc | 1 + testdata/expected/file.cpp | 1 + testdata/expected/file.cs | 1 + testdata/expected/file.dart | 1 + testdata/expected/file.el | 1 + testdata/expected/file.erl | 1 + testdata/expected/file.go | 1 + testdata/expected/file.gradle | 1 + testdata/expected/file.graphql | 1 + testdata/expected/file.groovy | 1 + testdata/expected/file.hcl | 1 + testdata/expected/file.hh | 1 + testdata/expected/file.hpp | 1 + testdata/expected/file.hs | 1 + testdata/expected/file.lisp | 1 + testdata/expected/file.m | 1 + testdata/expected/file.mm | 1 + testdata/expected/file.nix | 1 + testdata/expected/file.php | 1 + testdata/expected/file.proto | 1 + testdata/expected/file.pxd | 1 + testdata/expected/file.py | 1 + testdata/expected/file.pyx | 1 + testdata/expected/file.rb | 1 + testdata/expected/file.rs | 1 + testdata/expected/file.sdl | 1 + testdata/expected/file.swift | 1 + testdata/expected/file.tf | 1 + testdata/expected/file.toml | 1 + testdata/expected/file1.Dockerfile | 1 + testdata/expected/file1.sh | 1 + testdata/expected/file2.Dockerfile | 1 + testdata/expected/file2.sh | 1 + testdata/expected/file3.Dockerfile | 1 + tmpl.go | 27 ++++++++++++++++++++++++++- tmpl_test.go | 2 ++ 38 files changed, 64 insertions(+), 1 deletion(-) diff --git a/testdata/expected/Gemfile b/testdata/expected/Gemfile index 6975e59..36be64e 100644 --- a/testdata/expected/Gemfile +++ b/testdata/expected/Gemfile @@ -11,5 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== puts "Hello world!" diff --git a/testdata/expected/file.bzl b/testdata/expected/file.bzl index df3d19e..501d453 100644 --- a/testdata/expected/file.bzl +++ b/testdata/expected/file.bzl @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== # Say hello def hello(): diff --git a/testdata/expected/file.cc b/testdata/expected/file.cc index da72231..83687af 100644 --- a/testdata/expected/file.cc +++ b/testdata/expected/file.cc @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= #include using namespace std; diff --git a/testdata/expected/file.cpp b/testdata/expected/file.cpp index da72231..83687af 100644 --- a/testdata/expected/file.cpp +++ b/testdata/expected/file.cpp @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= #include using namespace std; diff --git a/testdata/expected/file.cs b/testdata/expected/file.cs index 414ab7a..9587712 100644 --- a/testdata/expected/file.cs +++ b/testdata/expected/file.cs @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= public class Hello { diff --git a/testdata/expected/file.dart b/testdata/expected/file.dart index db8d08c..e937ec1 100644 --- a/testdata/expected/file.dart +++ b/testdata/expected/file.dart @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= void main() { print('Hello World!'); diff --git a/testdata/expected/file.el b/testdata/expected/file.el index 27712d2..0ad4577 100644 --- a/testdata/expected/file.el +++ b/testdata/expected/file.el @@ -11,5 +11,6 @@ ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ;; See the License for the specific language governing permissions and ;; limitations under the License. +;; ============================================================================= (message "Hello world!") diff --git a/testdata/expected/file.erl b/testdata/expected/file.erl index fc7efdc..2d6f045 100644 --- a/testdata/expected/file.erl +++ b/testdata/expected/file.erl @@ -11,6 +11,7 @@ % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % See the License for the specific language governing permissions and % limitations under the License. +% ============================================================================== -module(hello). -export([hello_world/0]). diff --git a/testdata/expected/file.go b/testdata/expected/file.go index b371be6..3ac052a 100644 --- a/testdata/expected/file.go +++ b/testdata/expected/file.go @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= package main diff --git a/testdata/expected/file.gradle b/testdata/expected/file.gradle index 42e27e2..f1b1004 100644 --- a/testdata/expected/file.gradle +++ b/testdata/expected/file.gradle @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= application { mainClass = 'com.google.helloworld' diff --git a/testdata/expected/file.graphql b/testdata/expected/file.graphql index 0ae44ba..09f24cc 100644 --- a/testdata/expected/file.graphql +++ b/testdata/expected/file.graphql @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== query HeroNameAndFriends { hero { diff --git a/testdata/expected/file.groovy b/testdata/expected/file.groovy index a928aa1..6dff781 100644 --- a/testdata/expected/file.groovy +++ b/testdata/expected/file.groovy @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= def dummy_function() { println "Hello world" diff --git a/testdata/expected/file.hcl b/testdata/expected/file.hcl index 11aeaeb..8e9616d 100644 --- a/testdata/expected/file.hcl +++ b/testdata/expected/file.hcl @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= group "default" { targets = ["build"] diff --git a/testdata/expected/file.hh b/testdata/expected/file.hh index 74491fb..c19ec16 100644 --- a/testdata/expected/file.hh +++ b/testdata/expected/file.hh @@ -11,5 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= #define SOMETHING diff --git a/testdata/expected/file.hpp b/testdata/expected/file.hpp index 74491fb..c19ec16 100644 --- a/testdata/expected/file.hpp +++ b/testdata/expected/file.hpp @@ -11,5 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= #define SOMETHING diff --git a/testdata/expected/file.hs b/testdata/expected/file.hs index fc11cd5..68c09b4 100644 --- a/testdata/expected/file.hs +++ b/testdata/expected/file.hs @@ -11,6 +11,7 @@ -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License. +-- ============================================================================= module Main where diff --git a/testdata/expected/file.lisp b/testdata/expected/file.lisp index 2d2e8e2..843a559 100644 --- a/testdata/expected/file.lisp +++ b/testdata/expected/file.lisp @@ -11,6 +11,7 @@ ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ;; See the License for the specific language governing permissions and ;; limitations under the License. +;; ============================================================================= ; hello world lisp program. (print "Hello World") diff --git a/testdata/expected/file.m b/testdata/expected/file.m index fa3fa95..11814ae 100644 --- a/testdata/expected/file.m +++ b/testdata/expected/file.m @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= #import diff --git a/testdata/expected/file.mm b/testdata/expected/file.mm index fa3fa95..11814ae 100644 --- a/testdata/expected/file.mm +++ b/testdata/expected/file.mm @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= #import diff --git a/testdata/expected/file.nix b/testdata/expected/file.nix index a978a35..5211d63 100644 --- a/testdata/expected/file.nix +++ b/testdata/expected/file.nix @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== with import {}; mkShell { diff --git a/testdata/expected/file.php b/testdata/expected/file.php index 72d2848..ed2f9a5 100644 --- a/testdata/expected/file.php +++ b/testdata/expected/file.php @@ -12,6 +12,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= namespace App; phpinfo(); diff --git a/testdata/expected/file.proto b/testdata/expected/file.proto index 386c1ba..c617157 100644 --- a/testdata/expected/file.proto +++ b/testdata/expected/file.proto @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= syntax = "proto3"; diff --git a/testdata/expected/file.pxd b/testdata/expected/file.pxd index 0ffbcc7..81b3eb9 100644 --- a/testdata/expected/file.pxd +++ b/testdata/expected/file.pxd @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== cdef void main(): print("Hello World!") diff --git a/testdata/expected/file.py b/testdata/expected/file.py index a53f3e2..3dd433a 100644 --- a/testdata/expected/file.py +++ b/testdata/expected/file.py @@ -11,5 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== print "Hello World!" diff --git a/testdata/expected/file.pyx b/testdata/expected/file.pyx index 693aa92..e444711 100644 --- a/testdata/expected/file.pyx +++ b/testdata/expected/file.pyx @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== def main() -> None: print("Hello World!") diff --git a/testdata/expected/file.rb b/testdata/expected/file.rb index 230d3e7..5ac68ea 100644 --- a/testdata/expected/file.rb +++ b/testdata/expected/file.rb @@ -12,5 +12,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== puts "Hello world!" diff --git a/testdata/expected/file.rs b/testdata/expected/file.rs index 309f681..75c0404 100644 --- a/testdata/expected/file.rs +++ b/testdata/expected/file.rs @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= fn main() { println!("Hello World!"); diff --git a/testdata/expected/file.sdl b/testdata/expected/file.sdl index 0794c33..9e208c4 100644 --- a/testdata/expected/file.sdl +++ b/testdata/expected/file.sdl @@ -11,6 +11,7 @@ -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License. +-- ============================================================================= -- This is a fake table. CREATE TABLE Foo ( diff --git a/testdata/expected/file.swift b/testdata/expected/file.swift index a58e541..8ab9724 100644 --- a/testdata/expected/file.swift +++ b/testdata/expected/file.swift @@ -11,5 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +// ============================================================================= print("Hello, world!") diff --git a/testdata/expected/file.tf b/testdata/expected/file.tf index 06788c5..6eeba6d 100644 --- a/testdata/expected/file.tf +++ b/testdata/expected/file.tf @@ -11,5 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== resource "null_resource" "dummy" {} diff --git a/testdata/expected/file.toml b/testdata/expected/file.toml index 28eec5e..537a891 100644 --- a/testdata/expected/file.toml +++ b/testdata/expected/file.toml @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== [package] name = "not-a-real-crate" diff --git a/testdata/expected/file1.Dockerfile b/testdata/expected/file1.Dockerfile index bdcb6e2..3c47784 100644 --- a/testdata/expected/file1.Dockerfile +++ b/testdata/expected/file1.Dockerfile @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== FROM scratch CMD ["echo", "hello world"] diff --git a/testdata/expected/file1.sh b/testdata/expected/file1.sh index 8d682db..5b8dbe1 100644 --- a/testdata/expected/file1.sh +++ b/testdata/expected/file1.sh @@ -12,5 +12,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== echo hello diff --git a/testdata/expected/file2.Dockerfile b/testdata/expected/file2.Dockerfile index b626f22..455f4bb 100644 --- a/testdata/expected/file2.Dockerfile +++ b/testdata/expected/file2.Dockerfile @@ -12,6 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== FROM scratch CMD ["echo", "hello world"] diff --git a/testdata/expected/file2.sh b/testdata/expected/file2.sh index 3662fd6..f7463cc 100644 --- a/testdata/expected/file2.sh +++ b/testdata/expected/file2.sh @@ -12,4 +12,5 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== diff --git a/testdata/expected/file3.Dockerfile b/testdata/expected/file3.Dockerfile index dc32f8d..f6560d9 100644 --- a/testdata/expected/file3.Dockerfile +++ b/testdata/expected/file3.Dockerfile @@ -12,6 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# ============================================================================== FROM microsoft/nanoserver COPY testfile.txt c:\ diff --git a/tmpl.go b/tmpl.go index 08a2d08..1da0754 100644 --- a/tmpl.go +++ b/tmpl.go @@ -91,11 +91,36 @@ func executeTemplate(t *template.Template, d licenseData, top, mid, bot string) fmt.Fprintln(&out, top) } s := bufio.NewScanner(&buf) + found_empty_line := false for s.Scan() { - fmt.Fprintln(&out, strings.TrimRightFunc(mid+s.Text(), unicode.IsSpace)) + line := strings.TrimRightFunc(s.Text(), unicode.IsSpace) + found_empty_line = found_empty_line || line == "" + fmt.Fprintln(&out, strings.TrimRightFunc(mid+line, unicode.IsSpace)) } if bot != "" { fmt.Fprintln(&out, bot) + } else if found_empty_line && len(mid) < 80 { + // Found empty lines in the license header, so there are multiple paragraphs in the license. + // + // If the license does not specify a bottom suffix, add a visual separation line to the + // license header from the following code. For example: + // + // top, mid, bot = "", "# ", "" + // + // # License Header + // # ============================ + // + // Code starts here... + // + // top, mid, bot = "/**", " * ", " */" + // + // /** + // * License Header + // */ + // + // Code starts here... + // + fmt.Fprintln(&out, mid+strings.Repeat("=", 80-len(mid))) } fmt.Fprintln(&out) return out.Bytes(), nil diff --git a/tmpl_test.go b/tmpl_test.go index e263149..7a639f2 100644 --- a/tmpl_test.go +++ b/tmpl_test.go @@ -204,6 +204,7 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +================================================================================ `, }, @@ -242,6 +243,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +================================================================================ `, },