diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 5c23e7db2..bb93e2bc5 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -40,7 +40,7 @@ jobs: - name: Compile the conventions solution run: dotnet build --configuration Release conventions.sln - name: Compile F# scripts - run: dotnet fsi scripts/compileFSharpScripts.fsx + run: dotnet fsi scripts/compile_fsharp_scripts.fsx file-conventions-tests: name: Run FileConventions-lib unit tests @@ -195,36 +195,38 @@ jobs: apt install --yes --no-install-recommends dotnet-sdk-8.0 - name: Check all files end with EOL - run: dotnet fsi scripts/eofConvention.fsx + run: dotnet fsi scripts/eof_convention.fsx - name: Check all .fsx scripts have shebang - run: dotnet fsi scripts/shebangConvention.fsx + run: dotnet fsi scripts/shebang_convention.fsx - name: Check all F# scripts have execute permission - run: dotnet fsi scripts/executableConvention.fsx + run: dotnet fsi scripts/executable_convention.fsx - name: Check there are no mixed line-endings in any files - run: dotnet fsi scripts/mixedLineEndings.fsx + run: dotnet fsi scripts/mixed_line_endings.fsx - name: Check there are no unpinned GitHubActions image versions - run: dotnet fsi scripts/unpinnedGitHubActionsImageVersions.fsx - - name: Check there are no unpinned nuget package reference versions in .NET project files - run: dotnet fsi scripts/unpinnedNugetPackageReferenceVersionsInProjects.fsx + run: dotnet fsi scripts/unpinned_github_actions_image_versions.fsx + - name: Check there are no unpinned dotnet package versions + run: dotnet fsi scripts/unpinned_dotnet_package_versions.fsx - name: Check there are no unpinned nuget package reference versions in F# scripts - run: dotnet fsi scripts/unpinnedNugetPackageReferenceVersionsInFSharpScripts.fsx + run: dotnet fsi scripts/unpinned_nuget_package_reference_versions.fsx - name: Check there are no unpinned versions in `dotnet tool install` commands - run: dotnet fsi scripts/unpinnedDotnetToolInstallVersions.fsx + run: dotnet fsi scripts/unpinned_dotnet_tool_install_versions.fsx + - name: Check if script names (.fsx, .bat, and .sh files) are snake_case and CI job names are kebab-case. + run: dotnet fsi scripts/not_respecting_some_naming_conventions.fsx - name: Check commits 1 by 1 if: github.event_name == 'pull_request' - run: dotnet fsi scripts/checkCommits1by1.fsx + run: dotnet fsi scripts/check_commits_1by1.fsx - name: Check there are no inconsistent versions GitHubCI files - run: dotnet fsi scripts/inconsistentVersionsInGitHubCI.fsx + run: dotnet fsi scripts/inconsistent_versions_in_githubci.fsx - name: Check there are no inconsistent versions in nuget package references of F# scripts - run: dotnet fsi scripts/inconsistentNugetVersionsInFSharpScripts.fsx + run: dotnet fsi scripts/inconsistent_versions_in_fsharp_scripts.fsx - name: Check there are no non-verbose flags in scripts and CI YML files - run: dotnet fsi scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx + run: dotnet fsi scripts/non_verbose_flags_in_githubci_and_scripts.fsx - name: Sanity check nuget packages of .NET projects run: | dotnet restore - dotnet fsi scripts/inconsistentNugetVersionsInDotNetProjects.fsx + dotnet fsi scripts/inconsistent_nuget_versions_in_dotnet_projects.fsx - name: Check there are no inconsistent versions in nuget package references of F# scripts and projects - run: dotnet fsi scripts/inconsistentNugetVersionsInDotNetProjectsAndFSharpScripts.fsx + run: dotnet fsi scripts/inconsistent_nuget_versions_in_dotnet_projects_and_fsharp_scripts.fsx - name: Install prettier run: npm install --verbose prettier@2.8.3 - name: Change file permissions diff --git a/scripts/checkCommits1by1.fsx b/scripts/check_commits_1by1.fsx similarity index 99% rename from scripts/checkCommits1by1.fsx rename to scripts/check_commits_1by1.fsx index 5641260f0..4efcbd9be 100755 --- a/scripts/checkCommits1by1.fsx +++ b/scripts/check_commits_1by1.fsx @@ -1340,7 +1340,7 @@ if notUsedGitPush1by1 then sprintf "Please push the commits one by one to make sure every commit has a CI status; using this script is recommended:%s%s" Environment.NewLine - "https://github.com/nblockchain/conventions/blob/master/scripts/gitPush1by1.fsx" + "https://github.com/nblockchain/conventions/blob/master/scripts/git_push_1by1.fsx" Console.WriteLine() Console.Error.WriteLine errMsg diff --git a/scripts/compileFSharpScripts.fsx b/scripts/compile_fsharp_scripts.fsx similarity index 100% rename from scripts/compileFSharpScripts.fsx rename to scripts/compile_fsharp_scripts.fsx diff --git a/scripts/eofConvention.fsx b/scripts/eof_convention.fsx similarity index 100% rename from scripts/eofConvention.fsx rename to scripts/eof_convention.fsx diff --git a/scripts/executableConvention.fsx b/scripts/executable_convention.fsx similarity index 100% rename from scripts/executableConvention.fsx rename to scripts/executable_convention.fsx diff --git a/scripts/gitPush1by1.fsx b/scripts/git_push_1by1.fsx similarity index 100% rename from scripts/gitPush1by1.fsx rename to scripts/git_push_1by1.fsx diff --git a/scripts/inconsistentNugetVersionsInDotNetProjects.fsx b/scripts/inconsistent_nuget_versions_in_dotnet_projects.fsx similarity index 100% rename from scripts/inconsistentNugetVersionsInDotNetProjects.fsx rename to scripts/inconsistent_nuget_versions_in_dotnet_projects.fsx diff --git a/scripts/inconsistentNugetVersionsInDotNetProjectsAndFSharpScripts.fsx b/scripts/inconsistent_nuget_versions_in_dotnet_projects_and_fsharp_scripts.fsx similarity index 100% rename from scripts/inconsistentNugetVersionsInDotNetProjectsAndFSharpScripts.fsx rename to scripts/inconsistent_nuget_versions_in_dotnet_projects_and_fsharp_scripts.fsx diff --git a/scripts/inconsistentNugetVersionsInFSharpScripts.fsx b/scripts/inconsistent_nuget_versions_in_fsharp_scripts.fsx similarity index 100% rename from scripts/inconsistentNugetVersionsInFSharpScripts.fsx rename to scripts/inconsistent_nuget_versions_in_fsharp_scripts.fsx diff --git a/scripts/inconsistent_versions_in_fsharp_scripts.fsx b/scripts/inconsistent_versions_in_fsharp_scripts.fsx new file mode 100755 index 000000000..5458c12e7 --- /dev/null +++ b/scripts/inconsistent_versions_in_fsharp_scripts.fsx @@ -0,0 +1,23 @@ +#!/usr/bin/env -S dotnet fsi + +open System.IO +open System.Linq + +#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" +#r "nuget: YamlDotNet, Version=16.1.3" + +#load "../src/FileConventions/Library.fs" +#load "../src/FileConventions/Helpers.fs" + +let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo +let currentDir = Directory.GetCurrentDirectory() |> DirectoryInfo + +let targetDir = Helpers.PreferLessDeeplyNestedDir currentDir rootDir + +let inconsistentVersionsInFsharpScripts = + Helpers.GetFiles targetDir "*.fsx" + |> FileConventions.DetectInconsistentVersionsInNugetRefsInFSharpScripts + +if inconsistentVersionsInFsharpScripts then + failwith + "You shouldn't use inconsistent versions in nuget package references of F# scripts." diff --git a/scripts/inconsistentVersionsInGitHubCI.fsx b/scripts/inconsistent_versions_in_githubci.fsx similarity index 100% rename from scripts/inconsistentVersionsInGitHubCI.fsx rename to scripts/inconsistent_versions_in_githubci.fsx diff --git a/scripts/mixedLineEndings.fsx b/scripts/mixed_line_endings.fsx similarity index 100% rename from scripts/mixedLineEndings.fsx rename to scripts/mixed_line_endings.fsx diff --git a/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx b/scripts/non_verbose_flags_in_githubci_and_scripts.fsx similarity index 100% rename from scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx rename to scripts/non_verbose_flags_in_githubci_and_scripts.fsx diff --git a/scripts/not_respecting_some_naming_conventions.fsx b/scripts/not_respecting_some_naming_conventions.fsx new file mode 100755 index 000000000..8fa59e2fc --- /dev/null +++ b/scripts/not_respecting_some_naming_conventions.fsx @@ -0,0 +1,42 @@ +#!/usr/bin/env -S dotnet fsi + +open System +open System.IO + +#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" +#r "nuget: YamlDotNet, Version=16.1.3" +#load "../src/FileConventions/Library.fs" +#load "../src/FileConventions/Helpers.fs" + +let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo + +let invalidYmlFiles = + Helpers.GetInvalidFiles + rootDir + "*.yml" + FileConventions.DetectNotUsingKebabCaseInGitHubCIJobs + +Helpers.AssertNoInvalidFiles + invalidYmlFiles + "Please use kebab-case for CI job names in the following files:" + +let scriptExtensions = + seq { + ".fsx" + ".bat" + ".sh" + } + +let scriptsWithInvalidNames = + scriptExtensions + |> Seq.map(fun extension -> + Helpers.GetInvalidFiles + rootDir + ("*" + extension) + FileConventions.DetectNotUsingSnakeCaseInScriptName + ) + |> Seq.concat + +Helpers.AssertNoInvalidFiles + scriptsWithInvalidNames + "Please use snake_case for naming the following scripts:" diff --git a/scripts/shebangConvention.fsx b/scripts/shebang_convention.fsx similarity index 100% rename from scripts/shebangConvention.fsx rename to scripts/shebang_convention.fsx diff --git a/scripts/unpinnedNugetPackageReferenceVersionsInProjects.fsx b/scripts/unpinned_dotnet_package_versions.fsx similarity index 100% rename from scripts/unpinnedNugetPackageReferenceVersionsInProjects.fsx rename to scripts/unpinned_dotnet_package_versions.fsx diff --git a/scripts/unpinnedDotnetToolInstallVersions.fsx b/scripts/unpinned_dotnet_tool_install_versions.fsx similarity index 100% rename from scripts/unpinnedDotnetToolInstallVersions.fsx rename to scripts/unpinned_dotnet_tool_install_versions.fsx diff --git a/scripts/unpinnedGitHubActionsImageVersions.fsx b/scripts/unpinned_github_actions_image_versions.fsx similarity index 100% rename from scripts/unpinnedGitHubActionsImageVersions.fsx rename to scripts/unpinned_github_actions_image_versions.fsx diff --git a/scripts/unpinnedNugetPackageReferenceVersionsInFSharpScripts.fsx b/scripts/unpinned_nuget_package_reference_versions.fsx similarity index 100% rename from scripts/unpinnedNugetPackageReferenceVersionsInFSharpScripts.fsx rename to scripts/unpinned_nuget_package_reference_versions.fsx diff --git a/scripts/unpinned_nuget_package_reference_versions_in_fsharp_scripts.fsx b/scripts/unpinned_nuget_package_reference_versions_in_fsharp_scripts.fsx new file mode 100755 index 000000000..d48fc1e29 --- /dev/null +++ b/scripts/unpinned_nuget_package_reference_versions_in_fsharp_scripts.fsx @@ -0,0 +1,25 @@ +#!/usr/bin/env -S dotnet fsi + +open System +open System.IO + +#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" +#r "nuget: YamlDotNet, Version=16.1.3" + +#load "../src/FileConventions/Library.fs" +#load "../src/FileConventions/Helpers.fs" + +let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo + +let invalidFiles = + Helpers.GetInvalidFiles + rootDir + "*.fsx" + FileConventions.DetectMissingVersionsInNugetPackageReferences + +let message = + "The following files shouldn't miss the version number in `#r \"nuget: ` refs of F# scripts." + + Environment.NewLine + + "Please use the exact version of the package instead." + +Helpers.AssertNoInvalidFiles invalidFiles message diff --git a/scripts/unpinned_nuget_package_reference_versions_in_projects.fsx b/scripts/unpinned_nuget_package_reference_versions_in_projects.fsx new file mode 100755 index 000000000..7d3a73f05 --- /dev/null +++ b/scripts/unpinned_nuget_package_reference_versions_in_projects.fsx @@ -0,0 +1,28 @@ +#!/usr/bin/env -S dotnet fsi + +open System +open System.IO + +#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" +#r "nuget: YamlDotNet, Version=16.1.3" + +#load "../src/FileConventions/Library.fs" +#load "../src/FileConventions/Helpers.fs" + +let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo +let currentDir = Directory.GetCurrentDirectory() |> DirectoryInfo + +let targetDir = Helpers.PreferLessDeeplyNestedDir currentDir rootDir + +let invalidFiles = + Helpers.GetInvalidFiles + targetDir + "*.*proj" + FileConventions.DetectAsteriskInPackageReferenceItems + +let message = + "The following files shouldn't use asterisk (*) in PackageReference items of .NET projects." + + Environment.NewLine + + "Please use the exact version of the package instead." + +Helpers.AssertNoInvalidFiles invalidFiles message diff --git a/scripts/wrapLatestCommitMsg.fsx b/scripts/wrap_latest_commit_msg.fsx similarity index 100% rename from scripts/wrapLatestCommitMsg.fsx rename to scripts/wrap_latest_commit_msg.fsx diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithKebabCaseJobName.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithKebabCaseJobName.yml new file mode 100644 index 000000000..ec5f065b4 --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithKebabCaseJobName.yml @@ -0,0 +1,11 @@ +name: CI + +on: [push, pull_request] + +jobs: + file-conventions: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndKebabCaseJobNames.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndKebabCaseJobNames.yml new file mode 100644 index 000000000..0f6532c0d --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndKebabCaseJobNames.yml @@ -0,0 +1,18 @@ +name: CI + +on: [push, pull_request] + +jobs: + first-job: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" + + second-job: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndOnePascalCaseJobName.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndOnePascalCaseJobName.yml new file mode 100644 index 000000000..3eb5e7e66 --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithMultipleJobsAndOnePascalCaseJobName.yml @@ -0,0 +1,18 @@ +name: CI + +on: [push, pull_request] + +jobs: + first-job: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" + + SecondJob: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithPascalCaseJobName.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithPascalCaseJobName.yml new file mode 100644 index 000000000..763ad1f01 --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithPascalCaseJobName.yml @@ -0,0 +1,11 @@ +name: CI + +on: [push, pull_request] + +jobs: + FileConventions: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: echo "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/DummyPascalCase.fsx b/src/FileConventions.Test/DummyFiles/DummyPascalCase.fsx new file mode 100644 index 000000000..23c3e513e --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyPascalCase.fsx @@ -0,0 +1,3 @@ +#!/usr/bin/env -S dotnet fsi + +printfn "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/dummy_snake_case.fsx b/src/FileConventions.Test/DummyFiles/dummy_snake_case.fsx new file mode 100644 index 000000000..23c3e513e --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/dummy_snake_case.fsx @@ -0,0 +1,3 @@ +#!/usr/bin/env -S dotnet fsi + +printfn "Hello World!" diff --git a/src/FileConventions.Test/DummyFiles/kebab-case.fsx b/src/FileConventions.Test/DummyFiles/kebab-case.fsx new file mode 100644 index 000000000..23c3e513e --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/kebab-case.fsx @@ -0,0 +1,3 @@ +#!/usr/bin/env -S dotnet fsi + +printfn "Hello World!" diff --git a/src/FileConventions.Test/FileConventions.Test.fs b/src/FileConventions.Test/FileConventions.Test.fs index 81d82bc68..2e9caa263 100644 --- a/src/FileConventions.Test/FileConventions.Test.fs +++ b/src/FileConventions.Test/FileConventions.Test.fs @@ -639,3 +639,89 @@ let IsExecutableTest2() = )) Assert.That(IsExecutable fileInfo, Is.EqualTo false) + + +[] +let DetectNotUsingSnakeCaseInScriptName1() = + let fileInfo = + (FileInfo( + Path.Combine(dummyFilesDirectory.FullName, "dummy_snake_case.fsx") + )) + + Assert.That(DetectNotUsingSnakeCaseInScriptName fileInfo, Is.EqualTo false) + + +[] +let DetectNotUsingSnakeCaseInScriptName2() = + let fileInfo = + (FileInfo( + Path.Combine(dummyFilesDirectory.FullName, "DummyPascalCase.fsx") + )) + + Assert.That(DetectNotUsingSnakeCaseInScriptName fileInfo, Is.EqualTo true) + + +[] +let DetectNotUsingSnakeCaseInScriptName3() = + let fileInfo = + (FileInfo(Path.Combine(dummyFilesDirectory.FullName, "kebab-case.fsx"))) + + Assert.That(DetectNotUsingSnakeCaseInScriptName fileInfo, Is.EqualTo true) + + +[] +let DetectNotUsingKebabCaseInGitHubCIJobs1() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithPascalCaseJobName.yml" + ) + )) + + Assert.That(DetectNotUsingKebabCaseInGitHubCIJobs fileInfo, Is.EqualTo true) + + +[] +let DetectNotUsingKebabCaseInGitHubCIJobs2() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithKebabCaseJobName.yml" + ) + )) + + Assert.That( + DetectNotUsingKebabCaseInGitHubCIJobs fileInfo, + Is.EqualTo false + ) + + +[] +let DetectNotUsingKebabCaseInGitHubCIJobs3() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithMultipleJobsAndKebabCaseJobNames.yml" + ) + )) + + Assert.That( + DetectNotUsingKebabCaseInGitHubCIJobs fileInfo, + Is.EqualTo false + ) + + +[] +let DetectNotUsingKebabCaseInGitHubCIJobs4() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithMultipleJobsAndOnePascalCaseJobName.yml" + ) + )) + + Assert.That(DetectNotUsingKebabCaseInGitHubCIJobs fileInfo, Is.EqualTo true) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index e471f54cd..9fcfcd81a 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -7,6 +7,7 @@ open System.Text.RegularExpressions open Mono open Mono.Unix.Native +open YamlDotNet open YamlDotNet.RepresentationModel let SplitByEOLs (text: string) (numberOfEOLs: uint) = @@ -495,3 +496,57 @@ let NonVerboseFlags(fileInfo: FileInfo) = let IsExecutable(fileInfo: FileInfo) = let hasExecuteAccess = Syscall.access(fileInfo.FullName, AccessModes.X_OK) hasExecuteAccess = 0 + +let DetectNotUsingSnakeCaseInScriptName(fileInfo: FileInfo) = + let fileName = fileInfo.Name + let snakeCase = fileName.ToLower() = fileName && not(fileName.Contains "-") + not(snakeCase) + +let DetectNotUsingKebabCaseInGitHubCIJobs(fileInfo: FileInfo) = + assert (fileInfo.FullName.EndsWith ".yml") + + let read yaml = + use reader = new StringReader(yaml) + let stream = YamlStream() + stream.Load(reader) + stream.Documents + + let doc = read(File.ReadAllText fileInfo.FullName) + + let ymlNode = doc.[0].RootNode + + // Borrowed from https://stackoverflow.com/questions/46697298/whats-the-best-way-to-parse-yaml-in-f-on-net-core + let getMapping(ymlNode: YamlNode) = + let node = ymlNode :?> YamlMappingNode + + let mapping = + node.Children + |> Seq.map(fun kvp -> + let keyNode = kvp.Key :?> YamlScalarNode + keyNode.Value, kvp.Value + ) + |> Map.ofSeq + + mapping + + let jobNames = + match ymlNode.NodeType with + | YamlNodeType.Mapping -> + let mapping = getMapping ymlNode + let maybeJobs = mapping.TryFind "jobs" + + match maybeJobs with + | Some jobs -> + let mapping = getMapping jobs + (mapping |> Map.toSeq |> Seq.map fst) + | None -> Seq.empty + | _ -> Seq.empty + + jobNames + |> Seq.map(fun jobName -> + let isKebabCase = + jobName.ToLower() = jobName && not(jobName.Contains("_")) + + isKebabCase + ) + |> Seq.contains false