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

shebang incorrect in v1.0.0 causes CLI console error #8

Closed
Nos78 opened this issue Dec 11, 2022 · 5 comments
Closed

shebang incorrect in v1.0.0 causes CLI console error #8

Nos78 opened this issue Dec 11, 2022 · 5 comments

Comments

@Nos78
Copy link

Nos78 commented Dec 11, 2022

The latest release v1.0.0 causes a console error when trying to run phpandroid.phar

I downloaded the latest release, and placed it in /usr/bin as phpandroid, then tried to run it in a bash shell:

    noscere@bertram:~$ phpandroid
    /usr/bin/env: ‘php\r’: No such file or directory

A simple hexedit of the phpandroid.phar shows that the hashbang/shebang line has windows type file endings 0D0A /r/n (instead of just 0A or /n) and so it is interpretting the shebang as #!/usr/bin/env/php\r instead of #!/usr/bin/env php.

Unfortunately (for me) I couldn't just hexedit the line ending, changing it to 0A0A (cannot remove a byte, only change it, so effectively this adds a blank line with a unix line-ending character) because that breaks the SHA1 file signature.

I also tried to phar extract the archive and re-pack it with phar pack -b '#!/usr/bin/env php' -f phpandroid.phar * but this does not even insert a shebang at all? (again, viewing with hexedit shows no shebang where there should be one). In the end I downloaded the source and re-compiled my own binary using cd tasks && php generate_phar.php, which added the shebang correctly formatted with proper line ending.

For curiosity-sake, I delved into the code of phar.phar; the section which adds/modifies the hashbang is adding '/n' to the line if a new-line is not present; so I can only assume that the release was built on a windows machine and the /usr/bin/env php line at the top of stub.php had the wrong line ending?

@Nos78
Copy link
Author

Nos78 commented Dec 11, 2022

Long story short - for anyone encountering this issue, download the source and build it yourself! Thats the beauty of open-source. And, thanks @AnandPilania for creating a great tool, even if it has given me half a day of headache (to be fair, I do enjoy debugging - maybe not so much other peoples tools, but still fun 😁 ) - since they got rid of android create project I have been seeking a solution to my command-line only android app development environment. I finally found it with this little tool. Thanks.

To build your own release of this tool, you're going to need the vendor folder from the actual release; I'm presuming you have this, otherwise how did you encounter this issue 🤣 - you do still have the downloaded release, don't you?

    wget https://github.com/AnandPilania/php-android-cli/releases/download/v1.0.0/phpandroid.phar
    # download the source from github
    gitclone git@github.com:AnandPilania/php-android-cli.git
    # get the vendor folder out of the official release (this extracts to current working dir
    # into a new subdirectory 'tmp')
    phar extract -f phpandroid.phar tmp
    cd php-android-cli
    cp -r ../tmp/vendor .
    cd tasks
    php generate_phar.php # before running this, I modified ../index.php to change the version number, see below
    # clean up
    rm -rf ../../tmp
    # (optionally) put the new phpandroid.phar into your bin
    # I used /usr/bin but it probably should be /usr/local/bin or even ~/bin
    sudo mv phpandroid.phar /usr/local/bin/.
    # I removed the extension when copying to /usr/bin, but you should prolly symbolic link instead
    sudo ln -s /usr/local/bin/phpandroid.phar /usr/local/bin/phpandroid
    # why? well, what other linux commands have extensions? no, I can't think of any either

After following the above commands, you should have a shiny new phpandroid.phar in folder php-android-cli/tasks (OR /usr/local/bin/phpandroid.phar if you did the optional bit at the end). For added peace of mind, I also modified index.php, changing the version number to v1.0.99 and the app name to Console App [self-build] so that if I forgot about this, or something tries to upgrade the tool, the different version number should be enough to remind me (and if not, the descriptive name will!)

    $app = new Symfony\Component\Console\Application('PHP Android Console App [nos self-build]', 'v1.0.99');

@AnandPilania
Copy link
Owner

Hi @Nos78, sorry for the headache, actually it's a pending task for me to re-write this code from scratch with easy upgradable & maintainability. Hope will pick this soon.

@Nos78
Copy link
Author

Nos78 commented Dec 12, 2022

@AnandPilania look forward to it! Thanks for the quick response - can't count the number of times there is a nice useful tool with a github repo, and the owner doesn't ever reply 😞

@Nos78
Copy link
Author

Nos78 commented Dec 12, 2022

I have automated the entire process of this work around, just download this script:

https://github.com/Nos78/php-android-cli/blob/master/tasks/fix-phpandroid.sh

This script:

  • creates a temporary folder in your home directory,
  • downloads v1.0.0 release & source code,
  • extracts the vendor folder from the release binary (then deletes the downloaded binary)
  • and extracts the source code from downloaded zip (then deletes the downloaded zip.
  • moves the extracted vendor folder into the source,
  • modifies index.php to change the version number & text,
  • then executes php generate_phar.php to re-build the phpandroid.phar archive.
  • Finally, it moves the archive to your $HOME directory & sets the execute bit (chmod +x)
  • then cleans up by removing the whole temporary directory.

@Nos78
Copy link
Author

Nos78 commented Dec 12, 2022

(NOTE: I have commited but haven't pushed this commit for new version of fix-phpandroid.sh, I will wait until when and if @AnandPilania does his next release. If this bug is fixed, then there is no need for the modifications I describe below. If he refactors the source, as indicated in another post, then the script may need changes anyway, since we are modifying one of the source files directly)

I have modified my fix script so that it will download the latest release, rather than hardcoding v1.0.0 using this URL:

Unfortunately that cannot be used for source archive (zips or tars). It now gets the source by accessing the API to get
the latest release tag, via:

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

No branches or pull requests

2 participants