Class: CavalerieWeb::FileFolderManager

Inherits:
Object
  • Object
show all
Defined in:
lib/cavalerie_web/managers/file_folder_manager.rb

Class Method Summary collapse

Class Method Details

.clean_up_hosts_fileObject



101
102
103
# File 'lib/cavalerie_web/managers/file_folder_manager.rb', line 101

def self.clean_up_hosts_file
  ghost_command "bust"
end

.create_folder_if_not_exist(path) ⇒ Object



7
8
9
10
11
12
# File 'lib/cavalerie_web/managers/file_folder_manager.rb', line 7

def self.create_folder_if_not_exist path
  unless File.directory? path
    FileUtils.mkdir_p path
    puts Message.warning "Folder didn't exist and was created: #{path}"
  end
end

.create_local_config_file_if_not_exist(site) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cavalerie_web/managers/file_folder_manager.rb', line 72

def self.create_local_config_file_if_not_exist site
  local_config_file_sample = "#{$configs_path}/local-config.sample.php"
  local_config_file = "#{site[:path]}/local-config.php"

  unless File.exist? local_config_file
    docker_configs = Config.get_docker_configs
    FileUtils.cp local_config_file_sample, local_config_file
    local_config_file_content = File.read local_config_file

    local_config_file_content.gsub! "_db_name_here_", "#{site[:name]}-dev"
    local_config_file_content.gsub! "_db_host_here_", docker_configs["mysql-server_container_ip"]

    File.open(local_config_file, 'w') { |file| file << local_config_file_content }

    puts Message.notice "New #{site[:name]}'s local-config.php file created."
  end
end

.create_virtualhost_file_if_not_exist(site) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cavalerie_web/managers/file_folder_manager.rb', line 14

def self.create_virtualhost_file_if_not_exist site
  virtualhost_file_sample = "#{$configs_path}/VirtualHost.sample"
  virtualhost_file = "#{site[:path]}/VirtualHost"

  unless File.exist? virtualhost_file
    FileUtils.cp virtualhost_file_sample, virtualhost_file
    file_content = File.read virtualhost_file
    file_content.gsub! "_site_name_here_", site[:name]

    File.open(virtualhost_file, 'w') { |file| file << file_content }

    puts Message.notice "#{site[:name]}: VirtualHost file created."
  end
end

.delete_local_config_file_if_invalid(site) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/cavalerie_web/managers/file_folder_manager.rb', line 90

def self.delete_local_config_file_if_invalid site
  local_config_file = "#{site[:path]}/local-config.php"
  local_config_file_content = File.read local_config_file
  docker_configs = Config.get_docker_configs

  unless local_config_file_content.include?(site[:name]) && local_config_file_content.include?(docker_configs["mysql-server_container_ip"])
    File.delete local_config_file
    puts Message.warning "#{site[:name]}: local-config.php file was invalid and has been deleted."
  end
end

.delete_virtualhost_file_if_invalid(site) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/cavalerie_web/managers/file_folder_manager.rb', line 29

def self.delete_virtualhost_file_if_invalid site
  virtualhost_file = "#{site[:path]}/VirtualHost"
  virtualhost_file_content = File.read virtualhost_file

  unless virtualhost_file_content.include? site[:name]
    File.delete virtualhost_file
    puts Message.warning "#{site[:name]}: VirtualHost file was invalid and has been deleted."
  end
end

.get_global_virtualhosts_fileObject



39
40
41
42
43
44
45
46
# File 'lib/cavalerie_web/managers/file_folder_manager.rb', line 39

def self.get_global_virtualhosts_file
  virtualhost_file = "#{$configs_path}/sites_virtualhosts"
  if File.exist? virtualhost_file
    "\n#{File.read(virtualhost_file)}"
  else
    Message.warning "Apache virtualhosts file doesn't exist yet.\nDon't worry too much: it will be automatically\ncreated when you'll run 'cavalerie up'"
  end
end

.get_hosts_listObject



105
106
107
108
109
110
111
112
# File 'lib/cavalerie_web/managers/file_folder_manager.rb', line 105

def self.get_hosts_list
  begin
    raise UnableToAccessHostsFileOnWindowsOSError if OSHelper.is_windows?
    ghost_command "list"
  rescue UnableToAccessHostsFileOnWindowsOSError
    puts Message.error "Windows OS detected. Unable to access hosts file."
  end
end

.update_global_virtualhosts_fileObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/cavalerie_web/managers/file_folder_manager.rb', line 48

def self.update_global_virtualhosts_file
  virtualhost_file = "#{$configs_path}/sites_virtualhosts"
  File.open(virtualhost_file, 'w') do |file|
    SiteManager.get_all_sites.each do |site|
      file.write File.read(site[:path] + "/VirtualHost") + "\n\n"
    end
  end

  puts Message.notice "Apache virtualhosts updated."
end

.update_hosts_fileObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cavalerie_web/managers/file_folder_manager.rb', line 59

def self.update_hosts_file
  # Display warning message on Windows since we can't yet update hosts file on Windows
  if OSHelper.is_windows?
    puts Message.warning "Windows OS detected. Unable to auto-update hosts file. Please update manually."
  else
    clean_up_hosts_file
    SiteManager.get_all_sites.each do |site|
      ghost_command "add #{site[:name]}.dev"
    end
    puts Message.notice "Hosts file updated."
  end
end