-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.js
78 lines (71 loc) · 3.34 KB
/
extension.js
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
"use strict";
const {
MavenRepository,
MavenContentCoordinate
} = require('./maven-types')
const MavenClient = require('./maven-client')
const ContentSourceFactory = require('./content-source-factory')
/**
* This antora extension allows to add content from maven coordinates.
*/
class MavenContentSourceExtension {
antoraContext;
config;
mavenClient;
git;
logger;
/**
* The entry point of the extension, called by antora site generator if extension is enabled in playbook.
* @param config The config as seen in the playbook
*/
static register({config}) {
new MavenContentSourceExtension(this, config)
}
/**
* constructs the extension object and hooks it into the site generation pipeline.
* @param context Antora generator context
* @param config Extension configuration
*/
constructor(context, config) {
;(this.antoraContext = context)
// need to explicitly bind `this` when passing over a function:
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#binding_this_with_prototype_and_static_methods
.on('playbookBuilt', this.onPlaybookBuilt.bind(this))
this.config = config;
this.logger = this.antoraContext.getLogger('maven-content');
try {
this.git = this.antoraContext.require('@antora/content-aggregator/lib/git.js')
} catch (e) {
// starting with antora 3.1 git export has been made explicit with a different name. So let's try the new one.
// see https://gitlab.com/antora/antora/-/issues/984
this.git = this.antoraContext.require('@antora/content-aggregator/git')
}
this.mavenClient = new MavenClient(this.logger);
this.contentSourceFactory = new ContentSourceFactory(this.mavenClient, this.git, this.logger);
}
async onPlaybookBuilt({playbook}) {
this.logger.info("Add Maven Content Sources to playbook...");
const repositories = this.config.repositories?.map(entry => new MavenRepository(entry)) || [];
const coordinates = this.config.sources?.map(entry => new MavenContentCoordinate(entry)) || [];
if (this.config.mavenSettings) {
const settingsFile = this.config.mavenSettings === true ? this.mavenClient.findMavenSettingsFile() : this.config.mavenSettings;
repositories.push(...(await this.mavenClient.extractRepositoriesFromSettingsFile(settingsFile)));
}
// copy playbook as it is frozen deeply...
const mutablePlaybook = MavenContentSourceExtension.#unfreezePlaybookSources(playbook);
await this.contentSourceFactory.produceContentSourcesIntoPlaybook(repositories, coordinates, mutablePlaybook);
this.antoraContext.updateVariables({playbook: mutablePlaybook})
}
static #unfreezePlaybookSources(playbook) {
// see https://gitlab.com/antora/antora/-/issues/930
let mutablePlaybook = Object.assign({}, playbook, {env: playbook.env})
if (playbook.content) {
mutablePlaybook.content = Object.assign({}, playbook.content);
if (playbook.content.sources !== undefined) {
mutablePlaybook.content.sources = Object.assign([], playbook.content.sources)
}
}
return mutablePlaybook;
}
}
module.exports = MavenContentSourceExtension