Skip to content

Commit

Permalink
Merge pull request #3912 from Hannah-Sten/bibtex-distribution
Browse files Browse the repository at this point in the history
Add WSL support to the bibtex run configuration
  • Loading branch information
PHPirates authored Feb 22, 2025
2 parents a950d8a + 681d5a4 commit bc3125a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

### Added
* Add WSL support to the bibtex run configuration, using a new setting to choose the LaTeX distribution
* Default to 0.15.0 behaviour if inputs field is not found in Tectonic.toml

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import com.intellij.execution.process.KillableProcessHandler
import com.intellij.execution.process.ProcessHandler
import com.intellij.execution.process.ProcessTerminatedListener
import com.intellij.execution.runners.ExecutionEnvironment
import com.intellij.util.execution.ParametersListUtil
import nl.hannahsten.texifyidea.run.compiler.LatexCompiler.Companion.toWslPathIfNeeded
import nl.hannahsten.texifyidea.run.latex.LatexDistributionType

/**
* @author Sten Wessel
Expand All @@ -19,7 +22,25 @@ open class BibtexCommandLineState(
@Throws(ExecutionException::class)
override fun startProcess(): ProcessHandler {
val compiler = runConfig.compiler ?: throw ExecutionException("No valid compiler specified.")
val command: List<String> = compiler.getCommand(runConfig, environment.project) ?: throw ExecutionException("Compile command could not be created.")
val compilerCommand = compiler.getCommand(runConfig, environment.project)?.toMutableList() ?: throw ExecutionException("Compile command could not be created.")

// See LatexCompiler#getCommand
val command = if (runConfig.getLatexDistributionType() == LatexDistributionType.WSL_TEXLIVE) {
var wslCommand = GeneralCommandLine(compilerCommand).commandLineString

// Custom compiler arguments specified by the user
runConfig.compilerArguments?.let { arguments ->
ParametersListUtil.parse(arguments)
.forEach { wslCommand += " $it" }
}

wslCommand += " ${runConfig.bibWorkingDir?.path?.toWslPathIfNeeded(runConfig.getLatexDistributionType())}"

mutableListOf("bash", "-ic", wslCommand)
}
else {
compilerCommand
}

// The working directory is as specified by the user in the working directory.
// The fallback (if null or empty) directory is the directory of the main file.
Expand Down
28 changes: 28 additions & 0 deletions src/nl/hannahsten/texifyidea/run/bibtex/BibtexRunConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import nl.hannahsten.texifyidea.run.bibtex.logtab.BibtexLogTabComponent
import nl.hannahsten.texifyidea.run.compiler.BibliographyCompiler
import nl.hannahsten.texifyidea.run.latex.LatexDistributionType
import nl.hannahsten.texifyidea.settings.sdk.LatexSdkUtil
import org.jdom.Element

/**
Expand All @@ -32,6 +34,7 @@ class BibtexRunConfiguration(
private const val COMPILER_ARGUMENTS = "compiler-arguments"
private const val MAIN_FILE = "main-file"
private const val AUX_DIR = "aux-dir"
private const val LATEX_DISTRIBUTION = "latex-distribution"
}

var compiler: BibliographyCompiler? = null
Expand All @@ -45,6 +48,7 @@ class BibtexRunConfiguration(

var mainFile: VirtualFile? = null
var bibWorkingDir: VirtualFile? = null
internal var latexDistribution = LatexDistributionType.PROJECT_SDK

override fun getConfigurationEditor(): SettingsEditor<out RunConfiguration> = BibtexSettingsEditor(project)

Expand Down Expand Up @@ -108,6 +112,15 @@ class BibtexRunConfiguration(
else {
null
}

val distributionText = parent.getChildText(LATEX_DISTRIBUTION)
if (distributionText == null) {
// Make sure migration doesn't break the run configuration: keep the old behaviour
this.latexDistribution = LatexDistributionType.PROJECT_SDK
}
else {
this.latexDistribution = LatexDistributionType.valueOfIgnoreCase(distributionText)
}
}

override fun writeExternal(element: Element) {
Expand All @@ -122,6 +135,7 @@ class BibtexRunConfiguration(
this.environmentVariables.writeExternal(parent)
parent.addContent(Element(MAIN_FILE).apply { text = mainFile?.path ?: "" })
parent.addContent(Element(AUX_DIR).apply { text = bibWorkingDir?.path ?: "" })
parent.addContent(Element(LATEX_DISTRIBUTION).also { it.text = latexDistribution.name })
}

override fun isGeneratedName() = name == suggestedName()
Expand All @@ -131,4 +145,18 @@ class BibtexRunConfiguration(
fun setSuggestedName() {
name = suggestedName()
}

// Similar to LatexRunConfiguration
fun setDefaultDistribution(distributionType: LatexDistributionType) {
latexDistribution = distributionType
}

fun getLatexDistributionType(): LatexDistributionType {
return if (latexDistribution != LatexDistributionType.PROJECT_SDK) {
latexDistribution
}
else {
LatexSdkUtil.getLatexDistributionType(project) ?: LatexDistributionType.TEXLIVE
}
}
}
10 changes: 10 additions & 0 deletions src/nl/hannahsten/texifyidea/run/bibtex/BibtexSettingsEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.ui.RawCommandLineEditor
import com.intellij.ui.SeparatorComponent
import nl.hannahsten.texifyidea.run.compiler.BibliographyCompiler
import nl.hannahsten.texifyidea.run.latex.LatexDistributionType
import java.awt.event.ItemEvent
import javax.swing.JCheckBox
import javax.swing.JComponent
Expand All @@ -33,6 +34,8 @@ class BibtexSettingsEditor(private val project: Project) : SettingsEditor<Bibtex
* Is automatically set based on the LaTeX run config when created. */
private lateinit var bibWorkingDir: LabeledComponent<TextFieldWithBrowseButton>

private lateinit var latexDistribution: LabeledComponent<ComboBox<LatexDistributionType>>

override fun createEditor(): JComponent {
createUIComponents()
return panel
Expand All @@ -46,6 +49,7 @@ class BibtexSettingsEditor(private val project: Project) : SettingsEditor<Bibtex
enableCompilerPath.isSelected = runConfig.compilerPath != null
mainFile.component.text = runConfig.mainFile?.path ?: ""
bibWorkingDir.component.text = runConfig.bibWorkingDir?.path ?: ""
latexDistribution.component.selectedItem = runConfig.latexDistribution
}

override fun applyEditorTo(runConfig: BibtexRunConfiguration) {
Expand All @@ -55,6 +59,7 @@ class BibtexSettingsEditor(private val project: Project) : SettingsEditor<Bibtex
runConfig.environmentVariables = environmentVariables.envData
runConfig.mainFile = if (mainFile.component.text.isNotBlank()) LocalFileSystem.getInstance().findFileByPath(mainFile.component.text) else null
runConfig.bibWorkingDir = if (bibWorkingDir.component.text.isNotBlank()) LocalFileSystem.getInstance().findFileByPath(bibWorkingDir.component.text) else null
runConfig.latexDistribution = latexDistribution.component.selectedItem as LatexDistributionType? ?: LatexDistributionType.PROJECT_SDK
}

private fun createUIComponents() {
Expand Down Expand Up @@ -132,6 +137,11 @@ class BibtexSettingsEditor(private val project: Project) : SettingsEditor<Bibtex
)
bibWorkingDir = LabeledComponent.create(workingDirField, "Working directory for bibtex")
add(bibWorkingDir)

// LaTeX distribution, use project SDK as backwards compatible default
@Suppress("DialogTitleCapitalization")
latexDistribution = LabeledComponent.create(ComboBox(LatexDistributionType.entries.filter { it.isAvailable(project) }.toTypedArray() + arrayOf(LatexDistributionType.PROJECT_SDK)), "LaTeX Distribution")
add(latexDistribution)
}
}
}
4 changes: 3 additions & 1 deletion src/nl/hannahsten/texifyidea/run/compiler/BiberCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nl.hannahsten.texifyidea.run.compiler
import com.intellij.openapi.project.Project
import com.intellij.util.execution.ParametersListUtil
import nl.hannahsten.texifyidea.run.bibtex.BibtexRunConfiguration
import nl.hannahsten.texifyidea.run.compiler.LatexCompiler.Companion.toWslPathIfNeeded

/**
* @author Thomas Schouten
Expand All @@ -18,7 +19,8 @@ internal object BiberCompiler : Compiler<BibtexRunConfiguration> {
// Biber can find auxiliary files, but the flag is different from bibtex.
// The following flag assumes the command is executed in the directory where the .bcf control file is.
// The extra directory added is the directory from which the path to the .bib resource file is searched as specified in the .bcf file.
add("--input-directory=${runConfig.mainFile?.parent?.path ?: ""}")
val mainPath = runConfig.mainFile?.parent?.path?.toWslPathIfNeeded(runConfig.getLatexDistributionType()) ?: ""
add("--input-directory=$mainPath")

runConfig.compilerArguments?.let { addAll(ParametersListUtil.parse(it)) }

Expand Down
10 changes: 7 additions & 3 deletions src/nl/hannahsten/texifyidea/run/compiler/BibtexCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.util.execution.ParametersListUtil
import nl.hannahsten.texifyidea.run.bibtex.BibtexRunConfiguration
import nl.hannahsten.texifyidea.run.compiler.LatexCompiler.Companion.toWslPathIfNeeded
import nl.hannahsten.texifyidea.run.latex.LatexDistributionType
import nl.hannahsten.texifyidea.settings.sdk.LatexSdkUtil

/**
Expand All @@ -25,9 +27,11 @@ internal object BibtexCompiler : Compiler<BibtexRunConfiguration> {
runConfig.compilerArguments?.let { addAll(ParametersListUtil.parse(it)) }

// Include files from auxiliary directory on Windows
if (LatexSdkUtil.isMiktexAvailable) {
add("-include-directory=${runConfig.mainFile?.parent?.path ?: ""}")
addAll(moduleRoots.map { "-include-directory=${it.path}" })
// We (mis)use project SDK as default setting for backwards compatibility
if ((runConfig.getLatexDistributionType() == LatexDistributionType.PROJECT_SDK && LatexSdkUtil.isMiktexAvailable) || runConfig.getLatexDistributionType().isMiktex(project)) {
val mainPath = runConfig.mainFile?.parent?.path?.toWslPathIfNeeded(runConfig.getLatexDistributionType()) ?: ""
add("-include-directory=$mainPath")
addAll(moduleRoots.map { "-include-directory=${it.path.toWslPathIfNeeded(runConfig.getLatexDistributionType())}" })
}

add(runConfig.mainFile?.nameWithoutExtension ?: return null)
Expand Down
24 changes: 12 additions & 12 deletions src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,6 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
},
;

/**
* Convert Windows paths to WSL paths.
*/
private fun String.toPath(runConfig: LatexRunConfiguration): String =
if (runConfig.getLatexDistributionType() == LatexDistributionType.WSL_TEXLIVE) {
runCommand("wsl", "wslpath", "-a", this) ?: this
}
else this

/**
* Get the execution command for the latex compiler.
*
Expand Down Expand Up @@ -315,7 +306,7 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
"/out"
}
else {
runConfig.outputPath.getAndCreatePath()?.path?.toPath(runConfig)
runConfig.outputPath.getAndCreatePath()?.path?.toWslPathIfNeeded(runConfig.getLatexDistributionType())
}

val auxilPath = if (runConfig.getLatexDistributionType() == LatexDistributionType.DOCKER_MIKTEX) {
Expand All @@ -325,7 +316,7 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
null
}
else {
runConfig.auxilPath.getAndCreatePath()?.path?.toPath(runConfig)
runConfig.auxilPath.getAndCreatePath()?.path?.toWslPathIfNeeded(runConfig.getLatexDistributionType())
}

val command = createCommand(
Expand All @@ -345,7 +336,7 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
.forEach { wslCommand += " $it" }
}

wslCommand += " ${mainFile.path.toPath(runConfig)}"
wslCommand += " ${mainFile.path.toWslPathIfNeeded(runConfig.getLatexDistributionType())}"

return mutableListOf("bash", "-ic", wslCommand)
}
Expand Down Expand Up @@ -481,6 +472,15 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
it.executableName.equals(exe, true)
} ?: PDFLATEX
}

/**
* Convert Windows paths to WSL paths.
*/
fun String.toWslPathIfNeeded(distributionType: LatexDistributionType): String =
if (distributionType == LatexDistributionType.WSL_TEXLIVE) {
runCommand("wsl", "wslpath", "-a", this) ?: this
}
else this
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ class LatexRunConfiguration(
if (compilerArguments != null) bibtexRunConfiguration.compilerArguments = compilerArguments
bibtexRunConfiguration.mainFile = mainFile
bibtexRunConfiguration.setSuggestedName()
bibtexRunConfiguration.setDefaultDistribution(latexDistribution)

// On non-MiKTeX systems, add bibinputs for bibtex to work
if (!latexDistribution.isMiktex(project)) {
Expand Down

0 comments on commit bc3125a

Please sign in to comment.