Statistics
| Revision:

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

History | View | Annotate | Download (4 kB)

1
// Copyright © 2016 ENS de Lyon, CNRS, University of Franche-Comté
2
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses)
3
// @author mdecorde
4
// @author sheiden
5
// STANDARD DECLARATIONS
6
package org.txm.macro.urs.export
7

    
8
import groovy.transform.Field
9

    
10
import org.jfree.chart.JFreeChart
11
import org.kohsuke.args4j.*
12
import org.txm.Toolbox
13
import org.txm.annotation.urs.*
14
import org.txm.macro.urs.AnalecUtils
15
import org.txm.rcp.Application
16
import org.txm.rcp.IImageKeys
17
import org.txm.rcp.swt.widget.parameters.*
18
import org.txm.searchengine.cqp.corpus.*
19
import org.txm.searchengine.cqp.corpus.query.CQLQuery
20
import org.txm.utils.io.FileCopy;
21

    
22
import visuAnalec.elements.*
23

    
24
def scriptName = this.class.getSimpleName()
25
if (!(corpusViewSelection instanceof CQPCorpus)) {
26
        println "** $scriptName please select a Corpus to run the macro"
27
        return;
28
}
29

    
30
@Field @Option(name="new_name", usage="Corpus name in uppercase", widget="String", required=true, def="CORPUSNAME")
31
String new_name
32
if (!ParametersDialog.open(this)) return
33

    
34
new_name = new_name.toUpperCase()
35
def pattern = "[A-Z][-A-Z0-9]{1,20}"
36
if (!new_name.matches(pattern)) {
37
        println "New corpus name not conformant to CQP corpus name: "+pattern
38
        return false
39
}
40

    
41
MainCorpus mainCorpus = corpusViewSelection.getMainCorpus()
42
String name = mainCorpus.getName()
43
if (mainCorpus.isModified()) {
44
        println "Selected corpus is not saved. Aborting"
45
        return false
46
}
47

    
48
visuAnalec.donnees.Corpus analecCorpus = URSCorpora.getCorpus(mainCorpus.getName())
49
if (analecCorpus.isModifie()) {
50
        println "Selected Analec corpus is not saved. Aborting"
51
        return false
52
}
53

    
54
File binDirectory = mainCorpus.getProjectDirectory()
55
String binName = binDirectory.getName()
56

    
57

    
58
File newBinDirectory = new File(binDirectory.getParentFile(), new_name)
59

    
60
if (newBinDirectory.exists()) {
61
        println "The new corpus directory already exists: $newBinDirectory. Aborting."
62
        return false
63
}
64

    
65
FileCopy.copyFiles(binDirectory, newBinDirectory)
66
if (!newBinDirectory.exists()) {
67
        println "Fail to copy binary directory $binDirectory to $newBinDirectory"
68
        return
69
}
70

    
71
File ecFile = new File(newBinDirectory, "analec/${name}.ec")
72
File ecvFile = new File(newBinDirectory, "analec/${name}.ecv")
73
File cssFile = new File(newBinDirectory, "css/${name}.css")
74
File dataFile = new File(newBinDirectory, "data/${name}")
75
File htmlFile = new File(newBinDirectory, "HTML/${name}")
76
File defaultCSSFile = new File(newBinDirectory, "HTML/${name}/default/css/${name}.css")
77
File registryFile = new File(newBinDirectory, "registry/${name.toLowerCase()}")
78
File txmFile = new File(newBinDirectory, "txm/${name}")
79

    
80
File ecFile2 = new File(newBinDirectory, "analec/${new_name}.ec")
81
File ecvFile2 = new File(newBinDirectory, "analec/${new_name}.ecv")
82
File cssFile2 = new File(newBinDirectory, "css/${new_name}.css")
83
File dataFile2 = new File(newBinDirectory, "data/${new_name}")
84
File htmlFile2 = new File(newBinDirectory, "HTML/${new_name}")
85
File defaultCSSFile2 = new File(newBinDirectory, "HTML/${new_name}/default/css/${new_name}.css")
86
File registryFile2 = new File(newBinDirectory, "registry/${new_name.toLowerCase()}")
87
File txmFile2 = new File(newBinDirectory, "txm/${new_name}")
88

    
89
println ecFile.renameTo(ecFile2)
90
println ecvFile.renameTo(ecvFile2)
91
println cssFile.renameTo(cssFile2)
92
println dataFile.renameTo(dataFile2)
93
println htmlFile.renameTo(htmlFile2)
94
println defaultCSSFile.renameTo(defaultCSSFile2)
95
println registryFile.renameTo(registryFile2)
96
println txmFile.renameTo(txmFile2)
97

    
98
File importXMLFile = new File(newBinDirectory, "import.xml")
99

    
100
// patch registry
101
String oldcontent = registryFile2.getText();
102
content = oldcontent.replace(name, new_name)
103
content = content.replace(name.toLowerCase(), new_name.toLowerCase())
104
registryFile2.withWriter { writer -> 
105
        writer.write(content)
106
}
107

    
108
// patch import.xml
109

    
110

    
111
File zipFile = new File(newBinDirectory.getAbsolutePath()+".txm")
112
Zip.compress(newBinDirectory, zipFile, monitor)
113

    
114
if (!zipFile.exists()) {
115
        println "Fail to zip binary directory $binDirectory to $zipFile"
116
        return
117
} else {
118
        println "Done: $zipFile"
119
}