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

Generator - Add support for types with base type in another assembly #226

Merged
Merged
Changes from all 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
35 changes: 32 additions & 3 deletions tools/Generator/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ type TextWriter with
member this.printf fmt = fprintf this fmt
member this.printfn fmt = fprintfn this fmt

type RegistrableResolver() =
inherit BaseAssemblyResolver()

let cache = Dictionary<string, AssemblyDefinition>()

override this.Resolve(name) =
match cache.TryGetValue(name.FullName) with
| true, assembly -> assembly
| false, _ ->
let assembly = base.Resolve(name)
cache.[name.FullName] <- assembly
assembly

member this.RegisterAssembly(assembly: AssemblyDefinition): unit =
match cache.ContainsKey(assembly.Name.FullName) with
| true -> ()
| false ->
cache.[assembly.Name.FullName] <- assembly

member this.Dispose(disposing) =
cache.Values |> Seq.iter (fun asm -> asm.Dispose())
cache.Clear()
base.Dispose()

let (|NotNullOrWhitespace|_|) (s:string) = if String.IsNullOrWhiteSpace s then None else Some s

let ResolveType(this: Bindings, name: string) =
Expand Down Expand Up @@ -579,12 +603,16 @@ let BindTypes (bindings: Bindings, resolutions: IDictionary<TypeBinding, TypeDef
w.ToString ()


let LoadAssembly (path: string) : AssemblyDefinition =
let LoadAssembly (resolver: RegistrableResolver) (path: string) : AssemblyDefinition =
//if (path.StartsWith("packages")) {
// let user = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
// path = Path.Combine (user, ".nuget", path)
// }
AssemblyDefinition.ReadAssembly(path)
let readerParameters = ReaderParameters()
readerParameters.AssemblyResolver <- resolver
let assembly = AssemblyDefinition.ReadAssembly(path, readerParameters)
resolver.RegisterAssembly assembly
assembly


[<EntryPoint>]
Expand All @@ -598,7 +626,8 @@ let Main(args: string[]) =

let bindings = JsonConvert.DeserializeObject<Bindings> (File.ReadAllText (bindingsPath))

bindings.AssemblyDefinitions <- bindings.Assemblies.Select(LoadAssembly).ToList()
let resolver = new RegistrableResolver()
bindings.AssemblyDefinitions <- bindings.Assemblies.Select(LoadAssembly resolver).ToList()

let resolutions =
[ for x in bindings.Types do
Expand Down