Skip to content

Commit

Permalink
find-package-versions shouldn't require paket.dependencies to be pres…
Browse files Browse the repository at this point in the history
…ent as long as a source is explicitly specified.
  • Loading branch information
fsoikin committed Jul 1, 2017
1 parent cdbeaa0 commit 7ddc57b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
44 changes: 24 additions & 20 deletions src/Paket.Core/PublicAPI.fs
Expand Up @@ -31,8 +31,11 @@ type Dependencies(dependenciesFileName: string) =
emptyDir (Constants.NuGetCacheFolder)
emptyDir (Constants.GitRepoCacheFolder)

/// Tries to locate the paket.dependencies file in the current folder or a parent folder.
/// Tries to locate the paket.dependencies file in the current folder or a parent folder, throws an exception if unsuccessful.
static member Locate(): Dependencies = Dependencies.Locate(Directory.GetCurrentDirectory())

/// Tries to locate the paket.dependencies file in the current folder or a parent folder.
static member TryLocate(): Dependencies option = Dependencies.TryLocate(Directory.GetCurrentDirectory())

/// Returns an instance of the paket.lock file.
member this.GetLockFile() =
Expand All @@ -42,27 +45,28 @@ type Dependencies(dependenciesFileName: string) =
/// Returns an instance of the paket.dependencies file.
member this.GetDependenciesFile() = DependenciesFile.ReadFromFile dependenciesFileName

/// Tries to locate the paket.dependencies file in the given folder or a parent folder.
/// Tries to locate the paket.dependencies file in the given folder or a parent folder, throws an exception if unsuccessful.
static member Locate(path: string): Dependencies =
let rec findInPath(dir:DirectoryInfo,withError) =
let path = Path.Combine(dir.FullName,Constants.DependenciesFileName)
if File.Exists(path) then
path
else
let parent = dir.Parent
match parent with
| null ->
if withError then
failwithf "Could not find '%s'. To use Paket with this solution, please run 'paket init' first.%sIf you have already run 'paket.init' then ensure that '%s' is located in the top level directory of your repository.%sLike this:%sMySourceDir%s .paket%s paket.dependencies"
Constants.DependenciesFileName Environment.NewLine Constants.DependenciesFileName Environment.NewLine Environment.NewLine Environment.NewLine Environment.NewLine
else
Constants.DependenciesFileName
| _ -> findInPath(parent, withError)
match Dependencies.TryLocate path with
| None ->
failwithf "Could not find '%s'. To use Paket with this solution, please run 'paket init' first.%sIf you have already run 'paket.init' then ensure that '%s' is located in the top level directory of your repository.%sLike this:%sMySourceDir%s .paket%s paket.dependencies"
Constants.DependenciesFileName Environment.NewLine Constants.DependenciesFileName Environment.NewLine Environment.NewLine Environment.NewLine Environment.NewLine
| Some d ->
d

let dependenciesFileName = findInPath(DirectoryInfo path,true)
if verbose then
verbosefn "found: %s" dependenciesFileName
Dependencies(dependenciesFileName)
/// Tries to locate the paket.dependencies file in the given folder or a parent folder.
static member TryLocate(path: string): Dependencies option =
let rec findInPath(dir:DirectoryInfo) =
let path = Path.Combine(dir.FullName,Constants.DependenciesFileName)
match File.Exists path, dir.Parent with
| true, _ -> Some path
| false, null -> None
| false, parent -> findInPath parent

findInPath (DirectoryInfo path)
|> Option.map (fun dependenciesFileName ->
if verbose then verbosefn "found: %s" dependenciesFileName
Dependencies(dependenciesFileName))

/// Initialize paket.dependencies file in current directory
static member Init() = Dependencies.Init(Directory.GetCurrentDirectory())
Expand Down
26 changes: 19 additions & 7 deletions src/Paket/Program.fs
Expand Up @@ -370,17 +370,29 @@ let showGroups (results : ParseResults<ShowGroupsArgs>) =

let findPackageVersions (results : ParseResults<_>) =
let maxResults = defaultArg (results.TryGetResult <@ FindPackageVersionsArgs.MaxResults @>) 10000
let dependencies = Dependencies.Locate()
let dependencies = Dependencies.TryLocate()
let name =
match results.TryGetResult <@ FindPackageVersionsArgs.NuGet @> with
| Some name -> name
| None -> results.GetResult <@ FindPackageVersionsArgs.Name @>
let sources =
match results.TryGetResult <@ FindPackageVersionsArgs.Source @> with
| Some source -> [PackageSource.NuGetV2Source source]
| _ -> dependencies.GetSources() |> Seq.map (fun kv -> kv.Value) |> List.concat

for p in dependencies.FindPackageVersions(sources,name,maxResults) do
let sources =
match results.TryGetResult <@ FindPackageVersionsArgs.Source @>, dependencies with
| Some source, _ ->
[PackageSource.NuGetV2Source source]
| _, Some dependencies ->
dependencies.GetSources() |> Seq.map (fun kv -> kv.Value) |> List.concat
| _ ->
failwithf "Could not find '%s' at or above current directory, and no explicit source was given as parameter (e.g. 'paket.exe find-package-versions source https://www.nuget.org/api/v2')."
Constants.DependenciesFileName
let root =
match dependencies with
| Some d ->
d.RootPath
| None ->
traceWarnfn "Could not find '%s' at or above current directory. Using current directory as project root." Constants.DependenciesFileName
Directory.GetCurrentDirectory()

for p in Dependencies.FindPackageVersions(root,sources,name,maxResults) do
tracefn "%s" p

let push (results : ParseResults<_>) =
Expand Down

0 comments on commit 7ddc57b

Please sign in to comment.