<commit_before>﻿module private FSharp.Interop.ComProvider.TypeLibImport

open System
open System.IO
open System.Reflection
open System.Runtime.InteropServices
open System.Runtime.InteropServices.ComTypes
open Utility

[<DllImport("oleaut32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)>]
extern void private LoadTypeLib(string filename, ITypeLib& typelib);

let private converter = TypeLibConverter()

let rec private convertToAsm typeLib asmDir =
    let sink = { new ITypeLibImporterNotifySink with
        member this.ReportEvent(eventKind, eventCode, eventMsg) = ()
        member this.ResolveRef(typeLib) = convertToAsm typeLib asmDir :> Assembly}
    let flags = TypeLibImporterFlags.None
    let asmFile = Guid.NewGuid().ToString() + ".dll"
    let asmPath = Path.Combine(asmDir, asmFile)
    converter.ConvertTypeLibToAssembly(typeLib, asmPath, flags, sink, null, null, "", null)

let importTypeLib typeLibPath asmDir =
    let mutable typeLib : ITypeLib = null
    LoadTypeLib(typeLibPath, &typeLib)
    let asm = convertToAsm typeLib asmDir
    asm.Save(asm.GetName().Name + ".dll")
    asm<commit_msg>Better error message for when a type library fails to load<commit_after>﻿module private FSharp.Interop.ComProvider.TypeLibImport

open System
open System.IO
open System.Reflection
open System.Runtime.InteropServices
open System.Runtime.InteropServices.ComTypes
open Utility

[<DllImport("oleaut32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)>]
extern void private LoadTypeLib(string filename, ITypeLib& typelib);

let private converter = TypeLibConverter()

let rec private convertToAsm typeLib asmDir =
    let sink = { new ITypeLibImporterNotifySink with
        member this.ReportEvent(eventKind, eventCode, eventMsg) = ()
        member this.ResolveRef(typeLib) = convertToAsm typeLib asmDir :> Assembly}
    let flags = TypeLibImporterFlags.None
    let asmFile = Guid.NewGuid().ToString() + ".dll"
    let asmPath = Path.Combine(asmDir, asmFile)
    converter.ConvertTypeLibToAssembly(typeLib, asmPath, flags, sink, null, null, "", null)

let importTypeLib typeLibPath asmDir =
    let mutable typeLib : ITypeLib = null
    LoadTypeLib(typeLibPath, &typeLib)
    if typeLib = null then
        failwith ("Error loading type library. Please check that the " +
                  "component is correctly installed and registered.")
    let asm = convertToAsm typeLib asmDir
    asm.Save(asm.GetName().Name + ".dll")
    asm
{'fraction_non_alphanumeric': 0.08791208791208792, 'fraction_numerical': 0.001628001628001628, 'mean_word_length': 4.450110864745011, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6880866', 'n_tokens_mistral': 759, 'n_tokens_neox': 742, 'n_words': 206}