project_file_loader.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. require 'ceedling/constants'
  2. class ProjectFileLoader
  3. attr_reader :main_file, :user_file
  4. constructor :yaml_wrapper, :stream_wrapper, :system_wrapper, :file_wrapper
  5. def setup
  6. @main_file = nil
  7. @mixin_files = []
  8. @user_file = nil
  9. @main_project_filepath = ''
  10. @mixin_project_filepaths = []
  11. @user_project_filepath = ''
  12. end
  13. def find_project_files
  14. # first go hunting for optional user project file by looking for environment variable and then default location on disk
  15. user_filepath = @system_wrapper.env_get('CEEDLING_USER_PROJECT_FILE')
  16. if ( not user_filepath.nil? and @file_wrapper.exist?(user_filepath) )
  17. @user_project_filepath = user_filepath
  18. elsif (@file_wrapper.exist?(DEFAULT_CEEDLING_USER_PROJECT_FILE))
  19. @user_project_filepath = DEFAULT_CEEDLING_USER_PROJECT_FILE
  20. end
  21. # next check for mixin project files by looking for environment variable
  22. mixin_filepaths = @system_wrapper.env_get('CEEDLING_MIXIN_PROJECT_FILES')
  23. if ( not mixin_filepaths.nil? )
  24. mixin_filepaths.split(File::PATH_SEPARATOR).each do |filepath|
  25. if ( @file_wrapper.exist?(filepath) )
  26. @mixin_project_filepaths.push(filepath)
  27. end
  28. end
  29. end
  30. # next check for main project file by looking for environment variable and then default location on disk;
  31. # blow up if we don't find this guy -- like, he's so totally important
  32. main_filepath = @system_wrapper.env_get('CEEDLING_MAIN_PROJECT_FILE')
  33. if ( not main_filepath.nil? and @file_wrapper.exist?(main_filepath) )
  34. @main_project_filepath = main_filepath
  35. elsif (@file_wrapper.exist?(DEFAULT_CEEDLING_MAIN_PROJECT_FILE))
  36. @main_project_filepath = DEFAULT_CEEDLING_MAIN_PROJECT_FILE
  37. else
  38. # no verbosity checking since this is lowest level reporting anyhow &
  39. # verbosity checking depends on configurator which in turns needs this class (circular dependency)
  40. @stream_wrapper.stderr_puts('Found no Ceedling project file (*.yml)')
  41. raise
  42. end
  43. @main_file = File.basename( @main_project_filepath )
  44. @mixin_project_filepaths.each do |filepath|
  45. @mixin_files.push(File.basename( filepath ))
  46. end
  47. @user_file = File.basename( @user_project_filepath ) if ( not @user_project_filepath.empty? )
  48. end
  49. def yaml_merger(y1, y2)
  50. o1 = y1
  51. y2.each_pair do |k,v|
  52. if o1[k].nil?
  53. o1[k] = v
  54. else
  55. if (o1[k].instance_of? Hash)
  56. o1[k] = yaml_merger(o1[k], v)
  57. elsif (o1[k].instance_of? Array)
  58. o1[k] += v
  59. else
  60. o1[k] = v
  61. end
  62. end
  63. end
  64. return o1
  65. end
  66. def load_project_config
  67. config_hash = @yaml_wrapper.load(@main_project_filepath)
  68. # if there are mixin project files, then use them
  69. @mixin_project_filepaths.each do |filepath|
  70. mixin = @yaml_wrapper.load(filepath)
  71. config_hash = yaml_merger( config_hash, mixin )
  72. end
  73. # if there's a user project file, then use it
  74. if ( not @user_project_filepath.empty? )
  75. user_hash = @yaml_wrapper.load(@user_project_filepath)
  76. config_hash = yaml_merger( config_hash, user_hash )
  77. end
  78. return config_hash
  79. end
  80. end