PageRenderTime 41ms CodeModel.GetById 14ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/plugin-module-codegen-engine/src/main/java/com/atlassian/plugins/codegen/ProjectFilesRewriter.java

https://bitbucket.org/mmeinhold/amps
Java | 90 lines | 75 code | 10 blank | 5 comment | 9 complexity | d9f29a6ba6a41559612707ee465bd6e3 MD5 | raw file
 1package com.atlassian.plugins.codegen;
 2
 3import java.io.File;
 4import java.io.IOException;
 5import java.io.StringReader;
 6import java.util.Properties;
 7
 8import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
 9import com.atlassian.plugins.codegen.util.FileUtil;
10import com.atlassian.plugins.codegen.util.PluginXmlHelper;
11
12import com.google.common.io.Files;
13
14import org.apache.commons.io.FileUtils;
15
16import static com.google.common.base.Preconditions.checkNotNull;
17import static org.apache.commons.io.IOUtils.closeQuietly;
18
19/**
20 * Applies the changes from a {@link PluginProjectChangeset} that involve creating
21 * source or resource files.
22 */
23public 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        // We use a PluginXmlHelper to read some information from atlassian-plugin.xml if needed
36        PluginXmlHelper xmlHelper = new PluginXmlHelper(location);
37        
38        for (SourceFile sourceFile : changes.getItems(SourceFile.class))
39        {
40            File baseDir = sourceFile.getSourceGroup() == SourceFile.SourceGroup.TESTS ?
41                location.getTestDirectory() : location.getSourceDirectory();
42            File newFile = FileUtil.dotDelimitedFilePath(baseDir, sourceFile.getClassId().getFullName(), ".java");
43            Files.createParentDirs(newFile);
44            FileUtils.writeStringToFile(newFile, sourceFile.getContent());
45        }
46        for (ResourceFile resourceFile : changes.getItems(ResourceFile.class))
47        {
48            File resourceDir = location.getResourcesDir();
49            if (!resourceFile.getRelativePath().equals(""))
50            {
51                resourceDir = new File(resourceDir, resourceFile.getRelativePath());
52            }
53            File newFile = new File(resourceDir, resourceFile.getName());
54            Files.createParentDirs(newFile);
55            FileUtils.writeByteArrayToFile(newFile, resourceFile.getContent());
56        }
57        if (changes.hasItems(I18nString.class))
58        {
59            addI18nStrings(FileUtil.dotDelimitedFilePath(location.getResourcesDir(), xmlHelper.getDefaultI18nLocation(), ".properties"),
60                changes.getItems(I18nString.class));
61        }
62    }
63
64    private void addI18nStrings(File file, Iterable<I18nString> items) throws IOException
65    {
66        Files.createParentDirs(file);
67        String oldContent = file.exists() ? FileUtils.readFileToString(file) : "";
68        Properties oldProps = new Properties();
69        oldProps.load(new StringReader(oldContent));
70        StringBuilder newContent = new StringBuilder(oldContent);
71        boolean modified = false;
72        for (I18nString item : items)
73        {
74            if (!oldProps.containsKey(item.getName()))
75            {
76                if (!modified)
77                {
78                    newContent.append("\n");
79                }
80                modified = true;
81                newContent.append(item.getName()).append("=").append(item.getValue()).append("\n");
82                oldProps.put(item.getName(), item.getValue());
83            }
84        }
85        if (modified)
86        {
87            FileUtils.writeStringToFile(file, newContent.toString());
88        }
89    }
90}