Rework install shell config classes
This commit is contained in:
parent
d6175e0c66
commit
a59eedb748
@ -2,40 +2,52 @@ module Vagrant
|
||||
module Util
|
||||
# Generic installation of content to shell config file
|
||||
class InstallShellConfig
|
||||
PREPEND = "".freeze
|
||||
STRING_INSERT = "".freeze
|
||||
APPEND = "".freeze
|
||||
CONFIG_PATHS = [""].freeze
|
||||
|
||||
attr_accessor :prepend_string
|
||||
attr_accessor :string_insert
|
||||
attr_accessor :append_string
|
||||
attr_accessor :config_paths
|
||||
|
||||
def self.shell_installed(home)
|
||||
self::CONFIG_PATHS.each do |path|
|
||||
def initialize(prepend_string, string_insert, append_string, config_paths)
|
||||
@prepend_string = prepend_string
|
||||
@string_insert = string_insert
|
||||
@append_string = append_string
|
||||
@config_paths = config_paths
|
||||
@logger = Log4r::Logger.new("vagrant::util::install_shell_config")
|
||||
end
|
||||
|
||||
def shell_installed(home)
|
||||
@logger.info("Searching for config in home #{home}")
|
||||
@config_paths.each do |path|
|
||||
config_file = File.join(home, path)
|
||||
if File.exists?(config_file)
|
||||
@logger.info("Found config file #{config_file}")
|
||||
return config_file
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
def self.is_installed(path)
|
||||
def is_installed(path)
|
||||
File.foreach(path) do |line|
|
||||
if line.include?(self::PREPEND)
|
||||
if line.include?(@prepend_string)
|
||||
@logger.info("Found completion already installed in #{path}")
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
def self.install(home)
|
||||
def install(home)
|
||||
path = shell_installed(home)
|
||||
if path && !is_installed(path)
|
||||
File.open(path, "a") do |f|
|
||||
f.write("\n")
|
||||
f.write(self::PREPEND)
|
||||
f.write(@prepend_string)
|
||||
f.write("\n")
|
||||
f.write(self::STRING_INSERT)
|
||||
f.write(@string_insert)
|
||||
f.write("\n")
|
||||
f.write(self::APPEND)
|
||||
f.write(@append_string)
|
||||
f.write("\n")
|
||||
end
|
||||
end
|
||||
@ -45,25 +57,31 @@ module Vagrant
|
||||
|
||||
# Install autocomplete script to zsh config located as .zshrc
|
||||
class InstallZSHShellConfig < InstallShellConfig
|
||||
PREPEND = "# >>>> Vagrant zsh completion (start)".freeze
|
||||
STRING_INSERT = """fpath=(#{File.join(Vagrant.source_root, "contrib", "zsh")} $fpath)\ncompinit""".freeze
|
||||
APPEND = "# <<<< Vagrant zsh completion (end)".freeze
|
||||
CONFIG_PATHS = [".zshrc"].freeze
|
||||
def initialize()
|
||||
prepend_string = "# >>>> Vagrant zsh completion (start)".freeze
|
||||
string_insert = """fpath=(#{File.join(Vagrant.source_root, "contrib", "zsh")} $fpath)\ncompinit""".freeze
|
||||
append_string = "# <<<< Vagrant zsh completion (end)".freeze
|
||||
config_paths = [".zshrc".freeze].freeze
|
||||
super(prepend_string, string_insert, append_string, config_paths)
|
||||
end
|
||||
end
|
||||
|
||||
# Install autocomplete script to bash config located as .bashrc or .bash_profile
|
||||
class InstallBashShellConfig < InstallShellConfig
|
||||
PREPEND = "# >>>> Vagrant bash completion (start)".freeze
|
||||
STRING_INSERT = ". #{File.join(Vagrant.source_root, 'contrib', 'bash', 'completion.sh')}".freeze
|
||||
APPEND = "# <<<< Vagrant bash completion (end)".freeze
|
||||
CONFIG_PATHS = [".bashrc", ".bash_profile"].freeze
|
||||
def initialize()
|
||||
prepend_string = "# >>>> Vagrant bash completion (start)".freeze
|
||||
string_insert = ". #{File.join(Vagrant.source_root, 'contrib', 'bash', 'completion.sh')}".freeze
|
||||
append_string = "# <<<< Vagrant bash completion (end)".freeze
|
||||
config_paths = [".bashrc".freeze, ".bash_profile".freeze].freeze
|
||||
super(prepend_string, string_insert, append_string, config_paths)
|
||||
end
|
||||
end
|
||||
|
||||
# Install autocomplete script for supported shells
|
||||
class InstallCLIAutocomplete
|
||||
SUPPORTED_SHELLS = {
|
||||
"zsh" => Vagrant::Util::InstallZSHShellConfig,
|
||||
"bash" => Vagrant::Util::InstallBashShellConfig
|
||||
"zsh" => Vagrant::Util::InstallZSHShellConfig.new(),
|
||||
"bash" => Vagrant::Util::InstallBashShellConfig.new()
|
||||
}
|
||||
|
||||
def self.install
|
||||
|
||||
@ -8,7 +8,7 @@ describe Vagrant::Util::InstallZSHShellConfig do
|
||||
let(:home) { "#{Dir.tmpdir}/not-home" }
|
||||
let(:target_file) { "#{home}/.zshrc" }
|
||||
|
||||
subject { described_class }
|
||||
subject { described_class.new() }
|
||||
|
||||
before do
|
||||
Dir.mkdir(home)
|
||||
@ -21,7 +21,7 @@ describe Vagrant::Util::InstallZSHShellConfig do
|
||||
FileUtils.rm_rf(home)
|
||||
end
|
||||
|
||||
describe ".shell_installed" do
|
||||
describe "#shell_installed" do
|
||||
it "should return path to config file if exists" do
|
||||
expect(subject.shell_installed(home)).to eq(target_file)
|
||||
end
|
||||
@ -39,7 +39,7 @@ describe Vagrant::Util::InstallZSHShellConfig do
|
||||
|
||||
it "returns true if autocompletion is already installed" do
|
||||
File.open(target_file, "w") do |f|
|
||||
f.write(Vagrant::Util::InstallZSHShellConfig::PREPEND)
|
||||
f.write(subject.prepend_string)
|
||||
end
|
||||
expect(subject.is_installed(target_file)).to eq(true)
|
||||
end
|
||||
@ -50,9 +50,9 @@ describe Vagrant::Util::InstallZSHShellConfig do
|
||||
subject.install(home)
|
||||
file = File.open(target_file)
|
||||
content = file.read
|
||||
expect(content.include?(Vagrant::Util::InstallZSHShellConfig::PREPEND)).to eq(true)
|
||||
expect(content.include?(Vagrant::Util::InstallZSHShellConfig::STRING_INSERT)).to eq(true)
|
||||
expect(content.include?(Vagrant::Util::InstallZSHShellConfig::APPEND)).to eq(true)
|
||||
expect(content.include?(subject.prepend_string)).to eq(true)
|
||||
expect(content.include?(subject.string_insert)).to eq(true)
|
||||
expect(content.include?(subject.append_string)).to eq(true)
|
||||
file.close
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user