Test every environment variable when doing substitution to remove more directories

CHANGED: Use the environment variable that removes the most directories

Fixes https://gitlab.com/kicad/code/kicad/issues/2500
This commit is contained in:
Ian McInerney 2020-02-14 22:58:24 +00:00
parent 36de20a0a7
commit 43514a18bb
1 changed files with 19 additions and 6 deletions

View File

@ -64,11 +64,15 @@ static bool normalizeAbsolutePaths( const wxFileName& aPathA,
return true; return true;
} }
wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars, wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars,
const wxString& aProjectPath ) const wxString& aProjectPath )
{ {
wxFileName envPath; wxFileName envPath;
wxString tmp, varName, normalizedFullPath; wxString varName;
wxString remainingPath;
wxString normalizedFullPath;
int pathDepth = 0;
if( aEnvVars ) if( aEnvVars )
{ {
@ -81,10 +85,18 @@ wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars
envPath.SetPath( entry.second.GetValue() ); envPath.SetPath( entry.second.GetValue() );
wxString tmp;
if( normalizeAbsolutePaths( envPath, aFilePath, &tmp ) ) if( normalizeAbsolutePaths( envPath, aFilePath, &tmp ) )
{ {
varName = entry.first; int newDepth = envPath.GetDirs().GetCount();
break;
// Only use the variable if it removes more directories than the previous ones
if( newDepth > pathDepth )
{
pathDepth = newDepth;
varName = entry.first;
remainingPath = tmp;
}
} }
} }
} }
@ -94,7 +106,7 @@ wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars
{ {
envPath.SetPath( aProjectPath ); envPath.SetPath( aProjectPath );
if( normalizeAbsolutePaths( envPath, aFilePath, &tmp ) ) if( normalizeAbsolutePaths( envPath, aFilePath, &remainingPath ) )
varName = PROJECT_VAR_NAME; varName = PROJECT_VAR_NAME;
} }
@ -102,8 +114,8 @@ wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars
{ {
normalizedFullPath = wxString::Format( "${%s}/", varName ); normalizedFullPath = wxString::Format( "${%s}/", varName );
if( !tmp.IsEmpty() ) if( !remainingPath.IsEmpty() )
normalizedFullPath += tmp; normalizedFullPath += remainingPath;
normalizedFullPath += aFilePath.GetFullName(); normalizedFullPath += aFilePath.GetFullName();
} }
@ -111,6 +123,7 @@ wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars
return normalizedFullPath; return normalizedFullPath;
} }
wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars, wxString NormalizePath( const wxFileName& aFilePath, const ENV_VAR_MAP* aEnvVars,
const PROJECT* aProject ) const PROJECT* aProject )
{ {