Commit 1d1f662b authored by Vincent Horváth's avatar Vincent Horváth

implementation creating projects

parent 6a2c2e11
......@@ -16,15 +16,18 @@ public class AngularInterpreter extends BaseInterpreter implements FrontendInter
private File file;
private static final String TEMPLATE_ROOT_PATH = "templates/angular/";
private static final String PATTERN_PROJECT_URL = "https://github.com/wincdev/bfi-pattern-angular-rest/archive/master.zip";
private static final String PATTERN_PROJECT_NAME = "bfi-pattern-angular-rest-master";
private String outRootPath = "tmp/frontend/";
public AngularInterpreter(String baseRoot) {
super(baseRoot);
super(baseRoot, PATTERN_PROJECT_URL, PATTERN_PROJECT_NAME);
}
@Override
public void beforeGenerate() {
LOGGER.info("Angular interpreter generate form " + file.getName());
LOGGER.debug("Angular interpreter generate form " + file.getName());
}
......
package sk.tuke.bfi.angular.generator.preprocessor;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Element;
import sk.tuke.bfi.angular.generator.AngularReplacer;
import sk.tuke.bfi.core.entity.model.Editor;
import sk.tuke.bfi.core.entity.model.Field;
import sk.tuke.bfi.core.entity.model.Model;
import sk.tuke.bfi.core.preprocessor.annotation.Preprocessor;
import static sk.tuke.bfi.core.preprocessor.html.BfiAttribute.BFI_FIELD;
import static sk.tuke.bfi.core.preprocessor.html.BfiAttribute.BFI_MODEL;
@Preprocessor("output")
public class OutputPreprocessor extends AngularReplacer {
@Override
public void replace() {
Element element = getElement();
Model model = null;
final String modelName = element.attr(BFI_MODEL.toString());
final String fieldName = element.attr(BFI_FIELD.toString());
try {
model = getModel(modelName);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
final Field field = model.getField(fieldName);
if (field == null) {
throw new RuntimeException("Field " + fieldName + " is not exist in model " + modelName);
}
Element inputElement;
if (field.getEditor() == Editor.STRING) {
inputElement = getInputText(field, model);
} else {
inputElement = getInputText(field, model);
}
element.replaceWith(inputElement);
}
private Element getInputText(Field field, Model model) {
Element element = getElement();
Element outputElement = new Element("div");
outputElement.val("{" + model.getName().toLowerCase() + "." + field.getName() + "}");
for (Attribute attribute : element.attributes()) {
if (attribute.getKey().startsWith("bfi-")) {
continue;
}
outputElement.attr(attribute.getKey(), attribute.getValue());
}
return outputElement;
}
}
......@@ -68,6 +68,11 @@
<artifactId>ini4j</artifactId>
<version>0.5.1</version>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
......
......@@ -73,7 +73,7 @@ public class GeneratorImpl implements sk.tuke.bfi.core.generator.Generator {
private void generateBackendOneToOne(BaseInterface interpreter) {
final BaseEntity entity = interpreter.getEntity();
LOGGER.info("Starting generate for entity " + entity);
LOGGER.debug("Starting generate for entity " + entity);
this.projectRoot = interpreter.getProjectRoot();
final List<Class<?>> classes = getAnnotationClasses(Generator.class, interpreter.getClass().getPackage());
......@@ -133,7 +133,7 @@ public class GeneratorImpl implements sk.tuke.bfi.core.generator.Generator {
}
public void generateFrontend() {
LOGGER.info("Starting generate for frontend " + frontendInterpreter.getFile().getName());
LOGGER.debug("Starting generate for frontend " + frontendInterpreter.getFile().getName());
for (Map.Entry<String, BaseEntity> entityEntry : frontendInterpreter.getEntities().entrySet()) {
frontendInterpreter.setEntity(entityEntry.getValue());
frontendInterpreter.beforeGenerate();
......@@ -223,7 +223,7 @@ public class GeneratorImpl implements sk.tuke.bfi.core.generator.Generator {
private void prepareTemplates(final TemplateGeneratorInterface templateGenerator) throws Exception {
LOGGER.info("Start generate template " + templateGenerator.getClass());
LOGGER.debug("Start generate template " + templateGenerator.getClass());
Map<String, Method> preparedGeneratorMethods = new HashMap<>();
Map<String, Method> tmpPreparedGeneratorMethods = new HashMap<>();
......@@ -355,12 +355,12 @@ public class GeneratorImpl implements sk.tuke.bfi.core.generator.Generator {
private void savePreparedTemplate(final TemplateGeneratorInterface templateGenerator, final String templateKey) {
if (!enableOverwrite && templateGenerator.getTemplateHelper().isFileExist(templateGenerator.getOutputFileName(templateKey))) {
LOGGER.info("Disable overwrite for generator " + templateGenerator.getClass());
LOGGER.debug("Disable overwrite for generator " + templateGenerator.getClass());
return;
}
templateGenerator.getTemplateHelper().saveFile(templateGenerator.getOutputFileName(templateKey), templatesValues.get(templateKey));
LOGGER.info("Save generated template " + templateGenerator.getClass());
LOGGER.debug("Save generated template " + templateGenerator.getClass());
}
}
package sk.tuke.bfi.core.helper.impl;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.UUID;
public class FileDownloaderHelper {
private final File uploadRootDirectory;
public FileDownloaderHelper() {
uploadRootDirectory = new File("download");
if (!uploadRootDirectory.exists()) {
uploadRootDirectory.mkdir();
}
}
public File download(final String urlStr) throws IOException {
final URL url = new URL(urlStr);
final File destinationFile = new File(uploadRootDirectory.getAbsolutePath() + File.separator + getFileName(url));
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(destinationFile);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
return destinationFile;
}
public void clean() throws IOException {
FileUtils.deleteDirectory(uploadRootDirectory);
}
private String getFileName(final URL url) {
final String[] segments = url.getPath().split("/");
return String.format("%s-%s", UUID.randomUUID().toString().replace("-", ""), segments[segments.length - 1]);
}
}
package sk.tuke.bfi.core.helper.impl;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import sk.tuke.bfi.core.helper.Helper;
import java.io.*;
......@@ -10,6 +12,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class FileHelper implements Helper {
public List<File> finderByExtension(String dirName, String extension) {
......@@ -67,5 +70,9 @@ public class FileHelper implements Helper {
}
}
public static void unzip(final String source, final String destination) throws ZipException {
ZipFile zipFile = new ZipFile(source);
zipFile.extractAll(destination);
}
}
package sk.tuke.bfi.core.interpret;
import net.lingala.zip4j.exception.ZipException;
import org.apache.log4j.Logger;
import sk.tuke.bfi.core.builder.impl.LanguageBuilder;
import sk.tuke.bfi.core.entity.BaseEntity;
import sk.tuke.bfi.core.helper.impl.FileDownloaderHelper;
import sk.tuke.bfi.core.helper.impl.FileHelper;
import sk.tuke.bfi.core.interpret.types.BaseInterface;
import sk.tuke.bfi.core.validator.ValidationError;
import java.io.BufferedWriter;
......@@ -13,17 +19,23 @@ import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
public abstract class BaseInterpreter {
public abstract class BaseInterpreter implements BaseInterface {
private final static Logger LOGGER = Logger.getLogger(BaseInterpreter.class);
protected BaseEntity entity;
protected String projectRoot;
private String patternUrl;
private String patternName;
protected Map<String, BaseEntity> entities;
protected abstract String getTemplateRootPath();
protected abstract String getOutRootPath();
public BaseInterpreter(String projectRoot) {
public BaseInterpreter(final String projectRoot, final String patternUrl, final String patternName) {
this.projectRoot = projectRoot;
this.patternUrl = patternUrl;
this.patternName = patternName;
}
public void setEntities(Map<String, BaseEntity> models) {
......@@ -112,6 +124,25 @@ public abstract class BaseInterpreter {
this.entity = entity;
}
public void createProject() {
final FileDownloaderHelper fileDownloaderHelper = new FileDownloaderHelper();
final String parentDirectory = new File(getProjectRoot()).getParent();
final File file;
try {
file = fileDownloaderHelper.download(patternUrl);
FileHelper.unzip(file.getAbsolutePath(), new File(getProjectRoot()).getParent());
} catch (IOException | ZipException e) {
LOGGER.error(e.getMessage(), e);
}
File projectFile = new File(parentDirectory + File.separator + patternName);
projectFile.renameTo(new File(getProjectRoot()));
try {
fileDownloaderHelper.clean();
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
protected String getTemplatePath(String templateName) {
return getTemplateRootPath() + templateName + ".stg";
}
......
package sk.tuke.bfi.core.interpret.types;
import sk.tuke.bfi.core.entity.BaseEntity;
public interface BackendInterface extends BaseInterface{
}
......@@ -17,4 +17,6 @@ public interface BaseInterface {
public void setEntities(Map<String, BaseEntity> models);
public Map<String, BaseEntity> getEntities();
public void createProject();
}
......@@ -37,7 +37,7 @@ public abstract class AbstractFilesWatcher extends Thread {
public void run() {
try (WatchService watcher = FileSystems.getDefault().newWatchService()) {
registerRecursive(root, watcher);
LOGGER.info("File watcher is register for path " + root.toString());
LOGGER.debug("File watcher is register for path " + root.toString());
while (!isStopped()) {
WatchKey key;
try {
......
......@@ -17,6 +17,18 @@
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>sk.tuke.bfi</groupId>
<artifactId>angular</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>sk.tuke.bfi</groupId>
<artifactId>symfony</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
......
package sk.tuke.bfi.main.service;
import org.apache.commons.cli.*;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.ini4j.Ini;
import org.ini4j.IniPreferences;
import sk.tuke.bfi.core.ApplicationConfiguration;
......@@ -22,17 +24,21 @@ import java.util.prefs.Preferences;
public class RunService {
protected final static Logger LOGGER = Logger.getLogger(RunService.class);
public static final String DEFAULT_CONFIGURATION_NAME = "conf.ini";
private ClassLoaderService classLoaderHelper = new ClassLoaderService();
private RunService.Configuration configuration;
private Generator generator;
private LanguageBuilder languageBuilder;
private BackendInterface backendInterface;
private FrontendInterface frontendInterface;
private List<AbstractFilesWatcher> watchers = new ArrayList<>();
private List<Exception> exceptions = new ArrayList<>();
private RunOptions runOptions;
public void run(String[] args) throws Exception {
runOptions = new RunOptions(args);
setLogger();
if (!runOptions.parameterIsValid()) {
exceptions.addAll(runOptions.getExceptions());
throw new Exception("Run options is not valid");
......@@ -49,16 +55,18 @@ public class RunService {
}
if (runOptions.getCmd().hasOption(RunOptions.BUILD)) {
LOGGER.info("BFI building.");
build();
} else if (runOptions.getCmd().hasOption(RunOptions.WATCH)) {
LOGGER.info("BFI watching.");
watch();
} else if (runOptions.getCmd().hasOption(RunOptions.CLEAN)) {
clean();
} else if (runOptions.getCmd().hasOption(RunOptions.CREATE)) {
LOGGER.info("BFI creating.");
create();
}else{
} else {
build();
}
LOGGER.info("BFI is finished.");
}
public void build() {
......@@ -66,7 +74,8 @@ public class RunService {
}
public void create() {
backendInterface.createProject();
frontendInterface.createProject();
}
public List<Exception> getExceptions() {
......@@ -100,10 +109,6 @@ public class RunService {
}
}
public void clean() {
}
private void init() throws Exception {
if (!runOptions.getCmd().hasOption(RunOptions.DEV)) {
classLoaderHelper.load(configuration.getBackendJar(), configuration.getFrontendJar());
......@@ -113,13 +118,24 @@ public class RunService {
Object[] values = new Object[1];
values[0] = configuration.getBackendProjectPath();
final BackendInterface backend = classLoaderHelper.createObject(BackendInterface.class, configuration.getBackendClass(), parameters, values);
backendInterface = classLoaderHelper.createObject(BackendInterface.class, configuration.getBackendClass(), parameters, values);
values[0] = configuration.getFrontendProjectPath();
final FrontendInterface frontend = classLoaderHelper.createObject(FrontendInterface.class, configuration.getFrontendClass(), parameters, values);
frontendInterface = classLoaderHelper.createObject(FrontendInterface.class, configuration.getFrontendClass(), parameters, values);
generator = new GeneratorImpl(backend, frontend);
generator = new GeneratorImpl(backendInterface, frontendInterface);
languageBuilder = LanguageBuilder.getBuilder(configuration.getBfiProjectPath(), generator);
GeneratorState.getGeneratorState().initState(classLoaderHelper.getClassLoader(),configuration);
GeneratorState.getGeneratorState().initState(classLoaderHelper.getClassLoader(), configuration);
}
private void setLogger() {
if (runOptions.getCmd().hasOption(RunOptions.DEBUG)) {
Logger.getRootLogger().setLevel(Level.ALL);
} else {
Logger.getRootLogger().setLevel(Level.ERROR);
Logger.getRootLogger().setLevel(Level.INFO);
Logger.getRootLogger().setLevel(Level.TRACE);
Logger.getRootLogger().setLevel(Level.WARN);
}
}
private void loadConfiguration(final String filename) throws Exception {
......@@ -135,9 +151,9 @@ public class RunService {
public class RunOptions {
public static final String DEV = "dev";
public static final String DEBUG = "debug";
public static final String WATCH = "w";
public static final String BUILD = "b";
public static final String CLEAN = "cl";
public static final String CREATE = "c";
public static final String CONFIG_FILE = "cf";
......@@ -157,9 +173,9 @@ public class RunService {
private void seedOptions() {
options.addOption(DEV, "dev", false, "Run for development.");
options.addOption(DEBUG, "debug", false, "Run for debugging.");
options.addOption(WATCH, "watch", false, "Watching changes in projects files.");
options.addOption(BUILD, "build", false, "Build BFI.");
options.addOption(CLEAN, "clean", false, "Remove generated files in your project.");
options.addOption(CREATE, "create", false, "Creating projects.");
options.addOption(CONFIG_FILE, "config-file", true, "Set path for config file");
}
......
......@@ -7,13 +7,15 @@ import sk.tuke.bfi.core.interpret.types.BackendInterface;
public class SymfonyInterpreter extends BaseInterpreter implements BackendInterface {
private static final String TEMPLATE_ROOT_PATH = "templates/";
private static final String PATTERN_PROJECT_URL = "https://github.com/wincdev/bfi-pattern-symfony-rest/archive/master.zip";
private static final String PATTERN_PROJECT_NAME = "bfi-pattern-symfony-rest-master";
private StringBuffer doctrine = new StringBuffer();
private String baseNameFile;
private String baseName;
private Generator generator;
public SymfonyInterpreter(String projectRoot) {
super(projectRoot);
super(projectRoot, PATTERN_PROJECT_URL,PATTERN_PROJECT_NAME);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment