Skip to content

Support license header with shebang #496

Closed

Description

@irrandon

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.

Activity

  1. nedtwigg commented on Dec 26, 2019

    @nedtwigg
    Member

    Can you provide an example?

    spotless {...}
    # EXAMPLE SCRIPT:
    
    # ACTUAL OUTPUT:
    
    # DESIRED OUTPUT:
  2. vlsi commented on Dec 28, 2019

    @vlsi
    Contributor

    @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

  3. irrandon commented on Jan 8, 2020

    @irrandon
    Author

    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")
  4. nedtwigg commented on Jan 8, 2020

    @nedtwigg
    Member

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

  5. irrandon commented on Jan 9, 2020

    @irrandon
    Author

    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)

  6. deleted a comment from jbduncan on Jan 9, 2020
  7. deleted a comment from jbduncan on Jan 9, 2020
  8. deleted a comment from jbduncan on Jan 9, 2020
  9. nedtwigg commented on Jan 10, 2020

    @nedtwigg
    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.

  10. abelk2 commented on May 18, 2023

    @abelk2
    Contributor

    I think this is supported now with #1441.

  11. nedtwigg commented on May 22, 2023

    @nedtwigg
    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions