<commit_before>#load "Utils.fsx"
#load "BookInfos.fsx"

open System
open System.IO
open System.Collections.Generic
open Utils
open BookInfos
open BookConfig

let (template : Printf.StringFormat<_>) = "---
layout: post
title: \"%s\"
author: \"%s\"
isbn: %s
editor: %s
---

![Couverture](/img/%s)%s"

module Main = 

    let PostsFolder = "site/_posts/"
    let ImagesFolder = "site/img/"

    let private generatePost (bookConfig : BookConfig) = 
        if bookConfig.Generated then 
            bookConfig
        else
            let book = BookInfos.GetBookInfo bookConfig
            BookInfos.Check book
            printfn "\tdata found"
            File.WriteAllText
                (sprintf "%s%s-%s.md" PostsFolder book.ReadingDate (book.Title |> Utils.slugify), 
                 sprintf template book.Title book.Author book.Isbn book.Editor 
                     (Utils.downloadImageToSite book.ImageUrl book.Isbn ImagesFolder) book.Summary)
            new BookConfig(bookConfig.Isbn, bookConfig.Date, true)

    // forced: deploy will be executed even if there is no book page to generate
    let isDeployForced _ =
        let commitMessage = Environment.GetEnvironmentVariable("APPVEYOR_REPO_COMMIT_MESSAGE")
        let forcedBuild = Environment.GetEnvironmentVariable("APPVEYOR_FORCED_BUILD")
        (not (isNull commitMessage) && commitMessage.ToLowerInvariant().Contains("[force]") )
                || (not (isNull forcedBuild) && forcedBuild.ToLowerInvariant() = "true")

    let commitGeneratedPosts () =
        execProcessWithFail "git" "checkout master"
        execProcessWithFail "git" ("remote add sshorigin https://" + Environment.GetEnvironmentVariable("access_token") + ":x-oauth-basic@github.com/laedit/ReadingList.git")
        execProcessWithFail "git" "config --global credential.helper store"
        use sw = File.AppendText(Environment.GetEnvironmentVariable("USERPROFILE") + ".git-credentials")
        sw.Write("https://" + Environment.GetEnvironmentVariable("access_token") + ":x-oauth-basic@github.com");
        sw.Write("\n");
        execProcessWithFail "git" "config --global user.name \"<NAME>\""
        execProcessWithFail "git" ("config --global user.email \"" + Environment.GetEnvironmentVariable("git_mail") + "\"")
        
        
        execProcessWithFail "git" " add ."
        execProcessWithFail "git" "commit -m \"Add new posts [skip ci]\""
        execProcessWithFail "git" "push sshorigin master"

    let addBook(books:List<BookConfig>) =
        let isbn = Environment.GetEnvironmentVariable("isbn")
        let startDate = Environment.GetEnvironmentVariable("startDate")
        if not (isNull isbn || isNull startDate) then
            printfn "Add book '%s'" isbn
            books.Add(new BookConfig(isbn, startDate, false))
        books

    let generatePosts configFileName =
        let books = configFileName |> BookConfig.Load |> addBook

        printfn "start posts generation"
        let postsToGenerate = books |> Seq.exists (fun book -> not book.Generated)

        createFolderIfNotExists PostsFolder
        createFolderIfNotExists ImagesFolder
        let newBooksConfig = books |> Seq.map generatePost
        BookConfig.Write configFileName (new List<BookConfig>(Seq.toArray newBooksConfig))
        printfn "end posts generation"

        if postsToGenerate then
            commitGeneratedPosts()

        match postsToGenerate with
        | true -> Ok
        | false -> Stop "No posts to generate"

    let bakeSite () =
        System.Threading.Thread.CurrentThread.CurrentCulture <- System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR")
        if execProcess @"C:\tools\Pretzel\Pretzel" "bake site" > 0 then
            Stop "Pretzel have failed to build the site"
        else
            Ok

    let private checkTaskResult taskResult isDeployForced =
        match taskResult with
        | Ok -> ()
        | Stop message -> match isDeployForced with
                            | false -> cprintfn ConsoleColor.Yellow message; Environment.Exit 1
                            | true -> cprintfn ConsoleColor.Yellow "!! Deploy forced !!"

    let Build =
        printfn "start build"
        let isDeployForced = isDeployForced()

        // 1) Add book to list (if book info)
        printfn "%s" (Environment.GetEnvironmentVariable("isbn"))
        printfn "%s" (Environment.GetEnvironmentVariable("startDate"))

        // 2) Generate page for book (if needed)
        let generatePostsResult = generatePosts "books.yml"

        checkTaskResult generatePostsResult isDeployForced

        // 3) Build website (if needed)
        let bakeSiteResult = bakeSite()
        checkTaskResult generatePostsResult isDeployForced

        // 4) Deploy (if needed)

Main.Build<commit_msg>Remove unwanted printfn [ci skip]<commit_after>#load "Utils.fsx"
#load "BookInfos.fsx"

open System
open System.IO
open System.Collections.Generic
open Utils
open BookInfos
open BookConfig

let (template : Printf.StringFormat<_>) = "---
layout: post
title: \"%s\"
author: \"%s\"
isbn: %s
editor: %s
---

![Couverture](/img/%s)%s"

module Main = 

    let PostsFolder = "site/_posts/"
    let ImagesFolder = "site/img/"

    let private generatePost (bookConfig : BookConfig) = 
        if bookConfig.Generated then 
            bookConfig
        else
            let book = BookInfos.GetBookInfo bookConfig
            BookInfos.Check book
            printfn "\tdata found"
            File.WriteAllText
                (sprintf "%s%s-%s.md" PostsFolder book.ReadingDate (book.Title |> Utils.slugify), 
                 sprintf template book.Title book.Author book.Isbn book.Editor 
                     (Utils.downloadImageToSite book.ImageUrl book.Isbn ImagesFolder) book.Summary)
            new BookConfig(bookConfig.Isbn, bookConfig.Date, true)

    // forced: deploy will be executed even if there is no book page to generate
    let isDeployForced _ =
        let commitMessage = Environment.GetEnvironmentVariable("APPVEYOR_REPO_COMMIT_MESSAGE")
        let forcedBuild = Environment.GetEnvironmentVariable("APPVEYOR_FORCED_BUILD")
        (not (isNull commitMessage) && commitMessage.ToLowerInvariant().Contains("[force]") )
                || (not (isNull forcedBuild) && forcedBuild.ToLowerInvariant() = "true")

    let commitGeneratedPosts () =
        execProcessWithFail "git" "checkout master"
        execProcessWithFail "git" ("remote add sshorigin https://" + Environment.GetEnvironmentVariable("access_token") + ":x-oauth-basic@github.com/laedit/ReadingList.git")
        execProcessWithFail "git" "config --global credential.helper store"
        use sw = File.AppendText(Environment.GetEnvironmentVariable("USERPROFILE") + ".git-credentials")
        sw.Write("https://" + Environment.GetEnvironmentVariable("access_token") + ":x-oauth-basic@github.com");
        sw.Write("\n");
        execProcessWithFail "git" "config --global user.name \"<NAME>\""
        execProcessWithFail "git" ("config --global user.email \"" + Environment.GetEnvironmentVariable("git_mail") + "\"")
        
        
        execProcessWithFail "git" " add ."
        execProcessWithFail "git" "commit -m \"Add new posts [skip ci]\""
        execProcessWithFail "git" "push sshorigin master"

    // Add book to list (if book info)
    let addBook(books:List<BookConfig>) =
        let isbn = Environment.GetEnvironmentVariable("isbn")
        let startDate = Environment.GetEnvironmentVariable("startDate")
        if not (isNull isbn || isNull startDate) then
            printfn "Add book '%s'" isbn
            books.Add(new BookConfig(isbn, startDate, false))
        books

    let generatePosts configFileName =
        let books = configFileName |> BookConfig.Load |> addBook

        printfn "start posts generation"
        let postsToGenerate = books |> Seq.exists (fun book -> not book.Generated)

        createFolderIfNotExists PostsFolder
        createFolderIfNotExists ImagesFolder
        let newBooksConfig = books |> Seq.map generatePost
        BookConfig.Write configFileName (new List<BookConfig>(Seq.toArray newBooksConfig))
        printfn "end posts generation"

        if postsToGenerate then
            commitGeneratedPosts()

        match postsToGenerate with
        | true -> Ok
        | false -> Stop "No posts to generate"

    let bakeSite () =
        System.Threading.Thread.CurrentThread.CurrentCulture <- System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR")
        if execProcess @"C:\tools\Pretzel\Pretzel" "bake site" > 0 then
            Stop "Pretzel have failed to build the site"
        else
            Ok

    let private checkTaskResult taskResult isDeployForced =
        match taskResult with
        | Ok -> ()
        | Stop message -> match isDeployForced with
                            | false -> cprintfn ConsoleColor.Yellow message; Environment.Exit 1
                            | true -> cprintfn ConsoleColor.Yellow "!! Deploy forced !!"

    let Build =
        printfn "start build"
        let isDeployForced = isDeployForced()

        //  Generate page for book
        let generatePostsResult = generatePosts "books.yml"

        checkTaskResult generatePostsResult isDeployForced

        // Build website
        let bakeSiteResult = bakeSite()
        checkTaskResult generatePostsResult isDeployForced

        // Deploy

Main.Build
{'fraction_non_alphanumeric': 0.09903304643502285, 'fraction_numerical': 0.0008500690681117841, 'mean_word_length': 2.672259071400702, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 37, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '185397', 'n_tokens_mistral': 2618, 'n_tokens_neox': 2519, 'n_words': 673}