11#I @" ../../packages/build/FAKE/tools"
2- #I @" ../../packages/build/FSharp.Data/lib/net40"
2+ #I @" ../../packages/build/FSharp.Data/lib/net45"
3+ #I @" ../../packages/build/Mono.Cecil/lib/net40"
34#r @" FakeLib.dll"
5+ #r @" Mono.Cecil.dll"
46#r @" FSharp.Data.dll"
7+ #nowarn " 0044" //TODO sort out FAKE 5
58
69#load @" Paths.fsx"
710#load @" Tooling.fsx"
811#load @" Versioning.fsx"
912
1013open System
1114open System.IO
15+ open System.Reflection
1216open Fake
1317open FSharp.Data
18+ open Mono.Cecil
1419
1520open Paths
1621open Projects
@@ -20,20 +25,20 @@ open Versioning
2025module Build =
2126
2227 let private runningRelease = hasBuildParam " version" || hasBuildParam " apikey" || getBuildParam " target" = " canary" || getBuildParam " target" = " release"
23- let private quickBuild = not ( getBuildParam " target" = " release" || getBuildParam " target" = " canary" )
2428
2529 type private GlobalJson = JsonProvider< " ../../global.json" >
2630 let private pinnedSdkVersion = GlobalJson.GetSample() .Sdk.Version
2731 if isMono then setProcessEnvironVar " TRAVIS" " true"
32+
2833 let private buildingOnTravis = getEnvironmentVarAsBool " TRAVIS"
2934
3035 let private sln = sprintf " src/Elasticsearch%s .sln" ( if buildingOnTravis then " .DotNetCoreOnly" else " " )
31-
36+
3237 let private compileCore incremental =
3338 if not ( DotNetCli.isInstalled()) then failwith " You need to install the dotnet command line SDK to build for .NET Core"
3439 let runningSdkVersion = DotNetCli.getVersion()
3540 if ( runningSdkVersion <> pinnedSdkVersion) then failwithf " Attempting to run with dotnet.exe with %s but global.json mandates %s " runningSdkVersion pinnedSdkVersion
36- let incrementalFramework = DotNetFramework.Net45
41+ let incrementalFramework = DotNetFramework.Net46
3742 let sourceLink = if not incremental && not isMono && runningRelease then " 1" else " "
3843 let props =
3944 [
@@ -75,4 +80,102 @@ module Build =
7580 CleanDir Paths.BuildOutput
7681 DotNetCli.RunCommand ( fun p -> { p with TimeOut = TimeSpan.FromMinutes( 3. ) }) " clean src/Elasticsearch.sln -c Release" |> ignore
7782 DotNetProject.All |> Seq.iter( fun p -> CleanDir( Paths.BinFolder p.Name))
78-
83+
84+ type CustomResolver ( folder ) =
85+ inherit DefaultAssemblyResolver()
86+ member this.Folder = folder;
87+
88+ override this.Resolve name =
89+ try
90+ base .Resolve name
91+ with
92+ | ex ->
93+ AssemblyDefinition.ReadAssembly( Path.Combine( folder, " Elasticsearch.Net.dll" ));
94+
95+ let private rewriteNamespace nest f =
96+ trace " Rewriting namespaces"
97+ let folder = Paths.ProjectOutputFolder nest f
98+ let nestDll = sprintf " %s /%s .dll" folder nest.Name
99+ let nestRewrittenDll = sprintf " %s /%s -rewriten.dll" folder nest.Name
100+ use resolver = new CustomResolver( folder)
101+ let readerParams = ReaderParameters( AssemblyResolver = resolver, ReadWrite = true );
102+ use nestAssembly = AssemblyDefinition.ReadAssembly( nestDll, readerParams);
103+
104+ for item in nestAssembly.MainModule.Types do
105+ if item.Namespace.StartsWith( " Newtonsoft.Json" ) then
106+ item.Namespace <- item.Namespace.Replace( " Newtonsoft.Json" , " Nest.Json" )
107+
108+ // Touch custom attribute arguments
109+ // Cecil does not update the types referenced within these attributes automatically,
110+ // so enumerate them to ensure namespace renaming is reflected in these references.
111+ let touchAttributes ( attributes : Mono.Collections.Generic.Collection < CustomAttribute >) =
112+ for attr in attributes do
113+ if attr.HasConstructorArguments then
114+ for constArg in attr.ConstructorArguments do
115+ if constArg.Type.Name = " Type" then ignore()
116+
117+ // rewrite explicitly implemented interface definitions defined
118+ // in Newtonsoft.Json
119+ let rewriteName ( method : IMemberDefinition ) =
120+ if method.Name.Contains( " Newtonsoft.Json" ) then
121+ method.Name <- method.Name.Replace( " Newtonsoft.Json" , " Nest.Json" )
122+
123+ // recurse through all types and nested types
124+ let rec rewriteTypes ( types : Mono.Collections.Generic.Collection < TypeDefinition >) =
125+ for t in types do
126+ touchAttributes t.CustomAttributes
127+ for prop in t.Properties do
128+ touchAttributes prop.CustomAttributes
129+ rewriteName prop
130+ if prop.GetMethod <> null then rewriteName prop.GetMethod
131+ if prop.SetMethod <> null then rewriteName prop.SetMethod
132+ for method in t.Methods do
133+ touchAttributes method.CustomAttributes
134+ rewriteName method
135+ for over in method.Overrides do rewriteName method
136+ for field in t.Fields do touchAttributes field.CustomAttributes
137+ for interf in t.Interfaces do touchAttributes interf.CustomAttributes
138+ for event in t.Events do touchAttributes event.CustomAttributes
139+ if t.HasNestedTypes then rewriteTypes t.NestedTypes
140+
141+ nestAssembly.MainModule.Types |> rewriteTypes
142+
143+ let resources = nestAssembly.MainModule.Resources
144+ for i = resources.Count-1 downto 0 do
145+ let resource = resources.[ i]
146+ // remove the Newtonsoft signing key
147+ if resource.Name = " Newtonsoft.Json.Dynamic.snk" then resources.Remove( resource) |> ignore
148+
149+ let key = File.ReadAllBytes( Paths.Keys( " keypair.snk" ))
150+ let kp = StrongNameKeyPair( key)
151+ let wp = WriterParameters ( StrongNameKeyPair = kp);
152+ nestAssembly.Write( wp) |> ignore;
153+ trace " Finished rewriting namespaces"
154+
155+ let private ilRepackInternal () =
156+ let fw = if isMono then [ DotNetFramework.NetStandard1_ 3] else DotNetFramework.All
157+ for f in fw do
158+ let nest = Project Project.Nest
159+ let folder = Paths.ProjectOutputFolder nest f
160+ let nestDll = sprintf " %s /%s .dll" folder nest.Name
161+ let nestMergedDll = sprintf " %s /%s -merged.dll" folder nest.Name
162+ let jsonDll = sprintf " %s /Newtonsoft.Json.dll" folder
163+ let keyFile = Paths.Keys( " keypair.snk" );
164+ let options =
165+ [
166+ " /keyfile:" , keyFile;
167+ " /internalize" , " " ;
168+ " /lib:" , folder;
169+ " /out:" , nestDll;
170+ ]
171+ |> List.map ( fun ( p , v ) -> sprintf " %s%s " p v)
172+
173+ let args = [ nestDll; jsonDll;] |> List.append options;
174+
175+ Tooling.ILRepack.Exec args |> ignore
176+ rewriteNamespace nest f |> ignore
177+
178+ let ILRepack () =
179+ //nothing to IL merge in the 5.x branch
180+ ignore()
181+
0 commit comments