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

Rop init & init auto restore command #533

Merged
merged 7 commits into from
Jan 9, 2015
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 11 additions & 0 deletions src/Paket.Core/Domain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ type DomainMessage =
| DependencyNotFoundInLockFile of PackageName
| ReferenceNotFoundInLockFile of string * PackageName

| DownloadError of string
| ReleasesJsonParseError
| FileDeleteError of string

override this.ToString() =
match this with
| DirectoryDoesntExist(di) ->
Expand Down Expand Up @@ -88,3 +92,10 @@ type DomainMessage =
sprintf "Dependency %s from %s not found in lock file." name Constants.DependenciesFileName
| ReferenceNotFoundInLockFile(path, PackageName name) ->
sprintf "Reference %s from %s not found in lock file." name path

| DownloadError url ->
sprintf "Error occured while downloading from %s." url
| ReleasesJsonParseError ->
"Unable to parse Json from GitHub releases API."
| FileDeleteError path ->
sprintf "Unable to delete file %s." path
11 changes: 11 additions & 0 deletions src/Paket.Core/Environment.fs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,14 @@ module PaketEnv =
let ensureLockFileExists environment =
environment.LockFile
|> failIfNone (LockFileNotFound environment.RootDirectory)

let init (directory : DirectoryInfo) =
match locatePaketRootDirectory directory with
| Some rootDirectory ->
Logging.tracefn "Paket is already initialized in %s" rootDirectory.FullName
| None ->
let dependenciesFile =
DependenciesFile(
Path.Combine(directory.FullName, Constants.DependenciesFileName),
InstallOptions.Default, [], [], [])
dependenciesFile.Save()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this Save call can fail. For example insufficient rights to create a file.

5 changes: 2 additions & 3 deletions src/Paket.Core/NugetConvert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,8 @@ let replaceNugetWithPaket initAutoRestore installAfter result =
result.NugetEnv.NugetConfig.PackageRestoreAutomatic &&
result.NugetEnv.NugetConfig.PackageRestoreEnabled

let dependenciesFileName = result.PaketEnv.DependenciesFile.FileName
if initAutoRestore && (autoVSPackageRestore || result.NugetEnv.NugetTargets.IsSome) then
VSIntegration.InitAutoRestore dependenciesFileName
VSIntegration.InitAutoRestore result.PaketEnv |> returnOrFail

if installAfter then
UpdateProcess.Update(dependenciesFileName,true,true,true)
UpdateProcess.Update(result.PaketEnv.DependenciesFile.FileName,true,true,true)
25 changes: 11 additions & 14 deletions src/Paket.Core/PublicAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,16 @@ type Dependencies(dependenciesFileName: string) =
let dependenciesFileName = findInPath(DirectoryInfo path,true)
tracefn "found: %s" dependenciesFileName
Dependencies(dependenciesFileName)

/// Tries to create a paket.dependencies file in the given folder.
static member Create(): Dependencies = Dependencies.Create(Environment.CurrentDirectory)

/// Tries to create a paket.dependencies file in the given folder.
static member Create(path: string): Dependencies =
let dependenciesFileName = Path.Combine(path,Constants.DependenciesFileName)
if File.Exists dependenciesFileName then
Logging.tracefn "%s already exists" dependenciesFileName
else
DependenciesFile(dependenciesFileName, InstallOptions.Default, [], [], []).Save()
Dependencies(dependenciesFileName)


/// Initialize paket.dependencies file in current directory
static member Init() =
let currentDirectory = DirectoryInfo(Environment.CurrentDirectory)

Utils.RunInLockedAccessMode(
currentDirectory.FullName,
fun () -> PaketEnv.init currentDirectory
)

/// Converts the solution from NuGet to Paket.
static member ConvertFromNuget(force: bool,installAfter: bool,initAutoRestore: bool,credsMigrationMode: string option) : unit =
let currentDirectory = DirectoryInfo(Environment.CurrentDirectory)
Expand Down Expand Up @@ -163,7 +160,7 @@ type Dependencies(dependenciesFileName: string) =
member this.InitAutoRestore(): unit =
Utils.RunInLockedAccessMode(
this.RootPath,
fun () -> VSIntegration.InitAutoRestore(dependenciesFileName))
fun () -> VSIntegration.InitAutoRestore |> this.Process)

/// Returns the installed version of the given package.
member this.GetInstalledVersion(packageName: string): string option =
Expand Down
23 changes: 22 additions & 1 deletion src/Paket.Core/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ open System.Net
open System.Xml
open System.Text
open Paket.Logging
open Paket.Rop
open Paket.Domain

type Auth =
{ Username : string
Expand Down Expand Up @@ -245,4 +247,23 @@ module String =
let inline orElse v =
function
| Some x -> Some x
| None -> v
| None -> v

let downloadStringSync (url : string) (client : System.Net.WebClient) =
try
client.DownloadString url |> succeed
with _ ->
DownloadError url |> fail

let downloadFileSync (url : string) (fileName : string) (client : System.Net.WebClient) =
tracefn "Downloading file from %s to %s" url fileName
try
client.DownloadFile(url, fileName) |> succeed
with _ ->
DownloadError url |> fail

let deleteFile (fileName : string) =
try
File.Delete fileName |> succeed
with _ ->
FileDeleteError fileName |> fail
75 changes: 43 additions & 32 deletions src/Paket.Core/VSIntegration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,50 @@
open System.IO
open Logging
open System
open Rop
open Domain

let InitAutoRestore(dependenciesFileName) =
let root =
if dependenciesFileName = Constants.DependenciesFileName then
"."
else
Path.GetDirectoryName dependenciesFileName

CreateDir(Path.Combine(root,".paket"))
let getLatestVersionFromJson (data : string) =
try
let start = data.IndexOf("tag_name") + 11
let end' = data.IndexOf("\"", start)
(data.Substring(start, end' - start)) |> SemVer.Parse |> succeed
with _ ->
fail ReleasesJsonParseError

let InitAutoRestore environment =
let exeDir = Path.Combine(environment.RootDirectory.FullName, ".paket")
CreateDir(exeDir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one can also fail due to permissions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw. I don't like to have this one explicitly called, for me it should be part of download function.
No one needs this directory to be present besides the download, as it would fail.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for comments, i'll apply changes

use client = createWebClient None

let releasesUrl = "https://api.github.com/repos/fsprojects/Paket/releases";
let data = client.DownloadString(releasesUrl)
let start = data.IndexOf("tag_name") + 11
let end' = data.IndexOf("\"", start)
let latestVersion = data.Substring(start, end' - start);

for file in ["paket.targets"; "paket.bootstrapper.exe"] do
try
File.Delete(Path.Combine(root, ".paket", file))
with _ -> traceErrorfn "Unable to delete %s" file
try
client.DownloadFile(sprintf "https://github.com/fsprojects/Paket/releases/download/%s/%s" latestVersion file,
Path.Combine(root, ".paket", file))
tracefn "Downloaded %s" file
with _ -> traceErrorfn "Unable to download %s for version %s" file latestVersion

let projectsUnderPaket =
ProjectFile.FindAllProjects root
|> Array.filter (fun project -> ProjectFile.FindReferencesFile(FileInfo(project.FileName)).IsSome)

for project in projectsUnderPaket do
let relativePath =
createRelativePath project.FileName (Path.Combine(root, ".paket", "paket.targets"))
project.AddImportForPaketTargets(relativePath)
project.Save()
let download version file =
rop {
let fileName = Path.Combine(exeDir, file)
do! deleteFile fileName
let url = sprintf "https://github.com/fsprojects/Paket/releases/download/%s/%s" (string version) file
do! downloadFileSync url fileName client
}

rop {
let releasesUrl = "https://api.github.com/repos/fsprojects/Paket/releases";
let! data = client |> downloadStringSync releasesUrl
let! latestVersion = getLatestVersionFromJson data

let! downloads =
["paket.targets"; "paket.bootstrapper.exe"]
|> List.map (download latestVersion)
|> collect

let projectsUnderPaket =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't the projects already in environment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good spot! I totally forgot about it

ProjectFile.FindAllProjects environment.RootDirectory.FullName
|> Array.filter (fun project -> ProjectFile.FindReferencesFile(FileInfo(project.FileName)).IsSome)

projectsUnderPaket
|> Array.iter (fun project ->
let relativePath =
createRelativePath project.FileName (Path.Combine(exeDir, "paket.targets"))
project.AddImportForPaketTargets(relativePath)
project.Save()
)
}
2 changes: 1 addition & 1 deletion src/Paket/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ try

else
match command with
| Command.Init -> Dependencies.Create() |> ignore
| Command.Init -> Dependencies.Init()
| Command.Add ->
let packageName = results.GetResult <@ CLIArguments.Nuget @>
let version =
Expand Down