<commit_before>﻿module Paket.VSIntegration

open System.IO
open Logging
open System
open Rop
open Domain


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)
    use client = createWebClient None

    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 =
            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()
        )
    } <commit_msg>use projects from environment<commit_after>﻿module Paket.VSIntegration

open System.IO
open Logging
open System
open Rop
open Domain


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)
    use client = createWebClient None

    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

        environment.Projects
        |> List.map fst
        |> List.iter (fun project ->
            let relativePath = createRelativePath project.FileName (Path.Combine(exeDir, "paket.targets")) 
            project.AddImportForPaketTargets(relativePath)
            project.Save()
        )
    } 
{'fraction_non_alphanumeric': 0.09483793517406963, 'fraction_numerical': 0.0012004801920768306, 'mean_word_length': 2.4149590163934427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3690355', 'n_tokens_mistral': 982, 'n_tokens_neox': 913, 'n_words': 217}