All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.reprezen.genflow.rapidml.csharp.helpers.FileHelper Maven / Gradle / Ivy

There is a newer version: 1.4.2
Show newest version
package com.reprezen.genflow.rapidml.csharp.helpers;

import com.reprezen.genflow.api.template.IGenTemplateContext;
import com.reprezen.genflow.rapidml.csharp.Config;
import com.reprezen.genflow.rapidml.csharp.helpers.FileRole;
import com.reprezen.genflow.rapidml.csharp.helpers.FolderComponent;
import com.reprezen.genflow.rapidml.csharp.helpers.NamespaceComponent;
import com.reprezen.genflow.rapidml.csharp.helpers.SpecialFile;
import com.reprezen.genflow.rapidml.csharp.helpers.UsingItem;
import com.reprezen.rapidml.DataModel;
import com.reprezen.rapidml.Enumeration;
import com.reprezen.rapidml.ResourceAPI;
import com.reprezen.rapidml.ResourceDefinition;
import com.reprezen.rapidml.Structure;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.CollectionExtensions;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;

@SuppressWarnings("all")
public class FileHelper {
  private final IGenTemplateContext context;
  
  private final Config config;
  
  private List namespaceComponents = CollectionLiterals.newArrayList();
  
  private List folderComponents = CollectionLiterals.newArrayList();
  
  private Set usings = CollectionLiterals.newHashSet();
  
  public FileHelper(final IGenTemplateContext context, final Config config) {
    this.context = context;
    this.config = config;
  }
  
  public static FileHelper of(final Enumeration enumeration, final FileRole role, final IGenTemplateContext context, final Config config) {
    FileHelper _xblockexpression = null;
    {
      final FileHelper helper = new FileHelper(context, config);
      EObject _eContainer = enumeration.eContainer();
      final DataModel dataModel = ((DataModel) _eContainer);
      if (role != null) {
        switch (role) {
          case ENUMS:
            FileHelper _inNamespace = helper.inNamespace(NamespaceComponent.ROOT, dataModel);
            FileHelper _inFolder = _inNamespace.inFolder(FolderComponent.ROOT, FolderComponent.MODELS, dataModel);
            _inFolder.using(UsingItem.SYSTEM, UsingItem.JSON_CONVERTERS, UsingItem.GEN_ATTR, UsingItem.ENUM_SERIALIZATION);
            break;
          default:
            break;
        }
      } else {
      }
      _xblockexpression = helper;
    }
    return _xblockexpression;
  }
  
  public static FileHelper of(final Structure structure, final FileRole role, final IGenTemplateContext context, final Config config) {
    FileHelper _xblockexpression = null;
    {
      final FileHelper helper = new FileHelper(context, config);
      EObject _eContainer = structure.eContainer();
      final DataModel dataModel = ((DataModel) _eContainer);
      if (role != null) {
        switch (role) {
          case INTERFACE:
            FileHelper _inNamespace = helper.inNamespace(NamespaceComponent.ROOT, dataModel);
            FileHelper _inFolder = _inNamespace.inFolder(FolderComponent.ROOT, FolderComponent.MODELS, dataModel);
            _inFolder.using(UsingItem.SYSTEM, UsingItem.JSON_CONVERTERS, UsingItem.GEN_ATTR, UsingItem.COLLECTIONS, 
              UsingItem.JSON, UsingItem.JSON_SERIALIZATION);
            break;
          case POCOS:
            FileHelper _inNamespace_1 = helper.inNamespace(NamespaceComponent.ROOT, dataModel);
            FileHelper _inFolder_1 = _inNamespace_1.inFolder(FolderComponent.ROOT, FolderComponent.MODELS, dataModel, FolderComponent.POCOS);
            _inFolder_1.using(UsingItem.SYSTEM, UsingItem.GEN_ATTR, UsingItem.COLLECTIONS, UsingItem.REPREZEN);
            break;
          default:
            break;
        }
      } else {
      }
      _xblockexpression = helper;
    }
    return _xblockexpression;
  }
  
  public static FileHelper of(final ResourceDefinition resource, final FileRole role, final IGenTemplateContext context, final Config config) {
    FileHelper _xblockexpression = null;
    {
      final FileHelper helper = new FileHelper(context, config);
      EObject _eContainer = resource.eContainer();
      final ResourceAPI api = ((ResourceAPI) _eContainer);
      if (role != null) {
        switch (role) {
          case INTERFACE:
            FileHelper _inNamespace = helper.inNamespace(NamespaceComponent.ROOT, api);
            FileHelper _inFolder = _inNamespace.inFolder(FolderComponent.ROOT, FolderComponent.CONTROLLERS, api);
            _inFolder.using(UsingItem.GEN_ATTR, UsingItem.COLLECTIONS);
            Config.Framework _framework = config.getFramework();
            if (_framework != null) {
              switch (_framework) {
                case ASP_DOTNET_CORE_2_0_MVC:
                  helper.using(UsingItem.REPREZEN, UsingItem.CORE_MVC);
                  break;
                case ASP_DOTNET_WEBAPI_2:
                  break;
                default:
                  break;
              }
            }
            break;
          case DELEGATE_CONTROLLER:
            FileHelper _inNamespace_1 = helper.inNamespace(NamespaceComponent.ROOT, api, NamespaceComponent.DELEGATE);
            FileHelper _inFolder_1 = _inNamespace_1.inFolder(FolderComponent.ROOT, FolderComponent.CONTROLLERS, api, FolderComponent.DELEGATE);
            _inFolder_1.using(UsingItem.SYSTEM, UsingItem.GEN_ATTR, UsingItem.COLLECTIONS);
            Config.Framework _framework_1 = config.getFramework();
            if (_framework_1 != null) {
              switch (_framework_1) {
                case ASP_DOTNET_CORE_2_0_MVC:
                  helper.using(UsingItem.CORE_MVC, UsingItem.REPREZEN);
                  break;
                case ASP_DOTNET_WEBAPI_2:
                  helper.using(UsingItem.ROUTING);
                  break;
                default:
                  break;
              }
            }
            break;
          case ABSTRACT_DELEGATE_HANDLER:
            FileHelper _inNamespace_2 = helper.inNamespace(NamespaceComponent.ROOT, api, NamespaceComponent.DELEGATE);
            FileHelper _inFolder_2 = _inNamespace_2.inFolder(FolderComponent.ROOT, FolderComponent.CONTROLLERS, api, FolderComponent.DELEGATE);
            _inFolder_2.using(UsingItem.SYSTEM, UsingItem.COLLECTIONS);
            Config.Framework _framework_2 = config.getFramework();
            if (_framework_2 != null) {
              switch (_framework_2) {
                case ASP_DOTNET_CORE_2_0_MVC:
                  helper.using(UsingItem.CORE_MVC, UsingItem.REPREZEN);
                  break;
                case ASP_DOTNET_WEBAPI_2:
                  break;
                default:
                  break;
              }
            }
            break;
          default:
            break;
        }
      } else {
      }
      _xblockexpression = helper;
    }
    return _xblockexpression;
  }
  
  public static FileHelper of(final SpecialFile specialFile, final IGenTemplateContext context, final Config config) {
    FileHelper _xblockexpression = null;
    {
      final FileHelper helper = new FileHelper(context, config);
      if (specialFile != null) {
        switch (specialFile) {
          case REPREZEN:
            FileHelper _inNamespace = helper.inNamespace(NamespaceComponent.REPREZEN);
            FileHelper _inFolder = _inNamespace.inFolder(FolderComponent.ROOT, FolderComponent.REPREZEN);
            _inFolder.using(UsingItem.SYSTEM, UsingItem.JSON_SERIALIZATION, UsingItem.COLLECTIONS, UsingItem.LINQ);
            Config.Framework _framework = config.getFramework();
            if (_framework != null) {
              switch (_framework) {
                case ASP_DOTNET_CORE_2_0_MVC:
                  helper.using(UsingItem.CORE_MVC, UsingItem.DI, UsingItem.TASKS);
                  break;
                case ASP_DOTNET_WEBAPI_2:
                  helper.using(UsingItem.ROUTING);
                  break;
                default:
                  break;
              }
            }
            break;
          default:
            break;
        }
      }
      _xblockexpression = helper;
    }
    return _xblockexpression;
  }
  
  public FileHelper inNamespace(final Object... components) {
    FileHelper _xblockexpression = null;
    {
      CollectionExtensions.addAll(this.namespaceComponents, components);
      _xblockexpression = this;
    }
    return _xblockexpression;
  }
  
  public FileHelper inFolder(final Object... components) {
    FileHelper _xblockexpression = null;
    {
      CollectionExtensions.addAll(this.folderComponents, components);
      _xblockexpression = this;
    }
    return _xblockexpression;
  }
  
  public FileHelper using(final Object... usings) {
    FileHelper _xblockexpression = null;
    {
      CollectionExtensions.addAll(this.usings, usings);
      _xblockexpression = this;
    }
    return _xblockexpression;
  }
  
  public String getCsharpFileName(final CharSequence base) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append(base, "");
    _builder.append(".cs");
    return _builder.toString();
  }
  
  public Path writeFile(final CharSequence content, final String fileName) {
    try {
      File _folder = this.getFolder();
      File _file = new File(_folder, fileName);
      Path _path = _file.toPath();
      String _wrap = this.wrap(content);
      byte[] _bytes = _wrap.getBytes();
      return Files.write(_path, _bytes);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  private String wrap(final CharSequence content) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _usingSection = this.getUsingSection();
    _builder.append(_usingSection, "");
    _builder.newLineIfNotEmpty();
    _builder.append("            ");
    _builder.newLine();
    _builder.append("namespace ");
    String _namespace = this.getNamespace();
    _builder.append(_namespace, "");
    _builder.append(" {");
    _builder.newLineIfNotEmpty();
    _builder.append("    ");
    _builder.newLine();
    _builder.append("    ");
    _builder.append(content, "    ");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    return _builder.toString();
  }
  
  private File getFolder() {
    File _xblockexpression = null;
    {
      final Function1 _function = (Object it) -> {
        return this.resolveForFolder(it);
      };
      List _map = ListExtensions.map(this.folderComponents, _function);
      final String path = IterableExtensions.join(_map, "/");
      final File dir = new File(path);
      dir.mkdirs();
      _xblockexpression = dir;
    }
    return _xblockexpression;
  }
  
  private String getNamespace() {
    return this.getNamespace(((Object[])Conversions.unwrapArray(this.namespaceComponents, Object.class)));
  }
  
  private String getNamespace(final Object... components) {
    final Function1 _function = (Object it) -> {
      return this.resolveForNamespace(it);
    };
    List _map = ListExtensions.map(((List)Conversions.doWrapArray(components)), _function);
    return IterableExtensions.join(_map, ".");
  }
  
  private CharSequence getUsingSection() {
    CharSequence _xblockexpression = null;
    {
      final Function1 _function = (Object it) -> {
        return this.resolveForUsing(it);
      };
      Iterable _map = IterableExtensions.map(this.usings, _function);
      final List targets = IterableExtensions.sort(_map);
      StringConcatenation _builder = new StringConcatenation();
      {
        for(final String target : targets) {
          _builder.append("using ");
          _builder.append(target, "");
          _builder.append(";");
          _builder.newLineIfNotEmpty();
        }
      }
      _xblockexpression = _builder;
    }
    return _xblockexpression;
  }
  
  private String resolveForFolder(final Object component) {
    boolean _matched = false;
    if (!_matched) {
      if (component instanceof FolderComponent) {
        _matched=true;
        final FolderComponent fc = ((FolderComponent)component);
        if (fc != null) {
          switch (fc) {
            case ROOT:
              File _outputDirectory = this.context.getOutputDirectory();
              return _outputDirectory.getAbsolutePath();
            case MODELS:
              return this.config.getModelsFolder();
            case CONTROLLERS:
              return this.config.getControllersFolder();
            case POCOS:
              return "POCOs";
            default:
              return this.getFormatted(fc);
          }
        } else {
          return this.getFormatted(fc);
        }
      }
    }
    if (!_matched) {
      if (component instanceof DataModel) {
        _matched=true;
        return ((DataModel)component).getName();
      }
    }
    if (!_matched) {
      if (component instanceof ResourceAPI) {
        _matched=true;
        return ((ResourceAPI)component).getName();
      }
    }
    if (!_matched) {
      if (component instanceof String) {
        _matched=true;
        return ((String)component);
      }
    }
    String _valueOf = String.valueOf(component);
    String _plus = ("Unsupported folder component: " + _valueOf);
    throw new IllegalArgumentException(_plus);
  }
  
  private String getFormatted(final FolderComponent component) {
    String _switchResult = null;
    if (component != null) {
      switch (component) {
        case REPREZEN:
          _switchResult = "RepreZen";
          break;
        default:
          String _name = component.name();
          String _lowerCase = _name.toLowerCase();
          _switchResult = StringExtensions.toFirstUpper(_lowerCase);
          break;
      }
    } else {
      String _name = component.name();
      String _lowerCase = _name.toLowerCase();
      _switchResult = StringExtensions.toFirstUpper(_lowerCase);
    }
    return _switchResult;
  }
  
  private String resolveForNamespace(final Object component) {
    boolean _matched = false;
    if (!_matched) {
      if (component instanceof NamespaceComponent) {
        _matched=true;
        final NamespaceComponent nsc = ((NamespaceComponent)component);
        if (nsc != null) {
          switch (nsc) {
            case ROOT:
              return this.config.getRootNamespace();
            default:
              return this.getFormatted(((NamespaceComponent)component));
          }
        } else {
          return this.getFormatted(((NamespaceComponent)component));
        }
      }
    }
    if (!_matched) {
      if (component instanceof DataModel) {
        _matched=true;
        return ((DataModel)component).getName();
      }
    }
    if (!_matched) {
      if (component instanceof ResourceAPI) {
        _matched=true;
        return ((ResourceAPI)component).getName();
      }
    }
    if (!_matched) {
      if (component instanceof String) {
        _matched=true;
        return ((String)component);
      }
    }
    String _valueOf = String.valueOf(component);
    String _plus = ("Unsupported namespace component: " + _valueOf);
    throw new IllegalArgumentException(_plus);
  }
  
  private String getFormatted(final NamespaceComponent component) {
    String _switchResult = null;
    if (component != null) {
      switch (component) {
        case REPREZEN:
          _switchResult = "RepreZen";
          break;
        default:
          String _name = component.name();
          String _lowerCase = _name.toLowerCase();
          _switchResult = StringExtensions.toFirstUpper(_lowerCase);
          break;
      }
    } else {
      String _name = component.name();
      String _lowerCase = _name.toLowerCase();
      _switchResult = StringExtensions.toFirstUpper(_lowerCase);
    }
    return _switchResult;
  }
  
  private String resolveForUsing(final Object usingItem) {
    boolean _matched = false;
    if (!_matched) {
      if (usingItem instanceof UsingItem) {
        _matched=true;
        final UsingItem item = ((UsingItem)usingItem);
        if (item != null) {
          switch (item) {
            case ROOT:
              return this.config.getRootNamespace();
            default:
              return item.getNamespace();
          }
        } else {
          return item.getNamespace();
        }
      }
    }
    if (!_matched) {
      if (usingItem instanceof DataModel) {
        _matched=true;
        return this.getNamespace(NamespaceComponent.ROOT, usingItem);
      }
    }
    if (!_matched) {
      if (usingItem instanceof ResourceAPI) {
        _matched=true;
        return this.getNamespace(NamespaceComponent.ROOT, usingItem);
      }
    }
    if (!_matched) {
      if (usingItem instanceof String) {
        _matched=true;
        return ((String)usingItem);
      }
    }
    String _valueOf = String.valueOf(usingItem);
    String _plus = ("Unsupported \'using\' item: " + _valueOf);
    throw new IllegalArgumentException(_plus);
  }
}