Advertisement
diamondarts

LatexPlugin.gradle

Apr 10th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 7.68 KB | None | 0 0
  1. /***************************************************
  2.  * Usage:
  3.  * apply from:'LatexPlugin.gradle'
  4.  *
  5.  * // name of main LaTeX file
  6.  * docuTexMainFile 'release_notes_and_restrictions'
  7.  *
  8.  * // set the generated variants of the documentation
  9.  * variant '73' : 'RELEASE_NOTES_AND_RESTRICTIONS_NW73',
  10.  *         '70' : 'RELEASE_NOTES_AND_RESTRICTIONS_NW70'
  11. ****************************************************/
  12.  
  13. // Provide repository for 'splitindex'
  14. repositories{
  15.     ivy{ name = 'company_ivy'; url = "$companyFileShare/ivy-repo" }
  16.     //mavenCentral()
  17. }
  18.  
  19.  
  20. // splitindex.jar was added as jar in company ivy repo
  21. configurations { splitindex }
  22. dependencies {
  23.     splitindex group: 'splitindex', name: 'splitindex', version: '0.1'
  24. }
  25.  
  26. guessLanguage()
  27.  
  28. task preprocess {
  29.     description = 'Generates temporary LaTeX files to be used by makeIndex'
  30. }
  31. task makeIndex {
  32.     description = 'Creates temporary index lists'
  33. }
  34. task compile {
  35.     description = 'Creates actual PDF'
  36. }
  37. task rename {
  38.     description = 'Move PDF with technical name to output directory and applies nice name'
  39. }
  40. task deleteTemporaryLatexFiles(type:Delete) {
  41.     description = 'Deletes temporary files generated by LaTeX build process'
  42.     def inclds = {
  43.         include '*.log'
  44.         include '*.lof'
  45.         include '*.gz'
  46.         include '*.orig'
  47.         include '*.bak'
  48.         include '*.loa'
  49.         include '*.lol'
  50.         include '*.log'
  51.         include '*.lot'
  52.         include '*.bbl'
  53.         include '*.blg'
  54.         include '*.dvi'
  55.         include '*.out'
  56.         include '*.brf'
  57.         include '*.thm'
  58.         include '*.toc'
  59.         include '*.idx'
  60.         include '*.ilg'
  61.         include '*.ind'
  62.        
  63.         // In some projects the .aux files are committed for cross references between documents
  64.         exclude '*.aux'
  65.     }
  66.     delete fileTree('.', inclds)
  67.     delete fileTree('tmp', inclds)
  68. }
  69. task (convert2utf8) {
  70.     description = 'Converts *.tex files from ASCII to UTF-8 with BOM'
  71.  
  72.     inputs.file fileTree(dir: '.', includes: ['**/*.tex'])
  73.    
  74.     doLast {
  75.         inputs.files.each{
  76.             logger.quiet "Converting to UTF-8 with BOM: $it"   
  77.             def content = it.text
  78.             it.setText '\ufeff' + content, 'UTF-8'
  79.         }
  80.     }
  81. }
  82.  
  83. try {
  84.     tasks.build.dependsOn compile, rename
  85. } catch(Exception e) {
  86.     task build(dependsOn:[compile, rename])
  87. }
  88.  
  89. defaultTasks 'rename'
  90.  
  91. ext.language = this.&setLanguage
  92. ext.variant = this.&setVariants
  93. ext.destDir = file("$buildDir/docs")
  94.  
  95.  
  96. /*
  97. * in future releases the name of the main tex file may not be set in the build-gradle
  98. * but dynamically be calculated. Therefore the main tex file should be located in the
  99. * language specific folder of the documentation and should have a more dynamic name like "documentation.tex"
  100. */
  101. ext.docuTexMainFile = this.&setDocuTexMainFile
  102.  
  103. /*
  104. * First shot: Try some default names. If one is found, use that one as entry (main) latex file
  105. */
  106. ['documentation', 'document', 'main'].find {
  107.     def fileName = it + '.tex'
  108.     if(file(fileName).exists()) {
  109.         setDocuTexMainFile it // Don't pass the .tex suffix
  110.         return true // break the 'find' loop
  111.     }
  112. }
  113.  
  114. def setLanguage(String lang) {
  115.     ext.language = new Locale(lang)
  116.     //logger.quiet "Setting language to $language.displayLanguage"
  117. }
  118.  
  119. def guessLanguage() {
  120.     def matches = projectDir.name =~ /_(\w\w)_?$/
  121.     if (matches) {
  122.         logger.quiet "$project.name's language seems to be ${matches[0][1]}"
  123.         setLanguage(matches[0][1])
  124.     }
  125. }
  126.  
  127. def setDocuTexMainFile(String baseTexName){
  128.     ext.docuTexMainFile = baseTexName
  129.     logger.info "Setting docuTexMainFile to $baseTexName"
  130. }
  131.  
  132. def setVariants(Map m) {
  133.     def score1 = projectDir.name.endsWith('_') ? '' : '_'
  134.     m.each{ key, value ->
  135.         def jobname1 = "$projectDir.name$score1$key" + (key.endsWith('_') ? '' : '_')
  136.         def taskSuffix = key.capitalize()
  137.         if (!value) value = jobname1
  138.         task ("preprocess$taskSuffix") {
  139.             description = "Generates temporary LaTeX files of $jobname1"
  140.             ext.jobname = jobname1
  141.             preprocess.dependsOn "preprocess$taskSuffix"
  142.             doLast {
  143.                 def result = exec {
  144.                     /*environment 'TEXINPUTS', "..;D:/DEV/SRC/COMPANY/HG;%TEXINPUTS%"*/
  145.                     commandLine 'pdflatex'
  146.                     args "--jobname=$jobname"
  147.                     args "--output-directory=$temporaryDir"
  148.                     args '-interaction=batchmode' // Don't output to console
  149.                     args "${docuTexMainFile}.tex"
  150.                     ignoreExitValue = true // Dedicated exception handling below
  151.                 }
  152.                 outputLatexLogOnError(temporaryDir, jobname, result)
  153.             }
  154.         }
  155.         task ("makeIndex$taskSuffix") {
  156.             ext.jobname = jobname1
  157.             dependsOn "preprocess$taskSuffix"
  158.             makeIndex.dependsOn "makeIndex$taskSuffix"
  159.             doLast {
  160.                 /* exec {
  161.                     //makeindex <dateiname>.idx
  162.                     workingDir tasks["preprocess$taskSuffix"].temporaryDir
  163.                     commandLine 'makeindex'
  164.                     args jobname //.idx is set automatically
  165.                     args '-q' // quiet
  166.                     setIgnoreExitValue true
  167.                 } */
  168.                 // Use splitindex instead of makeindex
  169.                 // Use splitindex.jar to avoid message 'Perl interpreter could not be found'
  170.                 // (HG\company_tools\splitindex)
  171.                 javaexec {
  172.                     classpath configurations.splitindex
  173.                     main = 'splitindex'     // fully qualified name of main class
  174.                     workingDir tasks["preprocess$taskSuffix"].temporaryDir
  175.                     args jobname
  176.                     args '--'               // indicate to splitindex that what now follows are options to pass to makeindex
  177.                     args '-q'               // makindex: quiet
  178.                     ignoreExitValue = true  // In case there is not custom index ignore exit value
  179.                 }
  180.            }
  181.         }
  182.         task ("compile$taskSuffix") {
  183.             ext.jobname = jobname1
  184.             description = "Creates PDF of $jobname1"
  185.             dependsOn "makeIndex$taskSuffix"
  186.             compile.dependsOn "compile$taskSuffix"
  187.             outputs.file "${tasks["preprocess$taskSuffix"].temporaryDir}/${jobname}.pdf"
  188.             doLast {
  189.                 def result = exec {
  190.                     commandLine 'pdflatex'
  191.                     args "--jobname=$jobname"
  192.                     args "--output-directory=${tasks["preprocess$taskSuffix"].temporaryDir}"
  193.                     args '-interaction=batchmode' // Don't output to console
  194.                     args "${docuTexMainFile}.tex"
  195.                 }
  196.                 outputLatexLogOnError(temporaryDir, jobname, result)
  197.            }
  198.         }
  199.         task ("rename$taskSuffix", type:Copy) {
  200.             //println "rename ${jobname1}.pdf into $destDir/${value}.pdf"
  201.             rename.dependsOn "rename$taskSuffix"
  202.             from tasks["compile$taskSuffix"]
  203.             into destDir
  204.             rename { "${value}.pdf" }
  205.         }
  206.     }
  207. }
  208.  
  209. def outputLatexLogOnError(dir, jobname, executionResult) {
  210.     if (executionResult.exitValue == 1) {
  211.         def logFile = file("$dir/${jobname}.log")
  212.         if (!logFile.exists())
  213.             logFile = file("$dir/texput.log")
  214.         throw new RuntimeException("""Failed to compile Latex script ${project.docuTexMainFile}.tex
  215. --------------------
  216. Begin latex log file
  217. --------------------
  218. $logFile.text
  219. ------------------
  220. End latex log file
  221. ------------------""")
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement