<commit_before><?php namespace EnsphereCore\Commands\Ensphere\Install\Update;

use EnsphereCore\Commands\Ensphere\Traits\Module as ModuleTrait;
use Illuminate\Console\Command as IlluminateCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

class Command extends IlluminateCommand {

	use ModuleTrait;

	/**
	 * [$name description]
	 * @var string
	 */
	protected $name = 'ensphere:update';

	/**
	 * [$description description]
	 * @var string
	 */
	protected $description = 'Update vendor and application dependencies.';

	/**
	 * [$currentStructure description]
	 * @var [type]
	 */
	private $currentStructure;

	/**
	 * [fire description]
	 * @return [type] [description]
	 */
	public function fire()
	{
		$this->generateRegistrationFile();
		$this->publishVendorAssets();
		$this->combineDependencyAssets();
		$this->migrateRun();
	}

	/**
	 * [migrateRun description]
	 * @return [type] [description]
	 */
	private function migrateRun()
	{
		$this->info( shell_exec( "php artisan ensphere:migrate run" ) );
	}

	/**
	 * [generateRegistrationFile description]
	 * @return [type] [description]
	 */
	private function generateRegistrationFile()
	{
		$this->info('generating registration file...');
		$this->info( shell_exec( "php artisan ensphere:register" ) );
	}

	/**
	 * [publishVendorAssets description]
	 * @return [type] [description]
	 */
	private function publishVendorAssets()
	{
		$this->info('publishing config files...');
		$this->cleanModulePackageAssetFolders();
		$this->info( shell_exec( "php artisan vendor:publish --tag=config" ) );
		$this->info('pushing module assets to application...');
		$this->info( shell_exec( "php artisan vendor:publish --tag=forced --force" ) );
	}

	/**
	 * [cleanModulePackageAssetFolders description]
	 * @return [type] [description]
	 */
	protected function cleanModulePackageAssetFolders()
	{
		$iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( public_path( 'package' ) ), RecursiveIteratorIterator::SELF_FIRST );
		$thisData = $this->getCurrentVendorAndModuleName();
		foreach( $iterator as $file ) {<commit_msg>delete unwanted vendor assets on update<commit_after><?php namespace EnsphereCore\Commands\Ensphere\Install\Update;

use EnsphereCore\Commands\Ensphere\Traits\Module as ModuleTrait;
use Illuminate\Console\Command as IlluminateCommand;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Storage, File;

class Command extends IlluminateCommand {

	use ModuleTrait;

	/**
	 * [$name description]
	 * @var string
	 */
	protected $name = 'ensphere:update';

	/**
	 * [$description description]
	 * @var string
	 */
	protected $description = 'Update vendor and application dependencies.';

	/**
	 * [$currentStructure description]
	 * @var [type]
	 */
	private $currentStructure;

	/**
	 * [fire description]
	 * @return [type] [description]
	 */
	public function fire()
	{
		$this->generateRegistrationFile();
		$this->publishVendorAssets();
		$this->combineDependencyAssets();
		$this->migrateRun();
	}

	/**
	 * [migrateRun description]
	 * @return [type] [description]
	 */
	private function migrateRun()
	{
		$this->info( shell_exec( "php artisan ensphere:migrate run" ) );
	}

	/**
	 * [generateRegistrationFile description]
	 * @return [type] [description]
	 */
	private function generateRegistrationFile()
	{
		$this->info('generating registration file...');
		$this->info( shell_exec( "php artisan ensphere:register" ) );
	}

	/**
	 * [publishVendorAssets description]
	 * @return [type] [description]
	 */
	private function publishVendorAssets()
	{
		$this->info('publishing config files...');
        $this->deleteNonModulesVendorAssets();
		$this->cleanModulePackageAssetFolders();
		$this->info( shell_exec( "php artisan vendor:publish --tag=config" ) );
		$this->info('pushing module assets to application...');
		$this->info( shell_exec( "php artisan vendor:publish --tag=forced --force" ) );
	}

    protected function deleteNonModulesVendorAssets()
    {
        $modulePackages = $this->getPackages( base_path( 'EnsphereCore' ) );
        $it = new RecursiveDirectoryIterator( public_path( 'vendor' ) );
        $paths = [];
        foreach( $it as $file ) {
            if( $file->isDir() && ! in_array( $file->getFilename(), [ '.', '..' ] ) ) {
                $paths[$file->getBasename()] = $file->getRealPath();
            }
        }
        foreach( $modulePackages as $name => $detail ) {
            if( isset( $paths[$name] ) ) {
                unset( $paths[$name] );
            }
        }
        foreach( $paths as $path ) {
            File::cleanDirectory( $path );
            Storage::deleteDirectory( $path );
            rmdir( $path );
        }
    }

    /**
     * [getPackages description]
     * @param  [type] $path [description]
     * @return [type]       [description]
     */
    private function getPackages( $path ) {
        return json_decode( file_get_contents( $path . '/ensphere-assets.json' ) );
    }

	/**
	 * [cleanModulePackageAssetFolders description]
	 * @return [type] [description]
	 */
	protected function cleanModulePackageAssetFolders()
	{
		$iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( public_path( 'package' ) ), RecursiveIteratorIterator::SELF_FIRST );
		$thisData = $this->getCurrentVendorAndModuleName();
		foreach( $iterator as $file ) {
{'fraction_non_alphanumeric': 0.14439970717423134, 'fraction_numerical': 0.0, 'mean_word_length': 3.7980684811237926, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2363818', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1628, 'n_words': 352}