Class: PrestaShopAutomationTool::ConfigurationParser

Inherits:
Object
  • Object
show all
Defined in:
lib/prestashop-automation-tool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ ConfigurationParser

Returns a new instance of ConfigurationParser.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/prestashop-automation-tool.rb', line 15

def initialize root
	@root = File.realpath(root)

	vhosts = ApacheVhostsParser.parseDirectory

	if url = vhosts.urlFor(@root)
		fou = "http://#{url}/"
	else
		fou = "http://localhost/#{File.basename(@root)}/"
	end

	@config_fields = {
		database_user: '',
		database_password: '',
		database_name: '',
		database_prefix: 'ps_',
		database_host: 'localhost',
		version: '',
		front_office_url: fou,
		back_office_url: nil,
		installer_url: nil,
		admin_email: '[email protected]',
		admin_password: '123456789',
		default_customer_email: '[email protected]',
		default_customer_password: '123456789',
		tests_repository: 'https://github.com/djfm/prestashop-automation-tool-tests'
	}

	@config = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/prestashop-automation-tool.rb', line 13

def config
  @config
end

Instance Method Details

#ask_user_for_missing_info(options = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/prestashop-automation-tool.rb', line 77

def ask_user_for_missing_info options={}
	@config_fields.each_pair do |key, default_value|
		unless @config[key]
			if default_value
				default = default_value != '' ? " (default: \"#{default_value.to_s})\"" : ''
				unless options[:accept_defaults]
					print "#{key.to_s.split('_').map(&:capitalize).join ' '}#{default}? "
					value = $stdin.gets.strip
					value = default_value if value == ""
					@config[key] = value
				else
					@config[key] = default_value
				end
			end
		end
	end
end

#autocomplete_configObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/prestashop-automation-tool.rb', line 95

def autocomplete_config
	if m = @config[:database_host].match(/^(.*?)\:(.*?)$/)
		@config[:database_host] = m[1]
		@config[:database_port] = m[2]
	end
	if @admin_folder
		@config[:back_office_url] = URI.join(@config[:front_office_url], @admin_folder + '/').to_s
	end
	if @installer_folder
		@config[:installer_url] = URI.join(@config[:front_office_url], @installer_folder + '/').to_s
	end
end

#parseObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/prestashop-automation-tool.rb', line 45

def parse
	if File.exists? path=File.join(@root, 'config', 'settings.inc.php')
		config = Hash[File.read(path).scan(/\bdefine\s*\(\s*'(.*?)'\s*,\s*'(.*?)'\s*\)\s*;/)]

		mapping = {
			database_user: '_DB_USER_',
			database_password: '_DB_PASSWD_',
			database_name: '_DB_NAME_',
			database_prefix: '_DB_PREFIX_',
			database_host: '_DB_SERVER_',
			version: '_PS_VERSION_'
		}

		@config_fields.each_pair do |key, default_value|
			if (value = config[mapping[key]].to_s.strip) != ''
				@config[key] = value
			end
		end
	end

	Dir.entries('.').each do |entry|
		unless entry =~ /^\./
			if File.directory? entry
				if File.exists? File.join(entry, 'ajax-tab.php')
					@admin_folder = entry
				elsif File.exists? File.join(entry, 'index_cli.php')
					@installer_folder = entry
				end
			end
		end
	end
end