Skip to content

Commit

Permalink
Merge pull request #363 from Bhashinee/main
Browse files Browse the repository at this point in the history
Fix a couple of issues identified in the build command execution
  • Loading branch information
daneshk committed Apr 25, 2024
2 parents c2ee531 + 86934be commit d02591e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,44 +67,42 @@ public void testBuildWithMysql() throws IOException, InterruptedException {

@Test(enabled = true)
public void testBuildWithInvalidTargetModule() throws IOException, InterruptedException {
String log = "ERROR: invalid module name : 'persist_add_1' :";
String log = "error: build tool execution contains errors";
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_2");
assertContainLogs(log, project);
}

@Test(enabled = true)
public void testBuildWithInvalidCharachtersInTargetModule() throws IOException, InterruptedException {
String log = "ERROR: invalid module name : '*****' :";
String log = "error: build tool execution contains errors";
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_3");
assertContainLogs(log, project);
}

@Test(enabled = true)
public void testBuildWithInvalidLengthOfTargetModule() throws IOException, InterruptedException {
String log = "ERROR: invalid module name :";
String log = "error: build tool execution contains errors";
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_4");
assertContainLogs(log, project);
}

@Test(enabled = true)
public void testBuildWithInvalidDataSource() throws IOException, InterruptedException {
String log = "ERROR: the persist layer supports one of data stores:";
String log = "error: build tool execution contains errors";
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_5");
assertContainLogs(log, project);
}

@Test(enabled = true)
public void testBuildWithoutEntities() throws IOException, InterruptedException {
String log = "ERROR: the model definition file(model.bal) does not contain any entity definition.";
String log = "error: build tool execution contains errors";
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_6");
assertContainLogs(log, project);
}

@Test(enabled = true)
public void testBuildWithExistingDependency() throws IOException, InterruptedException {
String log = "ERROR: the 'Ballerina.toml' file is already updated with the Persist client native dependency" +
" but the version is different from the current version. Please remove the existing dependency and " +
"try again.";
String log = "error: build tool execution contains errors";
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_7");
assertContainLogs(log, project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package io.ballerina.persist;

import io.ballerina.tools.diagnostics.DiagnosticSeverity;

import java.util.Set;

/**
Expand Down Expand Up @@ -269,7 +271,39 @@ private REDIS() {}
public static final String MAX_AGE = "-1";

}
}

/**
* Enum class for containing diagnostic messages.
*/
public enum DiagnosticMessages {
INVALID_MODULE_NAME("PERSIST_CLIENT_01", "invalid module name : `%s`" + System.lineSeparator() +
"module name should follow the template \"<package_name>.<module_name>\"",
DiagnosticSeverity.ERROR),
ERROR_WHILE_GENERATING_CLIENT("PERSIST_CLIENT_02", "unexpected error occurred while generating the client"
+ System.lineSeparator() + "%s",
DiagnosticSeverity.ERROR);

private final String code;
private final String description;
private final DiagnosticSeverity severity;

DiagnosticMessages(String code, String description, DiagnosticSeverity severity) {
this.code = code;
this.description = description;
this.severity = severity;
}

public String getCode() {
return code;
}

public String getDescription() {
return description;
}

public DiagnosticSeverity getSeverity() {
return severity;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import io.ballerina.projects.buildtools.ToolConfig;
import io.ballerina.projects.buildtools.ToolContext;
import io.ballerina.projects.util.ProjectUtils;
import io.ballerina.toml.semantic.diagnostics.TomlNodeLocation;
import io.ballerina.tools.diagnostics.DiagnosticFactory;
import io.ballerina.tools.diagnostics.DiagnosticInfo;
import io.ballerina.tools.diagnostics.Location;

import java.io.IOException;
import java.io.PrintStream;
Expand Down Expand Up @@ -59,6 +63,7 @@ public void execute(ToolContext toolContext) {
String packageName;
String targetModule;

TomlNodeLocation location = toolContext.currentPackage().ballerinaToml().get().tomlAstNode().location();
Path projectPath = toolContext.currentPackage().project().sourceRoot();
Path generatedSourceDirPath = Paths.get(projectPath.toString(), BalSyntaxConstants.GENERATED_SOURCE_DIRECTORY);
try {
Expand All @@ -72,9 +77,8 @@ public void execute(ToolContext toolContext) {
validateDatastore(datastore);
if (!targetModule.equals(packageName)) {
if (!targetModule.startsWith(packageName + ".")) {
errStream.println("ERROR: invalid module name : '" + ballerinaTomlConfig.get(TARGET_MODULE)
+ "' :" + System.lineSeparator() + "module name should follow the template " +
"<package_name>.<module_name>");
createDiagnostics(toolContext, PersistToolsConstants.DiagnosticMessages.INVALID_MODULE_NAME,
location, ballerinaTomlConfig.get(TARGET_MODULE));
return;
}
String moduleName = targetModule.replace(packageName + ".", "");
Expand All @@ -84,12 +88,13 @@ public void execute(ToolContext toolContext) {
validatePersistDirectory(datastore, projectPath);
printExperimentalFeatureInfo(datastore);
try {
if (validateCache(toolContext, schemaFilePath)) {
if (validateCache(toolContext, schemaFilePath) && Files.exists(generatedSourceDirPath)) {
return;
}
} catch (NoSuchAlgorithmException e) {
errStream.println("INFO: unable to validate the cache. Generating sources for the schema file.");
}
BalProjectUtils.validateSchemaFile(schemaFilePath);
entityModule = BalProjectUtils.getEntities(schemaFilePath);
validateEntityModule(entityModule, schemaFilePath);
String syntaxTree = TomlSyntaxUtils.addPersistNativeDependency(
Expand All @@ -98,10 +103,14 @@ public void execute(ToolContext toolContext) {
Paths.get(projectPath.toString(), BALLERINA_TOML).toAbsolutePath().toString());
createGeneratedSourceDirIfNotExists(generatedSourceDirPath);
generateSources(datastore, entityModule, targetModule, projectPath, generatedSourceDirPath);
String modelHashVal = getHashValue(schemaFilePath);
Path cachePath = toolContext.cachePath();
updateCacheFile(cachePath, modelHashVal);
errStream.println("Persist client and entity types generated successfully in the " + targetModule +
" directory.");
} catch (BalException | IOException e) {
errStream.printf("ERROR: %s%n", e.getMessage());
} catch (BalException | IOException | NoSuchAlgorithmException e) {
createDiagnostics(toolContext, PersistToolsConstants.DiagnosticMessages.ERROR_WHILE_GENERATING_CLIENT,
location, e.getMessage());
}
}

Expand All @@ -123,17 +132,12 @@ private static boolean validateCache(ToolContext toolContext, Path schemaFilePat
Path cachePath = toolContext.cachePath();
String modelHashVal = getHashValue(schemaFilePath);
if (!Files.isDirectory(cachePath)) {
updateCacheFile(cachePath, modelHashVal);
return false;
}
// read the cache file
Path cacheFilePath = Paths.get(cachePath.toString(), CACHE_FILE);
String cacheContent = Files.readString(Paths.get(cacheFilePath.toString()));
boolean isCacheValid = cacheContent.equals(modelHashVal);
if (!isCacheValid) {
updateCacheFile(cachePath, modelHashVal);
}
return isCacheValid;
return cacheContent.equals(modelHashVal);
}

private static void updateCacheFile(Path cachePath, String modelHashVal) {
Expand All @@ -143,7 +147,7 @@ private static void updateCacheFile(Path cachePath, String modelHashVal) {
Files.createDirectories(cachePath);
Files.createFile(cacheFilePath);
}
Files.writeString(cacheFilePath, modelHashVal, StandardOpenOption.WRITE);
Files.writeString(cacheFilePath, modelHashVal, StandardCharsets.UTF_8, StandardOpenOption.WRITE);
} catch (IOException e) {
errStream.println("ERROR: failed to update the cache file: " + e.getMessage());
}
Expand Down Expand Up @@ -221,4 +225,12 @@ private void generateSources(String datastore, Module entityModule, String targe
break;
}
}

private static void createDiagnostics(ToolContext toolContext, PersistToolsConstants.DiagnosticMessages error,
Location location, String... args) {
String message = String.format(error.getDescription(), (Object[]) args);
DiagnosticInfo diagnosticInfo = new DiagnosticInfo(error.getCode(), message,
error.getSeverity());
toolContext.reportDiagnostic(DiagnosticFactory.createDiagnostic(diagnosticInfo, location));
}
}

0 comments on commit d02591e

Please sign in to comment.