Statistics
| Revision:

root / tmp / org.txm.analec.rcp / src / org / txm / macro / analec / export / ExportAsMacro.groovy @ 1094

History | View | Annotate | Download (4 kB)

1 671 mdecorde
// Copyright © 2016 ENS de Lyon, CNRS, University of Franche-Comté
2 671 mdecorde
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses)
3 671 mdecorde
// @author mdecorde
4 671 mdecorde
// @author sheiden
5 671 mdecorde
// STANDARD DECLARATIONS
6 671 mdecorde
package org.txm.macro.analec.export
7 671 mdecorde
8 671 mdecorde
import groovy.transform.Field
9 671 mdecorde
10 671 mdecorde
import org.jfree.chart.JFreeChart
11 671 mdecorde
import org.kohsuke.args4j.*
12 671 mdecorde
import org.txm.Toolbox
13 671 mdecorde
import org.txm.analec.*
14 671 mdecorde
import org.txm.macro.analec.AnalecUtils
15 671 mdecorde
import org.txm.rcp.Application
16 671 mdecorde
import org.txm.rcp.IImageKeys
17 671 mdecorde
import org.txm.rcp.swt.widget.parameters.*
18 671 mdecorde
import org.txm.searchengine.cqp.corpus.*
19 1061 mdecorde
import org.txm.searchengine.cqp.corpus.query.CQLQuery
20 671 mdecorde
import org.txm.utils.io.FileCopy;
21 671 mdecorde
22 671 mdecorde
import visuAnalec.elements.*
23 671 mdecorde
24 671 mdecorde
def scriptName = this.class.getSimpleName()
25 1094 mdecorde
if (!(corpusViewSelection instanceof CQPCorpus)) {
26 671 mdecorde
        println "** $scriptName please select a Corpus to run the macro"
27 671 mdecorde
        return;
28 671 mdecorde
}
29 671 mdecorde
30 671 mdecorde
@Field @Option(name="new_name", usage="Corpus name in uppercase", widget="String", required=true, def="CORPUSNAME")
31 671 mdecorde
String new_name
32 671 mdecorde
if (!ParametersDialog.open(this)) return
33 671 mdecorde
34 671 mdecorde
new_name = new_name.toUpperCase()
35 671 mdecorde
def pattern = "[A-Z][-A-Z0-9]{1,20}"
36 671 mdecorde
if (!new_name.matches(pattern)) {
37 671 mdecorde
        println "New corpus name not conformant to CQP corpus name: "+pattern
38 671 mdecorde
        return false
39 671 mdecorde
}
40 671 mdecorde
41 671 mdecorde
MainCorpus mainCorpus = corpusViewSelection.getMainCorpus()
42 671 mdecorde
String name = mainCorpus.getName()
43 671 mdecorde
if (mainCorpus.isModified()) {
44 671 mdecorde
        println "Selected corpus is not saved. Aborting"
45 671 mdecorde
        return false
46 671 mdecorde
}
47 671 mdecorde
48 671 mdecorde
visuAnalec.donnees.Corpus analecCorpus = AnalecCorpora.getCorpus(mainCorpus.getName())
49 671 mdecorde
if (analecCorpus.isModifie()) {
50 671 mdecorde
        println "Selected Analec corpus is not saved. Aborting"
51 671 mdecorde
        return false
52 671 mdecorde
}
53 671 mdecorde
54 1038 mdecorde
File binDirectory = mainCorpus.getProjectDirectory()
55 671 mdecorde
String binName = binDirectory.getName()
56 671 mdecorde
57 671 mdecorde
58 671 mdecorde
File newBinDirectory = new File(binDirectory.getParentFile(), new_name)
59 671 mdecorde
60 671 mdecorde
if (newBinDirectory.exists()) {
61 671 mdecorde
        println "The new corpus directory already exists: $newBinDirectory. Aborting."
62 671 mdecorde
        return false
63 671 mdecorde
}
64 671 mdecorde
65 671 mdecorde
FileCopy.copyFiles(binDirectory, newBinDirectory)
66 671 mdecorde
if (!newBinDirectory.exists()) {
67 671 mdecorde
        println "Fail to copy binary directory $binDirectory to $newBinDirectory"
68 671 mdecorde
        return
69 671 mdecorde
}
70 671 mdecorde
71 671 mdecorde
File ecFile = new File(newBinDirectory, "analec/${name}.ec")
72 671 mdecorde
File ecvFile = new File(newBinDirectory, "analec/${name}.ecv")
73 671 mdecorde
File cssFile = new File(newBinDirectory, "css/${name}.css")
74 671 mdecorde
File dataFile = new File(newBinDirectory, "data/${name}")
75 671 mdecorde
File htmlFile = new File(newBinDirectory, "HTML/${name}")
76 671 mdecorde
File defaultCSSFile = new File(newBinDirectory, "HTML/${name}/default/css/${name}.css")
77 671 mdecorde
File registryFile = new File(newBinDirectory, "registry/${name.toLowerCase()}")
78 671 mdecorde
File txmFile = new File(newBinDirectory, "txm/${name}")
79 671 mdecorde
80 671 mdecorde
File ecFile2 = new File(newBinDirectory, "analec/${new_name}.ec")
81 671 mdecorde
File ecvFile2 = new File(newBinDirectory, "analec/${new_name}.ecv")
82 671 mdecorde
File cssFile2 = new File(newBinDirectory, "css/${new_name}.css")
83 671 mdecorde
File dataFile2 = new File(newBinDirectory, "data/${new_name}")
84 671 mdecorde
File htmlFile2 = new File(newBinDirectory, "HTML/${new_name}")
85 671 mdecorde
File defaultCSSFile2 = new File(newBinDirectory, "HTML/${new_name}/default/css/${new_name}.css")
86 671 mdecorde
File registryFile2 = new File(newBinDirectory, "registry/${new_name.toLowerCase()}")
87 671 mdecorde
File txmFile2 = new File(newBinDirectory, "txm/${new_name}")
88 671 mdecorde
89 671 mdecorde
println ecFile.renameTo(ecFile2)
90 671 mdecorde
println ecvFile.renameTo(ecvFile2)
91 671 mdecorde
println cssFile.renameTo(cssFile2)
92 671 mdecorde
println dataFile.renameTo(dataFile2)
93 671 mdecorde
println htmlFile.renameTo(htmlFile2)
94 671 mdecorde
println defaultCSSFile.renameTo(defaultCSSFile2)
95 671 mdecorde
println registryFile.renameTo(registryFile2)
96 671 mdecorde
println txmFile.renameTo(txmFile2)
97 671 mdecorde
98 671 mdecorde
File importXMLFile = new File(newBinDirectory, "import.xml")
99 671 mdecorde
100 671 mdecorde
// patch registry
101 671 mdecorde
String oldcontent = registryFile2.getText();
102 671 mdecorde
content = oldcontent.replace(name, new_name)
103 671 mdecorde
content = content.replace(name.toLowerCase(), new_name.toLowerCase())
104 671 mdecorde
registryFile2.withWriter { writer ->
105 671 mdecorde
        writer.write(content)
106 671 mdecorde
}
107 671 mdecorde
108 671 mdecorde
// patch import.xml
109 671 mdecorde
110 671 mdecorde
111 671 mdecorde
File zipFile = new File(newBinDirectory.getAbsolutePath()+".txm")
112 671 mdecorde
Zip.compress(newBinDirectory, zipFile, monitor)
113 671 mdecorde
114 671 mdecorde
if (!zipFile.exists()) {
115 671 mdecorde
        println "Fail to zip binary directory $binDirectory to $zipFile"
116 671 mdecorde
        return
117 671 mdecorde
} else {
118 671 mdecorde
        println "Done: $zipFile"
119 671 mdecorde
}