Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support license header with shebang #496

Closed
irrandon opened this issue Dec 13, 2019 · 8 comments
Closed

Support license header with shebang #496

irrandon opened this issue Dec 13, 2019 · 8 comments

Comments

@irrandon
Copy link

Hi,
for the license header step to work for linux script files correct handling of shebang lines is required.
Currently it is not handled correctly.

@nedtwigg nedtwigg added the bug label Dec 26, 2019
@nedtwigg
Copy link
Member

Can you provide an example?

spotless {...}
# EXAMPLE SCRIPT:

# ACTUAL OUTPUT:

# DESIRED OUTPUT:

@vlsi
Copy link
Contributor

vlsi commented Dec 28, 2019

@irrandon , you might want to give https://github.com/autostyle/autostyle a try

It supports Shell (e.g. #!/), Batch (e.g. @echo off), XML (e.g. <?..?>), Java (including package-info.java and module-info.java), and Gradle (Kotlin / Gradle scripts) automatically.

Samples: apache/jmeter#549, apache/calcite#1682

@nedtwigg nedtwigg reopened this Dec 29, 2019
@irrandon
Copy link
Author

irrandon commented Jan 8, 2020

We have a temporary solution with a python script but a support via spotless would be nice :)

Here an example:

spotless{
  format('python') {
    licenseHeaderFile 'com/company/gradle/hash.license', '(import |""")'
    target '**/*.py'
  }
}

(If you add the shebang to the delimiter regex, it push the shebang under the license header, what would be invalid.)

hash.license:
#==========================================================================
#  Copyright (c) Awesome Corp., 1980-2020.
#
#  All rights reserved. All hardware and software names used are registered
#  trade names and/or registered trademarks of the respective manufacturers.
#
#  The user of this computer program acknowledges that the above copyright
#  notice, which constitutes the Universal Copyright Convention, will be
#  attached at the position in the function of the computer program which the
#  author has deemed to sufficiently express the reservation of copyright.
#  It is prohibited for customers, users and/or third parties to remove,
#  modify or move this copyright notice.
#==========================================================================
EXAMPLE SCRIPT:
#!/usr/bin/env python3
import sys
print("Hello world")
ACTUAL OUTPUT:
#==========================================================================
#  Copyright (c) Awesome Corp., 1980-2020.
#
#  All rights reserved. All hardware and software names used are registered
#  trade names and/or registered trademarks of the respective manufacturers.
#
#  The user of this computer program acknowledges that the above copyright
#  notice, which constitutes the Universal Copyright Convention, will be
#  attached at the position in the function of the computer program which the
#  author has deemed to sufficiently express the reservation of copyright.
#  It is prohibited for customers, users and/or third parties to remove,
#  modify or move this copyright notice.
#==========================================================================
import sys
print("Hello world")
DESIRED OUTPUT:
#!/usr/bin/env python3
#==========================================================================
#  Copyright (c) Awesome Corp., 1980-2020.
#
#  All rights reserved. All hardware and software names used are registered
#  trade names and/or registered trademarks of the respective manufacturers.
#
#  The user of this computer program acknowledges that the above copyright
#  notice, which constitutes the Universal Copyright Convention, will be
#  attached at the position in the function of the computer program which the
#  author has deemed to sufficiently express the reservation of copyright.
#  It is prohibited for customers, users and/or third parties to remove,
#  modify or move this copyright notice.
#==========================================================================
import sys
print("Hello world")

@nedtwigg
Copy link
Member

nedtwigg commented Jan 8, 2020

An easy workaround is to put the #!/usr/bin ... inside the license that you give to Spotless. Any problems with that?

@nedtwigg nedtwigg added question and removed bug labels Jan 8, 2020
@irrandon
Copy link
Author

irrandon commented Jan 9, 2020

The problem is that this only works, when you have the same shebang for all python files.
But we also have #!/usr/bin/env python2. The same problem is there with linux script files.(#!/usr/bin/env sh | #!/usr/bin/env bash)

@diffplug diffplug deleted a comment from jbduncan Jan 9, 2020
@diffplug diffplug deleted a comment from jbduncan Jan 9, 2020
@diffplug diffplug deleted a comment from jbduncan Jan 9, 2020
@nedtwigg
Copy link
Member

(comment deletions just to remove off-topic discussion and condense on the feature request)

Aha! This is a good feature request. It's tricky - the license step has so far had a very simple model: find the "top" of the file with regex Y, and then add header X. Solving this issue will require complicating the model somewhat.

A simple way to do this would be to allow something like $FIRST_LINE within the replacement text. So your hash.license could be

$FIRST_LINE
#==========================================================================
#  Copyright (c) Awesome Corp., 1980-2020.
#
#  All rights reserved. All hardware and software names used are registered
#  trade names and/or registered trademarks of the respective manufacturers.

The other option would be to make the license step generally more powerful and context-sensitive, as @vlsi has done in his fork, linked above. I would definitely merge a PR to add support for $FIRST_LINE, since it is so simple. I'm also open to a more powerful context-sensitive option, but backwards compatibility and long-term maintenance burden would be an important challenge to keep in mind.

nedtwigg added a commit to sameer-b/spotless that referenced this issue Mar 20, 2020
…yle. Makes it clear that an inner class ought to be used in the future, especially to support diffplug#496.
@abelk2
Copy link
Contributor

abelk2 commented May 18, 2023

I think this is supported now with #1441.

@nedtwigg
Copy link
Member

Thanks @abelk2! Relevant example:

format "licenseHeaderSh", {
  target "*.sh"
  licenseHeaderFile(file("$rootDir/config/license/sh.txt"), "^(?!##).+")
    .skipLinesMatching("^#!.+?\$")
  // license header MUST start with ##
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants