From 7b915d65f7b65a22b21c346ebc3eb14202627dbb Mon Sep 17 00:00:00 2001 From: Matthias Dittrich Date: Sat, 12 May 2018 20:10:16 +0200 Subject: [PATCH] Implement https://github.com/fsharp/FAKE/issues/1926 --- help/markdown/fake-commandline.md | 6 +++ src/app/Fake.Runtime/FakeRuntime.fs | 59 +++++++++++++++++------------ src/app/Fake.Runtime/Runners.fs | 1 + src/app/Fake.netcore/Cli.fs | 6 +++ src/app/Fake.netcore/Program.fs | 5 ++- 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/help/markdown/fake-commandline.md b/help/markdown/fake-commandline.md index 075d4bcc3eb..7f17241d1c1 100644 --- a/help/markdown/fake-commandline.md +++ b/help/markdown/fake-commandline.md @@ -21,11 +21,17 @@ Fake Options [fake_opts]: Fake Run Options [run_opts]: -d, --debug Debug the script. -n, --nocache Disable fake cache for this run. + -p, --partial-restore + Only restore the required group instead of a full restore, + can be set globally by setting the environment variable FAKE_PARTIAL_RESTORE to true. --fsiargs [*] Arguments passed to the f# interactive. Fake Build Options [build_opts]: -d, --debug Debug the script. -n, --nocache Disable fake cache for this run. + -p, --partial-restore + Only restore the required group instead of a full restore, + can be set globally by setting the environment variable FAKE_PARTIAL_RESTORE to true. --fsiargs [*] Arguments passed to the f# interactive. -f, --script The script to execute (defaults to `build.fsx`). diff --git a/src/app/Fake.Runtime/FakeRuntime.fs b/src/app/Fake.Runtime/FakeRuntime.fs index 15262573de1..87c21369883 100644 --- a/src/app/Fake.Runtime/FakeRuntime.fs +++ b/src/app/Fake.Runtime/FakeRuntime.fs @@ -3,6 +3,7 @@ open System open System.IO open Fake.Runtime +open Fake.Runtime.Runners open Paket type FakeSection = @@ -134,8 +135,10 @@ type AssemblyData = { IsReferenceAssembly : bool Info : Runners.AssemblyInfo } -let paketCachingProvider (script:string) (logLevel:Trace.VerboseLevel) cacheDir (paketApi:Paket.Dependencies) (paketDependenciesFile:Lazy) group = +let paketCachingProvider (config:FakeConfig) cacheDir (paketApi:Paket.Dependencies) (paketDependenciesFile:Lazy) group = use __ = Fake.Profile.startCategory Fake.Profile.Category.Paket + let logLevel = config.VerboseLevel + let script = config.ScriptFilePath let groupStr = match group with Some g -> g | None -> "Main" let groupName = Paket.Domain.GroupName (groupStr) #if DOTNETCORE @@ -359,8 +362,10 @@ let paketCachingProvider (script:string) (logLevel:Trace.VerboseLevel) cacheDir if needLocalLock then File.Copy(lockFilePath.FullName, localLock, true) // Restore - paketApi.Restore((*false, group, [], false, true*)) - |> ignore + if config.RestoreOnlyGroup then + paketApi.Restore(false,group,[],false,false,false,None) + else + paketApi.Restore() // https://github.com/fsharp/FAKE/issues/1908 writeIntellisenseFile cacheDir // write intellisense.fsx immediately @@ -463,10 +468,10 @@ let paketCachingProvider (script:string) (logLevel:Trace.VerboseLevel) cacheDir if writeIntellisenseTask.IsValueCreated then writeIntellisenseTask.Value.Wait() } -let restoreDependencies script logLevel cacheDir section = +let restoreDependencies config cacheDir section = match section with | PaketDependencies (paketDependencies, paketDependenciesFile, group) -> - paketCachingProvider script logLevel cacheDir paketDependencies paketDependenciesFile group + paketCachingProvider config cacheDir paketDependencies paketDependenciesFile group let tryFindGroupFromDepsFile scriptDir = let depsFile = Path.Combine(scriptDir, "paket.dependencies") @@ -498,8 +503,9 @@ let tryFindGroupFromDepsFile scriptDir = | _ -> None else None -let prepareFakeScript (tokenized:Lazy) logLevel script = - // read dependencies from the top +let prepareFakeScript (config:FakeConfig) = + // read dependencies from the top + let script = config.ScriptFilePath let scriptDir = Path.GetDirectoryName (script) let cacheDir = Path.Combine(scriptDir, ".fake", Path.GetFileName(script)) Directory.CreateDirectory (cacheDir) |> ignore @@ -508,7 +514,7 @@ let prepareFakeScript (tokenized:Lazy let scriptSectionCacheFile = Path.Combine(cacheDir, "fake-section.txt") let inline getSectionUncached () = use __ = Fake.Profile.startCategory Fake.Profile.Category.Analyzing - let newSection = tryReadPaketDependenciesFromScript tokenized.Value cacheDir script + let newSection = tryReadPaketDependenciesFromScript config.ScriptTokens.Value cacheDir script match newSection with | Some s -> Some s | None -> @@ -551,7 +557,7 @@ let prepareFakeScript (tokenized:Lazy match section with | Some section -> - restoreDependencies script logLevel cacheDir section + restoreDependencies config cacheDir section | None -> let defaultPaketCode = """ source https://api.nuget.org/v3/index.json @@ -567,10 +573,10 @@ If you know what you are doing you can silence this warning by setting the envir { Header = "paket-inline" Section = defaultPaketCode } |> writeFixedPaketDependencies cacheDir - restoreDependencies script logLevel cacheDir section + restoreDependencies config cacheDir section -let prepareAndRunScriptRedirect (logLevel:Trace.VerboseLevel) (fsiOptions:string list) scriptPath scriptArgs onErrMsg onOutMsg useCache = +let createConfig (logLevel:Trace.VerboseLevel) (fsiOptions:string list) scriptPath scriptArgs onErrMsg onOutMsg useCache restoreOnlyGroup = if logLevel.PrintVerbose then Trace.log (sprintf "prepareAndRunScriptRedirect(Script: %s, fsiOptions: %A)" scriptPath (System.String.Join(" ", fsiOptions))) let fsiOptionsObj = Yaaf.FSharp.Scripting.FsiOptions.ofArgs fsiOptions let newFsiOptions = @@ -584,19 +590,22 @@ let prepareAndRunScriptRedirect (logLevel:Trace.VerboseLevel) (fsiOptions:string use out = Yaaf.FSharp.Scripting.ScriptHost.CreateForwardWriter onOutMsg use err = Yaaf.FSharp.Scripting.ScriptHost.CreateForwardWriter onErrMsg let tokenized = lazy (File.ReadLines scriptPath |> FSharpParser.getTokenized scriptPath ("FAKE_DEPENDENCIES" :: newFsiOptions.Defines)) - let config = - { Runners.FakeConfig.VerboseLevel = logLevel - Runners.FakeConfig.ScriptFilePath = scriptPath - Runners.FakeConfig.ScriptTokens = tokenized - Runners.FakeConfig.CompileOptions = - { FsiOptions = newFsiOptions; RuntimeDependencies = [] } - Runners.FakeConfig.UseCache = useCache - Runners.FakeConfig.Out = out - Runners.FakeConfig.Err = err - Runners.FakeConfig.ScriptArgs = scriptArgs } - let provider = prepareFakeScript tokenized logLevel scriptPath - CoreCache.runScriptWithCacheProvider config provider -let inline prepareAndRunScript logLevel fsiOptions scriptPath scriptArgs useCache = - prepareAndRunScriptRedirect logLevel fsiOptions scriptPath scriptArgs (printf "%s") (printf "%s") useCache + { Runners.FakeConfig.VerboseLevel = logLevel + Runners.FakeConfig.ScriptFilePath = scriptPath + Runners.FakeConfig.ScriptTokens = tokenized + Runners.FakeConfig.CompileOptions = + { FsiOptions = newFsiOptions; RuntimeDependencies = [] } + Runners.FakeConfig.UseCache = useCache + Runners.FakeConfig.RestoreOnlyGroup = restoreOnlyGroup + Runners.FakeConfig.Out = out + Runners.FakeConfig.Err = err + Runners.FakeConfig.ScriptArgs = scriptArgs } + +let createConfigSimple (logLevel:Trace.VerboseLevel) (fsiOptions:string list) scriptPath scriptArgs useCache restoreOnlyGroup = + createConfig logLevel fsiOptions scriptPath scriptArgs (printf "%s") (printf "%s") useCache restoreOnlyGroup + +let prepareAndRunScript (config:FakeConfig) = + let provider = prepareFakeScript config + CoreCache.runScriptWithCacheProvider config provider diff --git a/src/app/Fake.Runtime/Runners.fs b/src/app/Fake.Runtime/Runners.fs index 519850e0186..5da9934c278 100644 --- a/src/app/Fake.Runtime/Runners.fs +++ b/src/app/Fake.Runtime/Runners.fs @@ -43,6 +43,7 @@ type FakeConfig = ScriptTokens : Lazy CompileOptions : CompileOptions UseCache : bool + RestoreOnlyGroup : bool Out: TextWriter Err: TextWriter ScriptArgs: string list } diff --git a/src/app/Fake.netcore/Cli.fs b/src/app/Fake.netcore/Cli.fs index b5716ececea..bbcbeaa4ee9 100644 --- a/src/app/Fake.netcore/Cli.fs +++ b/src/app/Fake.netcore/Cli.fs @@ -23,11 +23,17 @@ Fake Options [fake_opts]: Fake Run Options [run_opts]: -d, --debug Debug the script. -n, --nocache Disable fake cache for this run. + -p, --partial-restore + Only restore the required group instead of a full restore, + can be set globally by setting the environment variable FAKE_PARTIAL_RESTORE to true. --fsiargs [*] Arguments passed to the f# interactive. Fake Build Options [build_opts]: -d, --debug Debug the script. -n, --nocache Disable fake cache for this run. + -p, --partial-restore + Only restore the required group instead of a full restore, + can be set globally by setting the environment variable FAKE_PARTIAL_RESTORE to true. --fsiargs [*] Arguments passed to the f# interactive. -f, --script The script to execute (defaults to `build.fsx`). diff --git a/src/app/Fake.netcore/Program.fs b/src/app/Fake.netcore/Program.fs index 1e5c19c5dfc..e1c01004a8b 100644 --- a/src/app/Fake.netcore/Program.fs +++ b/src/app/Fake.netcore/Program.fs @@ -74,6 +74,7 @@ type RunArguments = { Debug : bool //SingleTarget : bool NoCache : bool + RestoreOnlyGroup : bool VerboseLevel : VerboseLevel IsBuild : bool // Did the user call `fake build` or `fake run`? } @@ -149,7 +150,8 @@ let runOrBuild (args : RunArguments) = let useCache = not args.NoCache try - if not (FakeRuntime.prepareAndRunScript args.VerboseLevel additionalArgs scriptFile args.ScriptArguments useCache) then false + let config = FakeRuntime.createConfigSimple args.VerboseLevel additionalArgs scriptFile args.ScriptArguments useCache args.RestoreOnlyGroup + if not (FakeRuntime.prepareAndRunScript config) then false else if args.VerboseLevel.PrintVerbose then log "Ready." true @@ -237,6 +239,7 @@ let parseAction (results:DocoptMap) = Debug = DocoptResult.hasFlag "--debug" results NoCache = DocoptResult.hasFlag "--nocache" results + RestoreOnlyGroup = DocoptResult.hasFlag "--partial-restore" results || Environment.GetEnvironmentVariable ("FAKE_PARTIAL_RESTORE") = "true" VerboseLevel = verboseLevel IsBuild = not isRun // Did the user call `fake build` or `fake run`? }