Advertisement
jacobdotcosta

Gradle LaTeX

Apr 29th, 2013
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.50 KB | None | 0 0
  1. import groovy.text.SimpleTemplateEngine
  2. import java.text.DateFormat
  3. import java.text.SimpleDateFormat
  4. import java.util.jar.Attributes
  5. import org.apache.tools.ant.taskdefs.condition.Os
  6. import org.gradle.api.Task
  7. import org.gradle.api.tasks.StopExecutionException
  8. import org.gradle.api.tasks.wrapper.Wrapper
  9. import org.gradle.util.GradleVersion
  10. import org.slf4j.Logger
  11. import org.slf4j.LoggerFactory
  12. import org.apache.tools.ant.filters.ReplaceTokens
  13.  
  14. apply plugin: 'groovy'
  15.  
  16. type = 'jar'
  17. buildTime = new Date()
  18. versionModifier = null
  19.  
  20. sourceSets {
  21.   main { resources { srcDir 'src/latex' } }
  22. }
  23. // PROPERTIES
  24. def stagingDirName = 'build/staging/docs'
  25. def docsDirName = 'build/docs'
  26.  
  27. task copyDocs << {
  28.   def stagingDir = file(stagingDirName)
  29.   stagingDir.mkdirs()
  30.   copy {
  31.     from('src/latex') { include '**/*.tex','**/*.inc' }
  32.     filter(ReplaceTokens, tokens: [release: version, docVersion: docVersion])
  33.     into stagingDir
  34.     includeEmptyDirs = false
  35.   }
  36. }
  37.  
  38. task myInitConfig(type:Copy) << {
  39.   def stagingDir = file(stagingDirName)
  40.   stagingDir.mkdirs()
  41.   from('src/latex') { include '**/*.tex' //
  42.   }
  43.   into stagingDir
  44.   includeEmptyDirs = false
  45. }
  46.  
  47. task genDocs << {
  48.   myInitConfig.execute()
  49.   cleanLatex(file(stagingDirName))
  50.   copyDocs.execute()
  51.   if (!isLatexInstallled()) {
  52.     throw new StopExecutionException('Latex is not installed. We skip the document!')
  53.   }
  54.   FileTree tree = fileTree(dir: stagingDirName)
  55.   tree.include '**/*.tex'
  56.   tree.each {File file ->
  57.     compileLatex(file)
  58.   }
  59. }
  60.  
  61. boolean isLatexInstallled() {
  62.   boolean ret_val = false;
  63.   try {
  64.     ant.exec(output: 'build/staging/findLatexOut', executable: 'pdflatex', dir: projectDir) { arg(line: "-help") }
  65.     ret_val = true
  66.   } catch (Throwable e) {
  67.     e.printStackTrace();
  68.     ret_val = false
  69.   }
  70.   return ret_val
  71. }
  72.  
  73. void cleanLatex(File dir) {
  74.   file(dir.path).mkdirs()
  75.   ant.delete() {
  76.     fileset(dir: dir, includes: "*.aux, *.lg, *.tmp, *.html, *.pdf, *.log, *.dvi, *.css, *.fdb_latexmk, *.idv, *.toc, *.xref, *.4ct, *.4tc, *.out")
  77.   }
  78. }
  79.  
  80. void compileLatex(File fileToProcess) {
  81.   3.times {
  82.     ant.exec(executable: 'pdflatex', dir: fileToProcess.parent, failonerror: true) { arg(value: fileToProcess.name) }
  83.   }
  84.   3.times {
  85.     ant.exec(executable: 'makeindex', dir: fileToProcess.parent, failonerror: true) { arg(value: fileToProcess.name) }
  86.   }
  87.   3.times {
  88.     ant.exec(executable: 'pdflatex', dir: fileToProcess.parent, failonerror: true) { arg(value: fileToProcess.name) }
  89.   }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement