-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cache config and add options to configure globs
- Loading branch information
1 parent
6a69acb
commit 1d0c32b
Showing
8 changed files
with
136 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import vscode, { CancellationTokenSource, FileType } from 'vscode' | ||
import { findAntoraFiles } from './findAntoraFiles' | ||
import yaml from 'js-yaml' | ||
import ospath from 'path' | ||
|
||
export class AntoraConfig { | ||
public contentSourceRootPath: string | ||
public contentSourceRootFsPath: string | ||
|
||
private static versionMap = new Map<string, number>() | ||
|
||
constructor (public uri: vscode.Uri, public config: { [key: string]: any }) { | ||
const path = uri.path | ||
this.contentSourceRootPath = path.slice(0, path.lastIndexOf('/')) | ||
this.contentSourceRootFsPath = ospath.dirname(uri.fsPath) | ||
if (config.version === true || config.version === undefined) { | ||
config.version = this.getVersionForPath(path) | ||
} | ||
} | ||
|
||
public getVersionForPath (path: string): string { | ||
const version = AntoraConfig.versionMap.get(path) | ||
if (version) return `V-${version}` | ||
|
||
const nextVersion = AntoraConfig.versionMap.size + 1 | ||
AntoraConfig.versionMap.set(path, nextVersion) | ||
return `V-${nextVersion}` | ||
} | ||
} | ||
|
||
const cache = { | ||
antoraConfigs: [] as AntoraConfig[], | ||
antoraConfigUris: [] as vscode.Uri[], | ||
} | ||
|
||
let ongoingRefreshPromise : Promise<void> | undefined | ||
|
||
async function refreshAntoraConfigs (token?: vscode.CancellationToken) { | ||
if (!ongoingRefreshPromise) { | ||
ongoingRefreshPromise = asyncTriggerRefresh(token).then(() => { | ||
ongoingRefreshPromise = undefined | ||
}) | ||
} | ||
await ongoingRefreshPromise | ||
|
||
async function asyncTriggerRefresh (token: vscode.CancellationToken): Promise<void> { | ||
const antoraConfigArray = vscode.workspace.getConfiguration('asciidoc.antora.search', null).get<string[]>('antoraConfig') | ||
const glob = '{' + antoraConfigArray.join(',') + '}' | ||
const antoraConfigUris = await findAntoraFiles(glob, token) | ||
|
||
const cancellationToken = new CancellationTokenSource() | ||
cancellationToken.token.onCancellationRequested((e) => { | ||
console.log('Cancellation requested, cause: ' + e) | ||
}) | ||
// check for Antora configuration | ||
const antoraConfigs = (await Promise.all(antoraConfigUris.map(async (antoraConfigUri) => { | ||
let config = {} | ||
const parentPath = antoraConfigUri.path.slice(0, antoraConfigUri.path.lastIndexOf('/')) | ||
const parentDirectoryStat = await vscode.workspace.fs.stat(antoraConfigUri.with({ path: parentPath })) | ||
if (parentDirectoryStat.type === (FileType.Directory | FileType.SymbolicLink) || parentDirectoryStat.type === FileType.SymbolicLink) { | ||
// ignore! | ||
return undefined | ||
} | ||
try { | ||
config = yaml.load(await vscode.workspace.fs.readFile(antoraConfigUri)) || {} | ||
} catch (err) { | ||
console.log(`Unable to parse ${antoraConfigUri}, cause:` + err.toString()) | ||
} | ||
return new AntoraConfig(antoraConfigUri, config) | ||
}))).filter((c) => c) // filter undefined | ||
|
||
cache.antoraConfigs = antoraConfigs | ||
cache.antoraConfigUris = antoraConfigUris | ||
} | ||
} | ||
|
||
export async function getAntoraConfigs (token?: vscode.CancellationToken) { | ||
if (!cache.antoraConfigs.length) { | ||
await refreshAntoraConfigs(token) | ||
} | ||
return cache.antoraConfigs | ||
} | ||
|
||
export async function getAntoraConfigUris (token?: vscode.CancellationToken) { | ||
if (!cache.antoraConfigUris.length) { | ||
await refreshAntoraConfigs(token) | ||
} | ||
return cache.antoraConfigUris | ||
} | ||
|
||
// initial refresh of antora configs | ||
refreshAntoraConfigs() | ||
|
||
// Update the configs when an antora.yml file changes | ||
vscode.workspace.onDidChangeTextDocument(async (event) => { | ||
if (event.document.uri.path.endsWith('antora.yml')) { | ||
await refreshAntoraConfigs() | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import vscode from 'vscode' | ||
|
||
export async function findAntoraFiles (glob: string, token?: vscode.CancellationToken) { | ||
const excludeArray = vscode.workspace.getConfiguration('asciidoc.antora.search', null).get<string[]>('exclude') | ||
const excludeString = '{' + excludeArray.join(',') + '}' | ||
return vscode.workspace.findFiles(glob, excludeString, 100, token) | ||
} |