19 |
# Reads and writes properties (properties.rb) to a persistent file in YAML |
# Reads and writes properties (properties.rb) to a persistent file in YAML |
20 |
# |
# |
21 |
class YAMLPropertiesReader |
class YAMLPropertiesReader |
22 |
|
|
23 |
|
@@userdir = nil |
24 |
|
|
25 |
def self.load_libraries |
def self.load_libraries |
26 |
require 'yaml' |
require 'yaml' |
27 |
end |
end |
28 |
|
|
29 |
|
## |
30 |
|
# Determine zhere the user directory is (if any) |
31 |
|
# |
32 |
|
def self.setup |
33 |
|
if RUBY_PLATFORM =~ /mswin32/ |
34 |
|
@@userdir = File.join(ENV['USERPROFILE'],'freeride') unless ENV['USERPROFILE'].nil? |
35 |
|
else |
36 |
|
@@userdir = File.join(ENV['HOME'],'.freeride') unless ENV['HOME'].nil? end |
37 |
|
end |
38 |
|
|
39 |
|
## |
40 |
|
# Given a relative path to a property file, return the full path |
41 |
|
# in the user directory. Return nil if no user directory defined |
42 |
|
# or absolute path name given. |
43 |
|
# |
44 |
|
def self.user_filename(filespec) |
45 |
|
return nil if @@userdir.nil? || filespec[0..0]=='/' |
46 |
|
File.expand_path(File.join(@@userdir,filespec)) |
47 |
|
end |
48 |
|
|
49 |
|
## |
50 |
|
# Save a property file. Try in the user directory first then in the |
51 |
|
# FR directory |
52 |
|
# |
53 |
def self.save(base_slot, filespec, name, version) |
def self.save(base_slot, filespec, name, version) |
54 |
|
user_filespec = user_filename(filespec) |
55 |
|
if !user_filespec.nil? |
56 |
|
filespec = user_filespec |
57 |
|
# if file first saved make sure to create the full path first |
58 |
|
unless File.exist?(filespec) |
59 |
|
require 'ftools' |
60 |
|
File.makedirs(File.dirname(filespec)) |
61 |
|
end |
62 |
|
end |
63 |
file = File.new(filespec, "w+") |
file = File.new(filespec, "w+") |
64 |
file.puts "#### Properties: #{name} - Version: #{version}" |
file.puts "#### Properties: #{name} - Version: #{version}" |
65 |
file.puts gather_data(base_slot).to_yaml(:SortKeys=>true) |
file.puts gather_data(base_slot).to_yaml(:SortKeys=>true) |
79 |
structure |
structure |
80 |
end |
end |
81 |
|
|
82 |
|
## |
83 |
|
# Load a property file. Try in the user directory first then in the |
84 |
|
# FR directory |
85 |
|
# |
86 |
def self.load(base_slot, filespec) |
def self.load(base_slot, filespec) |
87 |
|
user_filespec = user_filename(filespec) |
88 |
|
if !user_filespec.nil? && File.exist?(user_filespec) |
89 |
|
filespec = user_filespec |
90 |
|
end |
91 |
|
#puts "Loading Property File: #{filespec}" |
92 |
return unless File.exist?(filespec) |
return unless File.exist?(filespec) |
93 |
data = nil |
data = nil |
94 |
File.open(filespec) {|file| file.binmode; data = file.read} |
File.open(filespec) {|file| file.binmode; data = file.read} |
268 |
# It is important that these be set either to YAML or REXML. |
# It is important that these be set either to YAML or REXML. |
269 |
DefaultPropertiesReader = YAMLPropertiesReader |
DefaultPropertiesReader = YAMLPropertiesReader |
270 |
DefaultPropertiesReader.load_libraries |
DefaultPropertiesReader.load_libraries |
271 |
|
DefaultPropertiesReader.setup |
272 |
DefaultConfigurationReader = YAMLConfigurationReader |
DefaultConfigurationReader = YAMLConfigurationReader |
273 |
|
|
274 |
end |
end |