diff --git a/src/main/java/de/marhali/easyi18n/ionext/folder/FolderStrategy.java b/src/main/java/de/marhali/easyi18n/ionext/folder/FolderStrategy.java index c547a5e..180a101 100644 --- a/src/main/java/de/marhali/easyi18n/ionext/folder/FolderStrategy.java +++ b/src/main/java/de/marhali/easyi18n/ionext/folder/FolderStrategy.java @@ -1,14 +1,20 @@ package de.marhali.easyi18n.ionext.folder; +import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; +import de.marhali.easyi18n.model.ParserStrategyType; import de.marhali.easyi18n.model.SettingsState; +import de.marhali.easyi18n.model.TranslationData; import de.marhali.easyi18n.model.TranslationFile; import org.apache.commons.io.FilenameUtils; import org.jetbrains.annotations.NotNull; +import java.io.File; +import java.io.IOException; import java.util.List; +import java.util.Objects; /** * Represents a specific translation file directory structure. @@ -28,7 +34,16 @@ public abstract class FolderStrategy { * @param localesDirectory Configured translation file directory * @return translation files which matches the strategy */ - public abstract List findFiles(@NotNull VirtualFile localesDirectory); + public abstract @NotNull List analyzeFolderStructure(@NotNull VirtualFile localesDirectory); + + /** + * Analyzes the provided translation data and returns the directory structure based on the implementing strategy + * @param localesPath Configured locales path + * @param data Translation data to use for write action + * @return translation file structure + */ + public abstract @NotNull List constructFolderStructure(@NotNull String localesPath, + @NotNull ParserStrategyType type, @NotNull TranslationData data) throws IOException; /** * Checks if the provided file is not a directory and matches the configured file pattern @@ -38,4 +53,22 @@ public abstract class FolderStrategy { protected boolean isFileRelevant(@NotNull VirtualFile file) { return !file.isDirectory() && FilenameUtils.wildcardMatch(file.getName(), this.settings.getFilePattern()); } + + /** + * + * @param parent Directory path + * @param child File name with extension + * @return IntelliJ {@link VirtualFile} + * @throws IOException Could not access file + */ + protected @NotNull VirtualFile constructFile(@NotNull String parent, @NotNull String child) throws IOException { + File file = new File(parent, child); + boolean exists = file.createNewFile(); + + VirtualFile vf = exists + ? LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file) + : LocalFileSystem.getInstance().findFileByIoFile(file); + + return Objects.requireNonNull(vf); + } } diff --git a/src/main/java/de/marhali/easyi18n/ionext/folder/ModularLocaleFolderStrategy.java b/src/main/java/de/marhali/easyi18n/ionext/folder/ModularLocaleFolderStrategy.java index ba83b4c..e1d30ab 100644 --- a/src/main/java/de/marhali/easyi18n/ionext/folder/ModularLocaleFolderStrategy.java +++ b/src/main/java/de/marhali/easyi18n/ionext/folder/ModularLocaleFolderStrategy.java @@ -2,11 +2,14 @@ package de.marhali.easyi18n.ionext.folder; import com.intellij.openapi.vfs.VirtualFile; +import de.marhali.easyi18n.model.ParserStrategyType; import de.marhali.easyi18n.model.SettingsState; +import de.marhali.easyi18n.model.TranslationData; import de.marhali.easyi18n.model.TranslationFile; import org.jetbrains.annotations.NotNull; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -22,7 +25,7 @@ public class ModularLocaleFolderStrategy extends FolderStrategy { } @Override - public List findFiles(@NotNull VirtualFile localesDirectory) { + public @NotNull List analyzeFolderStructure(@NotNull VirtualFile localesDirectory) { List files = new ArrayList<>(); for(VirtualFile localeModuleDir : localesDirectory.getChildren()) { @@ -39,4 +42,23 @@ public class ModularLocaleFolderStrategy extends FolderStrategy { return files; } + + @Override + public @NotNull List constructFolderStructure( + @NotNull String localesPath, @NotNull ParserStrategyType type, + @NotNull TranslationData data) throws IOException { + + List files = new ArrayList<>(); + + for(String locale : data.getLocales()) { + for(String namespace : data.getRootNode().getChildren().keySet()) { + VirtualFile vf = super.constructFile(localesPath + "/" + locale, + namespace + "." + type.getFileExtension()); + + files.add(new TranslationFile(vf, locale, namespace)); + } + } + + return files; + } } diff --git a/src/main/java/de/marhali/easyi18n/ionext/folder/ModularNamespaceFolderStrategy.java b/src/main/java/de/marhali/easyi18n/ionext/folder/ModularNamespaceFolderStrategy.java index bf9656d..9b094fb 100644 --- a/src/main/java/de/marhali/easyi18n/ionext/folder/ModularNamespaceFolderStrategy.java +++ b/src/main/java/de/marhali/easyi18n/ionext/folder/ModularNamespaceFolderStrategy.java @@ -2,11 +2,14 @@ package de.marhali.easyi18n.ionext.folder; import com.intellij.openapi.vfs.VirtualFile; +import de.marhali.easyi18n.model.ParserStrategyType; import de.marhali.easyi18n.model.SettingsState; +import de.marhali.easyi18n.model.TranslationData; import de.marhali.easyi18n.model.TranslationFile; import org.jetbrains.annotations.NotNull; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -22,7 +25,7 @@ public class ModularNamespaceFolderStrategy extends FolderStrategy { } @Override - public List findFiles(@NotNull VirtualFile localesDirectory) { + public @NotNull List analyzeFolderStructure(@NotNull VirtualFile localesDirectory) { List files = new ArrayList<>(); for(VirtualFile namespaceModuleDir : localesDirectory.getChildren()) { @@ -39,4 +42,23 @@ public class ModularNamespaceFolderStrategy extends FolderStrategy { return files; } + + @Override + public @NotNull List constructFolderStructure( + @NotNull String localesPath, @NotNull ParserStrategyType type, + @NotNull TranslationData data) throws IOException { + + List files = new ArrayList<>(); + + for(String namespace : data.getRootNode().getChildren().keySet()) { + for(String locale : data.getLocales()) { + VirtualFile vf = super.constructFile(localesPath + "/" + namespace, + locale + "." + type.getFileExtension()); + + files.add(new TranslationFile(vf, locale, namespace)); + } + } + + return files; + } } diff --git a/src/main/java/de/marhali/easyi18n/ionext/folder/SingleFolderStrategy.java b/src/main/java/de/marhali/easyi18n/ionext/folder/SingleFolderStrategy.java index 81320f0..e9233f1 100644 --- a/src/main/java/de/marhali/easyi18n/ionext/folder/SingleFolderStrategy.java +++ b/src/main/java/de/marhali/easyi18n/ionext/folder/SingleFolderStrategy.java @@ -2,11 +2,14 @@ package de.marhali.easyi18n.ionext.folder; import com.intellij.openapi.vfs.VirtualFile; +import de.marhali.easyi18n.model.ParserStrategyType; import de.marhali.easyi18n.model.SettingsState; +import de.marhali.easyi18n.model.TranslationData; import de.marhali.easyi18n.model.TranslationFile; import org.jetbrains.annotations.NotNull; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -14,6 +17,7 @@ import java.util.List; * Single directory translation folder strategy. * Every child is recognized as a file for a specific language. * Directory => en.file, de.file, fr.file + * * @author marhali */ public class SingleFolderStrategy extends FolderStrategy { @@ -23,15 +27,32 @@ public class SingleFolderStrategy extends FolderStrategy { } @Override - public List findFiles(@NotNull VirtualFile localesDirectory) { + public @NotNull List analyzeFolderStructure(@NotNull VirtualFile localesDirectory) { List files = new ArrayList<>(); - for(VirtualFile file : localesDirectory.getChildren()) { - if(super.isFileRelevant(file)) { + for (VirtualFile file : localesDirectory.getChildren()) { + if (super.isFileRelevant(file)) { files.add(new TranslationFile(file, file.getNameWithoutExtension(), null)); } } return files; } + + @Override + public @NotNull List constructFolderStructure( + @NotNull String localesPath, @NotNull ParserStrategyType type, + @NotNull TranslationData data) throws IOException { + + List files = new ArrayList<>(); + + for (String locale : data.getLocales()) { + VirtualFile vf = super.constructFile(localesPath, + locale + "." + type.getFileExtension()); + + files.add(new TranslationFile(vf, locale, null)); + } + + return files; + } }