<commit_before>﻿module Fake.DynamicsNav

open System
open System.Diagnostics
open System.Text
open System.IO

[<RequireQualifiedAccess>]
type NavisionServerType =
| SqlServer
| NativeServer

type DynamicsNavParams =
    { ToolPath: string
      ServerName: string
      Database: string
      WorkingDir: string
      TempLogFile: string
      TimeOut: TimeSpan}

let getNAVClassicPath navClientVersion =
    let subKey = 
        match navClientVersion with
        | "601"
        | "602" -> @"SOFTWARE\Microsoft\Microsoft Dynamics NAV\60\Classic Client\W1 6.0"
        | "700" -> @"SOFTWARE\Microsoft\Microsoft Dynamics NAV\70\RoleTailored Client"
        | "501" -> @"software\microsoft\Dynamics Nav\Cside Client\W1 5.0 SP1"
        | "403" -> @"SOFTWARE\Navision\Microsoft Business Solutions-Navision\W1 4.00"
        | _     -> failwithf "Unknown NAV-Version %s" navClientVersion

    getRegistryValue HKEYLocalMachine subKey "Path"

let getNAVPath navClientVersion = (directoryInfo (getNAVClassicPath navClientVersion)).Parent.FullName

/// Creates the connection information to a Dynamics NAV instance
let createConnectionInfo navClientVersion serverMode serverName targetDatabase =
    let navServicePath = 
        try
            let navServiceRootPath =
                let subKey = 
                    match navClientVersion with
                    | "601"
                    | "602" -> @"SOFTWARE\Microsoft\Microsoft Dynamics NAV\60\Classic Client\W1 6.0"
                    | "700" -> @"SOFTWARE\Microsoft\Microsoft Dynamics NAV\70\Service"
                    | _     -> failwithf "Unknown NAV-Version %s" navClientVersion

                getRegistryValue HKEYLocalMachine subKey "Path"

            (directoryInfo navServiceRootPath).Parent.FullName @@ "Service"    
        with
        | exn -> @"C:\Program Files\Navision700\70\Service"

    let clientExe = 
        match serverMode with
        | NavisionServerType.SqlServer -> "finsql.exe"
        | NavisionServerType.NativeServer -> "fin.exe"

    let finExe = getNAVClassicPath navClientVersion @@ clientExe

    { ToolPath = finExe
      WorkingDir = null
      ServerName = serverName
      Database = targetDatabase
      TempLogFile = "./NavErrorMessages.txt"
      TimeOut = TimeSpan.FromMinutes 20.}

let private analyzeLogFile fileName =
    let lines = ReadFile fileName |> Seq.toList
    lines |> Seq.iter traceError
    DeleteFile fileName
    lines.Length

let private reportError text logFile =
    let errors = analyzeLogFile logFile
    failwith (text + (if errors = 1 then " with 1 error." else sprintf " with %d errors." errors))

let private import connectionInfo fileName =
    let fi = fileInfo fileName     
    let deleteFile,fi =
        if fi.Extension = ".nav" then
            true,fi.CopyTo(Path.Combine(fi.Directory.FullName,fi.Name + ".txt"))
        else
            false,fi

    let args = 
        sprintf "command=importobjects, file=\"%s\", logfile=\"%s\", servername=\"%s\", database=\"%s\"" 
            fi.FullName (FullName connectionInfo.TempLogFile) connectionInfo.ServerName connectionInfo.Database<commit_msg>Don't fail if the NavErrorMessages is missing<commit_after>﻿module Fake.DynamicsNav

open System
open System.Diagnostics
open System.Text
open System.IO

[<RequireQualifiedAccess>]
type NavisionServerType =
| SqlServer
| NativeServer

type DynamicsNavParams =
    { ToolPath: string
      ServerName: string
      Database: string
      WorkingDir: string
      TempLogFile: string
      TimeOut: TimeSpan}

let getNAVClassicPath navClientVersion =
    let subKey = 
        match navClientVersion with
        | "601"
        | "602" -> @"SOFTWARE\Microsoft\Microsoft Dynamics NAV\60\Classic Client\W1 6.0"
        | "700" -> @"SOFTWARE\Microsoft\Microsoft Dynamics NAV\70\RoleTailored Client"
        | "501" -> @"software\microsoft\Dynamics Nav\Cside Client\W1 5.0 SP1"
        | "403" -> @"SOFTWARE\Navision\Microsoft Business Solutions-Navision\W1 4.00"
        | _     -> failwithf "Unknown NAV-Version %s" navClientVersion

    getRegistryValue HKEYLocalMachine subKey "Path"

let getNAVPath navClientVersion = (directoryInfo (getNAVClassicPath navClientVersion)).Parent.FullName

/// Creates the connection information to a Dynamics NAV instance
let createConnectionInfo navClientVersion serverMode serverName targetDatabase =
    let navServicePath = 
        try
            let navServiceRootPath =
                let subKey = 
                    match navClientVersion with
                    | "601"
                    | "602" -> @"SOFTWARE\Microsoft\Microsoft Dynamics NAV\60\Classic Client\W1 6.0"
                    | "700" -> @"SOFTWARE\Microsoft\Microsoft Dynamics NAV\70\Service"
                    | _     -> failwithf "Unknown NAV-Version %s" navClientVersion

                getRegistryValue HKEYLocalMachine subKey "Path"

            (directoryInfo navServiceRootPath).Parent.FullName @@ "Service"    
        with
        | exn -> @"C:\Program Files\Navision700\70\Service"

    let clientExe = 
        match serverMode with
        | NavisionServerType.SqlServer -> "finsql.exe"
        | NavisionServerType.NativeServer -> "fin.exe"

    let finExe = getNAVClassicPath navClientVersion @@ clientExe

    { ToolPath = finExe
      WorkingDir = null
      ServerName = serverName
      Database = targetDatabase
      TempLogFile = "./NavErrorMessages.txt"
      TimeOut = TimeSpan.FromMinutes 20.}

let private analyzeLogFile fileName =
    try
        let lines = ReadFile fileName |> Seq.toList
        lines |> Seq.iter traceError
        DeleteFile fileName
        lines.Length
    with 
    | exn -> 
        traceError exn.Message
        1

let private reportError text logFile =    
    let errors = analyzeLogFile logFile
    failwith (text + (if errors = 1 then " with 1 error." else sprintf " with %d errors." errors))

let private import connectionInfo fileName =
    let fi = fileInfo fileName     
    let deleteFile,fi =
        if fi.Extension = ".nav" then
            true,fi.CopyTo(Path.Combine(fi.Directory.FullName,fi.Name + ".txt"))
        else
            false,fi

    let args = 
        sprintf "command=importobjects, file=\"%s\", logfile=\"%s\", servername=\"%s\", database=\"%s\"" 
            fi.FullName (FullName connectionInfo.TempLogFile) connectionInfo.ServerName connectionInfo.Database
{'fraction_non_alphanumeric': 0.0887185104052574, 'fraction_numerical': 0.01736817399468002, 'mean_word_length': 2.557039510294936, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1769808', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1745, 'n_words': 461}