Skip to content

Commit

Permalink
sanityCheckNuget.fsx: sanitycheck for external sln
Browse files Browse the repository at this point in the history
Co-authored-by: Afshin Arani <afshin@arani.dev>
Co-authored-by: Andres G. Aragoneses <knocte@gmail.com>
  • Loading branch information
3 people committed Nov 9, 2023
1 parent df195a9 commit b3a7d0a
Showing 1 changed file with 70 additions and 26 deletions.
96 changes: 70 additions & 26 deletions scripts/sanityCheckNuget.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,48 @@ open Fsdk.Process

#r "nuget: Microsoft.Build, Version=16.11.0"
open Microsoft.Build.Construction
let ScriptsDir = __SOURCE_DIRECTORY__ |> DirectoryInfo
let RootDir = Path.Combine(ScriptsDir.FullName, "..") |> DirectoryInfo

let NugetSolutionPackagesDir =
Path.Combine(RootDir.FullName, "packages") |> DirectoryInfo
type ScriptTarget =
| Solution of FileInfo
| Folder of DirectoryInfo

let args = Misc.FsxOnlyArguments()
let currentDirectory = Directory.GetCurrentDirectory()

if args.Length > 1 then
printf "Usage: %s [example.sln(optional)]" __SOURCE_FILE__

Environment.Exit 1

let target =
if args.IsEmpty then
currentDirectory |> DirectoryInfo |> ScriptTarget.Folder
else
let singleArg = args.[0]

if Directory.Exists singleArg then
failwithf "Use an .sln file insted of directory."
elif not(File.Exists singleArg) then
failwithf "'%s' does not exist." singleArg
elif singleArg.EndsWith ".sln" then
singleArg |> FileInfo |> ScriptTarget.Solution
else
failwithf
"'%s' argument is invalid. You should enter an .sln file or run this script on current directory"
singleArg


let nugetSolutionPackagesDir =
Path.Combine(currentDirectory, "packages") |> DirectoryInfo

let nugetPackageConfigDir =
Path.Combine(currentDirectory, "NuGet.config") |> FileInfo

if not nugetPackageConfigDir.Exists then
failwithf
"NuGet.config not found in '%s', please create it with the `globalPackagesFolder` key as `packages`."
nugetPackageConfigDir.FullName


module MapHelper =
let GetKeysOfMap(map: Map<'K, 'V>) : seq<'K> =
Expand Down Expand Up @@ -100,8 +137,11 @@ type private ComparableFileInfo =

let SanityCheckNugetPackages() =

let notPackagesFolder(dir: DirectoryInfo) : bool =
dir.FullName <> NugetSolutionPackagesDir.FullName
let notPackagesFolder
(solutionDir: DirectoryInfo)
(dir: DirectoryInfo)
: bool =
dir.FullName <> nugetSolutionPackagesDir.FullName

let notSubmodule(dir: DirectoryInfo) : bool =
let getSubmoduleDirsForThisRepo() : seq<DirectoryInfo> =
Expand All @@ -117,7 +157,7 @@ let SanityCheckNugetPackages() =
let submoduleFolder =
DirectoryInfo(
Path.Combine(
Directory.GetCurrentDirectory(),
currentDirectory,
submoduleFolderRelativePath
)
)
Expand Down Expand Up @@ -167,7 +207,7 @@ let SanityCheckNugetPackages() =
dir
.EnumerateDirectories()
.Where(notSubmodule)
.Where(notPackagesFolder) do
.Where(notPackagesFolder sol.Directory) do
for file in findNuspecFiles subdir do
yield file
}
Expand Down Expand Up @@ -322,23 +362,23 @@ let SanityCheckNugetPackages() =
: Map<string, seq<DependencyHolder>> =
solDir.Refresh()

if not NugetSolutionPackagesDir.Exists then
if not nugetSolutionPackagesDir.Exists then
failwithf
"'%s' subdir under solution dir %s doesn't exist, run `make` first"
NugetSolutionPackagesDir.Name
NugetSolutionPackagesDir.FullName
nugetSolutionPackagesDir.Name
nugetSolutionPackagesDir.FullName

let packageDirsAbsolutePaths =
NugetSolutionPackagesDir
nugetSolutionPackagesDir
.EnumerateDirectories()
.Select(fun dir -> dir.FullName)

if not(packageDirsAbsolutePaths.Any()) then
Console.Error.WriteLine(
sprintf
"'%s' subdir under solution dir %s doesn't contain any packages"
NugetSolutionPackagesDir.Name
NugetSolutionPackagesDir.FullName
nugetSolutionPackagesDir.Name
nugetSolutionPackagesDir.FullName
)

Console.Error.WriteLine
Expand All @@ -351,7 +391,7 @@ let SanityCheckNugetPackages() =
idealPackageDirs do
let pkgDirToLookFor =
Path.Combine(
NugetSolutionPackagesDir.FullName,
nugetSolutionPackagesDir.FullName,
packageDirNameThatShouldExist
)
|> DirectoryInfo
Expand All @@ -367,24 +407,24 @@ let SanityCheckNugetPackages() =
: seq<string> =
solDir.Refresh()

if not(NugetSolutionPackagesDir.Exists) then
if not(nugetSolutionPackagesDir.Exists) then
failwithf
"'%s' subdir under solution dir %s doesn't exist, run `make` first"
NugetSolutionPackagesDir.Name
NugetSolutionPackagesDir.FullName
nugetSolutionPackagesDir.Name
nugetSolutionPackagesDir.FullName
// "src" is a directory for source codes and build scripts,
// not for packages, so we need to exclude it from here
let packageDirNames =
NugetSolutionPackagesDir
nugetSolutionPackagesDir
.EnumerateDirectories()
.Select(fun dir -> dir.Name)
.Except([ "src" ])

if not(packageDirNames.Any()) then
failwithf
"'%s' subdir under solution dir %s doesn't contain any packages"
NugetSolutionPackagesDir.Name
NugetSolutionPackagesDir.FullName
nugetSolutionPackagesDir.Name
nugetSolutionPackagesDir.FullName

let packageDirsThatShouldExist =
MapHelper.GetKeysOfMap idealPackageDirs
Expand Down Expand Up @@ -566,13 +606,17 @@ let SanityCheckNugetPackages() =
yield solution
}

let slnFile = Path.Combine(RootDir.FullName, "conventions.sln") |> FileInfo

if not slnFile.Exists then
raise
<| FileNotFoundException("Solution file not found", slnFile.FullName)
match target with
| Solution solution -> sanityCheckNugetPackagesFromSolution solution
| Folder folder ->

let solutions = findSolutions folder

sanityCheckNugetPackagesFromSolution slnFile
if Seq.isEmpty solutions then
failwithf "There is no *.sln file located in: '%s'" folder.FullName

for sol in solutions do
sanityCheckNugetPackagesFromSolution sol

SanityCheckNugetPackages()

0 comments on commit b3a7d0a

Please sign in to comment.