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>
  • Loading branch information
Mersho and aarani committed Nov 6, 2023
1 parent df195a9 commit 68c7bd9
Showing 1 changed file with 51 additions and 24 deletions.
75 changes: 51 additions & 24 deletions scripts/sanityCheckNuget.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,32 @@ 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 args = Misc.FsxOnlyArguments()

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

Environment.Exit 1

let projDir =
if args.IsEmpty then
Directory.GetCurrentDirectory()
elif Directory.Exists args.[0] then
args.[0]
else
failwithf "'%s' directory does not exist." args.[0]

let NugetSolutionPackagesDir =
Path.Combine(RootDir.FullName, "packages") |> DirectoryInfo
let nugetPackageConfigDir = Path.Combine(projDir, "NuGet.config")

if File.Exists nugetPackageConfigDir then
Path.Combine(projDir, "packages") |> DirectoryInfo
else
failwithf
"There is no NuGet.config in '%s', which is required to find missing packages."
nugetPackageConfigDir


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

let SanityCheckNugetPackages() =

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

let notSubmodule(dir: DirectoryInfo) : bool =
Expand Down Expand Up @@ -167,7 +191,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 +346,25 @@ let SanityCheckNugetPackages() =
: Map<string, seq<DependencyHolder>> =
solDir.Refresh()

if not NugetSolutionPackagesDir.Exists then
let packagesDir = NugetSolutionPackagesDir

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

let packageDirsAbsolutePaths =
NugetSolutionPackagesDir
packagesDir
.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
packagesDir.Name
packagesDir.FullName
)

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

if not(NugetSolutionPackagesDir.Exists) then
let packagesDir = NugetSolutionPackagesDir

if not(packagesDir.Exists) then
failwithf
"'%s' subdir under solution dir %s doesn't exist, run `make` first"
NugetSolutionPackagesDir.Name
NugetSolutionPackagesDir.FullName
packagesDir.Name
packagesDir.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
packagesDir
.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
packagesDir.Name
packagesDir.FullName

let packageDirsThatShouldExist =
MapHelper.GetKeysOfMap idealPackageDirs
Expand Down Expand Up @@ -566,13 +594,12 @@ 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)
let solutions = findSolutions(projDir |> DirectoryInfo)

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

for sol in solutions do
sanityCheckNugetPackagesFromSolution sol

SanityCheckNugetPackages()

0 comments on commit 68c7bd9

Please sign in to comment.