Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some logic to write relative instead of absolute paths to cache, … #1950

Merged
merged 7 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/app/Fake.Runtime/CoreCache.fs
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ let findAndLoadInRuntimeDepsCached =
let result = assemblyCache.GetOrAdd(name.Name, (fun _ ->
wasCalled <- true
findAndLoadInRuntimeDeps loadContext name logLevel runtimeDependencies))
if isNull result then
if wasCalled && isNull result then
failwithf "Could not load '%A'.\nFull framework assemblies are not supported!\nYou might try to load a legacy-script with the new netcore runner.\nPlease take a look at the migration guide: https://fake.build/fake-migrate-to-fake-5.html" name
if not wasCalled then
if not wasCalled && not (isNull result) then
let loadedName = result.GetName()
let isPerfectMatch = loadedName.Name = name.Name && loadedName.Version = name.Version
if logLevel.PrintVerbose then
Expand Down Expand Up @@ -323,7 +323,7 @@ let prepareContext (config:FakeConfig) (cache:ICachingProvider) =
|> Seq.map File.ReadAllText
|> fun texts -> File.WriteAllText(fakeCacheContentsFile, String.Join("", texts))
// write fakeCacheDepsFile
File.WriteAllLines(fakeCacheDepsFile, locations)
File.WriteAllLines(fakeCacheDepsFile, locations |> Seq.map (Path.fixPathForCache config.ScriptFilePath))

let readFromCache () =
File.ReadAllText fakeCacheFile
Expand All @@ -340,6 +340,7 @@ let prepareContext (config:FakeConfig) (cache:ICachingProvider) =
let inline dependencyCacheUpdated () =
let contents =
File.ReadLines fakeCacheDepsFile
|> Seq.map (Path.readPathFromCache config.ScriptFilePath)
|> Seq.map (fun line -> if File.Exists line then Some (File.ReadAllText line) else None)
|> Seq.toList
if contents |> Seq.exists Option.isNone then false
Expand Down
1 change: 1 addition & 0 deletions src/app/Fake.Runtime/Environment.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ let environVarOrNone name =

/// Returns if the build parameter with the given name was set
let inline hasEnvironVar name = environVar name |> isNull |> not

13 changes: 7 additions & 6 deletions src/app/Fake.Runtime/FakeRuntime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open System.IO
open Fake.Runtime
open Fake.Runtime.Runners
open Paket
open System

type FakeSection =
| PaketDependencies of Paket.Dependencies * Lazy<Paket.DependenciesFile> * group : String option
Expand Down Expand Up @@ -385,7 +386,7 @@ let paketCachingProvider (config:FakeConfig) cacheDir (paketApi:Paket.Dependenci
let splits = line.Split(';')
let isRef = bool.Parse splits.[0]
let ver = splits.[1]
let loc = splits.[2]
let loc = Path.readPathFromCache config.ScriptFilePath splits.[2]
let fullName = splits.[3]
{ IsReferenceAssembly = isRef
Info =
Expand All @@ -395,12 +396,12 @@ let paketCachingProvider (config:FakeConfig) cacheDir (paketApi:Paket.Dependenci
|> Seq.toList

let writeToCache (list:AssemblyData list) =
list
list
|> Seq.map (fun item ->
sprintf "%b;%s;%s;%s"
item.IsReferenceAssembly
item.Info.Version
item.Info.Location
(Path.fixPathForCache config.ScriptFilePath item.Info.Location)
item.Info.FullName)
|> fun lines -> File.WriteAllLines(assemblyCacheFile, lines)
File.Copy(lockFilePath.FullName, assemblyCacheHashFile, true)
Expand Down Expand Up @@ -521,7 +522,7 @@ let prepareFakeScript (config:FakeConfig) =
let writeToCache (section : FakeSection option) =
match section with
| Some (PaketDependencies(p, _, group)) ->
sprintf "paket: %s, %s" p.DependenciesFile (match group with | Some g -> g | _ -> "<null>")
sprintf "paket: %s, %s" (Path.fixPathForCache config.ScriptFilePath p.DependenciesFile) (match group with | Some g -> g | _ -> "<null>")
| None -> "none"
|> fun t -> File.WriteAllText(scriptSectionCacheFile, t)
File.Copy (script, scriptSectionHashFile, true)
Expand All @@ -531,7 +532,7 @@ let prepareFakeScript (config:FakeConfig) =
if t.StartsWith("paket:") then
let s = t.Substring("paket: ".Length)
let splits = s.Split(',')
let depsFile = splits.[0]
let depsFile = Path.readPathFromCache config.ScriptFilePath splits.[0]
let group =
let trimmed = splits.[1].Trim()
if trimmed = "<null>" then None else Some trimmed
Expand Down Expand Up @@ -626,4 +627,4 @@ let retrieveHints (config:FakeConfig) (runResult:Runners.RunResult) =
yield "To further diagnose the problem you can set the 'FAKE_DETAILED_ERRORS' environment variable to 'true'"
if not config.VerboseLevel.PrintVerbose && Environment.GetEnvironmentVariable "FAKE_DETAILED_ERRORS" <> "true" then
yield "To further diagnose the problem you can run fake in verbose mode `fake -v run ...` or set the 'FAKE_DETAILED_ERRORS' environment variable to 'true'"
]
]
30 changes: 29 additions & 1 deletion src/app/Fake.Runtime/Path.fs
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
/// Contains basic functions for string manipulation.

module Fake.Runtime.Path
open System
open System.IO

// Normalizes path for different OS
let inline normalizePath (path : string) =
path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar)

let getCurrentDirectory () =
System.IO.Directory.GetCurrentDirectory()
System.IO.Directory.GetCurrentDirectory()

let private nugetDir = Path.GetFullPath(Paket.Constants.UserNuGetPackagesFolder).TrimEnd([|'/' ;'\\'|]) + string Path.DirectorySeparatorChar
let fixPathForCache scriptPath (s:string) =
let norm = Path.GetFullPath s
let scriptDir = Path.GetDirectoryName (Path.GetFullPath scriptPath) + "/"
if norm.StartsWith(nugetDir, if Paket.Utils.isWindows then StringComparison.OrdinalIgnoreCase else StringComparison.Ordinal) then
sprintf "nugetcache:///%s" (norm.Substring(nugetDir.Length).Replace("\\", "/"))
else
let scriptDir = Uri(scriptDir)
let other = Uri(norm)
let rel = scriptDir.MakeRelativeUri(other)
if rel.IsAbsoluteUri then rel.AbsoluteUri
else
sprintf "scriptpath:///%s" rel.OriginalString

let readPathFromCache scriptPath (s:string) =
let uri = Uri(s)
let scriptDir = Path.GetDirectoryName(Path.GetFullPath scriptPath)
if uri.Scheme = "nugetcache" then
let rel = uri.AbsolutePath.TrimStart [| '/'|]
Path.Combine (nugetDir, rel)
elif uri.Scheme = "scriptpath" then
let rel = uri.OriginalString.Substring("scriptpath:///".Length)
Path.Combine(scriptDir, rel)
else
uri.AbsolutePath
|> Path.GetFullPath
13 changes: 13 additions & 0 deletions src/test/Fake.Core.UnitTests/Fake.Runtime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ open Fake.IO
[<Tests>]
let tests =
testList "Fake.Runtime.Tests" [
testCase "Test that cache helpers work" <| fun _ ->
Path.fixPathForCache "build.fsx" "build.fsx"
|> Expect.equal "should detect script itself" "scriptpath:///build.fsx"
Path.readPathFromCache "build.fsx" "scriptpath:///build.fsx"
|> Expect.equal "should detect script itself" (Path.GetFullPath "build.fsx")

testCase "Test that cache helpers work for nuget cache" <| fun _ ->
let nugetLib = Paket.Constants.UserNuGetPackagesFolder </> "MyLib" </> "lib" </> "mylib.dll"
Path.fixPathForCache "build.fsx" nugetLib
|> Expect.equal "should detect script itself" "nugetcache:///MyLib/lib/mylib.dll"
Path.readPathFromCache "build.fsx" "nugetcache:///MyLib/lib/mylib.dll"
|> Expect.equal "should detect script itself" nugetLib

testCase "Test that we can properly find type names when the file name contains '.'" <| fun _ ->
// Add test if everything works with __SOURCE_FILE__
let name, parser =
Expand Down
1 change: 1 addition & 0 deletions src/test/Fake.Core.UnitTests/paket.references
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
group netcore

Paket.Core
FSharp.Core
NETStandard.Library
Expecto