Statistics
| Revision:

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

History | View | Annotate | Download (4.3 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
import org.txm.utils.io.IOUtils
22

    
23
import visuAnalec.elements.*
24

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

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

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

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

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

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

    
58

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

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

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

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

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

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

    
99
// patch name in settings
100
File projectPrefs = new File(newBinDirectory, ".settings/"+mainCorpus.getProject().getParametersNodePath())
101
File maincorpusPrefs = new File(newBinDirectory, ".settings/"+mainCorpus.getParametersNodePath())
102
IOUtils.write(projectPrefs, projectPrefs.getText().replace(name, new_name))
103
IOUtils.write(maincorpusPrefs, projectPrefs.getText().replace(name, new_name))
104

    
105
// patch registry
106
String oldcontent = registryFile2.getText();
107
content = oldcontent.replace(name, new_name)
108
content = content.replace(name.toLowerCase(), new_name.toLowerCase())
109
registryFile2.withWriter { writer -> 
110
        writer.write(content)
111
}
112

    
113

    
114

    
115

    
116
File zipFile = new File(newBinDirectory.getAbsolutePath()+".txm")
117
Zip.compress(newBinDirectory, zipFile, monitor)
118

    
119
if (!zipFile.exists()) {
120
        println "Fail to zip binary directory $binDirectory to $zipFile"
121
        return
122
} else {
123
        println "Done: $zipFile"
124
}