private bool LoadEntities([NotNull][ItemNotNull] IEnumerable <IGrouping <string, ProjectFile> > fileNamesByDirectory) { var hasChanged = false; var unmatchedEntities = ResourceEntities.ToList(); foreach (var directory in fileNamesByDirectory) { var directoryName = directory.Key; var filesByBaseName = directory.GroupBy(file => file.GetBaseName(), StringComparer.OrdinalIgnoreCase); foreach (var files in filesByBaseName) { if (!files.Any()) { continue; } var baseName = files.Key; var filesByProject = files.GroupBy(file => file.ProjectName); foreach (var item in filesByProject) { var projectName = item.Key; var projectFiles = item.ToArray(); if (string.IsNullOrEmpty(projectName) || !projectFiles.Any()) { continue; } var existingEntity = ResourceEntities.FirstOrDefault(entity => entity.EqualsAll(projectName, baseName, directoryName)); if (existingEntity != null) { if (existingEntity.Update(projectFiles)) { hasChanged = true; } unmatchedEntities.Remove(existingEntity); } else { ResourceEntities.Add(new ResourceEntity(this, projectName, baseName, directoryName, projectFiles)); hasChanged = true; } } } } ResourceEntities.RemoveRange(unmatchedEntities); hasChanged |= unmatchedEntities.Any(); return(hasChanged); }
private bool LoadEntities([NotNull] IEnumerable<IGrouping<string, ProjectFile>> fileNamesByDirectory) { Contract.Requires(fileNamesByDirectory != null); var hasChanged = false; var unmatchedEntities = ResourceEntities.ToList(); foreach (var directory in fileNamesByDirectory) { Contract.Assume(directory != null); var directoryName = directory.Key; Contract.Assume(!string.IsNullOrEmpty(directoryName)); var filesByBaseName = directory.GroupBy(file => file.GetBaseName()); foreach (var files in filesByBaseName) { if ((files == null) || !files.Any()) continue; var baseName = files.Key; Contract.Assume(!string.IsNullOrEmpty(baseName)); var filesByProject = files.GroupBy(file => file.ProjectName); foreach (var item in filesByProject) { Contract.Assume(item != null); var projectName = item.Key; var projectFiles = item.ToArray(); if (string.IsNullOrEmpty(projectName) || !projectFiles.Any()) continue; var existingEntity = ResourceEntities.FirstOrDefault(entity => entity.EqualsAll(projectName, baseName, directoryName)); if (existingEntity != null) { if (existingEntity.Update(projectFiles)) hasChanged = true; unmatchedEntities.Remove(existingEntity); } else { ResourceEntities.Add(new ResourceEntity(this, projectName, baseName, directoryName, projectFiles)); hasChanged = true; } } } } ResourceEntities.RemoveRange(unmatchedEntities); hasChanged |= unmatchedEntities.Any(); return hasChanged; }