spohlenz / tinymce-rails

Integration of TinyMCE with the Rails asset pipeline
Other
812 stars 256 forks source link

How can I leverage your asset_installer class to copy support files for my custom plugins? #168

Closed cnk closed 9 years ago

cnk commented 9 years ago

I have 2 custom built TinyMCE plugins. Both work fine in development mode but I need to figure out how to get the supporting files included in the asset pipeline compile step before moving them into production. (If I do a straight file copy, the plugins also work so my only issue is integration with the asset pipeline)

This project is an upgrade (rails 2.3.x -> 4.2) so I stayed with the tinymce-rails 3 branch (version 3.5.9) .

I found your lib/tasks/tinymce-assets.rake task with it's 'enhance' line. I wasn't sure if that would get picked up - and if so in what order, so I created a similar task but with a custom name (for now) and copied the code into it with some updates to pick up the files I want copied into place. I can instantiate the TinyMCE::Rails::AssetInstaller but calling install doesn't do anything. My assets variable is the directory containing the top level directory for the 2 plugins and target is what I expect.

What am I missing?

Thanks in advance,

require "tinymce/rails/asset_installer"
namespace :assets do
  desc "Copy supporting files into public/assets/javascripts/tinymce/plugins"
  task :include_tinymce_plugin_assets => [:environment, :precompile] do
    assets = Pathname.new(File.expand_path(File.dirname(__FILE__) + "/../../vendor/assets/javascripts/tinymce/plugins"))

    config   = Rails.application.config
    target   = File.join(Rails.public_path, config.assets.prefix, 'tinymce', 'plugins')
    manifest = config.assets.manifest

    puts assets                                                                                                                           
    puts target  
    puts TinyMCE::Rails::AssetInstaller.new(assets, target, manifest).class

    TinyMCE::Rails::AssetInstaller.new(assets, target, manifest).install
  end
end

 $ rake assets:include_tinymce_plugin_assets --trace
** Invoke assets:include_tinymce_plugin_assets (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Invoke assets:precompile (first_time)
** Invoke assets:environment
** Execute assets:precompile
** Execute assets:include_tinymce_plugin_assets
/data1/srv/rails/empcms4/vendor/assets/javascripts/tinymce/plugins
/data1/srv/rails/empcms4/public/assets/tinymce/plugins
TinyMCE::Rails::AssetInstaller
cnk commented 9 years ago

Looks like my caution about using the 'enhance' method to adde to the assets tasks was my problem. The code below copies the directories containing my custom plugins into the correct spot (from vendor/assets... into public/assets.... However, it does not appear to be writing them into the manifest even though the files from this project are getting recorded in the manifest.

Here is my rake task in case anyone needs to do something similar. It gets called when you run the normal 'rake assets:precompile' task.

## lib/tasks/tinymce_plugin_assets.rake
require "tinymce/rails/asset_installer"

assets_task = Rake::Task.task_defined?('assets:precompile:primary') ? 'assets:precompile:primary' : 'assets:precompile'

Rake::Task[assets_task].enhance do
  assets = Pathname.new(File.expand_path(File.dirname(__FILE__) + "/../../vendor/assets/javascripts/tinymce/plugins"))

  config   = Rails.application.config
  target   = File.join(Rails.public_path, config.assets.prefix, 'tinymce')
  manifest = config.assets.manifest

  TinyMCE::Rails::AssetInstaller.new(assets, target, manifest).install
end