ckeditor_file_utils.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:4k
源码类别:

Ajax

开发平台:

Others

  1. require 'fileutils'
  2. module CkeditorFileUtils
  3.   CKEDITOR_INSTALL_DIRECTORY = File.join(RAILS_ROOT, '/public/javascripts/ckeditor/')
  4.   PLUGIN_INSTALL_DIRECTORY =  File.join(RAILS_ROOT, '/vendor/plugins/easy-ckeditor/')
  5.   def CkeditorFileUtils.recursive_copy(options)
  6.     source = options[:source]
  7.     dest = options[:dest]
  8.     logging = options[:logging].nil? ? true : options[:logging]
  9.     Dir.foreach(source) do |entry|
  10.       next if entry =~ /^./
  11.       if File.directory?(File.join(source, entry))
  12.         unless File.exist?(File.join(dest, entry))
  13.           if logging
  14.             puts "Creating directory #{entry}..."
  15.           end
  16.           FileUtils.mkdir File.join(dest, entry)#, :noop => true#, :verbose => true
  17.         end
  18.         recursive_copy(:source => File.join(source, entry),
  19.                        :dest => File.join(dest, entry),
  20.                        :logging => logging)
  21.       else
  22.         if logging
  23.           puts "  Installing file #{entry}..."
  24.         end
  25.         FileUtils.cp File.join(source, entry), File.join(dest, entry)#, :noop => true#, :verbose => true
  26.       end
  27.     end
  28.   end
  29.   def CkeditorFileUtils.backup_existing
  30.     source = File.join(RAILS_ROOT,'/public/javascripts/ckeditor')
  31.     dest = File.join(RAILS_ROOT,'/public/javascripts/ckeditor_bck')
  32.     FileUtils.rm_r(dest) if File.exists? dest
  33.     FileUtils.mv source, dest
  34.   end
  35.   def CkeditorFileUtils.copy_configuration
  36.     # need to copy over the code if it doesn't already exist
  37.     config_file = File.join(RAILS_ROOT, '/vendor/plugins/easy-ckeditor/public/javascripts/ckcustom.js')
  38.     dest = File.join(RAILS_ROOT, '/public/javascripts/ckcustom.js')
  39.     backup_config = File.join(RAILS_ROOT, '/public/javascripts/ckeditor/config.bak')
  40.     config_symlink = File.join(RAILS_ROOT, '/public/javascripts/ckeditor/config.js')
  41.     FileUtils.cp(config_file, dest) unless File.exist?(dest)
  42.     if File.exist?(config_symlink)
  43.       unless File.symlink?(config_symlink)
  44.         FileUtils.rm(backup_config) if File.exist?(backup_config)
  45.         FileUtils.mv(config_symlink,backup_config)
  46.         FileUtils.cp(dest, config_symlink)
  47.       end
  48.     else
  49.       FileUtils.cp(dest, config_symlink)
  50.     end
  51.   end
  52.   def CkeditorFileUtils.create_uploads_directory
  53.     uploads = File.join(RAILS_ROOT, '/public/uploads')
  54.     FileUtils.mkdir(uploads) unless File.exist?(uploads)
  55.   end
  56.   def CkeditorFileUtils.install(log)
  57.     directory = File.join(RAILS_ROOT, '/vendor/plugins/easy-ckeditor/')
  58.     source = File.join(directory,'/public/javascripts/ckeditor/')
  59.     FileUtils.mkdir(CKEDITOR_INSTALL_DIRECTORY)
  60.     # recursively copy all our files over
  61.     recursive_copy(:source => source, :dest => CKEDITOR_INSTALL_DIRECTORY, :logging => log)
  62.     # create the upload directories
  63.     create_uploads_directory
  64.   end
  65.   ##################################################################
  66.   # remove the existing install (if any)
  67.   #
  68.   def  CkeditorFileUtils.destroy
  69.     if File.exist?(CKEDITOR_INSTALL_DIRECTORY)
  70.       FileUtils.rm_r(CKEDITOR_INSTALL_DIRECTORY)
  71.       FileUtils.rm(File.join(RAILS_ROOT, '/public/javascripts/ckcustom.js')) 
  72.       if File.exist? File.join(RAILS_ROOT, '/public/javascripts/ckcustom.js')
  73.     end
  74.   end
  75.   def CkeditorFileUtils.rm_plugin
  76.     if File.exist?(PLUGIN_INSTALL_DIRECTORY)
  77.       FileUtils.rm_r(PLUGIN_INSTALL_DIRECTORY)
  78.     end
  79.   end
  80.   def CkeditorFileUtils.destroy_and_install
  81.     CkeditorFileUtils.destroy
  82.     # now install fresh
  83.     install(true)
  84.     # copy over the config file (unless it exists)
  85.     copy_configuration
  86.   end
  87.   def CkeditorFileUtils.check_and_install
  88.     # check to see if already installed, if not install
  89.     unless File.exist?(CKEDITOR_INSTALL_DIRECTORY)
  90.       install(false)
  91.     end
  92.     # copy over the config file (unless it exists)
  93.     copy_configuration
  94.   end
  95. end