From a24c754f07de50a01e6106a0e4cedf1a849565bf Mon Sep 17 00:00:00 2001 From: Mehrshad Date: Mon, 30 Oct 2023 16:46:04 +0330 Subject: [PATCH] sanityCheckNuget.fsx: sanitycheck for external sln Co-authored-by: Afshin Arani Co-authored-by: Andres G. Aragoneses --- scripts/sanityCheckNuget.fsx | 117 ++++++++++++++++++++++++++++------- 1 file changed, 94 insertions(+), 23 deletions(-) diff --git a/scripts/sanityCheckNuget.fsx b/scripts/sanityCheckNuget.fsx index 17becfa8..80386642 100755 --- a/scripts/sanityCheckNuget.fsx +++ b/scripts/sanityCheckNuget.fsx @@ -23,11 +23,71 @@ 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 + +type ScriptTarget = + | Solution of FileInfo + | Folder of DirectoryInfo + +let args = Misc.FsxOnlyArguments() + +if args.Length > 2 then + printf + "Usage: %s [example.sln(optional) NuGet.config]\nIf NuGet.config was not found in current directory, +you must specify it in secound commandline argument." + __SOURCE_FILE__ + + Environment.Exit 1 + +let target = + if args.IsEmpty then + Directory.GetCurrentDirectory() |> 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(RootDir.FullName, "packages") |> DirectoryInfo + match target with + | Solution _ -> + let nugetPackageConfigDir = + let nugetConfigInCurrentDirectory = + Path.Combine(Directory.GetCurrentDirectory(), "NuGet.config") + + if File.Exists(nugetConfigInCurrentDirectory) then + nugetConfigInCurrentDirectory + else + if not(args.Length > 1) then + failwithf "You must specify NuGet.config." + + args.[1] + + + if not(File.Exists nugetPackageConfigDir) then + failwithf + "NuGet.config not found in '%s', please create it with the `globalPackagesFolder` key as `packages`." + nugetPackageConfigDir + + + Path.Combine( + (nugetPackageConfigDir |> FileInfo) + .Directory + .FullName, + "packages" + ) + |> DirectoryInfo + | Folder directoryInfo -> + Path.Combine(directoryInfo.FullName, "packages") |> DirectoryInfo + module MapHelper = let GetKeysOfMap(map: Map<'K, 'V>) : seq<'K> = @@ -100,7 +160,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 = @@ -167,7 +230,7 @@ let SanityCheckNugetPackages() = dir .EnumerateDirectories() .Where(notSubmodule) - .Where(notPackagesFolder) do + .Where(notPackagesFolder sol.Directory) do for file in findNuspecFiles subdir do yield file } @@ -322,14 +385,16 @@ let SanityCheckNugetPackages() = : Map> = 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) @@ -337,8 +402,8 @@ let SanityCheckNugetPackages() = 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 @@ -351,7 +416,7 @@ let SanityCheckNugetPackages() = idealPackageDirs do let pkgDirToLookFor = Path.Combine( - NugetSolutionPackagesDir.FullName, + packagesDir.FullName, packageDirNameThatShouldExist ) |> DirectoryInfo @@ -367,15 +432,17 @@ let SanityCheckNugetPackages() = : seq = 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" ]) @@ -383,8 +450,8 @@ let SanityCheckNugetPackages() = 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 @@ -566,13 +633,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()