/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

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