Skip to content

Commit

Permalink
support for custom link path in a project - references fsprojects#111
Browse files Browse the repository at this point in the history
  • Loading branch information
theimowski committed Oct 5, 2014
1 parent 9c73b1b commit 4602436
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 46 deletions.
70 changes: 31 additions & 39 deletions src/Paket/InstallProcess.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,15 @@ let private findPackagesWithContent (usedPackages:Dictionary<_,_>) =

let private copyContentFiles (project : ProjectFile, packagesWithContent) =

let onBlackList (fi : FileInfo) =
let rules : list<(FileInfo -> bool)> = [
let rules : list<(FileInfo -> bool)> = [
fun f -> f.Name = "_._"
fun f -> f.Name.EndsWith(".transform")
fun f -> f.Name.EndsWith(".pp")
fun f -> f.Name.EndsWith(".tt")
fun f -> f.Name.EndsWith(".ttinclude")
]
rules
|> List.exists (fun rule -> rule(fi))

let onBlackList (fi : FileInfo) = rules |> List.exists (fun rule -> rule(fi))

let rec copyDirContents (fromDir : DirectoryInfo, toDir : Lazy<DirectoryInfo>) =
fromDir.GetDirectories() |> Array.toList
Expand Down Expand Up @@ -119,13 +118,6 @@ let private removeCopiedFiles (project: ProjectFile) =
|> List.filter (fun fi -> not <| fi.FullName.Contains("paket-files"))
|> removeFilesAndTrimDirs

let extractReferencesFromListFile projectFile =
match ProjectFile.FindReferencesFile <| FileInfo(projectFile) with
| Some file -> File.ReadAllLines file
| None -> [||]
|> Array.map (fun s -> s.Trim())
|> Array.filter (fun s -> System.String.IsNullOrWhiteSpace s |> not)


/// Installs the given packageFile.
let Install(sources,force, hard, lockFile:LockFile) =
Expand All @@ -136,13 +128,15 @@ let Install(sources,force, hard, lockFile:LockFile) =
|> Async.RunSynchronously
|> Array.choose id

for project in ProjectFile.FindAllProjects(".") do
let applicableProjects =
ProjectFile.FindAllProjects(".")
|> List.choose (fun p -> ProjectFile.FindReferencesFile (FileInfo(p.FileName))
|> Option.map (fun r -> p, ReferencesFile.FromFile(r)))

for project,referenceFile in applicableProjects do
verbosefn "Installing to %s" project.FileName
let directPackages = extractReferencesFromListFile project.FileName

if directPackages |> Array.isEmpty |> not then verbosefn " - direct packages: %A" directPackages
let usedPackages = new Dictionary<_,_>()
let usedSourceFiles = new HashSet<_>()

let allPackages =
extractedPackages
Expand All @@ -151,40 +145,38 @@ let Install(sources,force, hard, lockFile:LockFile) =

let rec addPackage directly (name:string) =
let identity = name.ToLower()
if identity.StartsWith "file:" then
let sourceFile = name.Split(':').[1]
usedSourceFiles.Add sourceFile |> ignore
else
match allPackages |> Map.tryFind identity with
| Some package ->
match usedPackages.TryGetValue name with
| false,_ ->
usedPackages.Add(name,directly)
if not lockFile.Options.Strict then
for d,_ in package.Dependencies do
addPackage false d
| true,v -> usedPackages.[name] <- v || directly
| None -> failwithf "Project %s references package %s, but it was not found in the paket.lock file." project.FileName name

directPackages
|> Array.iter (addPackage true)
match allPackages |> Map.tryFind identity with
| Some package ->
match usedPackages.TryGetValue name with
| false,_ ->
usedPackages.Add(name,directly)
if not lockFile.Options.Strict then
for d,_ in package.Dependencies do
addPackage false d
| true,v -> usedPackages.[name] <- v || directly
| None -> failwithf "Project %s references package %s, but it was not found in the paket.lock file." project.FileName name

referenceFile.NugetPackages
|> List.iter (addPackage true)

project.UpdateReferences(extractedPackages,usedPackages,hard)

removeCopiedFiles project

let getGitHubFilePath name =
(lockFile.SourceFiles |> List.find (fun f -> Path.GetFileName(f.Name) = name)).FilePath

let gitHubFileItems =
lockFile.SourceFiles
|> List.filter (fun file -> usedSourceFiles.Contains(file.Name))
referenceFile.GitHubFiles
|> List.map (fun file ->
{ BuildAction = project.DetermineBuildAction file.Name
Include = createRelativePath project.FileName file.FilePath
Link = Some("paket-files/" + file.Name) })
Include = createRelativePath project.FileName (getGitHubFilePath file.Name)
Link = Some(Path.Combine(file.Link, Path.GetFileName(file.Name))) })

let nuGetFileItems =
if not lockFile.Options.OmitContent then
let packagesWithContent = findPackagesWithContent usedPackages
let files = copyContentFiles(project, packagesWithContent)
if not lockFile.Options.OmitContent
then
let files = copyContentFiles(project, findPackagesWithContent usedPackages)
files |> List.map (fun file ->
{ BuildAction = project.DetermineBuildAction file.Name
Include = createRelativePath project.FileName file.FullName
Expand Down
4 changes: 2 additions & 2 deletions src/Paket/Paket.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<DocumentationFile>..\..\bin\Paket.xml</DocumentationFile>
<StartArguments>outdated</StartArguments>
<StartArguments>update</StartArguments>
<StartAction>Project</StartAction>
<StartProgram>paket.exe</StartProgram>
<StartWorkingDirectory>
</StartWorkingDirectory>
<StartWorkingDirectory>D:\code\Paket</StartWorkingDirectory>
<StartWorkingDirectory>c:\sandbox\Solution2</StartWorkingDirectory>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
3 changes: 3 additions & 0 deletions src/Paket/ProjectFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ type ProjectFile =
for newItemGroup in newItemGroups.Values do
if newItemGroup.HasChildNodes then this.ProjectNode.AppendChild(newItemGroup) |> ignore

this.DeleteIfEmpty("//ns:ItemGroup")


member this.UpdateReferences(extracted, usedPackages : Dictionary<string,bool>, hard) =
this.DeletePaketNodes("Reference")
let installInfos = InstallRules.groupDLLs usedPackages extracted this.FileName
Expand Down
20 changes: 17 additions & 3 deletions src/Paket/ReferencesFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@ open System
open System.IO
open Logging

type GitHubReference =
{ Name : string
Link : string }

type ReferencesFile =
{ FileName: string
NugetPackages: list<string>
GitHubFiles: list<string> }
GitHubFiles: list<GitHubReference> }

static member DefaultLink = "paket-files"

static member FromLines(lines : string[]) =
let isGitHubFile (line: string) = line.StartsWith "File:"
let notEmpty (line: string) = not <| System.String.IsNullOrWhiteSpace line

{ FileName = ""
NugetPackages = lines |> Array.filter notEmpty |> Array.filter (isGitHubFile >> not) |> Array.toList
GitHubFiles = lines |> Array.filter notEmpty |> Array.filter isGitHubFile |> Array.map (fun s -> s.Replace("File:","")) |> Array.toList }
GitHubFiles =
lines
|> Array.filter notEmpty
|> Array.filter isGitHubFile
|> Array.map (fun s -> s.Replace("File:","").Split([|' '|], StringSplitOptions.RemoveEmptyEntries))
|> Array.map (fun segments -> { Name = segments.[0]; Link = if segments.Length = 2
then segments.[1]
else ReferencesFile.DefaultLink} )
|> Array.toList }

static member FromFile(fileName : string) =
let lines = File.ReadAllLines(fileName)
Expand All @@ -32,5 +46,5 @@ type ReferencesFile =
override this.ToString() =
List.append
this.NugetPackages
(this.GitHubFiles |> List.map (fun s -> "File:" + s))
(this.GitHubFiles |> List.map (fun s -> "File:" + s.Name + if s.Link <> ReferencesFile.DefaultLink then " " + s.Link else ""))
|> String.concat Environment.NewLine
24 changes: 22 additions & 2 deletions tests/Paket.Tests/ReferencesFile/ReferencesFileSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,31 @@ let ``should parse lines correctly``() =
refFile.NugetPackages.Head |> shouldEqual "Castle.Windsor"
refFile.NugetPackages.Tail.Tail.Head |> shouldEqual "jQuery"
refFile.GitHubFiles.Length |> shouldEqual 1
refFile.GitHubFiles.Head |> shouldEqual "FsUnit.fs"
refFile.GitHubFiles.Head.Name |> shouldEqual "FsUnit.fs"
refFile.GitHubFiles.Head.Link |> shouldEqual "paket-files"

[<Test>]
let ``should serialize itself correctly``() =
let refFile = {FileName = ""; NugetPackages = ["A"; "B"]; GitHubFiles = ["FromGithub.fs"]}
let refFile = {FileName = ""; NugetPackages = ["A"; "B"]; GitHubFiles = [{Name = "FromGithub.fs"; Link = ReferencesFile.DefaultLink}]}
let expected = [|"A"; "B"; "File:FromGithub.fs"|]

refFile.ToString() |> toLines |> shouldEqual expected

let refFileWithCustomPath = """
File:FsUnit.fs Tests\Common
"""

[<Test>]
let ``should parse custom path correctly``() =
let refFile = ReferencesFile.FromLines(toLines refFileWithCustomPath)
refFile.NugetPackages.Length |> shouldEqual 0
refFile.GitHubFiles.Length |> shouldEqual 1
refFile.GitHubFiles.Head.Name |> shouldEqual "FsUnit.fs"
refFile.GitHubFiles.Head.Link |> shouldEqual "Tests\Common"

[<Test>]
let ``should serialize customPath correctly``() =
let refFile = {FileName = ""; NugetPackages = []; GitHubFiles = [{Name = "FromGithub.fs"; Link = "CustomPath\Dir"}]}
let expected = [|"File:FromGithub.fs CustomPath\Dir"|]

refFile.ToString() |> toLines |> shouldEqual expected

0 comments on commit 4602436

Please sign in to comment.