| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package nl.toolforge.karma.core.manifest.util; |
| 20 |
|
|
| 21 |
|
import nl.toolforge.karma.core.KarmaRuntimeException; |
| 22 |
|
import nl.toolforge.karma.core.manifest.SourceModule; |
| 23 |
|
import org.apache.commons.logging.Log; |
| 24 |
|
import org.apache.commons.logging.LogFactory; |
| 25 |
|
|
| 26 |
|
import java.io.BufferedReader; |
| 27 |
|
import java.io.File; |
| 28 |
|
import java.io.FileOutputStream; |
| 29 |
|
import java.io.IOException; |
| 30 |
|
import java.io.InputStreamReader; |
| 31 |
|
import java.io.Reader; |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
40 |
public abstract class BaseModuleLayoutTemplate implements ModuleLayoutTemplate { |
| 38 |
|
|
| 39 |
85 |
protected static Log logger = LogFactory.getLog(BaseModuleLayoutTemplate.class); |
| 40 |
|
|
| 41 |
35 |
public abstract FileTemplate[] getFileElements(); |
| 42 |
|
|
| 43 |
|
public abstract String[] getDirectoryElements(); |
| 44 |
|
|
| 45 |
|
public final void createLayout(File baseDir) throws IOException { |
| 46 |
|
|
| 47 |
32 |
FileTemplate[] fileTemplates = getFileElements(); |
| 48 |
32 |
String[] templateFiles = new String[fileTemplates.length]; |
| 49 |
44 |
|
| 50 |
148 |
for (int i = 0; i < fileTemplates.length; i++) { |
| 51 |
|
|
| 52 |
215 |
FileTemplate fileTemplate = fileTemplates[i]; |
| 53 |
72 |
logger.debug("Write template '" + fileTemplate.getSource() + "' to '" + fileTemplate.getTarget() + "'."); |
| 54 |
171 |
Reader input = new BufferedReader(class="keyword">new InputStreamReader(SourceModule.class.getResourceAsStream(fileTemplate.getSource().toString().replace('\\','/')))); |
| 55 |
99 |
|
| 56 |
171 |
File outputFile = new File(baseDir + File.separator + fileTemplate.getTarget()); |
| 57 |
72 |
outputFile.getParentFile().mkdirs(); |
| 58 |
171 |
outputFile.createNewFile(); |
| 59 |
99 |
|
| 60 |
171 |
FileOutputStream output = new FileOutputStream(outputFile); |
| 61 |
|
|
| 62 |
12307 |
while (input.ready()) { |
| 63 |
12136 |
output.write(input.read()); |
| 64 |
16786 |
} |
| 65 |
16759 |
templateFiles[i] = fileTemplate.getTarget().getPath(); |
| 66 |
72 |
logger.debug("Wrote template."); |
| 67 |
99 |
} |
| 68 |
99 |
|
| 69 |
|
|
| 70 |
|
|
| 71 |
32 |
String[] dirs = getDirectoryElements(); |
| 72 |
|
|
| 73 |
180 |
for (int i=0; i < dirs.length; i++) { |
| 74 |
|
|
| 75 |
291 |
File dirToAdd = new File(baseDir, dirs[i]); |
| 76 |
|
|
| 77 |
143 |
|
| 78 |
104 |
if (!dirToAdd.mkdirs() && !dirToAdd.exists()) { |
| 79 |
|
|
| 80 |
143 |
|
| 81 |
0 |
throw new KarmaRuntimeException("Error while creating module layout."); |
| 82 |
|
} |
| 83 |
|
} |
| 84 |
32 |
} |
| 85 |
|
} |