Skip to content

Commit

Permalink
Modularize compile, original builder.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Nov 21, 2023
1 parent f283dc5 commit d64adb4
Show file tree
Hide file tree
Showing 15 changed files with 908 additions and 849 deletions.
1,074 changes: 278 additions & 796 deletions src/compile/build.ts

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions src/compile/external.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import vscode from 'vscode'
import { replaceArgumentPlaceholders } from '../utils/utils'

import { lw } from '../lw'
import type { Tool } from '../types'
import { queue } from './queue'

const logger = lw.log('Build', 'External')

/**
* Build LaTeX project using external command. This function creates a
* {@link Tool} containing the external command info and adds it to the
* queue. After that, this function tries to initiate a {@link buildLoop} if
* there is no one running.
*
* @param command The external command to be executed.
* @param args The arguments to {@link command}.
* @param pwd The current working directory. This argument will be overrided
* if there are workspace folders. If so, the root of the first workspace
* folder is used as the current working directory.
* @param rootFile Path to the root LaTeX file.
*/
export async function build(command: string, args: string[], pwd: string, buildLoop: () => Promise<void>, rootFile?: string) {
if (lw.compile.compiling) {
void logger.showErrorMessageWithCompilerLogButton('Please wait for the current build to finish.')
return
}

await vscode.workspace.saveAll()

const workspaceFolder = vscode.workspace.workspaceFolders?.[0]
const cwd = workspaceFolder?.uri.fsPath || pwd
if (rootFile !== undefined) {
args = args.map(replaceArgumentPlaceholders(rootFile, lw.file.tmpDirPath))
}
const tool: Tool = { name: command, command, args }

queue.add(tool, rootFile, 'External', Date.now(), true, cwd)

await buildLoop()
}
14 changes: 14 additions & 0 deletions src/compile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { ChildProcessWithoutNullStreams } from 'child_process'
import { build, autoBuild } from './build'
import { terminate } from './terminate'

export const compile = {
build,
autoBuild,
terminate,
compiling: false,
lastBuildTime: 0,
compiledPDFPath: '',
compiledRootFile: '' as string | undefined,
process: undefined as ChildProcessWithoutNullStreams | undefined
}
105 changes: 105 additions & 0 deletions src/compile/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import vscode from 'vscode'
import type { ExternalStep, RecipeStep, Step, StepQueue, Tool } from '../types'

const stepQueue: StepQueue = { steps: [], nextSteps: [] }

/**
* Add a {@link Tool} to the queue. The input {@link tool} is first wrapped
* to be a {@link RecipeStep} or {@link ExternalStep} with additional
* information, according to the nature {@link isExternal}. Then the wrapped
* {@link Step} is added to the current {@link steps} if they belongs to the
* same recipe, determined by the same {@link timestamp}, or added to the
* {@link nextSteps} for later execution.
*
* @param tool The {@link Tool} to be added to the queue.
* @param rootFile Path to the root LaTeX file.
* @param recipeName The name of the recipe which the {@link tool} belongs
* to.
* @param timestamp The timestamp when the recipe is called.
* @param isExternal Whether the {@link tool} is an external command.
* @param cwd The current working directory if the {@link tool} is an
* external command.
*/
function add(tool: Tool, rootFile: string | undefined, recipeName: string, timestamp: number, isExternal: boolean = false, cwd?: string) {
let step: Step
if (!isExternal && rootFile !== undefined) {
step = tool as RecipeStep
step.rootFile = rootFile
step.recipeName = recipeName
step.timestamp = timestamp
step.isRetry = false
step.isExternal = false
step.isSkipped = false
} else {
step = tool as ExternalStep
step.recipeName = 'External'
step.timestamp = timestamp
step.isExternal = true
step.cwd = cwd || ''
}
if (stepQueue.steps.length === 0 || step.timestamp === stepQueue.steps[0].timestamp) {
step.index = (stepQueue.steps[stepQueue.steps.length - 1]?.index ?? -1) + 1
stepQueue.steps.push(step)
} else if (stepQueue.nextSteps.length === 0 || step.timestamp === stepQueue.nextSteps[0].timestamp){
step.index = (stepQueue.nextSteps[stepQueue.nextSteps.length - 1]?.index ?? -1) + 1
stepQueue.nextSteps.push(step)
} else {
step.index = 0
stepQueue.nextSteps = [ step ]
}
}

function prepend(step: Step) {
stepQueue.steps.unshift(step)
}

function clear() {
stepQueue.nextSteps = []
stepQueue.steps = []
}

function isLastStep(step: Step) {
return stepQueue.steps.length === 0 || stepQueue.steps[0].timestamp !== step.timestamp
}

function getStepString(step: Step): string {
let stepString: string
if (step.timestamp !== stepQueue.steps[0]?.timestamp && step.index === 0) {
stepString = step.recipeName
} else if (step.timestamp === stepQueue.steps[0]?.timestamp) {
stepString = `${step.recipeName}: ${step.index + 1}/${stepQueue.steps[stepQueue.steps.length - 1].index + 1} (${step.name})`
} else {
stepString = `${step.recipeName}: ${step.index + 1}/${step.index + 1} (${step.name})`
}
if(step.rootFile) {
const rootFileUri = vscode.Uri.file(step.rootFile)
const configuration = vscode.workspace.getConfiguration('latex-workshop', rootFileUri)
const showFilename = configuration.get<boolean>('latex.build.rootfileInStatus', false)
if(showFilename) {
const relPath = vscode.workspace.asRelativePath(step.rootFile)
stepString = `${relPath}: ${stepString}`
}
}
return stepString
}

function getStep(): Step | undefined {
let step: Step | undefined
if (stepQueue.steps.length > 0) {
step = stepQueue.steps.shift()
} else if (stepQueue.nextSteps.length > 0) {
stepQueue.steps = stepQueue.nextSteps
stepQueue.nextSteps = []
step = stepQueue.steps.shift()
}
return step
}

export const queue = {
add,
prepend,
clear,
isLastStep,
getStep,
getStepString
}
Loading

0 comments on commit d64adb4

Please sign in to comment.