-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotnet.ts
329 lines (283 loc) · 10 KB
/
dotnet.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import * as spec from '@jsii/spec';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as xmlbuilder from 'xmlbuilder';
import { TargetBuilder, BuildOptions } from '../builder';
import * as logging from '../logging';
import { JsiiModule } from '../packaging';
import {
PackageInfo,
Target,
TargetOptions,
findLocalBuildDirs,
} from '../target';
import { shell, Scratch, setExtend, filterAsync } from '../util';
import { DotNetGenerator } from './dotnet/dotnetgenerator';
import { toReleaseVersion } from './version-utils';
import { TargetName } from '.';
export const TARGET_FRAMEWORK = 'netcoreapp3.1';
/**
* Build .NET packages all together, by generating an aggregate solution file
*/
export class DotnetBuilder implements TargetBuilder {
private readonly targetName = 'dotnet';
public constructor(
private readonly modules: readonly JsiiModule[],
private readonly options: BuildOptions,
) {}
public async buildModules(): Promise<void> {
if (this.modules.length === 0) {
return;
}
if (this.options.codeOnly) {
// Simple, just generate code to respective output dirs
await Promise.all(
this.modules.map((module) =>
this.generateModuleCode(
module,
this.outputDir(module.outputDirectory),
),
),
);
return;
}
// Otherwise make a single tempdir to hold all sources, build them together and copy them back out
const scratchDirs: Array<Scratch<any>> = [];
try {
const tempSourceDir = await this.generateAggregateSourceDir(this.modules);
scratchDirs.push(tempSourceDir);
// Build solution
logging.debug('Building .NET');
await shell(
'dotnet',
['build', '--force', '--configuration', 'Release'],
{
cwd: tempSourceDir.directory,
retry: { maxAttempts: 5 },
},
);
await this.copyOutArtifacts(tempSourceDir.object);
if (this.options.clean) {
await Scratch.cleanupAll(scratchDirs);
}
} catch (e) {
logging.warn(
`Exception occurred, not cleaning up ${scratchDirs
.map((s) => s.directory)
.join(', ')}`,
);
throw e;
}
}
private async generateAggregateSourceDir(
modules: readonly JsiiModule[],
): Promise<Scratch<TemporaryDotnetPackage[]>> {
return Scratch.make(async (tmpDir: string) => {
logging.debug(`Generating aggregate .NET source dir at ${tmpDir}`);
const csProjs = [];
const ret: TemporaryDotnetPackage[] = [];
// Code generator will make its own subdirectory
const generatedModules = modules.map((mod) =>
this.generateModuleCode(mod, tmpDir).then(() => mod),
);
for await (const mod of generatedModules) {
const loc = projectLocation(mod);
csProjs.push(loc.projectFile);
ret.push({
outputTargetDirectory: mod.outputDirectory,
artifactsDir: path.join(tmpDir, loc.projectDir, 'bin', 'Release'),
});
}
// Use 'dotnet' command line tool to build a solution file from these csprojs
await shell('dotnet', ['new', 'sln', '-n', 'JsiiBuild'], { cwd: tmpDir });
await shell('dotnet', ['sln', 'add', ...csProjs], { cwd: tmpDir });
await this.generateNuGetConfigForLocalDeps(tmpDir);
return ret;
});
}
private async copyOutArtifacts(packages: TemporaryDotnetPackage[]) {
logging.debug('Copying out .NET artifacts');
await Promise.all(packages.map(copyOutIndividualArtifacts.bind(this)));
async function copyOutIndividualArtifacts(
this: DotnetBuilder,
pkg: TemporaryDotnetPackage,
) {
const targetDirectory = this.outputDir(pkg.outputTargetDirectory);
await fs.mkdirp(targetDirectory);
await fs.copy(pkg.artifactsDir, targetDirectory, {
recursive: true,
filter: (_, dst) => {
return dst !== path.join(targetDirectory, TARGET_FRAMEWORK);
},
});
}
}
private async generateModuleCode(
module: JsiiModule,
where: string,
): Promise<void> {
const target = this.makeTarget(module);
logging.debug(`Generating ${this.targetName} code into ${where}`);
await target.generateCode(where, module.tarball);
}
/**
* Decide whether or not to append 'dotnet' to the given output directory
*/
private outputDir(declaredDir: string) {
return this.options.languageSubdirectory
? path.join(declaredDir, this.targetName)
: declaredDir;
}
/**
* Write a NuGet.config that will include build directories for local packages not in the current build
*
*/
private async generateNuGetConfigForLocalDeps(where: string): Promise<void> {
// Traverse the dependency graph of this module and find all modules that have
// an <outdir>/dotnet directory. We will add those as local NuGet repositories.
// This enables building against local modules.
const allDepsOutputDirs = new Set<string>();
const resolvedModules = this.modules.map(async (module) => ({
module,
localBuildDirs: await findLocalBuildDirs(
module.moduleDirectory,
this.targetName,
),
}));
for await (const { module, localBuildDirs } of resolvedModules) {
setExtend(allDepsOutputDirs, localBuildDirs);
// Also include output directory where we're building to, in case we build multiple packages into
// the same output directory.
allDepsOutputDirs.add(this.outputDir(module.outputDirectory));
}
const localRepos = Array.from(allDepsOutputDirs);
// If dotnet-runtime is checked-out and we can find a local repository, add it to the list.
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires,@typescript-eslint/no-require-imports,import/no-extraneous-dependencies
const jsiiDotNetRuntime = require('@jsii/dotnet-runtime');
logging.info(
`Using local version of the DotNet jsii runtime package at: ${jsiiDotNetRuntime.repository}`,
);
localRepos.push(jsiiDotNetRuntime.repository);
} catch {
// Couldn't locate @jsii/dotnet-runtime, which is owkay!
}
// Filter out nonexistant directories, .NET will be unhappy if paths don't exist
const existingLocalRepos = await filterAsync(localRepos, fs.pathExists);
logging.debug('local NuGet repos:', existingLocalRepos);
// Construct XML content.
const configuration = xmlbuilder.create('configuration', {
encoding: 'UTF-8',
});
const packageSources = configuration.ele('packageSources');
const nugetOrgAdd = packageSources.ele('add');
nugetOrgAdd.att('key', 'nuget.org');
nugetOrgAdd.att('value', 'https://api.nuget.org/v3/index.json');
nugetOrgAdd.att('protocolVersion', '3');
existingLocalRepos.forEach((repo, index) => {
const add = packageSources.ele('add');
add.att('key', `local-${index}`);
add.att('value', path.join(repo));
});
if (this.options.arguments['dotnet-nuget-global-packages-folder']) {
// Ensure we're not using the configured cache folder
configuration
.ele('config')
.ele('add')
.att('key', 'globalPackagesFolder')
.att(
'value',
path.resolve(
this.options.arguments['dotnet-nuget-global-packages-folder'],
'.nuget',
'packages',
),
);
}
const xml = configuration.end({ pretty: true });
// Write XML content to NuGet.config.
const filePath = path.join(where, 'NuGet.config');
logging.debug(`Generated ${filePath}`);
await fs.writeFile(filePath, xml);
}
private makeTarget(module: JsiiModule): Dotnet {
return new Dotnet(
{
arguments: this.options.arguments,
assembly: module.assembly,
fingerprint: this.options.fingerprint,
force: this.options.force,
packageDir: module.moduleDirectory,
rosetta: this.options.rosetta,
runtimeTypeChecking: this.options.runtimeTypeChecking,
targetName: this.targetName,
},
this.modules.map((m) => m.name),
);
}
}
interface TemporaryDotnetPackage {
/**
* Where the artifacts will be stored after build (relative to build dir)
*/
artifactsDir: string;
/**
* Where the artifacts ought to go for this particular module
*/
outputTargetDirectory: string;
}
function projectLocation(module: JsiiModule) {
const packageId: string = module.assembly.targets!.dotnet!.packageId;
return {
projectDir: packageId,
projectFile: path.join(packageId, `${packageId}.csproj`),
};
}
export default class Dotnet extends Target {
public static toPackageInfos(assm: spec.Assembly): {
[language: string]: PackageInfo;
} {
const packageId = assm.targets!.dotnet!.packageId;
const version = toReleaseVersion(assm.version, TargetName.DOTNET);
const packageInfo: PackageInfo = {
repository: 'Nuget',
url: `https://www.nuget.org/packages/${packageId}/${version}`,
usage: {
csproj: {
language: 'xml',
code: `<PackageReference Include="${packageId}" Version="${version}" />`,
},
dotnet: {
language: 'console',
code: `dotnet add package ${packageId} --version ${version}`,
},
'packages.config': {
language: 'xml',
code: `<package id="${packageId}" version="${version}" />`,
},
},
};
return { 'C#': packageInfo };
}
public static toNativeReference(_type: spec.Type, options: any) {
return {
'c#': `using ${options.namespace};`,
};
}
protected readonly generator: DotNetGenerator;
public constructor(
options: TargetOptions,
assembliesCurrentlyBeingCompiled: string[],
) {
super(options);
this.generator = new DotNetGenerator(
assembliesCurrentlyBeingCompiled,
options,
);
}
// eslint-disable-next-line @typescript-eslint/require-await
public async build(_sourceDir: string, _outDir: string): Promise<void> {
throw new Error('Should not be called; use builder instead');
}
/* eslint-enable @typescript-eslint/require-await */
}