Skip to content

Commit

Permalink
Add package path input to typst task and rework extension DSL for loc…
Browse files Browse the repository at this point in the history
…al packages
  • Loading branch information
tamaracha committed Oct 21, 2024
1 parent c26a305 commit a27535f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `localPackages` property in `TypstExtension` which is configured with a platform-dependent convention where local Typst packages are installed.
- `packagePath` can be set for `TypstCompileTask` which lets Gradle track changes in local package files and Typst to look for packages in the given directory. This is configured with `localPackages` from the Typst extension by default.

### Removed

- `useLocalPackages` function in `TypstSourceSet`

## [0.3.0] - 2024-10-19

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,17 @@ class GradleTypstPlugin : Plugin<Project> {
task.target.convention(project.layout.buildDirectory.dir("tools/typst"))
}
extension.compiler.convention(extractTask.flatMap { it.target })
val appDataDir = if (currentOs.isMacOsX) {
project.layout.projectDirectory.dir(project.providers.systemProperty("user.home")).map { it.dir("Library/Application Support") }
} else if (currentOs.isLinux) {
project.layout.projectDirectory.dir(project.providers.environmentVariable("XDG_DATA_HOME"))
} else {
project.layout.projectDirectory.dir(project.providers.environmentVariable("APPDATA"))
}
extension.localPackages.convention(appDataDir.map { it.dir("typst/packages") })
project.tasks.withType(TypstCompileTask::class.java).configureEach { task ->
task.compiler.convention(extension.compiler)
task.packagePath.set(extension.localPackages)
task.root.convention(project.layout.projectDirectory.asFile.absolutePath)
task.creationTimestamp.convention(extension.creationTimestamp)
task.useSystemFonts.convention(false)
Expand All @@ -52,7 +61,6 @@ class GradleTypstPlugin : Plugin<Project> {
s.data.add(sourceRoot.dir("data"))
s.fonts.add(sourceRoot.dir("fonts"))
s.images.add(imagesRoot)
s.useLocalPackages()
s.destinationDir.convention(project.layout.buildDirectory.dir("typst/${s.name}"))
val convertImagesTask = project.tasks.register("convert${title}Images", ConvertImagesTask::class.java) { task ->
task.onlyIf { task.source.get().asFile.exists() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ abstract class TypstExtension {
abstract val version: Property<String>
abstract val creationTimestamp: Property<String>
abstract val compiler: DirectoryProperty
abstract val localPackages: DirectoryProperty
companion object {
const val EXTENSION_NAME = "typst"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package de.infolektuell.gradle.typst.extensions

import org.gradle.api.Named
import org.gradle.api.file.*
import org.gradle.api.file.Directory
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.*
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import javax.inject.Inject

abstract class TypstSourceSet @Inject constructor(private val providers: ProviderFactory, private val layout: ProjectLayout) : Named {
abstract class TypstSourceSet : Named {
abstract val destinationDir: DirectoryProperty
abstract val documents: ListProperty<String>
abstract val inputs: MapProperty<String, String>
Expand All @@ -28,18 +27,4 @@ abstract class TypstSourceSet @Inject constructor(private val providers: Provide
images.addAll(sourceSet.flatMap { it.images })
fonts.addAll(sourceSet.flatMap { it.fonts })
}
fun useLocalPackages() {
typst.add(typstLocalPackages())
}
private fun typstLocalPackages(): Provider<Directory> {
val currentOs = DefaultNativePlatform.getCurrentOperatingSystem()
val appDataDir = if (currentOs.isMacOsX) {
layout.projectDirectory.dir(providers.systemProperty("user.home")).map { it.dir("Library/Application Support") }
} else if (currentOs.isLinux) {
layout.projectDirectory.dir(providers.environmentVariable("XDG_DATA_HOME"))
} else {
layout.projectDirectory.dir(providers.environmentVariable("APPDATA"))
}
return appDataDir.map { it.dir("typst/packages") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ abstract class TypstCompileTask @Inject constructor(private val executor: Worker
protected abstract class TypstAction @Inject constructor(private val execOperations: ExecOperations) : WorkAction<TypstAction.Params> {
interface Params : WorkParameters {
val executable: Property<String>
val packagePath: DirectoryProperty
val document: RegularFileProperty
val root: Property<String>
val variables: MapProperty<String, String>
Expand All @@ -43,9 +44,8 @@ abstract class TypstCompileTask @Inject constructor(private val executor: Worker
parameters.fontDirectories.get().forEach { action.args("--font-path", it.asFile.absolutePath) }
parameters.useSystemFonts.get().not().takeIf { it }?.let { action.args("--ignore-system-fonts") }
parameters.variables.get().forEach { (k, v) -> action.args("--input", "$k=$v") }
if (parameters.creationTimestamp.isPresent) {
action.args("--creation-timestamp", parameters.creationTimestamp.get())
}
if (parameters.creationTimestamp.isPresent) action.args("--creation-timestamp", parameters.creationTimestamp.get())
if (parameters.packagePath.isPresent) action.args("--package-path", parameters.packagePath.asFile.get().absolutePath)
action.args(parameters.document.get().asFile.absolutePath)
.args(parameters.target.asFile.get().absolutePath)
}
Expand All @@ -54,6 +54,9 @@ abstract class TypstCompileTask @Inject constructor(private val executor: Worker

@get:InputDirectory
abstract val compiler: DirectoryProperty
@get:Optional
@get:InputDirectory
abstract val packagePath: DirectoryProperty
@get:InputFiles
abstract val documents: ListProperty<RegularFile>
@get:Input
Expand Down Expand Up @@ -81,6 +84,7 @@ abstract class TypstCompileTask @Inject constructor(private val executor: Worker
documents.get().forEach { document ->
queue.submit(TypstAction::class.java) { params ->
params.executable.set(executable)
params.packagePath.set(packagePath)
params.document.set(document)
params.root.set(root)
params.variables.set(variables)
Expand Down

0 comments on commit a27535f

Please sign in to comment.