Skip to content

Commit

Permalink
Merge branch 'dev_tsplugin'
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchumacherCapgemini committed Dec 4, 2020
2 parents f35bb32 + 1162bd8 commit 2346fc9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cobigen/cobigen-tsplugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>tsplugin</artifactId>
<name>CobiGen - TypeScript Plug-in</name>
<version>7.0.0</version>
<version>7.1.0</version>
<packaging>jar</packaging>
<description>CobiGen - TypeScript Plug-in</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -101,48 +104,26 @@ private Boolean startServerConnection() {

@Override
public boolean isValidInput(Object input) {
String fileContents = null;

Path path = null;

if (input instanceof Path) {
path = (Path) input;
return true;
} else if (input instanceof File) {
path = ((File) input).toPath();
return true;
} else {
return false;
}

if (serverIsNotDeployed) {
LOG.error("We have not been able to send requests to the external server. "
+ "Most probably there is an error on the executable file. "
+ "Try to manually remove folder .cobigen/externalservers found at your user root folder");
return false;
}

// File content is not needed, as only the file extension is checked
fileContents = new String("");

String fileName = path.toString();
InputFileTo inputFile = new InputFileTo(fileName, fileContents, charset);

HttpURLConnection conn = request.getConnection("POST", "Content-Type", "application/json", "isValidInput");

if (request.sendRequest(inputFile, conn, charset)) {

String response = new String();
try (InputStreamReader isr = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(isr);) {

LOG.info("Receiving response from Server....");
response = br.readLine();

return Boolean.parseBoolean(response);
} catch (IOException e) {
connectionExc.handle(e);
try {
// Input corresponds to the parsed file
Map<String, Object> mapModel = createModel(input);
mapModel = (Map<String, Object>) mapModel.get("model");
if (Paths.get(mapModel.get("path").toString()) == null) {
return false;
}
return true;
} catch (Exception e) {
LOG.error("An exception occured while parsing the input", e);
return false;
}
}
return false;

}

@Override
Expand Down Expand Up @@ -202,6 +183,7 @@ public List<Object> getInputObjects(Object input, Charset inputCharset, boolean

try {
if (isValidInput(input)) {

String inputModel = (String) read(new File(input.toString()).toPath(), inputCharset);
Map<String, Object> mapModel = (Map<String, Object>) createModel(inputModel).get("model");

Expand Down Expand Up @@ -270,6 +252,7 @@ public Object read(Path path, Charset inputCharset, Object... additionalArgument
s.parallel().forEachOrdered((String line) -> {
inputModel.append(line);
});

return inputModel.toString();

} catch (Exception e) {
Expand All @@ -283,7 +266,10 @@ public Object read(Path path, Charset inputCharset, Object... additionalArgument

@Override
public boolean isMostLikelyReadable(Path path) {
return path.toFile().isFile() && path.toString().endsWith("." + VALID_EXTENSION);

List<String> validExtensions = Arrays.asList("ts", "js", "nest");
String fileExtension = FilenameUtils.getExtension(path.toString()).toLowerCase();
return validExtensions.contains(fileExtension);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ public void testValidInput() {

}

/**
* Sends an Object containing only the path of the file that needs to be parsed. Checks whether it is a
* valid input.
*
* @test fails
*/
@Test
public void testValidInputObjectString() {

// arrange
TypeScriptInputReader tsInputReader = new TypeScriptInputReader();
File baseFile = new File(testFileRootPath + "baseFile.ts");

String inputModel = (String) tsInputReader.read(baseFile.getAbsoluteFile().toPath(), Charset.defaultCharset());

boolean isValidInput = tsInputReader.isValidInput(inputModel);

LOG.debug("Valid input ? " + isValidInput);
assertTrue(isValidInput);

}

/**
* Sends a fileEto containing only the path of the file that needs to be parsed. Checks whether the file
* is most likely readable.
Expand Down
2 changes: 1 addition & 1 deletion documentation/master-cobigen.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DISCLAIMER: All Cobigen plugins are compatible with the latest release of Devonf
* CobiGen v7.0.0
* CobiGen - Java Plug-in v7.0.0
* CobiGen - XML Plug-in v7.0.0
* CobiGen - TypeScript Plug-in v7.0.0
* CobiGen - TypeScript Plug-in v7.1.0
* CobiGen - Property Plug-in v7.0.0
* CobiGen - Text Merger v7.0.0
* CobiGen - JSON Plug-in v7.0.0
Expand Down

0 comments on commit 2346fc9

Please sign in to comment.