ext {
	micrometerDocsVersion='1.0.2'
}


antora {
	version = '3.2.0-alpha.2'
	playbook = file('src/reference/antora/antora-playbook.yml')
	options = [
			'to-dir' : project.layout.buildDirectory.dir('site').get().toString(),
			clean: true,
			fetch: !project.gradle.startParameter.offline,
			stacktrace: true
	]
	dependencies = [
			'@antora/atlas-extension': '1.0.0-alpha.2',
			'@antora/collector-extension': '1.0.0-alpha.3',
			'@asciidoctor/tabs': '1.0.0-beta.6',
			'@springio/antora-extensions': '1.8.1',
			'@springio/asciidoctor-extensions': '1.0.0-alpha.9',
	]
}

tasks.named('generateAntoraYml') {
	asciidocAttributes = project.provider( {
		return ['project-version' : project.version ]
	} )
	baseAntoraYmlFile = file('src/reference/antora/antora.yml')
}

tasks.register('createAntoraPartials', Sync) {
	from tasks.filterMetricsDocsContent.outputs
	into layout.buildDirectory.dir('generated-antora-resources/modules/ROOT/partials')
}

tasks.register('generateAntoraResources') {
	dependsOn 'createAntoraPartials'
	dependsOn 'generateAntoraYml'
}

configurations {
	micrometerDocs
}

dependencies {
	micrometerDocs "io.micrometer:micrometer-docs-generator:$micrometerDocsVersion"
}

def observationInputDir = file('spring-integration-core/src/main/java/org/springframework/integration/support/management/observation').absolutePath
def generatedDocsDir = file('build/reference/generated').absolutePath

tasks.register('generateObservabilityDocs', JavaExec) {
	inputs.dir(observationInputDir)
	outputs.dir(generatedDocsDir)
	classpath configurations.micrometerDocs
	args observationInputDir, /.+/, generatedDocsDir
	mainClass = 'io.micrometer.docs.DocsGeneratorCommand'
}

tasks.register('filterMetricsDocsContent', Copy) {
	dependsOn generateObservabilityDocs
	from generatedDocsDir
	include '_*.adoc'
	into generatedDocsDir
	rename { filename -> filename.replace '_', '' }
	filter { line -> line.replaceAll('org.springframework.integration', 'o.s.i') }
}