file_path_utils.rb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. require 'rubygems'
  2. require 'rake' # for ext()
  3. require 'fileutils'
  4. require 'system_wrapper'
  5. # global utility methods (for plugins, project files, etc.)
  6. def ceedling_form_filepath(destination_path, original_filepath, new_extension=nil)
  7. filename = File.basename(original_filepath)
  8. filename.replace(filename.ext(new_extension)) if (!new_extension.nil?)
  9. return File.join( destination_path.gsub(/\\/, '/'), filename )
  10. end
  11. class FilePathUtils
  12. GLOB_MATCHER = /[\*\?\{\}\[\]]/
  13. constructor :configurator, :file_wrapper
  14. ######### class methods ##########
  15. # standardize path to use '/' path separator & begin with './' & have no trailing path separator
  16. def self.standardize(path)
  17. path.strip!
  18. path.gsub!(/\\/, '/')
  19. path.gsub!(/^((\+|-):)?\.\//, '')
  20. path.chomp!('/')
  21. return path
  22. end
  23. def self.os_executable_ext(executable)
  24. return executable.ext('.exe') if SystemWrapper.windows?
  25. return executable
  26. end
  27. # extract directory path from between optional add/subtract aggregation modifiers and up to glob specifiers
  28. # note: slightly different than File.dirname in that /files/foo remains /files/foo and does not become /files
  29. def self.extract_path(path)
  30. path = path.sub(/^(\+|-):/, '')
  31. # find first occurrence of path separator followed by directory glob specifier: *, ?, {, }, [, ]
  32. find_index = (path =~ GLOB_MATCHER)
  33. # no changes needed (lop off final path separator)
  34. return path.chomp('/') if (find_index.nil?)
  35. # extract up to first glob specifier
  36. path = path[0..(find_index-1)]
  37. # lop off everything up to and including final path separator
  38. find_index = path.rindex('/')
  39. return path[0..(find_index-1)] if (not find_index.nil?)
  40. # return string up to first glob specifier if no path separator found
  41. return path
  42. end
  43. # return whether the given path is to be aggregated (no aggregation modifier defaults to same as +:)
  44. def self.add_path?(path)
  45. return (path =~ /^-:/).nil?
  46. end
  47. # get path (and glob) lopping off optional +: / -: prefixed aggregation modifiers
  48. def self.extract_path_no_aggregation_operators(path)
  49. return path.sub(/^(\+|-):/, '')
  50. end
  51. # all the globs that may be in a path string work fine with one exception;
  52. # to recurse through all subdirectories, the glob is dir/**/** but our paths use
  53. # convention of only dir/**
  54. def self.reform_glob(path)
  55. return path if (path =~ /\/\*\*$/).nil?
  56. return path + '/**'
  57. end
  58. def self.form_ceedling_vendor_path(*filepaths)
  59. return File.join( CEEDLING_VENDOR, filepaths )
  60. end
  61. ######### instance methods ##########
  62. def form_temp_path(filepath, prefix='')
  63. return File.join( @configurator.project_temp_path, prefix + File.basename(filepath) )
  64. end
  65. ### release ###
  66. def form_release_build_cache_path(filepath)
  67. return File.join( @configurator.project_release_build_cache_path, File.basename(filepath) )
  68. end
  69. def form_release_dependencies_filepath(filepath)
  70. return File.join( @configurator.project_release_dependencies_path, File.basename(filepath).ext(@configurator.extension_dependencies) )
  71. end
  72. def form_release_build_c_object_filepath(filepath)
  73. return File.join( @configurator.project_release_build_output_c_path, File.basename(filepath).ext(@configurator.extension_object) )
  74. end
  75. def form_release_build_asm_object_filepath(filepath)
  76. return File.join( @configurator.project_release_build_output_asm_path, File.basename(filepath).ext(@configurator.extension_object) )
  77. end
  78. def form_release_build_c_objects_filelist(files)
  79. return (@file_wrapper.instantiate_file_list(files)).pathmap("#{@configurator.project_release_build_output_c_path}/%n#{@configurator.extension_object}")
  80. end
  81. def form_release_build_asm_objects_filelist(files)
  82. return (@file_wrapper.instantiate_file_list(files)).pathmap("#{@configurator.project_release_build_output_asm_path}/%n#{@configurator.extension_object}")
  83. end
  84. def form_release_build_c_list_filepath(filepath)
  85. return File.join( @configurator.project_release_build_output_c_path, File.basename(filepath).ext(@configurator.extension_list) )
  86. end
  87. def form_release_dependencies_filelist(files)
  88. return (@file_wrapper.instantiate_file_list(files)).pathmap("#{@configurator.project_release_dependencies_path}/%n#{@configurator.extension_dependencies}")
  89. end
  90. ### tests ###
  91. def form_test_build_cache_path(filepath)
  92. return File.join( @configurator.project_test_build_cache_path, File.basename(filepath) )
  93. end
  94. def form_pass_results_filepath(filepath)
  95. return File.join( @configurator.project_test_results_path, File.basename(filepath).ext(@configurator.extension_testpass) )
  96. end
  97. def form_fail_results_filepath(filepath)
  98. return File.join( @configurator.project_test_results_path, File.basename(filepath).ext(@configurator.extension_testfail) )
  99. end
  100. def form_runner_filepath_from_test(filepath)
  101. return File.join( @configurator.project_test_runners_path, File.basename(filepath, @configurator.extension_source)) + @configurator.test_runner_file_suffix + @configurator.extension_source
  102. end
  103. def form_test_filepath_from_runner(filepath)
  104. return filepath.sub(/#{TEST_RUNNER_FILE_SUFFIX}/, '')
  105. end
  106. def form_runner_object_filepath_from_test(filepath)
  107. return (form_test_build_object_filepath(filepath)).sub(/(#{@configurator.extension_object})$/, "#{@configurator.test_runner_file_suffix}\\1")
  108. end
  109. def form_test_build_object_filepath(filepath)
  110. return File.join( @configurator.project_test_build_output_path, File.basename(filepath).ext(@configurator.extension_object) )
  111. end
  112. def form_test_executable_filepath(filepath)
  113. return File.join( @configurator.project_test_build_output_path, File.basename(filepath).ext(@configurator.extension_executable) )
  114. end
  115. def form_test_build_map_filepath(filepath)
  116. return File.join( @configurator.project_test_build_output_path, File.basename(filepath).ext(@configurator.extension_map) )
  117. end
  118. def form_test_build_list_filepath(filepath)
  119. return File.join( @configurator.project_test_build_output_path, File.basename(filepath).ext(@configurator.extension_list) )
  120. end
  121. def form_preprocessed_file_filepath(filepath)
  122. return File.join( @configurator.project_test_preprocess_files_path, File.basename(filepath) )
  123. end
  124. def form_preprocessed_includes_list_filepath(filepath)
  125. return File.join( @configurator.project_test_preprocess_includes_path, File.basename(filepath) )
  126. end
  127. def form_test_build_objects_filelist(sources)
  128. return (@file_wrapper.instantiate_file_list(sources)).pathmap("#{@configurator.project_test_build_output_path}/%n#{@configurator.extension_object}")
  129. end
  130. def form_preprocessed_mockable_headers_filelist(mocks)
  131. # pathmapping note: "%{#{@configurator.cmock_mock_prefix},}n" replaces mock_prefix with nothing (signified by absence of anything after comma inside replacement brackets)
  132. return (@file_wrapper.instantiate_file_list(mocks)).pathmap("#{@configurator.project_test_preprocess_files_path}/%{#{@configurator.cmock_mock_prefix},}n#{@configurator.extension_header}")
  133. end
  134. def form_mocks_source_filelist(mocks)
  135. return (@file_wrapper.instantiate_file_list(mocks)).pathmap("#{@configurator.cmock_mock_path}/%n#{@configurator.extension_source}")
  136. end
  137. def form_test_dependencies_filelist(files)
  138. return (@file_wrapper.instantiate_file_list(files)).pathmap("#{@configurator.project_test_dependencies_path}/%n#{@configurator.extension_dependencies}")
  139. end
  140. def form_pass_results_filelist(path, files)
  141. return (@file_wrapper.instantiate_file_list(files)).pathmap("#{path}/%n#{@configurator.extension_testpass}")
  142. end
  143. end