project_file_loader.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. require '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. @user_file = nil
  8. @main_project_filepath = ''
  9. @user_project_filepath = ''
  10. end
  11. def find_project_files
  12. # first go hunting for optional user project file by looking for environment variable and then default location on disk
  13. user_filepath = @system_wrapper.env_get('CEEDLING_USER_PROJECT_FILE')
  14. if ( not user_filepath.nil? and @file_wrapper.exist?(user_filepath) )
  15. @user_project_filepath = user_filepath
  16. elsif (@file_wrapper.exist?(DEFAULT_CEEDLING_USER_PROJECT_FILE))
  17. @user_project_filepath = DEFAULT_CEEDLING_USER_PROJECT_FILE
  18. end
  19. # next check for main project file by looking for environment variable and then default location on disk;
  20. # blow up if we don't find this guy -- like, he's so totally important
  21. main_filepath = @system_wrapper.env_get('CEEDLING_MAIN_PROJECT_FILE')
  22. if ( not main_filepath.nil? and @file_wrapper.exist?(main_filepath) )
  23. @main_project_filepath = main_filepath
  24. elsif (@file_wrapper.exist?(DEFAULT_CEEDLING_MAIN_PROJECT_FILE))
  25. @main_project_filepath = DEFAULT_CEEDLING_MAIN_PROJECT_FILE
  26. else
  27. # no verbosity checking since this is lowest level reporting anyhow &
  28. # verbosity checking depends on configurator which in turns needs this class (circular dependency)
  29. @stream_wrapper.stderr_puts('Found no Ceedling project file (*.yml)')
  30. raise
  31. end
  32. @main_file = File.basename( @main_project_filepath )
  33. @user_file = File.basename( @user_project_filepath ) if ( not @user_project_filepath.empty? )
  34. end
  35. def load_project_config
  36. config_hash = {}
  37. # if there's no user project file, then just provide hash from project file
  38. if (@user_project_filepath.empty?)
  39. config_hash = @yaml_wrapper.load(@main_project_filepath)
  40. # if there is a user project file, load it too and merge it on top of the project file,
  41. # superseding anything that's common between them
  42. else
  43. config_hash = (@yaml_wrapper.load(@main_project_filepath)).merge(@yaml_wrapper.load(@user_project_filepath))
  44. end
  45. return config_hash
  46. end
  47. end