1 package com.atlassian.plugins.codegen;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.util.Properties;
9
10 import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
11
12 import com.google.common.io.Files;
13
14 import org.apache.commons.io.FileUtils;
15
16 import static com.google.common.base.Preconditions.checkNotNull;
17 import static org.apache.commons.io.IOUtils.closeQuietly;
18
19
20
21
22
23 public class ProjectFilesRewriter implements ProjectRewriter
24 {
25 private PluginModuleLocation location;
26
27 public ProjectFilesRewriter(PluginModuleLocation location)
28 {
29 this.location = checkNotNull(location, "location");
30 }
31
32 @Override
33 public void applyChanges(PluginProjectChangeset changes) throws Exception
34 {
35 for (SourceFile sourceFile : changes.getSourceFiles())
36 {
37 File baseDir = sourceFile.getSourceGroup() == SourceFile.SourceGroup.TESTS ?
38 location.getTestDirectory() : location.getSourceDirectory();
39 File packageDir = baseDir;
40 for (String packagePart : sourceFile.getClassId().getPackage().split("\\."))
41 {
42 packageDir = new File(packageDir, packagePart);
43 }
44 File newFile = new File(packageDir, sourceFile.getClassId().getName() + ".java");
45 Files.createParentDirs(newFile);
46 FileUtils.writeStringToFile(newFile, sourceFile.getContent());
47 }
48 for (ResourceFile resourceFile : changes.getResourceFiles())
49 {
50 File resourceDir = location.getResourcesDir();
51 if (!resourceFile.getRelativePath().equals(""))
52 {
53 resourceDir = new File(resourceDir, resourceFile.getRelativePath());
54 }
55 File newFile = new File(resourceDir, resourceFile.getName());
56 Files.createParentDirs(newFile);
57 FileUtils.writeStringToFile(newFile, resourceFile.getContent());
58 }
59 if (!changes.getI18nProperties().isEmpty())
60 {
61 File i18nFile = new File(location.getResourcesDir(), DEFAULT_I18N_NAME + ".properties");
62 if (!i18nFile.exists())
63 {
64 i18nFile.createNewFile();
65 }
66 Properties props = new Properties();
67 InputStream is = new FileInputStream(i18nFile);
68 props.load(is);
69 closeQuietly(is);
70 props.putAll(changes.getI18nProperties());
71 OutputStream os = new FileOutputStream(i18nFile);
72 props.store(os, "");
73 closeQuietly(os);
74 }
75 }
76 }