Class: Sunspot::Installer::SolrconfigUpdater

Inherits:
Object
  • Object
show all
Includes:
TaskHelper
Defined in:
lib/sunspot/installer/solrconfig_updater.rb

Constant Summary collapse

CONFIG_FILES =
%w(solrconfig.xml elevate.xml spellings.txt stopwords.txt synonyms.txt)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TaskHelper

#add_element, #say

Constructor Details

#initialize(solrconfig_path, options) ⇒ SolrconfigUpdater

Returns a new instance of SolrconfigUpdater.



18
19
20
21
22
23
24
25
26
# File 'lib/sunspot/installer/solrconfig_updater.rb', line 18

def initialize(solrconfig_path, options)
  @force = !!options[:force]
  unless @force || File.exist?(solrconfig_path)
    abort("#{File.expand_path(@solrconfig_path)} doesn't exist." +
          " Are you sure you're pointing to a SOLR_HOME?")
  end
  @solrconfig_path = solrconfig_path
  @verbose = !!options[:verbose]
end

Class Method Details

.execute(solrconfig_path, options = {}) ⇒ Object



13
14
15
# File 'lib/sunspot/installer/solrconfig_updater.rb', line 13

def execute(solrconfig_path, options = {})
  new(solrconfig_path, options).execute
end

Instance Method Details

#executeObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sunspot/installer/solrconfig_updater.rb', line 28

def execute
  if @force
    config_dir = File.dirname(@solrconfig_path)
    FileUtils.mkdir_p(config_dir)
    CONFIG_FILES.each do |file|
      source_path =
        File.join(File.dirname(__FILE__), '..', '..', '..', 'solr', 'solr', 'conf', file)
      FileUtils.cp(source_path, config_dir)
      say("Copied default #{file} to #{config_dir}")
    end
  else
    @document = File.open(@solrconfig_path) do |f|
      Nokogiri::XML(
        f, nil, nil, 
        Nokogiri::XML::ParseOptions::DEFAULT_XML |
        Nokogiri::XML::ParseOptions::NOBLANKS
      )
    end
    @root = @document.root
    maybe_add_more_like_this_handler
    original_path = "#{@solrconfig_path}.orig"
    FileUtils.cp(@solrconfig_path, original_path)
    say("Saved backup of original to #{original_path}")
    File.open(@solrconfig_path, 'w') do |file|
      @document.write_to(
        file,
        :indent => 2
      )
    end
    say("Wrote solrconfig to #{@solrconfig_path}")
  end
end