Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/eofConvention.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ open System
#r "nuget: YamlDotNet, Version=16.1.3"

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

open type FileConventions.EolAtEof

Expand Down
2 changes: 1 addition & 1 deletion scripts/executableConvention.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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/FileConventions.fs"
#load "../src/FileConventions/Helpers.fs"

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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/FileConventions.fs"
#load "../src/FileConventions/Helpers.fs"
#load "../src/FileConventions/NugetVersionsCheck.fs"
#load "../src/FileConventions/CombinedVersionCheck.fs"
Expand Down
2 changes: 1 addition & 1 deletion scripts/inconsistentNugetVersionsInFSharpScripts.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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/FileConventions.fs"
#load "../src/FileConventions/Helpers.fs"

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo
Expand Down
2 changes: 1 addition & 1 deletion scripts/inconsistentVersionsInGitHubCI.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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/FileConventions.fs"
#load "../src/FileConventions/Helpers.fs"

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo
Expand Down
2 changes: 1 addition & 1 deletion scripts/mixedLineEndings.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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/FileConventions.fs"
#load "../src/FileConventions/Helpers.fs"

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo
Expand Down
2 changes: 1 addition & 1 deletion scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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/FileConventions.fs"
#load "../src/FileConventions/Helpers.fs"

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo
Expand Down
2 changes: 1 addition & 1 deletion scripts/shebangConvention.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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/FileConventions.fs"
#load "../src/FileConventions/Helpers.fs"

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo
Expand Down
2 changes: 1 addition & 1 deletion scripts/unpinnedDotnetToolInstallVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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/FileConventions.fs"
#load "../src/FileConventions/Helpers.fs"

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo
Expand Down
2 changes: 1 addition & 1 deletion scripts/unpinnedGitHubActionsImageVersions.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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/FileConventions.fs"
#load "../src/FileConventions/Helpers.fs"

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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/FileConventions.fs"
#load "../src/FileConventions/Helpers.fs"

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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/FileConventions.fs"
#load "../src/FileConventions/Helpers.fs"

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo
Expand Down
3 changes: 1 addition & 2 deletions scripts/wrapLatestCommitMsg.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ open System.Text.RegularExpressions
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/FileConventions.fs"

#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62"

Expand Down
1 change: 1 addition & 0 deletions src/FileConventions.Test/FileConventions.Test.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ open Fsdk
open Fsdk.Process

open FileConventions
open Workflow

open type FileConventions.EolAtEof

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ open System.Text.RegularExpressions

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

let SplitByEOLs (text: string) (numberOfEOLs: uint) =
if numberOfEOLs = 0u then
Expand Down Expand Up @@ -359,119 +358,6 @@ let private GetVersionsMapFromFiles
)
Map.empty

let private DetectInconsistentVersionsInYamlFiles
(fileInfos: seq<FileInfo>)
(extractVersionsFunction: YamlNode -> seq<string * string>)
=
let envVarRegex =
Regex(@"\s*\$\{\{\s*([^\s\}]+)\s*\}\}\s*", RegexOptions.Compiled)

let yamlDocuments =
Seq.map
(fun (fileInfo: FileInfo) ->
let yaml = YamlStream()
use reader = new StreamReader(fileInfo.FullName)
yaml.Load reader
yaml.Documents[0].RootNode
)
fileInfos

let versionMap =
Seq.fold
(fun mapping (yamlDoc: YamlNode) ->
let matches =
Seq.collect extractVersionsFunction yamlDoc.AllNodes

matches
|> Seq.fold
(fun acc (key, value) ->
let actualValue =
let variableRegexMatch = envVarRegex.Match value

if variableRegexMatch.Success then
let yamlDict = yamlDoc :?> YamlMappingNode

match yamlDict.Children.TryGetValue "env" with
| true, (:? YamlMappingNode as envDict) ->
let referenceString =
variableRegexMatch.Groups.[1].Value

let envVarName =
if referenceString.StartsWith "env." then
referenceString.[4..]
else
referenceString

match
envDict.Children.TryGetValue envVarName
with
| true, envVarValue ->
(envVarValue :?> YamlScalarNode).Value
| false, _ -> value
| _ -> value
else
value

match Map.tryFind key acc with
| Some prevSet ->
Map.add key (Set.add actualValue prevSet) acc
| None -> Map.add key (Set.singleton actualValue) acc
)
mapping
)
Map.empty
yamlDocuments

versionMap
|> Seq.map(fun item -> Seq.length item.Value > 1)
|> Seq.contains true

let DetectInconsistentVersionsInGitHubCIWorkflow(fileInfos: seq<FileInfo>) =
fileInfos
|> Seq.iter(fun fileInfo -> assert (fileInfo.FullName.EndsWith ".yml"))

let extractVersions(node: YamlNode) =
match node with
| :? YamlMappingNode as yamlDict ->
yamlDict.Children
|> Seq.collect(fun (KeyValue(keyNode, valueNode)) ->
match keyNode, valueNode with
| (:? YamlScalarNode as keyScalar),
(:? YamlScalarNode as valueScalar) when
keyScalar.Value = "uses"
->
match valueScalar.Value.Split "@v" with
| [| name; version |] -> Seq.singleton(name, version)
| _ -> Seq.empty
| (:? YamlScalarNode as keyScalar),
(:? YamlMappingNode as valueMapping) when
keyScalar.Value = "with"
->
valueMapping.Children
|> Seq.choose(fun (KeyValue(innerKeyNode, innerValueNode)) ->
match innerKeyNode, innerValueNode with
| (:? YamlScalarNode as keyScalar),
(:? YamlScalarNode as valueScalar) ->
match keyScalar.Value.Split '-' with
| [| name; "version" |] ->
Some(name, valueScalar.Value)
| _ -> None
| _ -> None
)
| _ -> Seq.empty
)
| _ -> Seq.empty

DetectInconsistentVersionsInYamlFiles fileInfos extractVersions

let DetectInconsistentVersionsInGitHubCI(dir: DirectoryInfo) =
let ymlFiles = dir.GetFiles("*.yml", SearchOption.AllDirectories)

if Seq.isEmpty ymlFiles then
false
else
DetectInconsistentVersionsInGitHubCIWorkflow ymlFiles

let GetVersionsMapForNugetRefsInFSharpScripts(fileInfos: seq<FileInfo>) =
fileInfos
|> Seq.iter(fun fileInfo -> assert (fileInfo.FullName.EndsWith ".fsx"))
Expand Down
3 changes: 2 additions & 1 deletion src/FileConventions/FileConventions.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

<ItemGroup>
<Compile Include="Helpers.fs" />
<Compile Include="Workflow.fs" />
<Compile Include="NugetVersionsCheck.fs" />
<Compile Include="Library.fs" />
<Compile Include="FileConventions.fs" />
<Compile Include="CombinedVersionCheck.fs" />
</ItemGroup>

Expand Down
119 changes: 119 additions & 0 deletions src/FileConventions/Workflow.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
module Workflow

open System.IO
open System.Text.RegularExpressions

open YamlDotNet.RepresentationModel

let private DetectInconsistentVersionsInYamlFiles
(fileInfos: seq<FileInfo>)
(extractVersionsFunction: YamlNode -> seq<string * string>)
=
let envVarRegex =
Regex(@"\s*\$\{\{\s*([^\s\}]+)\s*\}\}\s*", RegexOptions.Compiled)

let yamlDocuments =
Seq.map
(fun (fileInfo: FileInfo) ->
let yaml = YamlStream()
use reader = new StreamReader(fileInfo.FullName)
yaml.Load reader
yaml.Documents[0].RootNode
)
fileInfos

let versionMap =
Seq.fold
(fun mapping (yamlDoc: YamlNode) ->
let matches =
Seq.collect extractVersionsFunction yamlDoc.AllNodes

matches
|> Seq.fold
(fun acc (key, value) ->
let actualValue =
let variableRegexMatch = envVarRegex.Match value

if variableRegexMatch.Success then
let yamlDict = yamlDoc :?> YamlMappingNode

match yamlDict.Children.TryGetValue "env" with
| true, (:? YamlMappingNode as envDict) ->
let referenceString =
variableRegexMatch.Groups.[1].Value

let envVarName =
if referenceString.StartsWith "env." then
referenceString.[4..]
else
referenceString

match
envDict.Children.TryGetValue envVarName
with
| true, envVarValue ->
(envVarValue :?> YamlScalarNode).Value
| false, _ -> value
| _ -> value
else
value

match Map.tryFind key acc with
| Some prevSet ->
Map.add key (Set.add actualValue prevSet) acc
| None -> Map.add key (Set.singleton actualValue) acc
)
mapping
)
Map.empty
yamlDocuments

versionMap
|> Seq.map(fun item -> Seq.length item.Value > 1)
|> Seq.contains true

let DetectInconsistentVersionsInGitHubCIWorkflow(fileInfos: seq<FileInfo>) =
fileInfos
|> Seq.iter(fun fileInfo -> assert (fileInfo.FullName.EndsWith ".yml"))

let extractVersions(node: YamlNode) =
match node with
| :? YamlMappingNode as yamlDict ->
yamlDict.Children
|> Seq.collect(fun (KeyValue(keyNode, valueNode)) ->
match keyNode, valueNode with
| (:? YamlScalarNode as keyScalar),
(:? YamlScalarNode as valueScalar) when
keyScalar.Value = "uses"
->
match valueScalar.Value.Split "@v" with
| [| name; version |] -> Seq.singleton(name, version)
| _ -> Seq.empty
| (:? YamlScalarNode as keyScalar),
(:? YamlMappingNode as valueMapping) when
keyScalar.Value = "with"
->
valueMapping.Children
|> Seq.choose(fun (KeyValue(innerKeyNode, innerValueNode)) ->
match innerKeyNode, innerValueNode with
| (:? YamlScalarNode as keyScalar),
(:? YamlScalarNode as valueScalar) ->
match keyScalar.Value.Split '-' with
| [| name; "version" |] ->
Some(name, valueScalar.Value)
| _ -> None
| _ -> None
)
| _ -> Seq.empty
)
| _ -> Seq.empty

DetectInconsistentVersionsInYamlFiles fileInfos extractVersions

let DetectInconsistentVersionsInGitHubCI(dir: DirectoryInfo) =
let ymlFiles = dir.GetFiles("*.yml", SearchOption.AllDirectories)

if Seq.isEmpty ymlFiles then
false
else
DetectInconsistentVersionsInGitHubCIWorkflow ymlFiles
Loading