Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to format generated code files. #673

Merged
merged 2 commits into from May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions perf/FormattedFiles.cs
Expand Up @@ -37,7 +37,8 @@ public void FilesFormattedFolder()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand All @@ -52,7 +53,8 @@ public void FilesFormattedProject()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand All @@ -67,7 +69,8 @@ public void FilesFormattedSolution()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand Down
9 changes: 6 additions & 3 deletions perf/NoFilesFormatted.cs
Expand Up @@ -37,7 +37,8 @@ public void NoFilesFormattedFolder()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand All @@ -52,7 +53,8 @@ public void NoFilesFormattedProject()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand All @@ -67,7 +69,8 @@ public void NoFilesFormattedSolution()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand Down
6 changes: 4 additions & 2 deletions perf/RealWorldSolution.cs
Expand Up @@ -39,7 +39,8 @@ public void FilesFormattedSolution()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand All @@ -54,7 +55,8 @@ public void FilesFormattedFolder()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand Down
13 changes: 8 additions & 5 deletions src/CodeFormatter.cs
Expand Up @@ -37,7 +37,7 @@ internal static class CodeFormatter
CancellationToken cancellationToken,
bool createBinaryLog = false)
{
var (workspaceFilePath, workspaceType, logLevel, saveFormattedFiles, _, fileMatcher, reportPath) = options;
var (workspaceFilePath, workspaceType, logLevel, saveFormattedFiles, _, fileMatcher, reportPath, includeGeneratedFiles) = options;
var logWorkspaceWarnings = logLevel == LogLevel.Trace;

logger.LogInformation(string.Format(Resources.Formatting_code_files_in_workspace_0, workspaceFilePath));
Expand All @@ -62,7 +62,7 @@ internal static class CodeFormatter
logger.LogTrace(Resources.Determining_formattable_files);

var (fileCount, formatableFiles) = await DetermineFormattableFiles(
solution, projectPath, fileMatcher, logger, cancellationToken).ConfigureAwait(false);
solution, projectPath, fileMatcher, includeGeneratedFiles, logger, cancellationToken).ConfigureAwait(false);

var determineFilesMS = workspaceStopwatch.ElapsedMilliseconds - loadWorkspaceMS;
logger.LogTrace(Resources.Complete_in_0_ms, determineFilesMS);
Expand Down Expand Up @@ -264,6 +264,7 @@ private static void LogWorkspaceDiagnostics(ILogger logger, bool logWorkspaceWar
Solution solution,
string projectPath,
Matcher fileMatcher,
bool includeGeneratedFiles,
ILogger logger,
CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -296,7 +297,7 @@ private static void LogWorkspaceDiagnostics(ILogger logger, bool logWorkspaceWar

// Get project documents and options with .editorconfig settings applied.
var getProjectDocuments = project.DocumentIds.Select(documentId => GetDocumentAndOptions(
project, documentId, fileMatcher, codingConventionsManager, optionsApplier, cancellationToken));
project, documentId, fileMatcher, includeGeneratedFiles, codingConventionsManager, optionsApplier, cancellationToken));
getDocumentsAndOptions.AddRange(getProjectDocuments);
}

Expand Down Expand Up @@ -335,13 +336,14 @@ private static void LogWorkspaceDiagnostics(ILogger logger, bool logWorkspaceWar
Project project,
DocumentId documentId,
Matcher fileMatcher,
bool includeGeneratedFiles,
ICodingConventionsManager codingConventionsManager,
EditorConfigOptionsApplier optionsApplier,
CancellationToken cancellationToken)
{
var document = project.Solution.GetDocument(documentId);

if (document is null || await ShouldIgnoreDocument(document, fileMatcher, cancellationToken))
if (document is null || await ShouldIgnoreDocument(document, fileMatcher, includeGeneratedFiles, cancellationToken))
{
return (null, null, null, false);
}
Expand All @@ -364,6 +366,7 @@ private static void LogWorkspaceDiagnostics(ILogger logger, bool logWorkspaceWar
private static async Task<bool> ShouldIgnoreDocument(
Document document,
Matcher fileMatcher,
bool includeGeneratedFiles,
CancellationToken cancellationToken)
{
if (!fileMatcher.Match(document.FilePath).HasMatches)
Expand All @@ -375,7 +378,7 @@ private static void LogWorkspaceDiagnostics(ILogger logger, bool logWorkspaceWar
{
return true;
}
else if (await GeneratedCodeUtilities.IsGeneratedCodeAsync(document, cancellationToken).ConfigureAwait(false))
else if (!includeGeneratedFiles && await GeneratedCodeUtilities.IsGeneratedCodeAsync(document, cancellationToken).ConfigureAwait(false))
{
// Ignore generated code files.
return true;
Expand Down
9 changes: 7 additions & 2 deletions src/FormatOptions.cs
Expand Up @@ -14,6 +14,7 @@ internal class FormatOptions
public bool ChangesAreErrors { get; }
public Matcher FileMatcher { get; }
public string? ReportPath { get; }
public bool IncludeGeneratedFiles { get; }

public FormatOptions(
string workspaceFilePath,
Expand All @@ -22,7 +23,8 @@ internal class FormatOptions
bool saveFormattedFiles,
bool changesAreErrors,
Matcher fileMatcher,
string? reportPath)
string? reportPath,
bool includeGeneratedFiles)
{
WorkspaceFilePath = workspaceFilePath;
WorkspaceType = workspaceType;
Expand All @@ -31,6 +33,7 @@ internal class FormatOptions
ChangesAreErrors = changesAreErrors;
FileMatcher = fileMatcher;
ReportPath = reportPath;
IncludeGeneratedFiles = includeGeneratedFiles;
}

public void Deconstruct(
Expand All @@ -40,7 +43,8 @@ internal class FormatOptions
out bool saveFormattedFiles,
out bool changesAreErrors,
out Matcher fileMatcher,
out string? reportPath)
out string? reportPath,
out bool includeGeneratedFiles)
{
workspaceFilePath = WorkspaceFilePath;
workspaceType = WorkspaceType;
Expand All @@ -49,6 +53,7 @@ internal class FormatOptions
changesAreErrors = ChangesAreErrors;
fileMatcher = FileMatcher;
reportPath = ReportPath;
includeGeneratedFiles = IncludeGeneratedFiles;
}
}
}
10 changes: 8 additions & 2 deletions src/Program.cs
Expand Up @@ -55,6 +55,11 @@ private static async Task<int> Main(string[] args)
{
Argument = new Argument<string?>() { Arity = ArgumentArity.ExactlyOne }
},
new Option(new[] { "--include-generated" }, Resources.Include_generated_code_files_in_formatting_operations)
{
Argument = new Argument<bool>(),
IsHidden = true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I marked it as hidden because I don't expect this to be super common and I didn't want to add more to the help screen. I can likely be talked down from this stance.

},
};

rootCommand.Description = "dotnet-format";
Expand All @@ -64,7 +69,7 @@ private static async Task<int> Main(string[] args)
return await rootCommand.InvokeAsync(args);
}

public static async Task<int> Run(string? folder, string? workspace, string? verbosity, bool check, string[] include, string[] exclude, string? report, IConsole console = null!)
public static async Task<int> Run(string? folder, string? workspace, string? verbosity, bool check, string[] include, string[] exclude, string? report, bool includeGenerated, IConsole console = null!)
{
// Setup logging.
var serviceCollection = new ServiceCollection();
Expand Down Expand Up @@ -159,7 +164,8 @@ public static async Task<int> Run(string? folder, string? workspace, string? ver
saveFormattedFiles: !check,
changesAreErrors: check,
fileMatcher,
reportPath: report);
reportPath: report,
includeGenerated);

var formatResult = await CodeFormatter.FormatWorkspaceAsync(
formatOptions,
Expand Down
3 changes: 3 additions & 0 deletions src/Resources.resx
Expand Up @@ -225,4 +225,7 @@
<data name="Unable_to_locate_MSBuild_Ensure_the_NET_SDK_was_installed_with_the_official_installer" xml:space="preserve">
<value>Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer.</value>
</data>
<data name="Include_generated_code_files_in_formatting_operations" xml:space="preserve">
<value>Include generated code files in formatting operations.</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/xlf/Resources.cs.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Formátují se soubory kódu v pracovním prostoru {0}.</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Načítá se pracovní prostor.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.de.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Codedateien im Arbeitsbereich "{0}" werden formatiert.</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Arbeitsbereich wird geladen.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.es.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Aplicar formato a archivos de código en espacio de trabajo "{0}".</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Cargando área de trabajo.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.fr.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Mise en forme des fichiers de code dans l'espace de travail '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Chargement de l'espace de travail.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.it.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Formattazione del file di codice nell'area di lavoro '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Caricamento dell'area di lavoro.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.ja.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">ワークスペース '{0}' でコード ファイルを書式設定します。</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">ワークスペースを読み込んでいます。</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.ko.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">'{0}' 작업 영역에서 코드 파일의 서식을 지정합니다.</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">작업 영역을 로드하는 중입니다.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.pl.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Formatowanie plików kodu w obszarze roboczym „{0}”.</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Ładowanie obszaru roboczego.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.pt-BR.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Formatação de arquivos de código no espaço de trabalho '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Carregando espaço de trabalho.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.ru.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Форматирование кода файлов в рабочей области "{0}".</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Загрузка рабочей области.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.tr.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Çalışma alanı '{0}' kod dosyalarında biçimlendirme.</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">Çalışma alanı yükleniyor.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.zh-Hans.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">正在设置工作区“{0}”中代码文件的格式。</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">正在加载工作区。</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.zh-Hant.xlf
Expand Up @@ -102,6 +102,11 @@
<target state="translated">正在將工作區 '{0}' 中的程式碼檔案格式化。</target>
<note />
</trans-unit>
<trans-unit id="Include_generated_code_files_in_formatting_operations">
<source>Include generated code files in formatting operations.</source>
<target state="new">Include generated code files in formatting operations.</target>
<note />
</trans-unit>
<trans-unit id="Loading_workspace">
<source>Loading workspace.</source>
<target state="translated">正在載入工作區。</target>
Expand Down