guard/guard-compass

compass callabacks called twice

Opened this issue · 4 comments

this callback defined in compass config:

on_stylesheet_saved do |filename|
    $local_path_to_css_file = css_path + '/' + File.basename(filename)
    puts $local_path_to_css_file
    Net::SFTP.start( sftp_host, sftp_user, :password => sftp_pass ) do |sftp|
        puts sftp.upload! $local_path_to_css_file, remote_theme_dir_absolute + File.basename(filename)
    end
end

will run twice on a single save:

overwrite app.css 
public/stylesheets/app.css
#<Net::SFTP::Operations::Upload:0x007fc5660a0a18>
public/stylesheets/app.css
#<Net::SFTP::Operations::Upload:0x007fc565225448>

As you can see the ftp upload gets performed twice... this only happens with guard compass. using compass directly does not result in the callback happening twice.

I would move the callback into guard itself (instead of the compass callback in the compass config file) if you provided a hook callback on compass completion within guard-compass. I could then move this file upload code there (although im not sure how i could know the file that was updated which compass passes into the callback.)

Hi,

There's actually already a way to do this in Guard, using callbacks. There's one issue though, the args passed to the run_on_changes_end callback is the result of the run_on_changes method (i.e. true/false) not the compiled files but you could probably work-around this with a solution like this:

# Guardfile
class GuardCompass::SFTPUpload
  def initialize
    @filenames = []
  end

  def call(guard_class, event, *args)
    event.to_s =~ /(.*)_(begin|end)/
    puts "#{guard_class} received these args: #{args} for #{event}"

    if $2 == 'begin'
      @filenames = args
    else
      @filenames.each do |filename|
        $local_path_to_css_file = css_path + '/' + File.basename(filename)
        puts $local_path_to_css_file
        Net::SFTP.start( sftp_host, sftp_user, :password => sftp_pass ) do |sftp|
          puts sftp.upload! $local_path_to_css_file, remote_theme_dir_absolute + File.basename(filename)
        end
      end
      @filenames = []
    end
  end
end

guard :compass do
  callback(GuardCompass::SFTPUpload.new, [:run_on_changes_end])
end

I haven't tried this code so it probably needs to be adapted but I hope that helps (and that could actually work).

@lukeholder Have you had a chance to try my proposal?

not yet been very busy, looks good! will let you know soon.