Skip to content

Commit

Permalink
FileConventions: implement the function
Browse files Browse the repository at this point in the history
Implement DetectNotUsingKebabCaseInGitHubCIJobs function.
  • Loading branch information
tehraninasab committed Aug 8, 2023
1 parent 674d4f7 commit 50de519
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 11 deletions.
2 changes: 1 addition & 1 deletion scripts/eofConvention.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System.IO
open System

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#r "nuget: YamlDotNet, Version=13.0.2"
#load "../src/FileConventions/Helpers.fs"
#load "../src/FileConventions/Library.fs"

Expand Down
1 change: 1 addition & 0 deletions scripts/executableConvention.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"
#r "nuget: YamlDotNet, Version=13.0.2"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 1 addition & 1 deletion scripts/inconsistentVersionsInFSharpScripts.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System.IO
open System.Linq

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#r "nuget: YamlDotNet, Version=13.0.2"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 1 addition & 1 deletion scripts/inconsistentVersionsInGitHubCI.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#r "nuget: YamlDotNet, Version=13.0.2"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 1 addition & 1 deletion scripts/mixedLineEndings.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#r "nuget: YamlDotNet, Version=13.0.2"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 1 addition & 1 deletion scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#r "nuget: YamlDotNet, Version=13.0.2"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 1 addition & 1 deletion scripts/shebangConvention.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#r "nuget: YamlDotNet, Version=13.0.2"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 1 addition & 1 deletion scripts/unpinnedDotnetPackageVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#r "nuget: YamlDotNet, Version=13.0.2"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 1 addition & 1 deletion scripts/unpinnedDotnetToolInstallVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#r "nuget: YamlDotNet, Version=13.0.2"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 1 addition & 1 deletion scripts/unpinnedGitHubActionsImageVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#r "nuget: YamlDotNet, Version=13.0.2"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 1 addition & 1 deletion scripts/unpinnedNugetPackageReferenceVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#r "nuget: YamlDotNet, Version=13.0.2"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
1 change: 1 addition & 0 deletions scripts/wrapLatestCommitMsg.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open System
open System.Text.RegularExpressions

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"
#r "nuget: YamlDotNet, Version=13.0.2"

#load "../src/FileConventions/Library.fs"

Expand Down
1 change: 1 addition & 0 deletions src/FileConventions/FileConventions.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="Mono.Posix" Version="7.1.0-final.1.21458.1" />
<PackageReference Include="Mono.Unix" Version="7.1.0-final.1.21458.1" />
<PackageReference Include="YamlDotNet" Version="13.0.2" />
</ItemGroup>

</Project>
49 changes: 48 additions & 1 deletion src/FileConventions/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ open System.Text.RegularExpressions

open Mono
open Mono.Unix.Native
open YamlDotNet
open YamlDotNet.RepresentationModel

let HasCorrectShebang(fileInfo: FileInfo) =
let fileText = File.ReadLines fileInfo.FullName
Expand Down Expand Up @@ -398,4 +400,49 @@ let DetectNotUsingSnakeCaseInScriptName(fileInfo: FileInfo) =

let DetectNotUsingKebabCaseInGitHubCIJobs(fileInfo: FileInfo) =
assert (fileInfo.FullName.EndsWith ".yml")
false

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

0 comments on commit 50de519

Please sign in to comment.