From 444ea54d7df71a99a42782a0a3b489ebc89eab93 Mon Sep 17 00:00:00 2001 From: realmarv Date: Wed, 2 Aug 2023 13:26:37 +0330 Subject: [PATCH] scripts: split styleChecker script Split styleChecker into styleCheck and styleApply. --- .github/workflows/CI.yml | 2 +- scripts/eofConvention.fsx | 3 +- scripts/executableConvention.fsx | 1 + .../inconsistentVersionsInFSharpScripts.fsx | 5 +- scripts/inconsistentVersionsInGitHubCI.fsx | 5 +- scripts/mixedLineEndings.fsx | 5 +- .../nonVerboseFlagsInGitHubCIAndScripts.fsx | 5 +- scripts/shebangConvention.fsx | 5 +- scripts/styleApply.fsx | 19 + scripts/styleCheck.fsx | 187 ++++++++ scripts/styleChecker.fsx | 439 ------------------ scripts/unpinnedDotnetPackageVersions.fsx | 5 +- scripts/unpinnedDotnetToolInstallVersions.fsx | 5 +- .../unpinnedGitHubActionsImageVersions.fsx | 5 +- .../unpinnedNugetPackageReferenceVersions.fsx | 5 +- scripts/wrapLatestCommitMsg.fsx | 3 +- src/FileConventions/Config.fs | 5 + src/FileConventions/FileConventions.fsproj | 7 +- src/FileConventions/Helpers.fs | 176 +++++++ src/FileConventions/Library.fs | 84 ++++ 20 files changed, 510 insertions(+), 461 deletions(-) create mode 100755 scripts/styleApply.fsx create mode 100755 scripts/styleCheck.fsx delete mode 100755 scripts/styleChecker.fsx create mode 100644 src/FileConventions/Config.fs diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 3cc4b3cab..df56397a0 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -214,4 +214,4 @@ jobs: dotnet fantomless --recurse . git diff --exit-code - name: Check style of our F#, C#, TypeScript, YML and XAML code - run: sudo dotnet fsi scripts/styleChecker.fsx + run: sudo dotnet fsi scripts/styleCheck.fsx diff --git a/scripts/eofConvention.fsx b/scripts/eofConvention.fsx index 3f2b712b9..951b4d623 100755 --- a/scripts/eofConvention.fsx +++ b/scripts/eofConvention.fsx @@ -4,7 +4,8 @@ open System.IO open System #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" #load "../src/FileConventions/Helpers.fs" #load "../src/FileConventions/Library.fs" diff --git a/scripts/executableConvention.fsx b/scripts/executableConvention.fsx index cecaf6b5b..712d5651e 100755 --- a/scripts/executableConvention.fsx +++ b/scripts/executableConvention.fsx @@ -4,6 +4,7 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" #load "../src/FileConventions/Library.fs" #load "../src/FileConventions/Helpers.fs" diff --git a/scripts/inconsistentVersionsInFSharpScripts.fsx b/scripts/inconsistentVersionsInFSharpScripts.fsx index 93922496e..75486b972 100755 --- a/scripts/inconsistentVersionsInFSharpScripts.fsx +++ b/scripts/inconsistentVersionsInFSharpScripts.fsx @@ -4,9 +4,10 @@ open System.IO open System.Linq #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - -#load "../src/FileConventions/Library.fs" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" #load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo let currentDir = Directory.GetCurrentDirectory() |> DirectoryInfo diff --git a/scripts/inconsistentVersionsInGitHubCI.fsx b/scripts/inconsistentVersionsInGitHubCI.fsx index f8041a45d..a845b1fee 100755 --- a/scripts/inconsistentVersionsInGitHubCI.fsx +++ b/scripts/inconsistentVersionsInGitHubCI.fsx @@ -3,9 +3,10 @@ open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - -#load "../src/FileConventions/Library.fs" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" #load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo diff --git a/scripts/mixedLineEndings.fsx b/scripts/mixedLineEndings.fsx index 427826a61..2d2c6e7a3 100755 --- a/scripts/mixedLineEndings.fsx +++ b/scripts/mixedLineEndings.fsx @@ -4,9 +4,10 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - -#load "../src/FileConventions/Library.fs" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" #load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo diff --git a/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx b/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx index 54bd123d5..24c59b4bc 100755 --- a/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx +++ b/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx @@ -4,9 +4,10 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - -#load "../src/FileConventions/Library.fs" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" #load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo diff --git a/scripts/shebangConvention.fsx b/scripts/shebangConvention.fsx index 965a9aad3..5b7f61810 100755 --- a/scripts/shebangConvention.fsx +++ b/scripts/shebangConvention.fsx @@ -4,9 +4,10 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - -#load "../src/FileConventions/Library.fs" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" #load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo diff --git a/scripts/styleApply.fsx b/scripts/styleApply.fsx new file mode 100755 index 000000000..0ef9a3cc9 --- /dev/null +++ b/scripts/styleApply.fsx @@ -0,0 +1,19 @@ +#!/usr/bin/env -S dotnet fsi + +#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" +#load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" + +open System.IO + +open FileConventions + +let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo + +StyleFSharpFiles rootDir +StyleTypeScriptFiles() +StyleYmlFiles() +StyleCSharpFiles rootDir +StyleXamlFiles() diff --git a/scripts/styleCheck.fsx b/scripts/styleCheck.fsx new file mode 100755 index 000000000..f84933deb --- /dev/null +++ b/scripts/styleCheck.fsx @@ -0,0 +1,187 @@ +#!/usr/bin/env -S dotnet fsi + +#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" +#load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" + +open System +open System.IO + +open Fsdk +open Fsdk.Process + +open Helpers +open FileConventions + +let ContainsFiles (rootDir: DirectoryInfo) (searchPattern: string) = + Helpers.GetFiles rootDir searchPattern |> Seq.length > 0 + +let GitDiff() : ProcessResult = + + // Since we changed file modes in the prettier step we need the following command to + // make git ignore mode changes in files and doesn't include them in the git diff command. + Process + .Execute( + { + Command = "git" + Arguments = "config core.fileMode false" + }, + Echo.Off + ) + .UnwrapDefault() + |> ignore + + let processResult = + Process.Execute( + { + Command = "git" + Arguments = "diff --exit-code" + }, + Echo.Off + ) + + processResult + +let GitRestore() = + Process + .Execute( + { + Command = "git" + Arguments = "restore ." + }, + Echo.Off + ) + .UnwrapDefault() + |> ignore + +let CheckStyleOfFSharpFiles(rootDir: DirectoryInfo) : bool = + let suggestion = + Some "Please style your F# code using: `dotnet fantomless --recurse .`" + + GitRestore() + + let success = + if ContainsFiles rootDir "*.fs" || ContainsFiles rootDir ".fsx" then + StyleFSharpFiles rootDir + let processResult = GitDiff() + UnwrapProcessResult suggestion true processResult |> ignore + IsProcessSuccessful processResult + + else + true + + success + +let CheckStyleOfTypeScriptFiles(rootDir: DirectoryInfo) : bool = + let pattern = + $".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.ts" + + let suggestion = + Some + $"Please style your TypeScript code using: `npx prettier --quote-props=consistent --write {pattern}`" + + GitRestore() + + let success = + if ContainsFiles rootDir "*.ts" then + StyleTypeScriptFiles() + let processResult = GitDiff() + UnwrapProcessResult suggestion true processResult |> ignore + IsProcessSuccessful processResult + + else + true + + success + +let CheckStyleOfYmlFiles(rootDir: DirectoryInfo) : bool = + let pattern = + $".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.yml" + + let suggestion = + Some + $"Please style your YML code using: `npx prettier --quote-props=consistent --write {pattern}`" + + GitRestore() + + let success = + if ContainsFiles rootDir "*.yml" then + StyleYmlFiles() + let processResult = GitDiff() + UnwrapProcessResult suggestion true processResult |> ignore + IsProcessSuccessful processResult + else + true + + success + +let CheckStyleOfCSharpFiles(rootDir: DirectoryInfo) : bool = + let suggestion = + Some + "Please style your C# code using: `dotnet format whitespace . --folder" + + GitRestore() + + let success = + if ContainsFiles rootDir "*.cs" then + StyleCSharpFiles rootDir + let processResult = GitDiff() + UnwrapProcessResult suggestion true processResult |> ignore + IsProcessSuccessful processResult + else + true + + success + +let CheckStyleOfXamlFiles(rootDir: DirectoryInfo) : bool = + let prettierPath = Path.Combine(".", "node_modules", ".bin", "prettier") + + let pattern = $"**{Path.DirectorySeparatorChar}*.xaml" + + let suggestion = + "Please style your XAML code using:" + + Environment.NewLine + + $"`{prettierPath} --xml-whitespace-sensitivity ignore --tab-width 4 --prose-wrap preserve --write {pattern}`" + |> Some + + GitRestore() + + let success = + if ContainsFiles rootDir "*.xaml" then + StyleXamlFiles() + let processResult = GitDiff() + UnwrapProcessResult suggestion true processResult |> ignore + IsProcessSuccessful processResult + else + true + + success + +let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo + +// We need this step so we can change the files using `npx prettier --write` in the prettier calls. +// Otherwise we get permission denied error in the CI. +Process + .Execute( + { + Command = "chmod" + Arguments = "777 --recursive ." + }, + Echo.Off + ) + .UnwrapDefault() +|> ignore + +let processSuccessStates = + [| + CheckStyleOfFSharpFiles rootDir + CheckStyleOfCSharpFiles rootDir + CheckStyleOfTypeScriptFiles rootDir + CheckStyleOfYmlFiles rootDir + CheckStyleOfXamlFiles rootDir + |] + +if processSuccessStates |> Seq.contains false then + Environment.Exit 1 diff --git a/scripts/styleChecker.fsx b/scripts/styleChecker.fsx deleted file mode 100755 index f36bf04fa..000000000 --- a/scripts/styleChecker.fsx +++ /dev/null @@ -1,439 +0,0 @@ -#!/usr/bin/env -S dotnet fsi - -#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" -#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" -#load "../src/FileConventions/Helpers.fs" - -open System -open System.IO - -open Fsdk -open Fsdk.Process - -open Helpers - -let fantomlessToolVersion = "4.7.997-prerelease" -let prettierVersion = "2.8.3" -let pluginXmlVersion = "v2.2.0" - -let InstallFantomlessTool(version: string) = - let isFantomlessInstalled = - let installedPackages: string = - Process - .Execute( - { - Command = "dotnet" - Arguments = "tool list" - }, - Echo.Off - ) - .UnwrapDefault() - - installedPackages.Split Environment.NewLine - |> Seq.map(fun line -> - line.Contains "fantomless-tool" - && line.Contains fantomlessToolVersion - ) - |> Seq.contains true - - if not(isFantomlessInstalled) then - Process - .Execute( - { - Command = "dotnet" - Arguments = "new tool-manifest --force" - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - - Process - .Execute( - { - Command = "dotnet" - Arguments = - $"tool install fantomless-tool --version {version}" - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - - Process - .Execute( - { - Command = "dotnet" - Arguments = "tool restore" - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - -let UnwrapProcessResult - (maybeSuggestion: Option) - (ignoreErrorExitCode: bool) - (processResult: ProcessResult) - : string = - let errMsg = - sprintf - "Error when running '%s %s'" - processResult.Details.Command - processResult.Details.Args - - match processResult.Result with - | Success output -> - Console.WriteLine output - output - | Error(_, output) -> - if processResult.Details.Echo = Echo.Off then - output.PrintToConsole() - Console.WriteLine() - Console.Out.Flush() - - let fullErrMsg = - match maybeSuggestion with - | Some suggestion -> errMsg + Environment.NewLine + suggestion - | None -> errMsg - - Console.Error.WriteLine fullErrMsg - - if ignoreErrorExitCode then - fullErrMsg - else - raise <| ProcessFailed errMsg - - | WarningsOrAmbiguous output -> - if processResult.Details.Echo = Echo.Off then - output.PrintToConsole() - Console.WriteLine() - Console.Out.Flush() - - let fullErrMsg = sprintf "%s (with warnings?)" errMsg - Console.Error.WriteLine fullErrMsg - fullErrMsg - -let IsProcessSuccessful(processResult: ProcessResult) : bool = - match processResult.Result with - | Success output -> true - | _ -> false - -let InstallPrettier(version: string) = - let isPrettierInstalled = - Process.Execute( - { - Command = "npm" - Arguments = $"list prettier@{version}" - }, - Echo.All - ) - |> IsProcessSuccessful - - if not(isPrettierInstalled) then - Process.Execute( - { - Command = "npm" - Arguments = $"install prettier@{version}" - }, - Echo.Off - ) - |> UnwrapProcessResult None false - |> ignore - -let StyleFSharpFiles(rootDir: DirectoryInfo) = - InstallFantomlessTool fantomlessToolVersion - - Process - .Execute( - { - Command = "dotnet" - Arguments = $"fantomless --recurse {rootDir.FullName}" - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - -let StyleCSharpFiles(rootDir: DirectoryInfo) = - Process - .Execute( - { - Command = "dotnet" - Arguments = $"format whitespace {rootDir.FullName} --folder" - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - -let InstallPrettierPluginXml(version: string) = - let isPrettierPluginXmlInstalled = - Process.Execute( - { - Command = "npm" - Arguments = $"list @prettier/plugin-xml@{version}" - }, - Echo.Off - ) - |> IsProcessSuccessful - - if not(isPrettierPluginXmlInstalled) then - Process - .Execute( - { - Command = "npm" - Arguments = $"install @prettier/plugin-xml@{version}" - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - -let StyleXamlFiles() = - InstallPrettier prettierVersion - InstallPrettierPluginXml pluginXmlVersion - - Process - .Execute( - { - Command = "npm" - Arguments = - $"install --save-dev prettier@{prettierVersion} @prettier/plugin-xml@{pluginXmlVersion}" - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - - let pattern = $"**{Path.DirectorySeparatorChar}*.xaml" - - Process - .Execute( - { - Command = - Path.Combine( - Directory.GetCurrentDirectory(), - "node_modules", - ".bin", - "prettier" - ) - - Arguments = - $"--xml-whitespace-sensitivity ignore --tab-width 4 --prose-wrap preserve --write {pattern}" - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - -let RunPrettier(arguments: string) = - - // We need this step so we can change the files using `npx prettier --write` in the next step. - // Otherwise we get permission denied error in the CI. - Process - .Execute( - { - Command = "chmod" - Arguments = "777 --recursive ." - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - - Process.Execute( - { - Command = "npx" - Arguments = $"prettier {arguments}" - }, - Echo.Off - ) - |> UnwrapProcessResult None false - |> ignore - - - // Since after installing commitlint dependencies package.json file changes, we need to - // run the following command to ignore package.json file - Process - .Execute( - { - Command = "git" - Arguments = "restore package.json" - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - -let StyleTypeScriptFiles() = - let pattern = - $".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.ts" - - RunPrettier $"--quote-props=consistent --write {pattern}" - -let StyleYmlFiles() = - let pattern = - $".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.yml" - - RunPrettier $"--quote-props=consistent --write {pattern}" - -let ContainsFiles (rootDir: DirectoryInfo) (searchPattern: string) = - Helpers.GetFiles rootDir searchPattern |> Seq.length > 0 - -let GitDiff() : ProcessResult = - - // Since we changed file modes in the prettier step we need the following command to - // make git ignore mode changes in files and doesn't include them in the git diff command. - Process - .Execute( - { - Command = "git" - Arguments = "config core.fileMode false" - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - - let processResult = - Process.Execute( - { - Command = "git" - Arguments = "diff --exit-code" - }, - Echo.Off - ) - - processResult - -let GitRestore() = - Process - .Execute( - { - Command = "git" - Arguments = "restore ." - }, - Echo.Off - ) - .UnwrapDefault() - |> ignore - -let CheckStyleOfFSharpFiles(rootDir: DirectoryInfo) : bool = - let suggestion = - Some "Please style your F# code using: `dotnet fantomless --recurse .`" - - GitRestore() - - let success = - if ContainsFiles rootDir "*.fs" || ContainsFiles rootDir ".fsx" then - StyleFSharpFiles rootDir - let processResult = GitDiff() - UnwrapProcessResult suggestion true processResult |> ignore - IsProcessSuccessful processResult - - else - true - - success - -let CheckStyleOfTypeScriptFiles(rootDir: DirectoryInfo) : bool = - let pattern = - $".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.ts" - - let suggestion = - Some - $"Please style your TypeScript code using: `npx prettier --quote-props=consistent --write {pattern}`" - - GitRestore() - - let success = - if ContainsFiles rootDir "*.ts" then - InstallPrettier prettierVersion - StyleTypeScriptFiles() - let processResult = GitDiff() - UnwrapProcessResult suggestion true processResult |> ignore - IsProcessSuccessful processResult - - else - true - - success - -let CheckStyleOfYmlFiles(rootDir: DirectoryInfo) : bool = - let pattern = - $".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.yml" - - let suggestion = - Some - $"Please style your YML code using: `npx prettier --quote-props=consistent --write {pattern}`" - - GitRestore() - - let success = - if ContainsFiles rootDir "*.yml" then - InstallPrettier prettierVersion - StyleYmlFiles() - let processResult = GitDiff() - UnwrapProcessResult suggestion true processResult |> ignore - IsProcessSuccessful processResult - else - true - - success - -let CheckStyleOfCSharpFiles(rootDir: DirectoryInfo) : bool = - let suggestion = - Some - "Please style your C# code using: `dotnet format whitespace . --folder" - - GitRestore() - - let success = - if ContainsFiles rootDir "*.cs" then - StyleCSharpFiles rootDir - let processResult = GitDiff() - UnwrapProcessResult suggestion true processResult |> ignore - IsProcessSuccessful processResult - else - true - - success - -let CheckStyleOfXamlFiles(rootDir: DirectoryInfo) : bool = - let prettierPath = Path.Combine(".", "node_modules", ".bin", "prettier") - - let pattern = $"**{Path.DirectorySeparatorChar}*.xaml" - - let suggestion = - "Please style your XAML code using:" - + Environment.NewLine - + $"`{prettierPath} --xml-whitespace-sensitivity ignore --tab-width 4 --prose-wrap preserve --write {pattern}`" - |> Some - - GitRestore() - - let success = - if ContainsFiles rootDir "*.xaml" then - StyleXamlFiles() - let processResult = GitDiff() - UnwrapProcessResult suggestion true processResult |> ignore - IsProcessSuccessful processResult - else - true - - success - -let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo - -let processSuccessStates = - [| - CheckStyleOfFSharpFiles rootDir - CheckStyleOfCSharpFiles rootDir - CheckStyleOfTypeScriptFiles rootDir - CheckStyleOfYmlFiles rootDir - CheckStyleOfXamlFiles rootDir - |] - -if processSuccessStates |> Seq.contains false then - Environment.Exit 1 diff --git a/scripts/unpinnedDotnetPackageVersions.fsx b/scripts/unpinnedDotnetPackageVersions.fsx index 56da33c01..e66de33aa 100755 --- a/scripts/unpinnedDotnetPackageVersions.fsx +++ b/scripts/unpinnedDotnetPackageVersions.fsx @@ -4,9 +4,10 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - -#load "../src/FileConventions/Library.fs" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" #load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo diff --git a/scripts/unpinnedDotnetToolInstallVersions.fsx b/scripts/unpinnedDotnetToolInstallVersions.fsx index 92f35dcc6..c3b3dfe5d 100755 --- a/scripts/unpinnedDotnetToolInstallVersions.fsx +++ b/scripts/unpinnedDotnetToolInstallVersions.fsx @@ -4,9 +4,10 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - -#load "../src/FileConventions/Library.fs" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" #load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo diff --git a/scripts/unpinnedGitHubActionsImageVersions.fsx b/scripts/unpinnedGitHubActionsImageVersions.fsx index 07d31cc97..7a4418068 100755 --- a/scripts/unpinnedGitHubActionsImageVersions.fsx +++ b/scripts/unpinnedGitHubActionsImageVersions.fsx @@ -4,9 +4,10 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - -#load "../src/FileConventions/Library.fs" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" #load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo diff --git a/scripts/unpinnedNugetPackageReferenceVersions.fsx b/scripts/unpinnedNugetPackageReferenceVersions.fsx index 72e0e74b5..5808451f7 100755 --- a/scripts/unpinnedNugetPackageReferenceVersions.fsx +++ b/scripts/unpinnedNugetPackageReferenceVersions.fsx @@ -4,9 +4,10 @@ open System open System.IO #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - -#load "../src/FileConventions/Library.fs" +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" +#load "../src/FileConventions/Config.fs" #load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo diff --git a/scripts/wrapLatestCommitMsg.fsx b/scripts/wrapLatestCommitMsg.fsx index 5b6592b0e..da000ba13 100755 --- a/scripts/wrapLatestCommitMsg.fsx +++ b/scripts/wrapLatestCommitMsg.fsx @@ -5,7 +5,8 @@ open System open System.Text.RegularExpressions #r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" - +#load "../src/FileConventions/Config.fs" +#load "../src/FileConventions/Helpers.fs" #load "../src/FileConventions/Library.fs" #r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" diff --git a/src/FileConventions/Config.fs b/src/FileConventions/Config.fs new file mode 100644 index 000000000..58f739e52 --- /dev/null +++ b/src/FileConventions/Config.fs @@ -0,0 +1,5 @@ +module Config + +let FantomlessToolVersion = "4.7.997-prerelease" +let PrettierVersion = "2.8.3" +let PluginXmlVersion = "v2.2.0" diff --git a/src/FileConventions/FileConventions.fsproj b/src/FileConventions/FileConventions.fsproj index 9c8c6d177..59f98657e 100644 --- a/src/FileConventions/FileConventions.fsproj +++ b/src/FileConventions/FileConventions.fsproj @@ -6,8 +6,13 @@ - + + + + + + diff --git a/src/FileConventions/Helpers.fs b/src/FileConventions/Helpers.fs index b46cbdddf..63b7a5f7a 100644 --- a/src/FileConventions/Helpers.fs +++ b/src/FileConventions/Helpers.fs @@ -4,6 +4,9 @@ open System open System.IO open System.Linq +open Fsdk +open Fsdk.Process + let NotInDir (dirName: string) (fileInfo: FileInfo) = not( fileInfo.FullName.Contains @@ -59,3 +62,176 @@ let AssertNoInvalidFiles (invalidFiles: seq) (message: string) = |> String.concat Environment.NewLine) failwith message + +let InstallFantomlessTool(version: string) = + let isFantomlessInstalled = + let installedPackages: string = + Process + .Execute( + { + Command = "dotnet" + Arguments = "tool list" + }, + Echo.Off + ) + .UnwrapDefault() + + installedPackages.Split Environment.NewLine + |> Seq.map(fun line -> + line.Contains "fantomless-tool" && line.Contains version + ) + |> Seq.contains true + + if not(isFantomlessInstalled) then + Process + .Execute( + { + Command = "dotnet" + Arguments = "new tool-manifest --force" + }, + Echo.Off + ) + .UnwrapDefault() + |> ignore + + Process + .Execute( + { + Command = "dotnet" + Arguments = + $"tool install fantomless-tool --version {version}" + }, + Echo.Off + ) + .UnwrapDefault() + |> ignore + + Process + .Execute( + { + Command = "dotnet" + Arguments = "tool restore" + }, + Echo.Off + ) + .UnwrapDefault() + |> ignore + +let UnwrapProcessResult + (maybeSuggestion: Option) + (ignoreErrorExitCode: bool) + (processResult: ProcessResult) + : string = + let errMsg = + sprintf + "Error when running '%s %s'" + processResult.Details.Command + processResult.Details.Args + + match processResult.Result with + | Success output -> + Console.WriteLine output + output + | Error(_, output) -> + if processResult.Details.Echo = Echo.Off then + output.PrintToConsole() + Console.WriteLine() + Console.Out.Flush() + + let fullErrMsg = + match maybeSuggestion with + | Some suggestion -> errMsg + Environment.NewLine + suggestion + | None -> errMsg + + Console.Error.WriteLine fullErrMsg + + if ignoreErrorExitCode then + fullErrMsg + else + raise <| ProcessFailed errMsg + + | WarningsOrAmbiguous output -> + if processResult.Details.Echo = Echo.Off then + output.PrintToConsole() + Console.WriteLine() + Console.Out.Flush() + + let fullErrMsg = sprintf "%s (with warnings?)" errMsg + Console.Error.WriteLine fullErrMsg + fullErrMsg + +let IsProcessSuccessful(processResult: ProcessResult) : bool = + match processResult.Result with + | Success _ -> true + | _ -> false + +let InstallPrettier(version: string) = + let isPrettierInstalled = + Process.Execute( + { + Command = "npm" + Arguments = $"list prettier@{version}" + }, + Echo.All + ) + |> IsProcessSuccessful + + if not(isPrettierInstalled) then + Process.Execute( + { + Command = "npm" + Arguments = $"install prettier@{version}" + }, + Echo.Off + ) + |> UnwrapProcessResult None false + |> ignore + +let InstallPrettierPluginXml(version: string) = + let isPrettierPluginXmlInstalled = + Process.Execute( + { + Command = "npm" + Arguments = $"list @prettier/plugin-xml@{version}" + }, + Echo.Off + ) + |> IsProcessSuccessful + + if not(isPrettierPluginXmlInstalled) then + Process + .Execute( + { + Command = "npm" + Arguments = $"install @prettier/plugin-xml@{version}" + }, + Echo.Off + ) + .UnwrapDefault() + |> ignore + +let RunPrettier(arguments: string) = + + Process.Execute( + { + Command = "npx" + Arguments = $"prettier {arguments}" + }, + Echo.Off + ) + |> UnwrapProcessResult None false + |> ignore + + + // Since after installing commitlint dependencies package.json file changes, we need to + // run the following command to ignore package.json file + Process + .Execute( + { + Command = "git" + Arguments = "restore package.json" + }, + Echo.Off + ) + .UnwrapDefault() + |> ignore diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index 6a5ea3748..39e8e42fc 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -7,6 +7,11 @@ open System.Text.RegularExpressions open Mono open Mono.Unix.Native +open Fsdk +open Fsdk.Process + +open Helpers +open Config let HasCorrectShebang(fileInfo: FileInfo) = let fileText = File.ReadLines fileInfo.FullName @@ -390,3 +395,82 @@ let NonVerboseFlags(fileInfo: FileInfo) = let IsExecutable(fileInfo: FileInfo) = let hasExecuteAccess = Syscall.access(fileInfo.FullName, AccessModes.X_OK) hasExecuteAccess = 0 + +let StyleFSharpFiles(rootDir: DirectoryInfo) = + InstallFantomlessTool FantomlessToolVersion + + Process + .Execute( + { + Command = "dotnet" + Arguments = $"fantomless --recurse {rootDir.FullName}" + }, + Echo.Off + ) + .UnwrapDefault() + |> ignore + +let StyleCSharpFiles(rootDir: DirectoryInfo) = + Process + .Execute( + { + Command = "dotnet" + Arguments = $"format whitespace {rootDir.FullName} --folder" + }, + Echo.Off + ) + .UnwrapDefault() + |> ignore + +let StyleXamlFiles() = + InstallPrettier PrettierVersion + InstallPrettierPluginXml PluginXmlVersion + + Process + .Execute( + { + Command = "npm" + Arguments = + $"install --save-dev prettier@{PrettierVersion} @prettier/plugin-xml@{PluginXmlVersion}" + }, + Echo.Off + ) + .UnwrapDefault() + |> ignore + + let pattern = $"**{Path.DirectorySeparatorChar}*.xaml" + + Process + .Execute( + { + Command = + Path.Combine( + Directory.GetCurrentDirectory(), + "node_modules", + ".bin", + "prettier" + ) + + Arguments = + $"--xml-whitespace-sensitivity ignore --tab-width 4 --prose-wrap preserve --write {pattern}" + }, + Echo.Off + ) + .UnwrapDefault() + |> ignore + +let StyleTypeScriptFiles() = + InstallPrettier PrettierVersion + + let pattern = + $".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.ts" + + RunPrettier $"--quote-props=consistent --write {pattern}" + +let StyleYmlFiles() = + InstallPrettier PrettierVersion + + let pattern = + $".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.yml" + + RunPrettier $"--quote-props=consistent --write {pattern}"