Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get package name in script?

I'm doing a short post package install/update script to copy some files from the vendor directory into my public one.

Following the example of the composer site however when I execute it I get an error:

Fatal error: Call to undefined method Composer\DependencyResolver\Operation\UpdateOperation::getPackage() in S:\Projects\composer-scripts\FileCopy.php on line 17

The code is:

namespace composer-scipts;

use Composer\Script\Event;

class FileCopy
{
    public static function postPackageInstall( Event $event )
    {
        $packageName = $event->getOperation()->getPackage()->getName();

        echo "$packageName\n";
    }

    public static function postPackageUpdate( Event $event )
    {
        $packageName = $event->getOperation()->getPackage()->getName();

        echo "$packageName\n";
    }
}

Can anyone please advise?

like image 503
Adam M. Avatar asked Feb 14 '23 22:02

Adam M.


1 Answers

Following further testing I have identified the issue, which is essentially due to two different interfaces having the same/a similar method but with different signatures. Thusly I have ended up with:

public static function postPackageInstall( Event $event )
{
    $packageName = $event->getOperation()->getPackage()->getName();

    if( $packageName == 'twbs/bootstrap' )
    {
        self::copyFiles();
    }
}

public static function postPackageUpdate( Event $event )
{
    $packageName = $event->getOperation()->getInitialPackage()->getName();

    if( $packageName == 'twbs/bootstrap' )
    {
        self::copyFiles();
    }
}

So, postPackageInstall uses getPackage() where-as postPackageUpdate uses getInitialPackage().

like image 123
Adam M. Avatar answered Feb 17 '23 03:02

Adam M.