3

I want to run asset precompile task inside the rails application,As I had many dependencies who will change the code,in that case all the time whenever they change i need to run the script as I cant give server access to them so I am providing the GUI for them from that they alone can run the script,so,I have built UI to run the task with some parameter like

system("Template='#{params[:template]}' Theme='#{params[:theme]}' rake assets:precompile)

I am getting two values from UI(params[:template],params[:theme]).Another thing i want to run this task in another path(site path) means Admin side UI is there that task should execute in Site directory,

  if(params[:theme_script] == "true")
      template=Template.where(:name => params[:template]).first
     if template
      theme = template.themes.where(:name => params[:theme]).first
      if theme
    #   Dir.chdir "#{THEMEPATH}"do
    #     `Template="#{template.name}" Theme="#{theme.name}" rake assets:precompile`
    #   end
    #      sleep 10
    #      system("#{Rails.root.to_s}/lib/shell_script.sh")
    #      RunRake.run_rake(template.name,theme.name)
    #   Dir.chdir "#{THEMEPATH}"do
    #     Rake::Task['assets:precompile'].invoke
    #   end
          ENV["Template"] = template.name
          ENV["Theme"] = theme.name
          precompile_task = "bundle exec rake assets:precompile --trace 2>&1"
          output = Dir.chdir(THEMEPATH) { %x[ #{precompile_task} ] }
          flash[:notice] = "Asset created successfully"
      else
        flash[:notice] = "U have enter invalid data"
      end
    else
      flash[:notice] = "U have enter invalid data"
    end
  end

This is my code am checking multiple condition and am allowing to execute the task.

I have tried this code by putting in controller and lib, but this is not working.

I have tried with shell script also.

Could please anyone can help me.

Jenorish
  • 1,694
  • 14
  • 19
  • That's *really* not how asset compilation is meant to be used. You should be doing this automatically at deploy time via a build script, not manually. There should be no reason for you to want a GUI for this. – user229044 Jan 30 '14 at 12:34
  • Thanks for the reply,As I had many dependencies who will change the code,in that case all the time whenever they change i need to run the script as I cant give server access to them so I am providing the GUI for them from that they alone can run the script. – Jenorish Jan 30 '14 at 13:12
  • @МалъСкрылевъ please help me how i can pass the parameters? – Jenorish Jan 30 '14 at 13:18

1 Answers1

5

You can just setup an environment variable for , and then issue #invoke method from a controller. So, prepare the files:

gem 'rake'

config/initializers/rake.rb:

Rake.load_rakefile Rails.root.join( 'Rakefile' )

app/controllers/your_controller:

ENV["Template"] = template.name
ENV["Theme"] = theme.name
Rake::Task[ 'assets:precompile' ].invoke

Issue bundle install, then run console rails c, and type:

Rake::Task.tasks.map(&:name).grep 'assets:precompile'
# => ["assets:precompile"]

As you can see, the task assets:precompile is loaded successfully. Then just issue the action for the controller.

To run the task for an other app you shell run also the other instance, similar to as you had done:

system( "other_app_run.sh '#{template.name}' '${theme.name}'" )

other_app_run.sh:

#!/bin/bash

source "$HOME/.rvm/scripts/rvm"
cd /other/app/path
export Template="$1"
export Theme="$2"
rake assets:precompile
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69
  • I have edited my question and posted my code how i am trying.Could please verify and help me. – Jenorish Feb 02 '14 at 07:16
  • @Kingston well, the question is the same, what is happening with the invoke? – Малъ Скрылевъ Feb 02 '14 at 09:58
  • i got so many errors Command failed with status (1): [/home/kingston/.rvm/rubies/ruby-1.9.3-p448/...] , Application has been already initialized., A SystemExit occurred in translations#excecute: exit,,.... undefined method `each_logical_path' for nil:NilClass in admin side i haven’t use asset concept.so config.assets.enabled = false is there.i think the directory is not changing. – Jenorish Feb 03 '14 at 05:16
  • if i run the task in theme side means task is running successfully,But i built the GUI in Admin side which doesn't have assets concept,Now what is the problem when am trying to run the task admin side to theme side that time directory is not changing.so,am getting error. – Jenorish Feb 03 '14 at 10:23
  • and which concept do you have there? – Малъ Скрылевъ Feb 03 '14 at 10:30
  • Open a new question belonging to the specific GUI Admin, describle the problem for it, because too broad question isn't allowed here. – Малъ Скрылевъ Feb 03 '14 at 10:32
  • if i run the task in theme side means task is running successfully,But i built the GUI in Admin side which doesn't have assets concept,Now what is the problem is when am trying to run the task admin side to theme side that time directory is not changing.so,am getting error. actionpack-3.1.3/lib/sprockets/assets.rake line no 30 unless Rails.application.config.assets.enabled warn "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true" exit end In admin side Rails.application.config.assets.enabled is false . – Jenorish Feb 03 '14 at 10:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46667/discussion-between-kingston-and--) – Jenorish Feb 03 '14 at 10:40
  • @Kingston have my answer been helpful? – Малъ Скрылевъ Feb 25 '14 at 11:10
  • sorry for late reply.i was not feeling well,so i haven’t try it.Today i have tried but same result it's not working. – Jenorish Apr 01 '14 at 10:32