Automatically call asset:publish --bench=“vendor/package” during development in Laravel 4

烂漫一生 提交于 2019-12-05 10:12:39

You can use Guard for tasks like this. For example, here is a portion from my Guardfile to automatically publish assets from a package whenever they are changed:

guard :shell do
    watch(%r{^workbench/vendor/package/public/.+\..+$}) {
        `php artisan asset:publish --bench="vendor/package"`
    }
end

You can also have it automatically compile Sass, setup livereload, etc. Take a look at Jeffrey Way's screencast to get started.

You can achieve this using Grunt with a shell watch command, e.g:

$ npm install grunt-shell

In vendor/yourname/yourpackage/Gruntfile.js:

    shell: {                           
        assets: {                      
            options: {                 
                stdout: true
            },
            command: 'cd ../../..; php artisan asset:publish yourname/yourpackage'
        },
        views: {                      
            options: {                
                stdout: true
            },
            command: 'cd ../../..; php artisan view:publish yourname/yourpackage;'
        }
    },
    watch: {
        assets: {
            files: ['./public/**/*.js', './public/**/*.css'],
            tasks: ['shell:assets']
        },
        views: {
            files: ['./src/views/**/*.php', './src/views/**/*.md'],
            tasks: ['shell:views']                          
        }
    }

...

grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-contrib-watch');

grunt.registerTask('default', ['watch']);

Then start the watch:

$ grunt watch

This can naturally work after other grunt commands such as less or uglify has compiled your assets into public, thus changing them and triggering the publish.

This is what I'm doing:

<?php namespace Vendor\Package;

use \App;
use Illuminate\Filesystem\Filesystem;

class DecoyServiceProvider extends ServiceProvider {
    public function boot() {
        $this->package('bkwld/decoy');

        // Auto-publish the assets when developing locally
        if (App::environment() == 'local' && !App::runningInConsole()) {
            $workbench = realpath(base_path().'/workbench');
            if (strpos(__FILE__, $workbench) === false) App::make('asset.publisher')->publishPackage('vendor/package');
            else App::make('asset.publisher')->publishPackage('vendor/package', $workbench);
        }
    }   
}

This publishes the assets automatically whether the bundle is in the workbench or not, as long as the request is from the local environment.

Edit /workbench/yourappspace/yourapp/AppServiceProvider.php

<?php namespace YourAppSpace\YourApp;

use Illuminate\Support\ServiceProvider;
use Artisan;

In the boot() method,

public function boot()
{
$this->package('yourappspace/yourapp');
$app = $this->app;
include __DIR__.'/../../routes.php';

Artisan::call('asset:publish', array('--bench' => 'yourappspace/yourapp'));
}

Now every time you update your code and refresh your browser, your assets are published with Artisan.

There is an other option, that you add a composer command. For exapmple, when deploying a project to a server with install:

"post-install-cmd": [
    "php artisan clear-compiled",
    "php artisan optimize",
    "php artisan asset:publish vendor-name/package-name"
],

BTW in 5.1 right way:

\Artisan::call('vendor:publish', [ '--tag' => ['public'], '--provider' => 'Some\Your\ServiceProvider', '--force' => true ]);

Try directly editing the css and js files in the

{app_root}/public/bundles/{bundle}/{css|js}

directory and after you're complete, just copy the modified files back to the bundle's public directory. This is probably the wrong way to do it, but hey, it works. Another option is to create a task and run a

Command::run(array('task_name'));

There are a few examples in the official documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!