Module: Config
- Defined in:
- lib/canuby/config.rb
Overview
Canuby's Config utility to read and write canuby.yml files
Structure: projects => name => url, version, build_tool, project_file, output_dir, outputs
Class Method Summary collapse
-
.check(quiet = false) ⇒ Object
Checks if the config structure is valid.
- .invalid(option) ⇒ Object
-
.load ⇒ Object
Load config from file or default if no file exists.
-
.load_default(args) ⇒ Object
Default config file.
-
.migrate(from_version, to_version) ⇒ Object
Migrate the config to a newer version.
-
.write ⇒ Object
Write config to file.
Class Method Details
.check(quiet = false) ⇒ Object
Checks if the config structure is valid. Does not check if the actual content is working.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/canuby/config.rb', line 61 def self.check(quiet = false) invalid('Missing projects key') unless $options.respond_to? :projects invalid('Missing project value') unless $options.projects.key?($options.projects.keys[0]) $options.projects.each_key do |project| %w[url version build_tool project_file output_dir outputs].each_entry do |key| invalid("#{project} misses #{key} key") unless $options.projects[project.to_s].key?(key) end end logger.warn('Config is valid!') unless quiet end |
.invalid(option) ⇒ Object
72 73 74 |
# File 'lib/canuby/config.rb', line 72 def self.invalid(option) raise StandardError, "#{option}. Canuby can't continue" end |
.load ⇒ Object
Load config from file or default if no file exists.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/canuby/config.rb', line 31 def self.load # skip if run trough rake or bundler args = ArgParser.min(ARGV) logger.debug("Ignoring config file #{$options.ignore_config_file}") if ENV['Testing'] != 'true' && $options.ignore_config_file != 'true' && File.exist?(args.yml_file) yaml_file = YAML.load_file(args.yml_file) migrate(yaml_file[:config_version], Canuby::CONFIG_VERSION) $options = OpenStruct.new(args.to_h.merge!(yaml_file)) else load_default(args) end end |
.load_default(args) ⇒ Object
Default config file. Mostly used for testing.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/canuby/config.rb', line 45 def self.load_default(args) default = { 'projects' => { 'Googletest' => { 'url' => 'https://github.com/google/googletest', 'version' => '1.0.0', \ 'build_tool' => 'msbuild', 'project_file' => 'googletest-distribution', 'output_dir' => 'googlemock/gtest', 'outputs' => ['gtest.lib', 'gtest_main.lib'] }, \ 'Dummy' => { 'url' => 'https://github.com/google/googletest', 'version' => '1.0.0', 'build_tool' => 'msbuild', 'project_file' => 'googletest-distribution', 'output_dir' => 'googlemock/gtest', 'outputs' => ['gtest.lib'] }, \ 'Dummy2' => { 'url' => 'https://github.com/google/googletest', 'version' => '1.0.0', 'build_tool' => 'msbuild', 'project_file' => 'googletest-distribution', 'output_dir' => 'googlemock/gtest', 'outputs' => ['gtest_main.lib'] } } } $options = OpenStruct.new(args.to_h.merge!(default)) end |
.migrate(from_version, to_version) ⇒ Object
Migrate the config to a newer version. Not used until Canuby hits a more stable state.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/canuby/config.rb', line 77 def self.migrate(from_version, to_version) return unless from_version != to_version logger.info("Migrating from #{from_version} to #{to_version}") case from_version when 0 logger.warn('No compatibility gurantied!') # TODO when 1 logger.error('TODO') # TODO end end |
.write ⇒ Object
Write config to file
92 93 94 95 96 97 98 99 |
# File 'lib/canuby/config.rb', line 92 def self.write write_config = $options.to_h [:target, :yml_file].each do |key| write_config.delete(key) end File.write('canuby.yml', write_config.to_yaml) end |