Skip to content

Commit

Permalink
extract common method
Browse files Browse the repository at this point in the history
  • Loading branch information
superyyrrzz committed Jul 9, 2016
1 parent 78bbd51 commit d9b55d3
Showing 1 changed file with 45 additions and 77 deletions.
122 changes: 45 additions & 77 deletions src/docfx/SubCommands/DocumentBuilderWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ private static IEnumerable<DocumentBuildParameters> ConfigToParameter(BuildJsonC
{
parameters.Files = GetFileCollectionFromFileMapping(
baseDirectory,
GlobUtility.ExpandFileMapping(baseDirectory, pair.Value.Content),
GlobUtility.ExpandFileMapping(baseDirectory, pair.Value.Overwrite),
GlobUtility.ExpandFileMapping(baseDirectory, pair.Value.Resource));
GlobUtility.ExpandFileMapping(baseDirectory, pair.Value.GetFileMapping(FileMappingType.Content)),
GlobUtility.ExpandFileMapping(baseDirectory, pair.Value.GetFileMapping(FileMappingType.Overwrite)),
GlobUtility.ExpandFileMapping(baseDirectory, pair.Value.GetFileMapping(FileMappingType.Resource)));
parameters.VersionName = pair.Key;
yield return parameters;
}
Expand All @@ -250,90 +250,48 @@ private static IEnumerable<DocumentBuildParameters> ConfigToParameter(BuildJsonC
private static Dictionary<string, FileMappingParameters> GroupFileMappings(FileMapping content,
FileMapping overwrite, FileMapping resource)
{
var result = new Dictionary<string, FileMappingParameters> { [string.Empty] = new FileMappingParameters() };

if (content != null)
var result = new Dictionary<string, FileMappingParameters>
{
foreach (var item in content.Items)
{
var version = item.VersionName ?? string.Empty;
FileMappingParameters fileMappingParameters;
if (result.TryGetValue(version, out fileMappingParameters))
{
if (fileMappingParameters.Content != null)
{
fileMappingParameters.Content.Add(item);
}
else
{
fileMappingParameters.Content = new FileMapping(item);
}
}
else
{
result[version] = new FileMappingParameters
{
Content = new FileMapping(item)
};
}
}
}
[string.Empty] = new FileMappingParameters()
};

AddFileMappingTypeGroup(result, content, FileMappingType.Content);
AddFileMappingTypeGroup(result, overwrite, FileMappingType.Overwrite);
AddFileMappingTypeGroup(result, resource, FileMappingType.Resource);

if (overwrite != null)
return result;
}

private static void AddFileMappingTypeGroup(
Dictionary<string, FileMappingParameters> fileMappingsDictionary,
FileMapping fileMapping,
FileMappingType type)
{
if (fileMapping == null) return;
foreach (var item in fileMapping.Items)
{
foreach (var item in overwrite.Items)
var version = item.VersionName ?? string.Empty;
FileMappingParameters parameters;
if (fileMappingsDictionary.TryGetValue(version, out parameters))
{
var version = item.VersionName ?? string.Empty;
FileMappingParameters fileMappingParameters;
if (result.TryGetValue(version, out fileMappingParameters))
FileMapping mapping;
if (parameters.TryGetValue(type, out mapping))
{
if (fileMappingParameters.Overwrite != null)
{
fileMappingParameters.Overwrite.Add(item);
}
else
{
fileMappingParameters.Overwrite = new FileMapping(item);
}
mapping.Add(item);
}
else
{
result[version] = new FileMappingParameters
{
Overwrite = new FileMapping(item)
};
parameters[type] = new FileMapping(item);
}
}
}

if (resource != null)
{
foreach (var item in resource.Items)
else
{
var version = item.VersionName ?? string.Empty;
FileMappingParameters fileMappingParameters;
if (result.TryGetValue(version, out fileMappingParameters))
{
if (fileMappingParameters.Resource != null)
{
fileMappingParameters.Resource.Add(item);
}
else
{
fileMappingParameters.Resource = new FileMapping(item);
}
}
else
fileMappingsDictionary[version] = new FileMappingParameters
{
result[version] = new FileMappingParameters
{
Resource = new FileMapping(item)
};
}
[type] = new FileMapping(item)
};
}
}

return result;
}

private static FileMetadata ConvertToFileMetadataItem(string baseDirectory, Dictionary<string, FileMetadataPairs> fileMetadata)
Expand Down Expand Up @@ -407,11 +365,21 @@ private static string ConvertToDestinationPath(string path, string src, string d
return Path.Combine(dest ?? string.Empty, relativePath);
}

private class FileMappingParameters
private class FileMappingParameters : Dictionary<FileMappingType, FileMapping>
{
public FileMapping GetFileMapping(FileMappingType type)
{
FileMapping result;
this.TryGetValue(type, out result);
return result;
}
}

private enum FileMappingType
{
public FileMapping Content { get; set; }
public FileMapping Overwrite { get; set; }
public FileMapping Resource { get; set; }
Content,
Overwrite,
Resource,
}
}
}

0 comments on commit d9b55d3

Please sign in to comment.