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 May 23, 2023
1 parent bcd3832 commit 2e58c29
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions scripts/eofConvention.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open System.IO
open System

#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/mixedLineEndings.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open System
open System.IO

#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/shebangConvention.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open System
open System.IO

#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/unpinnedDotnetPackageVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open System
open System.IO

#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/unpinnedDotnetToolInstallVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open System
open System.IO

#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/unpinnedGitHubActionsImageVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open System
open System.IO

#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/unpinnedNugetPackageReferenceVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open System
open System.IO

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

Expand Down
4 changes: 4 additions & 0 deletions src/FileConventions/FileConventions.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<Compile Include="Helpers.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="YamlDotNet" Version="13.0.2" />
</ItemGroup>

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

open YamlDotNet
open YamlDotNet.RepresentationModel

let HasCorrectShebang(fileInfo: FileInfo) =
let fileText = File.ReadLines fileInfo.FullName

Expand Down Expand Up @@ -130,4 +133,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 2e58c29

Please sign in to comment.