Skip to content

Commit 846b750

Browse files
committedJan 7, 2025·
Extension: Implement dynamic loading attributes
1 parent 9bcdc88 commit 846b750

12 files changed

+3832
-2918
lines changed
 

‎.drone.star

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ def build(ctx, environment, latest_version, deployment_branch, base_branch, pdf_
9191
"pull": "always",
9292
"image": "owncloudci/nodejs:18",
9393
"commands": [
94-
"yarn install",
94+
"npm install",
9595
],
9696
},
9797
{
9898
"name": "docs-build",
9999
"pull": "always",
100100
"image": "owncloudci/nodejs:18",
101101
"commands": [
102-
"yarn antora",
102+
"npm run antora",
103103
],
104104
},
105105
{

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
global-attributes.yml
12
site-dev.yml
23
yarn-error.log
34
cache/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
// Console print antora.yml attributes used per component
4+
5+
module.exports.register = function () {
6+
this.once('contentClassified', ({ playbook, contentCatalog }) => {
7+
console.log('antora-playbook.yml attributes')
8+
console.log(playbook.asciidoc.attributes)
9+
contentCatalog.getComponents().forEach((component) => {
10+
component.versions.forEach((componentVersion) => {
11+
getUniqueOrigins(contentCatalog, componentVersion).forEach((origin) => {
12+
console.log(`antora.yml attributes (${componentVersion.version}@${componentVersion.name})`)
13+
console.log(origin.descriptor.asciidoc?.attributes || {})
14+
})
15+
})
16+
})
17+
})
18+
}
19+
20+
function getUniqueOrigins (contentCatalog, componentVersion) {
21+
return contentCatalog.findBy({ component: componentVersion.name, version: componentVersion.version }).reduce((origins, file) => {
22+
const origin = file.src.origin
23+
if (origin && !origins.includes(origin)) origins.push(origin)
24+
return origins
25+
}, [])
26+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
// Console print site.yml attributes used per component build
4+
5+
module.exports.register = function () {
6+
this.once('contentClassified', ({ siteAsciiDocConfig, contentCatalog }) => {
7+
console.log('site-wide attributes')
8+
console.log(siteAsciiDocConfig.attributes)
9+
contentCatalog.getComponents().forEach((component) => {
10+
component.versions.forEach((componentVersion) => {
11+
console.log(`${componentVersion.version}@${componentVersion.name} attributes`)
12+
console.log(componentVersion.asciidoc.attributes)
13+
})
14+
})
15+
})
16+
}

‎ext-antora/comp-version.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
// Extension to print the component + version that will be processed
4+
5+
module.exports.register = function () {
6+
this.once('contentAggregated', ({ contentAggregate }) => {
7+
console.log('\nProcessing the following components and versions\n')
8+
const component_table = []
9+
contentAggregate.forEach((bucket) => {
10+
component_table.push ({Name: bucket.name, Version: bucket.version || '~', Files: bucket.files.length})
11+
})
12+
console.table(component_table)
13+
console.log() // do not delete, else we get a double empty line
14+
})
15+
}
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// v1.0.0
2+
"use strict"
3+
4+
const fs = require('fs')
5+
const http = require('http')
6+
const https = require('https')
7+
const yaml = require('js-yaml')
8+
9+
// this extension loads (global) attributes into the playbook (site.yml)
10+
// !! attributes in the playbook take precedence over loaded attributes !!
11+
// allows test building a repo like in multi repo envs with custom local attribute values
12+
// you can temporarily disable loading by setting 'enabled: false'
13+
// error handling:
14+
// if no 'attributefile' is configured: warn, but continue processing
15+
// if loading or processing the file caused an error (like file not found, needs fixing): stop
16+
17+
module.exports.register = function ({ config }) {
18+
const logger = this.getLogger('load-global-site-attributes-extension')
19+
let attrib_file = ''
20+
let attrib_yaml = {}
21+
let orig_playbook = {}
22+
let result = {}
23+
24+
this.on("playbookBuilt", async ({ playbook }) => {
25+
// get the original playbook asciidoc attributes, note it can be empty
26+
orig_playbook = JSON.parse(JSON.stringify(playbook.asciidoc.attributes)) || {}
27+
28+
// only if attributefile is configured in site.yml pointing to a resource (file or url)
29+
if (config.attributefile) {
30+
try {
31+
// define the get function to use and load the file
32+
if (config.attributefile.startsWith('http')) {
33+
attrib_file = await get_file_from_url(config.attributefile)
34+
} else {
35+
attrib_file = await get_file_from_local(config.attributefile)
36+
}
37+
38+
// convert and update
39+
attrib_yaml = await convert_yaml(attrib_file)
40+
result = Object.assign(attrib_yaml, playbook.asciidoc.attributes)
41+
playbook.asciidoc.attributes = result
42+
this.updateVariables( playbook )
43+
44+
// loading or processing the file caused an error
45+
} catch (error) {
46+
logger.error(error)
47+
this.stop()
48+
}
49+
} else {
50+
logger.warn('attributefile is not configured in the playbook (site.yml).')
51+
}
52+
// console.log(this.getVariables())
53+
})
54+
}
55+
56+
function get_file_from_url(url) {
57+
// promise a file from url
58+
// when executed it returns the contents if found
59+
return new Promise((resolve, reject) => {
60+
const client = url.startsWith('https') ? https : http
61+
const req = client.request(url, (res) => {
62+
if (res.statusCode < 200 || res.statusCode >= 300) {
63+
reject(`Request Failed.\n` +
64+
`Status Code: ${res.statusCode}\n` +
65+
`${url}`)
66+
}
67+
var body = []
68+
res.on('data', function(chunk) {
69+
body.push(chunk)
70+
})
71+
res.on('end', function() {
72+
try {
73+
body = Buffer.concat(body).toString()
74+
} catch(error) {
75+
reject(error)
76+
}
77+
resolve(body)
78+
})
79+
})
80+
req.on('error', (error) => {
81+
reject(error.message)
82+
})
83+
// send the request
84+
req.end()
85+
})
86+
}
87+
88+
function get_file_from_local(file) {
89+
// promise a file from local filesystem
90+
// when executed it returns the contents if found
91+
return new Promise((resolve, reject) => {
92+
fs.readFile(file, 'utf8', (error, data) => {
93+
if (error) reject(error)
94+
resolve(data)
95+
})
96+
})
97+
}
98+
99+
function convert_yaml(data) {
100+
// promise to parse yaml data
101+
// when executed it returns the parsed contents
102+
return new Promise((resolve, reject) => {
103+
try {
104+
var d = yaml.load(data)
105+
// the parser found an error, no object will be returned
106+
} catch (error) {
107+
reject(`yaml parser: ` + error)
108+
}
109+
resolve(d)
110+
})
111+
}

‎lib/extensions/remote-include-processor.js

-203
This file was deleted.

0 commit comments

Comments
 (0)