From e83032db42546ac58e06d234312259b6f26ed9f1 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Fri, 20 Oct 2023 18:38:32 +0800 Subject: [PATCH] scripts: refactor to respect DRY --- scripts/fsxHelper.fs | 24 +++++++++++++++++++ scripts/make.fsx | 52 ++++++++++++----------------------------- scripts/sanitycheck.fsx | 34 +++++++-------------------- 3 files changed, 48 insertions(+), 62 deletions(-) diff --git a/scripts/fsxHelper.fs b/scripts/fsxHelper.fs index 74664b745..0b4dc9362 100644 --- a/scripts/fsxHelper.fs +++ b/scripts/fsxHelper.fs @@ -6,8 +6,32 @@ open System.IO open Fsdk open Fsdk.Process +type SolutionFile = + | Default + | Linux + | Mac + module FsxHelper = + let GetSolution (solType: SolutionFile) = + let solFileName = + match solType with + | Default -> + #if !LEGACY_FRAMEWORK + "gwallet.core.sln" + #else + "gwallet.core-legacy.sln" + #endif + | Linux -> "gwallet.linux-legacy.sln" + | Mac -> "gwallet.mac-legacy.sln" + + let slnFile = + Path.Combine("src", solFileName) + |> FileInfo + if not slnFile.Exists then + raise <| FileNotFoundException("Solution file not found", slnFile.FullName) + slnFile + let ScriptsDir = __SOURCE_DIRECTORY__ |> DirectoryInfo let RootDir = Path.Combine(ScriptsDir.FullName, "..") |> DirectoryInfo let SourceDir = Path.Combine(ScriptsDir.FullName, "../src/") |> DirectoryInfo diff --git a/scripts/make.fsx b/scripts/make.fsx index 71c9e55a1..11e143e59 100644 --- a/scripts/make.fsx +++ b/scripts/make.fsx @@ -5,14 +5,6 @@ open System.IO open System.Linq open System.Diagnostics -open System.Text -open System.Text.RegularExpressions -#r "System.Core.dll" -open System.Xml -#r "System.Xml.Linq.dll" -open System.Xml.Linq -open System.Xml.XPath - #if !LEGACY_FRAMEWORK #r "nuget: Fsdk, Version=0.6.0--date20230818-1152.git-83d671b" #else @@ -34,25 +26,6 @@ let UNIX_NAME = "geewallet" let CONSOLE_FRONTEND = "GWallet.Frontend.Console" let GTK_FRONTEND = "GWallet.Frontend.XF.Gtk" -type SolutionFile = - | Default - | Linux - | Mac - -let GetSolution (solType: SolutionFile) = - let solFileName = - match solType with - | Default -> -#if !LEGACY_FRAMEWORK - "gwallet.core.sln" -#else - "gwallet.core-legacy.sln" -#endif - | Linux -> "gwallet.linux-legacy.sln" - | Mac -> "gwallet.mac-legacy.sln" - - Path.Combine("src", solFileName) - type ProjectFile = | XFFrontend | GtkFrontend @@ -63,7 +36,12 @@ let GetProject (projFile: ProjectFile) = | GtkFrontend -> Path.Combine("GWallet.Frontend.XF.Gtk", "GWallet.Frontend.XF.Gtk.fsproj") | XFFrontend -> Path.Combine("GWallet.Frontend.XF", "GWallet.Frontend.XF.fsproj") - Path.Combine("src", projFileName) + let prjFile = + Path.Combine("src", projFileName) + |> FileInfo + if not prjFile.Exists then + raise <| FileNotFoundException("Project file not found", prjFile.FullName) + prjFile let BACKEND = "GWallet.Backend" @@ -154,11 +132,11 @@ FRONTEND_PATH="$DIR_OF_THIS_SCRIPT/../lib/$UNIX_NAME/$GWALLET_PROJECT.exe" exec mono "$FRONTEND_PATH" "$@" """ -let NugetRestore projectOrSolutionRelativePath = +let NugetRestore (projectOrSolution: FileInfo) = let nugetArgs = sprintf "restore %s -DisableParallelProcessing -SolutionDirectory ." - projectOrSolutionRelativePath + projectOrSolution.FullName let proc = Network.RunNugetCommand FsxHelper.NugetExe @@ -194,13 +172,13 @@ let PrintNugetVersion () = let BuildSolutionOrProject (buildToolAndBuildArg: string*string) - (fileName: string) + (file: FileInfo) (binaryConfig: BinaryConfig) (maybeConstant: Option) (extraOptions: string) = #if LEGACY_FRAMEWORK - NugetRestore fileName + NugetRestore file #endif let buildTool,buildArg = buildToolAndBuildArg @@ -255,7 +233,7 @@ let BuildSolutionOrProject configOption let buildArgs = sprintf "%s %s %s %s" buildArg - fileName + file.FullName configOptions extraOptions let buildProcess = Process.Execute ({ Command = buildTool; Arguments = buildArgs }, Echo.All) @@ -273,7 +251,7 @@ let JustBuild binaryConfig maybeConstant: Frontend*FileInfo = let maybeBuildTool = Map.tryFind "BuildTool" buildConfigContents let maybeLegacyBuildTool = Map.tryFind "LegacyBuildTool" buildConfigContents - let solutionFileName = GetSolution SolutionFile.Default + let solutionFile = FsxHelper.GetSolution SolutionFile.Default let getBuildToolAndArgs(buildTool: string) = match buildTool with | "dotnet" -> @@ -307,7 +285,7 @@ let JustBuild binaryConfig maybeConstant: Frontend*FileInfo = | None, Some buildTool -> BuildSolutionOrProject (getBuildToolAndArgs buildTool) - solutionFileName + solutionFile binaryConfig maybeConstant String.Empty @@ -328,7 +306,7 @@ let JustBuild binaryConfig maybeConstant: Frontend*FileInfo = | Misc.Platform.Mac -> //this is because building in release requires code signing keys if binaryConfig = BinaryConfig.Debug then - let solution = GetSolution SolutionFile.Mac + let solution = FsxHelper.GetSolution SolutionFile.Mac // somehow, msbuild doesn't restore the frontend dependencies (e.g. Xamarin.Forms) when targetting // the {LINUX|MAC}_SOLUTION_FILE below, so we need this workaround. TODO: just finish migrating to MAUI(dotnet restore) NugetRestore solution @@ -337,7 +315,7 @@ let JustBuild binaryConfig maybeConstant: Frontend*FileInfo = Frontend.Console | Misc.Platform.Linux -> if FsxHelper.AreGtkLibsPresent Echo.All then - let solution = GetSolution SolutionFile.Linux + let solution = FsxHelper.GetSolution SolutionFile.Linux // somehow, msbuild doesn't restore the frontend dependencies (e.g. Xamarin.Forms) when targetting // the {LINUX|MAC}_SOLUTION_FILE below, so we need this workaround. TODO: just finish migrating to MAUI(dotnet restore) NugetRestore solution diff --git a/scripts/sanitycheck.fsx b/scripts/sanitycheck.fsx index afaf61e4d..ce315652b 100755 --- a/scripts/sanitycheck.fsx +++ b/scripts/sanitycheck.fsx @@ -421,42 +421,26 @@ let SanityCheckNugetPackages () = //let solutions = Directory.GetCurrentDirectory() |> DirectoryInfo |> findSolutions //NOTE: we hardcode the solutions rather than the line above, because e.g. Linux OS can't build/restore iOS proj - let solutionFileNames = [ - Path.Combine(FsxHelper.SourceDir.FullName, "gwallet.linux-legacy.sln") - Path.Combine(FsxHelper.SourceDir.FullName, "gwallet.mac-legacy.sln") - Path.Combine(FsxHelper.SourceDir.FullName, "gwallet.core-legacy.sln") - Path.Combine(FsxHelper.SourceDir.FullName, "gwallet.core.sln") - ] - - let solutionFiles = solutionFileNames |> List.map FileInfo - - let checkFilesExist (fileNames: List) = - fileNames - |> List.map (fun fileName -> fileName.Exists) - |> List.fold (&&) true - - let allFilesExist = checkFilesExist solutionFiles - - if not allFilesExist then - failwith "Solution files were not found to do sanity check." - - match Misc.GuessPlatform() with + let sol = + match Misc.GuessPlatform() with // xbuild cannot build .NETStandard projects so we cannot build the non-Core parts: | Misc.Platform.Linux when "dotnet" = Environment.GetEnvironmentVariable "BuildTool" -> - sanityCheckNugetPackagesFromSolution solutionFiles.[3] + FsxHelper.GetSolution SolutionFile.Default | Misc.Platform.Linux when "msbuild" = Environment.GetEnvironmentVariable "LegacyBuildTool" -> - sanityCheckNugetPackagesFromSolution solutionFiles.[0] + FsxHelper.GetSolution SolutionFile.Linux // .NET-only macOS only builds gwallet.core.sln | Misc.Platform.Mac when "dotnet" = Environment.GetEnvironmentVariable "BuildTool" && Environment.GetEnvironmentVariable "LegacyBuildTool" = "" -> - sanityCheckNugetPackagesFromSolution solutionFiles.[3] + FsxHelper.GetSolution SolutionFile.Default | Misc.Platform.Mac when "msbuild" = Environment.GetEnvironmentVariable "LegacyBuildTool" -> - sanityCheckNugetPackagesFromSolution solutionFiles.[1] + FsxHelper.GetSolution SolutionFile.Mac | _ (* stockmono linux and windows *) -> // TODO: have a windows solution file - sanityCheckNugetPackagesFromSolution solutionFiles.[2] + FsxHelper.GetSolution SolutionFile.Default + + sanityCheckNugetPackagesFromSolution sol FindOffendingPrintfUsage()