@@ -163,6 +163,8 @@ def installed_in_plugin_root?(name)
163163 # @param [Pathname] index file path
164164 # @param [Boolean] is the index file global index
165165 def load_index ( index_file , global = false )
166+ base = base_for_index ( global )
167+
166168 SharedHelpers . filesystem_access ( index_file , :read ) do |index_f |
167169 valid_file = index_f &.exist? && !index_f . size . zero?
168170 break unless valid_file
@@ -174,8 +176,8 @@ def load_index(index_file, global = false)
174176
175177 @commands . merge! ( index [ "commands" ] )
176178 @hooks . merge! ( index [ "hooks" ] )
177- @load_paths . merge! ( index [ "load_paths" ] )
178- @plugin_paths . merge! ( index [ "plugin_paths" ] )
179+ @load_paths . merge! ( transform_index_paths ( index [ "load_paths" ] ) { | p | absolutize_path ( p , base ) } )
180+ @plugin_paths . merge! ( transform_index_paths ( index [ "plugin_paths" ] ) { | p | absolutize_path ( p , base ) } )
179181 @sources . merge! ( index [ "sources" ] ) unless global
180182 end
181183 end
@@ -184,11 +186,13 @@ def load_index(index_file, global = false)
184186 # instance variables in YAML format. (The instance variables are supposed
185187 # to be only String key value pairs)
186188 def save_index
189+ base = base_for_index ( false )
190+
187191 index = {
188192 "commands" => @commands ,
189193 "hooks" => @hooks ,
190- "load_paths" => @load_paths ,
191- "plugin_paths" => @plugin_paths ,
194+ "load_paths" => transform_index_paths ( @load_paths ) { | p | relativize_path ( p , base ) } ,
195+ "plugin_paths" => transform_index_paths ( @plugin_paths ) { | p | relativize_path ( p , base ) } ,
192196 "sources" => @sources ,
193197 }
194198
@@ -198,6 +202,40 @@ def save_index
198202 File . open ( index_f , "w" ) { |f | f . puts YAMLSerializer . dump ( index ) }
199203 end
200204 end
205+
206+ def base_for_index ( global )
207+ global ? Plugin . global_root : Plugin . root
208+ end
209+
210+ def transform_index_paths ( paths )
211+ return { } unless paths
212+
213+ paths . transform_values do |value |
214+ if value . is_a? ( Array )
215+ value . map { |path | yield path }
216+ else
217+ yield value
218+ end
219+ end
220+ end
221+
222+ def relativize_path ( path , base )
223+ pathname = Pathname . new ( path )
224+ return path unless pathname . absolute?
225+
226+ base_path = Pathname . new ( base )
227+ if pathname == base_path || pathname . to_s . start_with? ( base_path . to_s + File ::SEPARATOR )
228+ pathname . relative_path_from ( base_path ) . to_s
229+ else
230+ path
231+ end
232+ end
233+
234+ def absolutize_path ( path , base )
235+ pathname = Pathname . new ( path )
236+ pathname = Pathname . new ( base ) . join ( pathname ) unless pathname . absolute?
237+ pathname . to_s
238+ end
201239 end
202240 end
203241end
0 commit comments