forked from microsoft/node-api-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeApi.Generator.targets
More file actions
183 lines (162 loc) · 11.6 KB
/
NodeApi.Generator.targets
File metadata and controls
183 lines (162 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<Project>
<PropertyGroup>
<GenerateNodeApiTypeDefinitions Condition=" '$(GenerateNodeApiTypeDefinitions)' == '' ">true</GenerateNodeApiTypeDefinitions>
<NodeApiTypeDefinitionsFileName Condition=" '$(NodeApiTypeDefinitionsFileName)' == '' ">$(TargetName).d.ts</NodeApiTypeDefinitionsFileName>
<NodeApiGeneratorAssemblyName>Microsoft.JavaScript.NodeApi.Generator</NodeApiGeneratorAssemblyName>
<NodeApiGeneratorDirectory>$(MSBuildThisFileDirectory)../lib/net8.0/</NodeApiGeneratorDirectory>
<NodeApiGeneratorNet4xDirectory>$(MSBuildThisFileDirectory)../lib/net472/</NodeApiGeneratorNet4xDirectory>
<NodeApiGeneratorNetStandardDirectory>$(MSBuildThisFileDirectory)../lib/netstandard2.0/</NodeApiGeneratorNetStandardDirectory>
<!--
With .NET Framework 4.x, invoke the generator executable; otherwise use the dotnet CLI (which will check the runtimeconfig.json).
The TS generator tool selection is based on the current project target framework, NOT the current MSBuild .NET version.
-->
<NodeApiGeneratorCommand Condition=" $(TargetFramework.StartsWith('net4')) ">"$(NodeApiGeneratorNet4xDirectory)$(NodeApiGeneratorAssemblyName).exe"</NodeApiGeneratorCommand>
<NodeApiGeneratorCommand Condition=" !$(TargetFramework.StartsWith('net4')) ">dotnet "$(NodeApiGeneratorDirectory)$(NodeApiGeneratorAssemblyName).dll"</NodeApiGeneratorCommand>
<!-- Try to infer the module type from package.json in the project directory. Otherwise default to generating both module types.-->
<NodeApiPackageJson Condition=" '$(NodeApiPackageJson)' == '' ">$(ProjectDir)package.json</NodeApiPackageJson>
<NodeApiJSModuleType Condition=" '$(NodeApiJSModuleType)' == '' AND Exists('$(NodeApiPackageJson)') ">"$(NodeApiPackageJson)"</NodeApiJSModuleType>
<NodeApiJSModuleType Condition=" '$(NodeApiJSModuleType)' == '' ">commonjs,esm</NodeApiJSModuleType>
<NodeApiTargetFramework Condition=" '$(NodeApiTargetFramework)' == '' ">$(TargetFramework)</NodeApiTargetFramework>
<NodeApiTypeDefinitionsGeneratorOptions>--module $(NodeApiJSModuleType) --framework $(NodeApiTargetFramework) $(NodeApiTypedefsGeneratorOptions)</NodeApiTypeDefinitionsGeneratorOptions>
</PropertyGroup>
<!--
Resolves NodeApi source-generator (analyzer) assemblies.
The recommended practice is to build analyzers targeting only .NET Standard 2.0, and place them in a special "analyzers" directory
in the package where they are found automatically. But this package contains separate targets for .NET Standard 2.0, .NET Framework 4.x,
and .NET 6.0 (and later) in the "lib" directory. This task chooses the correct target based on the currnet MSBuild process's .NET version
(NOT the current target framework), and adds the assemblies to the Analyzer item group.
-->
<Target Name="ResolveNodeApiAnalyzer" AfterTargets="ResolvePackageDependenciesForBuild">
<ItemGroup>
<Analyzer Condition=" $([System.Environment]::Version.Major) > 4 " Include="$(NodeApiGeneratorDirectory)*.dll" />
<Analyzer Condition=" $([System.Environment]::Version.Major) <= 4 " Include="$(NodeApiGeneratorNetStandardDirectory)*.dll" />
</ItemGroup>
</Target>
<Target Name="ConfigureNodeApiTypeDefinitions"
Condition=" '$(GenerateNodeApiTypeDefinitions)' == 'true' "
BeforeTargets="BeforeBuild"
>
<!-- When the project does not have any source files, copy all reference assemblies to output and generate typedefs for them. -->
<PropertyGroup Condition=" '@(Compile)' == '' ">
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<CopyBuildOutputToOutputDirectory>false</CopyBuildOutputToOutputDirectory>
<CopyOutputSymbolsToOutputDirectory>false</CopyOutputSymbolsToOutputDirectory>
<GenerateNodeApiTypeDefinitionsForReferences Condition=" '$(GenerateNodeApiTypeDefinitionsForReferences)' == '' ">true</GenerateNodeApiTypeDefinitionsForReferences>
</PropertyGroup>
</Target>
<!--
Generates TS type definitions for a Node API module project.
-->
<Target Name="GenerateNodeApiTypeDefinitions"
AfterTargets="Build"
Inputs="$(TargetPath)"
Outputs="$(TargetDir)$(NodeApiTypeDefinitionsFileName)"
Condition=" '$(GenerateNodeApiTypeDefinitions)' == 'true' AND Exists('$(TargetPath)') "
>
<PropertyGroup>
<NodeApiGeneratorResponseFile>$(IntermediateOutputPath)$(NodeApiGeneratorAssemblyName).rsp</NodeApiGeneratorResponseFile>
<_NodeApiGeneratorTargetingPacks>@(TargetingPack, '%3B')</_NodeApiGeneratorTargetingPacks>
<_NodeApiGeneratorAssemblyReferences>@(ReferencePathWithRefAssemblies, '%3B')</_NodeApiGeneratorAssemblyReferences>
</PropertyGroup>
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--assembly "$(TargetPath)"" Overwrite="true" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--packs "$(_NodeApiGeneratorTargetingPacks)"" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--reference "$(_NodeApiGeneratorAssemblyReferences)"" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--typedefs "$(TargetDir)$(NodeApiTypeDefinitionsFileName)"" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--exclude "$(NodeApiExcludeFromMetadata.Replace(';', '%3B'))"" Condition=" '$(NodeApiExcludeFromMetadata)' != '' " />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="$(NodeApiTypeDefinitionsGeneratorOptions)" />
<!-- Run the generator using args from the response file. Note the '@' indicates the response file NOT an MSBuild item-list. -->
<Exec Command="$(NodeApiGeneratorCommand) "@$(NodeApiGeneratorResponseFile)""
ConsoleToMSBuild="true" />
</Target>
<!--
Publishes TS type definitions for a Node API module project.
-->
<Target Name="PublishNodeApiTypeDefinitions"
AfterTargets="Publish"
Inputs="$(TargetDir)$(NodeApiTypeDefinitionsFileName)"
Outputs="$(PublishDir)$(NodeApiTypeDefinitionsFileName)"
Condition=" '$(GenerateNodeApiTypeDefinitions)' == 'true' AND Exists('$(TargetDir)$(NodeApiTypeDefinitionsFileName)') "
>
<Copy SourceFiles="$(TargetDir)$(NodeApiTypeDefinitionsFileName)" DestinationFolder="$(PublishDir)" />
<Copy SourceFiles="$(TargetDir)$(TargetName).js" DestinationFolder="$(PublishDir)" Condition="Exists('$(TargetDir)$(TargetName).js')" />
<Copy SourceFiles="$(TargetDir)$(TargetName).cjs" DestinationFolder="$(PublishDir)" Condition="Exists('$(TargetDir)$(TargetName).cjs')" />
<Copy SourceFiles="$(TargetDir)$(TargetName).mjs" DestinationFolder="$(PublishDir)" Condition="Exists('$(TargetDir)$(TargetName).mjs')" />
</Target>
<Target Name="CleanNodeApiTypeDefinitions" AfterTargets="CoreClean">
<Delete Files="$(TargetDir)$(NodeApiTypeDefinitionsFileName)" />
<Delete Files="$(PublishDir)$(NodeApiTypeDefinitionsFileName)" />
</Target>
<!--
Supports building a project that does not compile any code, but only collects and outputs
a set of package references in the output directory for use by a JavaScript project.
-->
<Target Name="ResolveNodeApiReferenceAssemblies"
Condition=" '$(GenerateNodeApiTypeDefinitionsForReferences)' == 'true' "
AfterTargets="ResolvePackageAssets">
<ItemGroup>
<!-- TODO: Should resource assemblies be copied? They might be needed at runtime. -->
<ResourceCopyLocalItems Remove="@(ResourceCopyLocalItems)" />
<!-- Do not copy the generator assembly because it is not needed at runtime. -->
<!-- (Code analysis assemblies are used by the generator.) -->
<RuntimeCopyLocalItems Remove="@(RuntimeCopyLocalItems)"
Condition=" '%(Filename)' == 'Microsoft.JavaScript.NodeApi.Generator' " />
<RuntimeCopyLocalItems Remove="@(RuntimeCopyLocalItems)"
Condition=" '%(Filename)' == 'Microsoft.CodeAnalysis' " />
<RuntimeCopyLocalItems Remove="@(RuntimeCopyLocalItems)"
Condition=" '%(Filename)' == 'Microsoft.CodeAnalysis.CSharp' " />
<!-- Generate type definitions for assemblies from package references. -->
<NodeApiReferenceAssemblies Include="@(RuntimeCopyLocalItems)" />
</ItemGroup>
</Target>
<Target Name="ResolveNodeApiProjectReferenceAssemblies"
Condition=" '$(GenerateNodeApiTypeDefinitionsForReferences)' == 'true' "
BeforeTargets="GenerateNodeApiReferenceTypeDefinitions"
DependsOnTargets="GenerateBuildDependencyFile">
<ItemGroup>
<!-- Generate type definitions for assemblies from project references. -->
<NodeApiReferenceAssemblies Include="@(UserRuntimeAssembly)" />
</ItemGroup>
</Target>
<!--
Generates TS type definitions for all referenced assemblies in the output directory.
-->
<Target Name="GenerateNodeApiReferenceTypeDefinitions"
AfterTargets="Build"
Inputs="@(NodeApiReferenceAssemblies)"
Outputs="@(NodeApiReferenceAssemblies->'$(TargetDir)%(Filename).d.ts')"
Condition=" '$(GenerateNodeApiTypeDefinitions)' == 'true' AND '$(Compile)' == '' "
>
<ItemGroup Condition="$(TargetFramework.StartsWith('net4'))">
<NodeApiSystemReferenceAssembly Include="mscorlib" />
<NodeApiSystemReferenceAssembly Include="System" />
</ItemGroup>
<ItemGroup Condition="! $(TargetFramework.StartsWith('net4'))">
<NodeApiSystemReferenceAssembly Include="System.Runtime" />
<NodeApiSystemReferenceAssembly Include="System.Console" />
</ItemGroup>
<ItemGroup>
<!-- This does not use @(NodeApiReferenceAssemblies), like the target Inputs, to avoid excluding items that are up-to-date. -->
<_NodeApiAllReferenceAssemblies Include="@(UserRuntimeAssembly)" />
<_NodeApiAllReferenceAssemblies Include="@(RuntimeCopyLocalItems)" />
<_NodeApiAllReferenceAssemblies Include="@(NodeApiSystemReferenceAssembly)" />
<_NodeApiAllTypeDefs Include="@(UserRuntimeAssembly->'$(TargetDir)%(Filename).d.ts')" />
<_NodeApiAllTypeDefs Include="@(RuntimeCopyLocalItems->'$(TargetDir)%(Filename).d.ts')" />
<_NodeApiAllTypeDefs Include="@(NodeApiSystemReferenceAssembly->'$(TargetDir)%(Identity).d.ts')" />
</ItemGroup>
<PropertyGroup>
<NodeApiGeneratorResponseFile>$(IntermediateOutputPath)$(NodeApiGeneratorAssemblyName).rsp</NodeApiGeneratorResponseFile>
<_NodeApiGeneratorTargetingPacks>@(TargetingPack, '%3B')</_NodeApiGeneratorTargetingPacks>
<_NodeApiGeneratorAssemblyReferences>@(_NodeApiAllReferenceAssemblies, '%3B')</_NodeApiGeneratorAssemblyReferences>
<_NodeApiGeneratorTypeDefs>@(_NodeApiAllTypeDefs, '%3B')</_NodeApiGeneratorTypeDefs>
</PropertyGroup>
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--assemblies "$(_NodeApiGeneratorAssemblyReferences)"" Overwrite="true" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--packs "$(_NodeApiGeneratorTargetingPacks)"" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--typedefs "$(_NodeApiGeneratorTypeDefs)"" />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--nowarn" Condition=" '$(NodeApiTypeDefinitionsEnableWarnings)' != 'true' " />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="--exclude "$(NodeApiExcludeFromMetadata.Replace(';', '%3B'))"" Condition=" '$(NodeApiExcludeFromMetadata)' != '' " />
<WriteLinesToFile File="$(NodeApiGeneratorResponseFile)" Lines="$(NodeApiTypeDefinitionsGeneratorOptions)" />
<!-- Run the generator using args from the response file. Note the '@' indicates the response file NOT an MSBuild item-list. -->
<Exec Command="$(NodeApiGeneratorCommand) "@$(NodeApiGeneratorResponseFile)""
ConsoleToMSBuild="true" />
</Target>
</Project>