Skip to content

Commit

Permalink
Adding $only filter
Browse files Browse the repository at this point in the history
  • Loading branch information
osfameron committed Jun 8, 2023
1 parent d108171 commit e05b7bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
6 changes: 5 additions & 1 deletion patch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ asciidoc:
content:
sources:
$prune: 2 # just keep X branches
$local: # only the ones we have checked out in ../
$local: # only the ones we have checked out locally in ../
paths: ..
only: true

$filter: # restrict to certain tags
tags: mobile

$only: # alternatively just restrict to specific repos
- docs-sync-gateway
- docs-couchbase-lite

# Modify individual source declarations
$override:
docs-sync-gateway:
Expand Down
38 changes: 24 additions & 14 deletions playbook.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $.verbose = false
const playbookFile = argv.playbook ?? argv._.shift() ?? 'antora-playbook.yml'
const patchFile = argv.patch ?? argv._.shift() ?? 'patch.yml'

console.info(`Composing ${playbookFile} with ${patchFile}`)
console.error(`Composing ${playbookFile} with ${patchFile}`)

var playbook = YAML.parse(fs.readFileSync(playbookFile).toString())
const patch = YAML.parse(fs.readFileSync(patchFile).toString())
Expand Down Expand Up @@ -35,6 +35,21 @@ function cmp (a, b) {
}
}


/**
* @param {Array.<>} node - any array node in playbook
* @param {Object} filters - object of key/values to filter for (using `cmp` function)
* All provided filters must match.
*/
function $filter (orig, param) {
if (! Array.isArray(orig)) {
throw new Error(`$filter run on something that isn't an array! ${JSON.stringify(param)}`)
}
return orig.filter(item =>
Object.entries(param)
.every(([k,v]) => cmp(item[k], v)))
}

/**
* @param {Object[]} sources - expects to be called only on `content.sources` from playbook
* @param {Object} config
Expand Down Expand Up @@ -104,9 +119,7 @@ function $prune (sources, limit) {
}

const matchRepo = (url, source) => {
console.log(`Trying to match ${url} with ${source}`)
if (url === '.') {
console.log(`Ooooo, ${source === 'docs-site'}`)
return source === 'docs-site'
}
return path.basename(url, '.git') === source
Expand All @@ -132,19 +145,15 @@ async function $override (sources, overrides) {
}

/**
* @param {Array.<>} node - any array node in playbook
* @param {Object} filters - object of key/values to filter for (using `cmp` function)
* All provided filters must match.
* @param {Object[]} sources - expects to be called only on `content.sources` from playbook
* @param {Array.<string>} only - just the names of repos to keep
*/
function $filter (orig, param) {
if (! Array.isArray(orig)) {
throw new Error(`$filter run on something that isn't an array! ${JSON.stringify(param)}`)
}
return orig.filter(item =>
Object.entries(param)
.every(([k,v]) => cmp(item[k], v)))
async function $only (sources, only) {
return sources.filter(({url, tags}) =>
tags === 'ALL' || only.some(source => matchRepo(url, source)))
}


const functions = {
// explicit override
$replace: (_orig, param) => param,
Expand All @@ -161,7 +170,8 @@ const functions = {
// Special functions on content.sources
$prune,
$local,
$override
$override,
$only
}

async function apply_patch(node, patch) {
Expand Down

0 comments on commit e05b7bf

Please sign in to comment.