From e72d012496f2d2bc6874c92d31d60cb82a5c8d5c Mon Sep 17 00:00:00 2001 From: ushirask Date: Thu, 23 Mar 2023 16:52:58 +0530 Subject: [PATCH 01/70] Add currentConstSymbolType --- .../util/diagnostic/DiagnosticErrorCode.java | 4 +- .../analyzer/ConstantValueResolver.java | 59 ++++++++++--------- .../semantics/analyzer/SemanticAnalyzer.java | 2 +- .../src/main/resources/compiler.properties | 3 + 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java index cf1446ee155e..f192723ab404 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java @@ -799,7 +799,9 @@ public enum DiagnosticErrorCode implements DiagnosticCode { EMPTY_REGEXP_STRING_DISALLOWED( "BCS4044", "empty.regexp.string.disallowed"), UNSUPPORTED_EMPTY_CHARACTER_CLASS( - "BCS4045", "unsupported.empty.character.class") + "BCS4045", "unsupported.empty.character.class"), + ANNOTATION_ELEMENT_SHOULD_BE_LITERAL( + "BCS4046", "annotation.element.should.be.a.literal") ; private String diagnosticId; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java index f0c68289294c..afd433f92a23 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java @@ -95,6 +95,7 @@ public class ConstantValueResolver extends BLangNodeVisitor { private static final CompilerContext.Key CONSTANT_VALUE_RESOLVER_KEY = new CompilerContext.Key<>(); private BConstantSymbol currentConstSymbol; + private BType currentConstSymbolType; private BLangConstantValue result; private BLangDiagnosticLog dlog; private Location currentPos; @@ -144,7 +145,9 @@ public void visit(BLangConstant constant) { return; // Already visited. } BConstantSymbol tempCurrentConstSymbol = this.currentConstSymbol; + BType tempCurrentConstSymbolType = this.currentConstSymbolType; this.currentConstSymbol = constant.symbol; + this.currentConstSymbolType = constant.symbol.type; this.resolvingConstants.add(this.currentConstSymbol); this.currentConstSymbol.value = constructBLangConstantValue(constant.expr); this.resolvingConstants.remove(this.currentConstSymbol); @@ -154,6 +157,7 @@ public void visit(BLangConstant constant) { checkUniqueness(constant); unresolvedConstants.remove(this.currentConstSymbol); this.currentConstSymbol = tempCurrentConstSymbol; + this.currentConstSymbolType = tempCurrentConstSymbolType; } @Override @@ -221,7 +225,6 @@ public void visit(BLangRecordLiteral recordLiteral) { for (RecordLiteralNode.RecordField field : recordLiteral.fields) { String key; BLangConstantValue value; - if (field.isKeyValueField()) { BLangRecordLiteral.BLangRecordKeyValueField keyValuePair = (BLangRecordLiteral.BLangRecordKeyValueField) field; @@ -234,8 +237,10 @@ public void visit(BLangRecordLiteral recordLiteral) { } else { continue; } - + BType tempCurrentConstSymbolType = this.currentConstSymbolType; + this.currentConstSymbolType = keyValuePair.getBType(); value = constructBLangConstantValue(keyValuePair.valueExpr); + this.currentConstSymbolType = tempCurrentConstSymbolType; } else if (field.getKind() == NodeKind.SIMPLE_VARIABLE_REF) { BLangRecordLiteral.BLangRecordVarNameField varNameField = (BLangRecordLiteral.BLangRecordVarNameField) field; @@ -300,7 +305,7 @@ private BLangConstantValue calculateConstValue(BLangConstantValue lhs, BLangCons if (lhs == null || rhs == null || lhs.value == null || rhs.value == null) { // This is a compilation error. // This is to avoid NPE exceptions in sub-sequent validations. - return new BLangConstantValue(null, this.currentConstSymbol.type); + return new BLangConstantValue(null, this.currentConstSymbolType); } // See Types.isAllowedConstantType() for supported types. try { @@ -337,20 +342,20 @@ private BLangConstantValue calculateConstValue(BLangConstantValue lhs, BLangCons } // This is a compilation error already logged. // This is to avoid NPE exceptions in sub-sequent validations. - return new BLangConstantValue(null, this.currentConstSymbol.type); + return new BLangConstantValue(null, this.currentConstSymbolType); } - private BLangConstantValue evaluateUnaryOperator(BLangConstantValue value, OperatorKind kind) { + private BLangConstantValue evaluateUnaryOperator(BLangConstantValue value, OperatorKind kind) { if (value == null || value.value == null) { // This is a compilation error. // This is to avoid NPE exceptions in sub-sequent validations. - return new BLangConstantValue(null, this.currentConstSymbol.type); + return new BLangConstantValue(null, this.currentConstSymbolType); } try { switch (kind) { case ADD: - return new BLangConstantValue(value.value, currentConstSymbol.type); + return new BLangConstantValue(value.value, currentConstSymbolType); case SUB: return calculateNegation(value); case BITWISE_COMPLEMENT: @@ -363,25 +368,25 @@ private BLangConstantValue evaluateUnaryOperator(BLangConstantValue value, Opera } // This is a compilation error already logged. // This is to avoid NPE exceptions in sub-sequent validations. - return new BLangConstantValue(null, this.currentConstSymbol.type); + return new BLangConstantValue(null, this.currentConstSymbolType); } private BLangConstantValue calculateBitWiseOp(BLangConstantValue lhs, BLangConstantValue rhs, BiFunction func) { - switch (Types.getReferredType(this.currentConstSymbol.type).tag) { + switch (Types.getReferredType(this.currentConstSymbolType).tag) { case TypeTags.INT: Long val = func.apply((Long) lhs.value, (Long) rhs.value); - return new BLangConstantValue(val, this.currentConstSymbol.type); + return new BLangConstantValue(val, this.currentConstSymbolType); default: dlog.error(currentPos, DiagnosticErrorCode.CONSTANT_EXPRESSION_NOT_SUPPORTED); } - return new BLangConstantValue(null, this.currentConstSymbol.type); + return new BLangConstantValue(null, this.currentConstSymbolType); } private BLangConstantValue calculateAddition(BLangConstantValue lhs, BLangConstantValue rhs) { Object result = null; - switch (Types.getReferredType(this.currentConstSymbol.type).tag) { + switch (Types.getReferredType(this.currentConstSymbolType).tag) { case TypeTags.INT: case TypeTags.BYTE: // Byte will be a compiler error. try { @@ -405,12 +410,12 @@ private BLangConstantValue calculateAddition(BLangConstantValue lhs, BLangConsta result = String.valueOf(lhs.value) + String.valueOf(rhs.value); break; } - return new BLangConstantValue(result, currentConstSymbol.type); + return new BLangConstantValue(result, currentConstSymbolType); } private BLangConstantValue calculateSubtract(BLangConstantValue lhs, BLangConstantValue rhs) { Object result = null; - switch (Types.getReferredType(this.currentConstSymbol.type).tag) { + switch (Types.getReferredType(this.currentConstSymbolType).tag) { case TypeTags.INT: case TypeTags.BYTE: // Byte will be a compiler error. try { @@ -431,12 +436,12 @@ private BLangConstantValue calculateSubtract(BLangConstantValue lhs, BLangConsta result = resultDecimal != null ? resultDecimal.toPlainString() : null; break; } - return new BLangConstantValue(result, currentConstSymbol.type); + return new BLangConstantValue(result, currentConstSymbolType); } private BLangConstantValue calculateMultiplication(BLangConstantValue lhs, BLangConstantValue rhs) { Object result = null; - switch (Types.getReferredType(this.currentConstSymbol.type).tag) { + switch (Types.getReferredType(this.currentConstSymbolType).tag) { case TypeTags.INT: case TypeTags.BYTE: // Byte will be a compiler error. try { @@ -457,17 +462,17 @@ private BLangConstantValue calculateMultiplication(BLangConstantValue lhs, BLang result = resultDecimal != null ? resultDecimal.toPlainString() : null; break; } - return new BLangConstantValue(result, currentConstSymbol.type); + return new BLangConstantValue(result, currentConstSymbolType); } private BLangConstantValue calculateDivision(BLangConstantValue lhs, BLangConstantValue rhs) { Object result = null; - switch (Types.getReferredType(this.currentConstSymbol.type).tag) { + switch (Types.getReferredType(this.currentConstSymbolType).tag) { case TypeTags.INT: case TypeTags.BYTE: // Byte will be a compiler error. if ((Long) lhs.value == Long.MIN_VALUE && (Long) rhs.value == -1) { dlog.error(currentPos, DiagnosticErrorCode.INT_RANGE_OVERFLOW_ERROR); - return new BLangConstantValue(null, this.currentConstSymbol.type); + return new BLangConstantValue(null, this.currentConstSymbolType); } result = (Long) ((Long) lhs.value / (Long) rhs.value); break; @@ -483,12 +488,12 @@ private BLangConstantValue calculateDivision(BLangConstantValue lhs, BLangConsta result = resultDecimal != null ? resultDecimal.toPlainString() : null; break; } - return new BLangConstantValue(result, currentConstSymbol.type); + return new BLangConstantValue(result, currentConstSymbolType); } private BLangConstantValue calculateMod(BLangConstantValue lhs, BLangConstantValue rhs) { Object result = null; - switch (Types.getReferredType(this.currentConstSymbol.type).tag) { + switch (Types.getReferredType(this.currentConstSymbolType).tag) { case TypeTags.INT: case TypeTags.BYTE: // Byte will be a compiler error. result = (Long) ((Long) lhs.value % (Long) rhs.value); @@ -504,13 +509,13 @@ private BLangConstantValue calculateMod(BLangConstantValue lhs, BLangConstantVal result = resultDecimal.toPlainString(); break; } - return new BLangConstantValue(result, currentConstSymbol.type); + return new BLangConstantValue(result, currentConstSymbolType); } private Object calculateNegationForInt(BLangConstantValue value) { if ((Long) (value.value) == Long.MIN_VALUE) { dlog.error(currentPos, DiagnosticErrorCode.INT_RANGE_OVERFLOW_ERROR); - return new BLangConstantValue(null, this.currentConstSymbol.type); + return new BLangConstantValue(null, this.currentConstSymbolType); } return -1 * ((Long) (value.value)); } @@ -548,18 +553,18 @@ private BLangConstantValue calculateNegation(BLangConstantValue value) { private BLangConstantValue calculateBitWiseComplement(BLangConstantValue value) { Object result = null; - if (Types.getReferredType(this.currentConstSymbol.type).tag == TypeTags.INT) { + if (Types.getReferredType(this.currentConstSymbolType).tag == TypeTags.INT) { result = ~((Long) (value.value)); } - return new BLangConstantValue(result, currentConstSymbol.type); + return new BLangConstantValue(result, currentConstSymbolType); } private BLangConstantValue calculateBooleanComplement(BLangConstantValue value) { Object result = null; - if (Types.getReferredType(this.currentConstSymbol.type).tag == TypeTags.BOOLEAN) { + if (Types.getReferredType(this.currentConstSymbolType).tag == TypeTags.BOOLEAN) { result = !((Boolean) (value.value)); } - return new BLangConstantValue(result, currentConstSymbol.type); + return new BLangConstantValue(result, currentConstSymbolType); } BLangConstantValue constructBLangConstantValueWithExactType(BLangExpression expression, diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index e39056fddb66..005bccabbd8a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -4387,7 +4387,7 @@ private void checkAnnotConstantExpression(BLangExpression expression) { ((BLangListConstructorExpr) expression).exprs.forEach(this::checkAnnotConstantExpression); break; default: - dlog.error(expression.pos, DiagnosticErrorCode.EXPRESSION_IS_NOT_A_CONSTANT_EXPRESSION); + dlog.error(expression.pos, DiagnosticErrorCode.ANNOTATION_ELEMENT_SHOULD_BE_LITERAL); break; } } diff --git a/compiler/ballerina-lang/src/main/resources/compiler.properties b/compiler/ballerina-lang/src/main/resources/compiler.properties index aff8578d678e..7ca2ff174957 100644 --- a/compiler/ballerina-lang/src/main/resources/compiler.properties +++ b/compiler/ballerina-lang/src/main/resources/compiler.properties @@ -1967,3 +1967,6 @@ error.empty.regexp.string.disallowed=\ error.unsupported.empty.character.class=\ empty character class disallowed + +error.annotation.element.should.be.a.literal=\ + annotation element should be a literal From 98464f65bb3c787ee51beefc5edfbaf9aea2ed75 Mon Sep 17 00:00:00 2001 From: ushirask Date: Fri, 24 Mar 2023 11:34:26 +0530 Subject: [PATCH 02/70] Add test cases --- .../semantics/analyzer/SemanticAnalyzer.java | 2 +- .../AnnotationAttachmentNegativeTest.java | 13 +++++- .../AnnotationAttachmentSymbolsTest.java | 24 +++++++++++ .../annotations/annot_attachments.bal | 30 ++++++++++++++ .../annot_attachments_negative.bal | 41 +++++++++++++++++++ 5 files changed, 108 insertions(+), 2 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index 005bccabbd8a..d108a41af919 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -4368,7 +4368,7 @@ private void checkAnnotConstantExpression(BLangExpression expression) { BSymbol symbol = ((BLangSimpleVarRef) expression).symbol; // Symbol can be null in some invalid scenarios. Eg - const string m = { name: "Ballerina" }; if (symbol != null && (symbol.tag & SymTag.CONSTANT) != SymTag.CONSTANT) { - dlog.error(expression.pos, DiagnosticErrorCode.EXPRESSION_IS_NOT_A_CONSTANT_EXPRESSION); + dlog.error(expression.pos, DiagnosticErrorCode.ANNOTATION_ELEMENT_SHOULD_BE_LITERAL); } break; case RECORD_LITERAL_EXPR: diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java index bea03e6649b6..fbee6962630e 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java @@ -37,7 +37,7 @@ public class AnnotationAttachmentNegativeTest { @BeforeClass public void setup() { compileResult = BCompileUtil.compile("test-src/annotations/annot_attachments_negative.bal"); - Assert.assertEquals(compileResult.getErrorCount(), 272); + Assert.assertEquals(compileResult.getErrorCount(), 277); } @Test @@ -507,4 +507,15 @@ public void testInvalidAttachmentsOnEnum() { validateError(compileResult, index++, "annotation 'v23' is not allowed on type", line += 7, 1); validateError(compileResult, index, "annotation 'v22' is not allowed on const", line + 2, 5); } + + @Test + public void testInvalidConstAnnotElements() { + int index = 272; + int line = 943; + validateError(compileResult, index++, "annotation element should be a literal", line, 16); + validateError(compileResult, index++, "annotation element should be a literal", line += 7, 17); + validateError(compileResult, index++, "annotation element should be a literal", line += 7, 16); + validateError(compileResult, index++, "annotation element should be a literal", line += 9, 16); + validateError(compileResult, index++, "annotation element should be a literal", line += 7, 16); + } } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentSymbolsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentSymbolsTest.java index 107c4888cebf..e5b8194f7502 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentSymbolsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentSymbolsTest.java @@ -370,6 +370,30 @@ public void testNonSourceAnnotsWithConstLists() { Assert.assertEquals(arrList.get(1).value, 2L); } + @Test + public void testConstAnnots() { + List attachmentsF1 = + ((BTypeDefinitionSymbol) getTypeDefinition(compileResult.getAST().getTypeDefinitions(), "F1").symbol) + .getAnnotations(); + Assert.assertEquals(attachmentsF1.size(), 1); + assertAttachmentSymbol(attachmentsF1.get(0), "v29", true, "increment", 2L); + List attachmentsF2 = + ((BTypeDefinitionSymbol) getTypeDefinition(compileResult.getAST().getTypeDefinitions(), "F2").symbol) + .getAnnotations(); + Assert.assertEquals(attachmentsF2.size(), 1); + assertAttachmentSymbol(attachmentsF2.get(0), "v29", true, "increment", 1L); + List attachmentsF3 = + ((BTypeDefinitionSymbol) getTypeDefinition(compileResult.getAST().getTypeDefinitions(), "F3").symbol) + .getAnnotations(); + Assert.assertEquals(attachmentsF3.size(), 1); + assertAttachmentSymbol(attachmentsF3.get(0), "v29", true, "increment", -1L); + List attachmentsF4 = + ((BTypeDefinitionSymbol) getTypeDefinition(compileResult.getAST().getTypeDefinitions(), "F4").symbol) + .getAnnotations(); + Assert.assertEquals(attachmentsF4.size(), 1); + assertAttachmentSymbol(attachmentsF4.get(0), "v29", true, "increment", -2L); + } + private BLangTypeDefinition getTypeDefinition(List typeDefinitions, String name) { for (TypeDefinition typeDefinition : typeDefinitions) { BLangTypeDefinition bLangTypeDefinition = (BLangTypeDefinition) typeDefinition; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments.bal b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments.bal index b0ffa6937973..37ad9aec0a6b 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments.bal @@ -307,3 +307,33 @@ public const annotation record {| int increment = 1; |} v29 on source type; type Qux record {| int x; |}; + +const int x = 1; + +@v29 { + increment: +2 +} +type F1 record {| + int x; +|}; + +@v29 { + increment: +x +} +type F2 record {| + int x; +|}; + +@v29 { + increment: -x +} +type F3 record {| + int x; +|}; + +@v29 { + increment: -2 +} +type F4 record {| + int x; +|}; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments_negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments_negative.bal index c0adce7da697..13466f8e7808 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments_negative.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments_negative.bal @@ -934,3 +934,44 @@ public enum Color5 { ORANGE, GREEN } + +public const annotation record {| int increment; |} v25 on source type; + +int x = 1; + +@v25 { + increment: x + 1 +} +type F1 record {| + int x; +|}; + +@v25 { + increment: -x +} +type F2 record {| + int x; +|}; + +@v25 { + increment: 1 + 2 +} +type F3 record {| + int x; +|}; + +const int y = 3; + +@v25 { + increment: y + 1 +} +type F4 record {| + int x; +|}; + +@v25 { + increment: y + x +} +type F5 record {| + int x; +|}; \ No newline at end of file From 2efdab6c642823b25951b6bcd08380fd62503309 Mon Sep 17 00:00:00 2001 From: Ushira Karunasena Date: Fri, 24 Mar 2023 11:36:44 +0530 Subject: [PATCH 03/70] Add new line --- .../test-src/annotations/annot_attachments_negative.bal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments_negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments_negative.bal index 13466f8e7808..8dd83a0524d2 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments_negative.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annot_attachments_negative.bal @@ -974,4 +974,4 @@ type F4 record {| } type F5 record {| int x; -|}; \ No newline at end of file +|}; From 29aa84740967f1543d312b958cdfdfee6bd597a4 Mon Sep 17 00:00:00 2001 From: ushirask Date: Fri, 24 Mar 2023 12:33:34 +0530 Subject: [PATCH 04/70] Fix test case --- .../compiler/semantics/analyzer/SemanticAnalyzer.java | 2 +- .../test/annotations/AnnotationAttachmentNegativeTest.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index d108a41af919..005bccabbd8a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -4368,7 +4368,7 @@ private void checkAnnotConstantExpression(BLangExpression expression) { BSymbol symbol = ((BLangSimpleVarRef) expression).symbol; // Symbol can be null in some invalid scenarios. Eg - const string m = { name: "Ballerina" }; if (symbol != null && (symbol.tag & SymTag.CONSTANT) != SymTag.CONSTANT) { - dlog.error(expression.pos, DiagnosticErrorCode.ANNOTATION_ELEMENT_SHOULD_BE_LITERAL); + dlog.error(expression.pos, DiagnosticErrorCode.EXPRESSION_IS_NOT_A_CONSTANT_EXPRESSION); } break; case RECORD_LITERAL_EXPR: diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java index fbee6962630e..707d7cadd1c5 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java @@ -513,9 +513,9 @@ public void testInvalidConstAnnotElements() { int index = 272; int line = 943; validateError(compileResult, index++, "annotation element should be a literal", line, 16); - validateError(compileResult, index++, "annotation element should be a literal", line += 7, 17); + validateError(compileResult, index++, "expression is not a constant expression", line += 7, 17); validateError(compileResult, index++, "annotation element should be a literal", line += 7, 16); validateError(compileResult, index++, "annotation element should be a literal", line += 9, 16); - validateError(compileResult, index++, "annotation element should be a literal", line += 7, 16); + validateError(compileResult, index, "annotation element should be a literal", line + 7, 16); } } From c85607ae95b73ffc5a1e6edac9c4544baf17abbf Mon Sep 17 00:00:00 2001 From: ChathurangaJayanath Date: Thu, 11 May 2023 01:08:45 +0530 Subject: [PATCH 05/70] update cli help with graphql service generation --- .../resources/cli-help/ballerina-graphql.help | 26 ++++++++++++++++--- .../resources/cli-help/ballerina-help.help | 5 ++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help index 860c3e2cbd34..eb4b29720611 100644 --- a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help +++ b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help @@ -1,6 +1,7 @@ NAME - ballerina-graphql - Generate the Ballerina client sources for a GraphQL config file - and generate the GraphQL schema for a Ballerina GraphQL service. + ballerina-graphql - Generate the Ballerina client sources for a GraphQL config file, + generate the GraphQL schema for a Ballerina GraphQL service + and generate the Ballerina service sources for a GraphQL schema. SYNOPSIS @@ -9,6 +10,10 @@ SYNOPSIS bal graphql [-i | --input] [-o | --output] [-s | --service] + bal graphql [-i | --input] + [-o | --output] + [-m | --mode] + [-r | --use-records-for-objects] DESCRIPTION @@ -21,11 +26,12 @@ DESCRIPTION OPTIONS - -i, --input + -i, --input This is mandatory input. The given GraphQL config file which is configured with GraphQL schemas (SDL) and queries, will generate the Ballerina GraphQL client sources. The given Ballerina GraphQL service file will generate the GraphQL schema (SDL) file relevant to the service. + The given GraphQL schema file will generate the Ballerina GraphQL service sources. -o, --output Location of the generated Ballerina source code or GraphQL schema. If this path is not specified, the output will be written to the same directory from which the command is @@ -35,6 +41,14 @@ OPTIONS GraphQL schema. This option is used with the GraphQL schema generation command. If this base path is not specified, schemas will be generated for each of the GraphQL services in the input file. + -m, --mode + This mode is used to identify the operation mode. It can be `client`, `schema`, or `service`. Argument `client` + indicates the Ballerina client source code generation, argument `schema` indicates the GraphQL schema + generation, and argument `service` indicates the Ballerina GraphQL service source code generation. Mode is only + mandatory for Ballerina GraphQL service source code generation. + -r, --use-records-for-objects + This flag is used with no argument. It is used only in the Ballerina GraphQL service generation. It will make + the Ballerina CLI tool uses record types for GraphQL object types whenever possible. EXAMPLES Generate Ballerina Graphql clients using a GraphQL config file (`graphql.config.yaml`). @@ -46,3 +60,9 @@ EXAMPLES Generate a GraphQL schema for a selected GraphQL service from the given input file. $ bal graphql -i graphql_service.bal -o ./output_path -s /service_base_path + + Generate a Ballerina GraphQL service using a GraphQL schema file (`schema.graphql`) + $ bal graphql -i schema.graphql -m service -o ./output_path + + Generate Ballerina GraphQL service using a GraphQL schema file (`schema.graphql`) inclduing record types whenever possible + $ bal graphql -i schema.graphql -m service -o ./output_path -r diff --git a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help index 9ad342c85c68..b614a23d48fd 100755 --- a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help +++ b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help @@ -41,8 +41,9 @@ COMMANDS format Format Ballerina source files grpc Generate the Ballerina sources for a given Protocol Buffer definition - graphql Generate the Ballerina client sources for a GraphQL config file - and generate the GraphQL schema for a Ballerina GraphQL service. + graphql Generate the Ballerina client sources for a GraphQL config file, + generate the GraphQL schema for a Ballerina GraphQL service and + generate the Ballerina GraphQL service for a GraphQL schema. openapi Generate the Ballerina sources for a given OpenAPI definition and vice versa asyncapi Generate the Ballerina sources for a given AsyncAPI definition From 645e1f1c50053d5db5b25032dd96c1a8670c7c9e Mon Sep 17 00:00:00 2001 From: ChathurangaJayanath Date: Fri, 12 May 2023 00:25:53 +0530 Subject: [PATCH 06/70] add suggested changes --- .../resources/cli-help/ballerina-graphql.help | 16 ++++++++-------- .../main/resources/cli-help/ballerina-help.help | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help index eb4b29720611..430dbe82e032 100644 --- a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help +++ b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help @@ -1,6 +1,6 @@ NAME ballerina-graphql - Generate the Ballerina client sources for a GraphQL config file, - generate the GraphQL schema for a Ballerina GraphQL service + generate the GraphQL schema for a Ballerina GraphQL service, and generate the Ballerina service sources for a GraphQL schema. @@ -42,13 +42,13 @@ OPTIONS If this base path is not specified, schemas will be generated for each of the GraphQL services in the input file. -m, --mode - This mode is used to identify the operation mode. It can be `client`, `schema`, or `service`. Argument `client` - indicates the Ballerina client source code generation, argument `schema` indicates the GraphQL schema - generation, and argument `service` indicates the Ballerina GraphQL service source code generation. Mode is only + This mode is used to identify the operation mode. It can be `client`, `schema`, or `service`. The `client` argument + indicates the Ballerina client source code generation, the `schema` argument indicates the GraphQL schema + generation, and the `service` argument indicates the Ballerina GraphQL service source code generation. Mode is only mandatory for Ballerina GraphQL service source code generation. -r, --use-records-for-objects - This flag is used with no argument. It is used only in the Ballerina GraphQL service generation. It will make - the Ballerina CLI tool uses record types for GraphQL object types whenever possible. + This flag is used without an argument. It is used only in the Ballerina GraphQL service generation. It will make + the Ballerina CLI tool to use record types for GraphQL object types whenever possible. EXAMPLES Generate Ballerina Graphql clients using a GraphQL config file (`graphql.config.yaml`). @@ -61,8 +61,8 @@ EXAMPLES Generate a GraphQL schema for a selected GraphQL service from the given input file. $ bal graphql -i graphql_service.bal -o ./output_path -s /service_base_path - Generate a Ballerina GraphQL service using a GraphQL schema file (`schema.graphql`) + Generate a Ballerina GraphQL service using a GraphQL schema file (`schema.graphql`). $ bal graphql -i schema.graphql -m service -o ./output_path - Generate Ballerina GraphQL service using a GraphQL schema file (`schema.graphql`) inclduing record types whenever possible + Generate a Ballerina GraphQL service using a GraphQL schema file (`schema.graphql`) including record types whenever possible. $ bal graphql -i schema.graphql -m service -o ./output_path -r diff --git a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help index b614a23d48fd..ae2036e45752 100755 --- a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help +++ b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help @@ -42,8 +42,8 @@ COMMANDS grpc Generate the Ballerina sources for a given Protocol Buffer definition graphql Generate the Ballerina client sources for a GraphQL config file, - generate the GraphQL schema for a Ballerina GraphQL service and - generate the Ballerina GraphQL service for a GraphQL schema. + generate the GraphQL schema for a Ballerina GraphQL service, and + generate the Ballerina GraphQL service for a GraphQL schema openapi Generate the Ballerina sources for a given OpenAPI definition and vice versa asyncapi Generate the Ballerina sources for a given AsyncAPI definition From 6fa993b640da57d211e4fd144c2f8f11310f86e3 Mon Sep 17 00:00:00 2001 From: ushirask Date: Mon, 19 Jun 2023 09:49:57 +0530 Subject: [PATCH 07/70] Address review suggestions --- .../util/diagnostic/DiagnosticErrorCode.java | 3 +- .../analyzer/ConstantValueResolver.java | 60 +++++++++---------- .../semantics/analyzer/SemanticAnalyzer.java | 2 +- .../src/main/resources/compiler.properties | 3 - .../AnnotationAttachmentNegativeTest.java | 8 +-- 5 files changed, 34 insertions(+), 42 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java index a7ce888ecce7..2a33f8e0843f 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java @@ -806,8 +806,7 @@ public enum DiagnosticErrorCode implements DiagnosticCode { SEQ_ARG_FOLLOWED_BY_ANOTHER_SEQ_ARG("BCS4048", "seq.arg.followed.by.another.seq.arg"), QUERY_CONSTRUCT_TYPES_CANNOT_BE_USED_WITH_COLLECT("BCS4049", "query.construct.types.cannot.be.used.with.collect"), VARIABLE_IS_SEQUENCED_MORE_THAN_ONCE("BCS4050", "variable.is.sequenced.more.than.once"), - INVALID_GROUPING_KEY_TYPE("BCS4051", "invalid.grouping.key.type"), - ANNOTATION_ELEMENT_SHOULD_BE_LITERAL("BCS4052", "annotation.element.should.be.a.literal") + INVALID_GROUPING_KEY_TYPE("BCS4051", "invalid.grouping.key.type") ; private String diagnosticId; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java index afd433f92a23..ad86de711fbe 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java @@ -95,7 +95,6 @@ public class ConstantValueResolver extends BLangNodeVisitor { private static final CompilerContext.Key CONSTANT_VALUE_RESOLVER_KEY = new CompilerContext.Key<>(); private BConstantSymbol currentConstSymbol; - private BType currentConstSymbolType; private BLangConstantValue result; private BLangDiagnosticLog dlog; private Location currentPos; @@ -145,9 +144,7 @@ public void visit(BLangConstant constant) { return; // Already visited. } BConstantSymbol tempCurrentConstSymbol = this.currentConstSymbol; - BType tempCurrentConstSymbolType = this.currentConstSymbolType; this.currentConstSymbol = constant.symbol; - this.currentConstSymbolType = constant.symbol.type; this.resolvingConstants.add(this.currentConstSymbol); this.currentConstSymbol.value = constructBLangConstantValue(constant.expr); this.resolvingConstants.remove(this.currentConstSymbol); @@ -157,7 +154,6 @@ public void visit(BLangConstant constant) { checkUniqueness(constant); unresolvedConstants.remove(this.currentConstSymbol); this.currentConstSymbol = tempCurrentConstSymbol; - this.currentConstSymbolType = tempCurrentConstSymbolType; } @Override @@ -225,6 +221,7 @@ public void visit(BLangRecordLiteral recordLiteral) { for (RecordLiteralNode.RecordField field : recordLiteral.fields) { String key; BLangConstantValue value; + if (field.isKeyValueField()) { BLangRecordLiteral.BLangRecordKeyValueField keyValuePair = (BLangRecordLiteral.BLangRecordKeyValueField) field; @@ -237,10 +234,8 @@ public void visit(BLangRecordLiteral recordLiteral) { } else { continue; } - BType tempCurrentConstSymbolType = this.currentConstSymbolType; - this.currentConstSymbolType = keyValuePair.getBType(); + value = constructBLangConstantValue(keyValuePair.valueExpr); - this.currentConstSymbolType = tempCurrentConstSymbolType; } else if (field.getKind() == NodeKind.SIMPLE_VARIABLE_REF) { BLangRecordLiteral.BLangRecordVarNameField varNameField = (BLangRecordLiteral.BLangRecordVarNameField) field; @@ -305,7 +300,7 @@ private BLangConstantValue calculateConstValue(BLangConstantValue lhs, BLangCons if (lhs == null || rhs == null || lhs.value == null || rhs.value == null) { // This is a compilation error. // This is to avoid NPE exceptions in sub-sequent validations. - return new BLangConstantValue(null, this.currentConstSymbolType); + return new BLangConstantValue(null, this.currentConstSymbol.type); } // See Types.isAllowedConstantType() for supported types. try { @@ -342,20 +337,20 @@ private BLangConstantValue calculateConstValue(BLangConstantValue lhs, BLangCons } // This is a compilation error already logged. // This is to avoid NPE exceptions in sub-sequent validations. - return new BLangConstantValue(null, this.currentConstSymbolType); + return new BLangConstantValue(null, this.currentConstSymbol.type); } private BLangConstantValue evaluateUnaryOperator(BLangConstantValue value, OperatorKind kind) { if (value == null || value.value == null) { // This is a compilation error. // This is to avoid NPE exceptions in sub-sequent validations. - return new BLangConstantValue(null, this.currentConstSymbolType); + return new BLangConstantValue(null, this.currentConstSymbol.type); } try { switch (kind) { case ADD: - return new BLangConstantValue(value.value, currentConstSymbolType); + return new BLangConstantValue(value.value, currentConstSymbol.type); case SUB: return calculateNegation(value); case BITWISE_COMPLEMENT: @@ -368,25 +363,25 @@ private BLangConstantValue evaluateUnaryOperator(BLangConstantValue value, Opera } // This is a compilation error already logged. // This is to avoid NPE exceptions in sub-sequent validations. - return new BLangConstantValue(null, this.currentConstSymbolType); + return new BLangConstantValue(null, this.currentConstSymbol.type); } private BLangConstantValue calculateBitWiseOp(BLangConstantValue lhs, BLangConstantValue rhs, BiFunction func) { - switch (Types.getReferredType(this.currentConstSymbolType).tag) { + switch (Types.getReferredType(this.currentConstSymbol.type).tag) { case TypeTags.INT: Long val = func.apply((Long) lhs.value, (Long) rhs.value); - return new BLangConstantValue(val, this.currentConstSymbolType); + return new BLangConstantValue(val, this.currentConstSymbol.type); default: dlog.error(currentPos, DiagnosticErrorCode.CONSTANT_EXPRESSION_NOT_SUPPORTED); } - return new BLangConstantValue(null, this.currentConstSymbolType); + return new BLangConstantValue(null, this.currentConstSymbol.type); } private BLangConstantValue calculateAddition(BLangConstantValue lhs, BLangConstantValue rhs) { Object result = null; - switch (Types.getReferredType(this.currentConstSymbolType).tag) { + switch (Types.getReferredType(this.currentConstSymbol.type).tag) { case TypeTags.INT: case TypeTags.BYTE: // Byte will be a compiler error. try { @@ -410,12 +405,12 @@ private BLangConstantValue calculateAddition(BLangConstantValue lhs, BLangConsta result = String.valueOf(lhs.value) + String.valueOf(rhs.value); break; } - return new BLangConstantValue(result, currentConstSymbolType); + return new BLangConstantValue(result, currentConstSymbol.type); } private BLangConstantValue calculateSubtract(BLangConstantValue lhs, BLangConstantValue rhs) { Object result = null; - switch (Types.getReferredType(this.currentConstSymbolType).tag) { + switch (Types.getReferredType(this.currentConstSymbol.type).tag) { case TypeTags.INT: case TypeTags.BYTE: // Byte will be a compiler error. try { @@ -436,12 +431,12 @@ private BLangConstantValue calculateSubtract(BLangConstantValue lhs, BLangConsta result = resultDecimal != null ? resultDecimal.toPlainString() : null; break; } - return new BLangConstantValue(result, currentConstSymbolType); + return new BLangConstantValue(result, currentConstSymbol.type); } private BLangConstantValue calculateMultiplication(BLangConstantValue lhs, BLangConstantValue rhs) { Object result = null; - switch (Types.getReferredType(this.currentConstSymbolType).tag) { + switch (Types.getReferredType(this.currentConstSymbol.type).tag) { case TypeTags.INT: case TypeTags.BYTE: // Byte will be a compiler error. try { @@ -462,17 +457,17 @@ private BLangConstantValue calculateMultiplication(BLangConstantValue lhs, BLang result = resultDecimal != null ? resultDecimal.toPlainString() : null; break; } - return new BLangConstantValue(result, currentConstSymbolType); + return new BLangConstantValue(result, currentConstSymbol.type); } private BLangConstantValue calculateDivision(BLangConstantValue lhs, BLangConstantValue rhs) { Object result = null; - switch (Types.getReferredType(this.currentConstSymbolType).tag) { + switch (Types.getReferredType(this.currentConstSymbol.type).tag) { case TypeTags.INT: case TypeTags.BYTE: // Byte will be a compiler error. if ((Long) lhs.value == Long.MIN_VALUE && (Long) rhs.value == -1) { dlog.error(currentPos, DiagnosticErrorCode.INT_RANGE_OVERFLOW_ERROR); - return new BLangConstantValue(null, this.currentConstSymbolType); + return new BLangConstantValue(null, this.currentConstSymbol.type); } result = (Long) ((Long) lhs.value / (Long) rhs.value); break; @@ -488,12 +483,12 @@ private BLangConstantValue calculateDivision(BLangConstantValue lhs, BLangConsta result = resultDecimal != null ? resultDecimal.toPlainString() : null; break; } - return new BLangConstantValue(result, currentConstSymbolType); + return new BLangConstantValue(result, currentConstSymbol.type); } private BLangConstantValue calculateMod(BLangConstantValue lhs, BLangConstantValue rhs) { Object result = null; - switch (Types.getReferredType(this.currentConstSymbolType).tag) { + switch (Types.getReferredType(this.currentConstSymbol.type).tag) { case TypeTags.INT: case TypeTags.BYTE: // Byte will be a compiler error. result = (Long) ((Long) lhs.value % (Long) rhs.value); @@ -509,13 +504,13 @@ private BLangConstantValue calculateMod(BLangConstantValue lhs, BLangConstantVal result = resultDecimal.toPlainString(); break; } - return new BLangConstantValue(result, currentConstSymbolType); + return new BLangConstantValue(result, currentConstSymbol.type); } private Object calculateNegationForInt(BLangConstantValue value) { if ((Long) (value.value) == Long.MIN_VALUE) { dlog.error(currentPos, DiagnosticErrorCode.INT_RANGE_OVERFLOW_ERROR); - return new BLangConstantValue(null, this.currentConstSymbolType); + return new BLangConstantValue(null, this.currentConstSymbol.type); } return -1 * ((Long) (value.value)); } @@ -553,18 +548,18 @@ private BLangConstantValue calculateNegation(BLangConstantValue value) { private BLangConstantValue calculateBitWiseComplement(BLangConstantValue value) { Object result = null; - if (Types.getReferredType(this.currentConstSymbolType).tag == TypeTags.INT) { + if (Types.getReferredType(this.currentConstSymbol.type).tag == TypeTags.INT) { result = ~((Long) (value.value)); } - return new BLangConstantValue(result, currentConstSymbolType); + return new BLangConstantValue(result, currentConstSymbol.type); } private BLangConstantValue calculateBooleanComplement(BLangConstantValue value) { Object result = null; - if (Types.getReferredType(this.currentConstSymbolType).tag == TypeTags.BOOLEAN) { + if (Types.getReferredType(this.currentConstSymbol.type).tag == TypeTags.BOOLEAN) { result = !((Boolean) (value.value)); } - return new BLangConstantValue(result, currentConstSymbolType); + return new BLangConstantValue(result, currentConstSymbol.type); } BLangConstantValue constructBLangConstantValueWithExactType(BLangExpression expression, @@ -575,6 +570,7 @@ BLangConstantValue constructBLangConstantValueWithExactType(BLangExpression expr BLangConstantValue constructBLangConstantValueWithExactType(BLangExpression expression, BConstantSymbol constantSymbol, SymbolEnv env, Stack anonTypeNameSuffixes) { + this.currentConstSymbol = constantSymbol; BLangConstantValue value = constructBLangConstantValue(expression); constantSymbol.value = value; @@ -819,7 +815,7 @@ private void addAssociatedTypeDefinition(BLangConstant constant, BIntersectionTy private BType createRecordType(BLangExpression expr, BConstantSymbol constantSymbol, Object value, Location pos, SymbolEnv env) { - if (expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) { + if (expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF || expr.getKind() == NodeKind.UNARY_EXPR) { return expr.getBType(); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index 126331c4981d..c7c1f365dde8 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -4416,7 +4416,7 @@ private void checkAnnotConstantExpression(BLangExpression expression) { ((BLangListConstructorExpr) expression).exprs.forEach(this::checkAnnotConstantExpression); break; default: - dlog.error(expression.pos, DiagnosticErrorCode.ANNOTATION_ELEMENT_SHOULD_BE_LITERAL); + dlog.error(expression.pos, DiagnosticErrorCode.EXPRESSION_IS_NOT_A_CONSTANT_EXPRESSION); break; } } diff --git a/compiler/ballerina-lang/src/main/resources/compiler.properties b/compiler/ballerina-lang/src/main/resources/compiler.properties index ef427f40f748..aed2011c5d33 100644 --- a/compiler/ballerina-lang/src/main/resources/compiler.properties +++ b/compiler/ballerina-lang/src/main/resources/compiler.properties @@ -1968,9 +1968,6 @@ error.empty.regexp.string.disallowed=\ error.unsupported.empty.character.class=\ empty character class disallowed -error.annotation.element.should.be.a.literal=\ - annotation element should be a literal - error.cyclic.type.reference.not.yet.supported=\ cyclic type reference not yet supported for ''{0}'' diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java index 707d7cadd1c5..d9c3efca0a8f 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationAttachmentNegativeTest.java @@ -512,10 +512,10 @@ public void testInvalidAttachmentsOnEnum() { public void testInvalidConstAnnotElements() { int index = 272; int line = 943; - validateError(compileResult, index++, "annotation element should be a literal", line, 16); + validateError(compileResult, index++, "expression is not a constant expression", line, 16); validateError(compileResult, index++, "expression is not a constant expression", line += 7, 17); - validateError(compileResult, index++, "annotation element should be a literal", line += 7, 16); - validateError(compileResult, index++, "annotation element should be a literal", line += 9, 16); - validateError(compileResult, index, "annotation element should be a literal", line + 7, 16); + validateError(compileResult, index++, "expression is not a constant expression", line += 7, 16); + validateError(compileResult, index++, "expression is not a constant expression", line += 9, 16); + validateError(compileResult, index, "expression is not a constant expression", line + 7, 16); } } From 26c73033821c5a5bf5f821cf0837f164ce7cf511 Mon Sep 17 00:00:00 2001 From: Ushira Karunasena Date: Tue, 20 Jun 2023 15:36:58 +0530 Subject: [PATCH 08/70] Address review suggestions --- .../compiler/semantics/analyzer/ConstantValueResolver.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java index ad86de711fbe..931fb63aff46 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java @@ -340,7 +340,7 @@ private BLangConstantValue calculateConstValue(BLangConstantValue lhs, BLangCons return new BLangConstantValue(null, this.currentConstSymbol.type); } - private BLangConstantValue evaluateUnaryOperator(BLangConstantValue value, OperatorKind kind) { + private BLangConstantValue evaluateUnaryOperator(BLangConstantValue value, OperatorKind kind) { if (value == null || value.value == null) { // This is a compilation error. // This is to avoid NPE exceptions in sub-sequent validations. @@ -350,7 +350,7 @@ private BLangConstantValue evaluateUnaryOperator(BLangConstantValue value, Opera try { switch (kind) { case ADD: - return new BLangConstantValue(value.value, currentConstSymbol.type); + return new BLangConstantValue(value.value, value.type); case SUB: return calculateNegation(value); case BITWISE_COMPLEMENT: @@ -815,7 +815,7 @@ private void addAssociatedTypeDefinition(BLangConstant constant, BIntersectionTy private BType createRecordType(BLangExpression expr, BConstantSymbol constantSymbol, Object value, Location pos, SymbolEnv env) { - if (expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF || expr.getKind() == NodeKind.UNARY_EXPR) { + if (expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) { return expr.getBType(); } From 55db945dca186fdef596c86d43417d06e17322c0 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Mon, 29 May 2023 16:48:35 +0530 Subject: [PATCH 09/70] Replace template name in module imports for central templates --- .../io/ballerina/cli/cmd/CommandUtil.java | 2 +- .../io/ballerina/cli/cmd/NewCommandTest.java | 30 +++++ .../testorg/centralSample/1.0.0/any/bala.json | 4 + .../1.0.0/any/dependency-graph.json | 103 ++++++++++++++++++ .../centralSample/1.0.0/any/docs/Package.md | 1 + .../docs/modules/centralSample.mod1/Module.md | 6 + .../any/modules/centralSample.mod1/mod1.bal | 10 ++ .../1.0.0/any/modules/centralSample/main.bal | 5 + .../centralSample/1.0.0/any/package.json | 13 +++ .../io/ballerina/projects/util/FileUtils.java | 35 +++++- .../projects/util/ProjectConstants.java | 1 + 11 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/bala.json create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/dependency-graph.json create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/Package.md create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/modules/centralSample.mod1/Module.md create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample.mod1/mod1.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample/main.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/package.json diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java index 4b4925a48f54..9d51b5e56996 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java @@ -275,7 +275,7 @@ private static void addModules(Path balaPath, Path projectPath, String packageNa destDir = projectPath.resolve(ProjectConstants.MODULES_ROOT).resolve(moduleDirName); Files.createDirectories(destDir); } - Files.walkFileTree(moduleRoot, new FileUtils.Copy(moduleRoot, destDir)); + Files.walkFileTree(moduleRoot, new FileUtils.Copy(moduleRoot, destDir, templatePkgName, packageName)); // Copy Module.md Path moduleMdSource = moduleMdDirRoot.resolve(moduleDir).resolve(ProjectConstants.MODULE_MD_FILE_NAME); diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 8220e39c313b..5518ea7a22e7 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -617,6 +617,36 @@ public void testNewCommandWithTemplateCentralPullWithVersion() throws IOExceptio Assert.assertTrue(readOutput().contains("Created new package")); } + @Test(description = "Test pulling a central template and replacing the template name in module imports") + public void testNewCommandCentralTemplateReplaceImports() throws IOException { + String templateArg = "testorg/centralSample:1.0.0"; + String packageName = "central_sample"; + Path packageDir = tmpDir.resolve(packageName); + String[] args = {packageDir.toString(), "-t", templateArg}; + NewCommand newCommand = new NewCommand(printStream, false, homeCache); + new CommandLine(newCommand).parseArgs(args); + newCommand.execute(); + + Assert.assertTrue(Files.exists(packageDir)); + + Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.BALLERINA_TOML))); + String expectedTomlContent = "[package]\n" + + "org = \"testorg\"\n" + + "name = \"" + packageName + "\"\n" + + "version = \"1.0.0\"\n" + + "export = [\"central_sample\"]\n" + + "distribution = \"" + RepoUtils.getBallerinaShortVersion() + "\"\n\n" + + "[build-options]\n" + + "observabilityIncluded = true\n"; + Assert.assertEquals( + readFileAsString(packageDir.resolve(ProjectConstants.BALLERINA_TOML)), expectedTomlContent); + + String mainContent = readFileAsString(packageDir.resolve("main.bal")); + Assert.assertTrue(mainContent.contains("import central_sample.mod1;")); + Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.PACKAGE_MD_FILE_NAME))); + Assert.assertTrue(readOutput().contains("Created new package")); + } + @Test public void testMultiModuleTemplate() throws IOException { // Test if no arguments was passed in diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/bala.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/bala.json new file mode 100644 index 000000000000..fd0a01921270 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/bala.json @@ -0,0 +1,4 @@ +{ + "bala_version": "2.0.0", + "built_by": "WSO2" +} \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/dependency-graph.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/dependency-graph.json new file mode 100644 index 000000000000..3572d54adf1f --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/dependency-graph.json @@ -0,0 +1,103 @@ +{ + "packages": [ + { + "org": "ballerina", + "name": "lang.value", + "version": "0.0.0", + "transitive": false, + "dependencies": [ + { + "org": "ballerina", + "name": "jballerina.java", + "version": "0.0.0", + "transitive": false, + "dependencies": [], + "modules": [] + } + ], + "modules": [] + }, + { + "org": "testorg", + "name": "centralSample", + "version": "1.0.0", + "transitive": false, + "dependencies": [ + { + "org": "ballerina", + "name": "io", + "version": "1.4.1", + "transitive": false, + "dependencies": [], + "modules": [] + } + ], + "modules": [] + }, + { + "org": "ballerina", + "name": "jballerina.java", + "version": "0.0.0", + "transitive": false, + "dependencies": [], + "modules": [] + }, + { + "org": "ballerina", + "name": "io", + "version": "1.4.1", + "transitive": false, + "dependencies": [ + { + "org": "ballerina", + "name": "lang.value", + "version": "0.0.0", + "transitive": false, + "dependencies": [], + "modules": [] + }, + { + "org": "ballerina", + "name": "jballerina.java", + "version": "0.0.0", + "transitive": false, + "dependencies": [], + "modules": [] + } + ], + "modules": [] + } + ], + "modules": [ + { + "org": "testorg", + "package_name": "centralSample", + "version": "1.0.0", + "module_name": "centralSample", + "dependencies": [ + { + "org": "testorg", + "package_name": "centralSample", + "version": "1.0.0", + "module_name": "centralSample.mod1", + "dependencies": [] + } + ] + }, + { + "org": "testorg", + "package_name": "centralSample", + "version": "1.0.0", + "module_name": "centralSample.mod1", + "dependencies": [ + { + "org": "ballerina", + "package_name": "io", + "version": "1.4.1", + "module_name": "io", + "dependencies": [] + } + ] + } + ] +} \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/Package.md b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/Package.md new file mode 100644 index 000000000000..b0be5c9c96e9 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/Package.md @@ -0,0 +1 @@ +central template sample diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/modules/centralSample.mod1/Module.md b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/modules/centralSample.mod1/Module.md new file mode 100644 index 000000000000..8a69f51930aa --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/modules/centralSample.mod1/Module.md @@ -0,0 +1,6 @@ +Prints "Hello, World!" with a main function. +[//]: # (above is the module summary) + +# Module Overview +Provides an overview about the module when generating the API documentations. +For example, refer to https://lib.ballerina.io/ballerina/io/latest diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample.mod1/mod1.bal b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample.mod1/mod1.bal new file mode 100644 index 000000000000..286b25df4812 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample.mod1/mod1.bal @@ -0,0 +1,10 @@ +import ballerina/io; +# Returns the string `Hello` with the input string name. +# +# + name - name as a string +public function hello(string name) { + if !(name is "") { + io:println("Hello, " + name); + } + io:println("Hello, World!"); +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample/main.bal b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample/main.bal new file mode 100644 index 000000000000..142eb8fee68d --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample/main.bal @@ -0,0 +1,5 @@ +import centralSample.mod1; + +public function main() { + mod1:hello("Ballerina"); +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/package.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/package.json new file mode 100644 index 000000000000..44273abe37f2 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/package.json @@ -0,0 +1,13 @@ +{ + "organization": "testorg", + "name": "centralSample", + "version": "1.0.0", + "export": [ + "centralSample" + ], + "ballerina_version": "2201.6.0-SNAPSHOT", + "implementation_vendor": "WSO2", + "language_spec_version": "2022R4", + "platform": "any", + "template": true +} \ No newline at end of file diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java index 61b608d2da5b..b53e6c4b8862 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java @@ -47,6 +47,8 @@ import static io.ballerina.projects.util.ProjectConstants.BALLERINA_TOML; import static io.ballerina.projects.util.ProjectConstants.BLANG_SOURCE_EXT; import static io.ballerina.projects.util.ProjectConstants.COMPILER_PLUGIN_TOML; +import static io.ballerina.projects.util.ProjectConstants.DOT; +import static io.ballerina.projects.util.ProjectConstants.IMPORT; import static io.ballerina.projects.util.ProjectConstants.MODULES_ROOT; import static io.ballerina.projects.util.ProjectConstants.RESOURCE_DIR_NAME; import static io.ballerina.projects.util.ProjectConstants.TEST_DIR_NAME; @@ -291,6 +293,23 @@ public static void deleteDeprecatedMetaFile(Path metaFilePath) { } } + public static void replaceTemplateName(Path path, String templateName, String packageName) { + Optional fileName = Optional.ofNullable(path.getFileName()); + if (fileName.isPresent() && fileName.get().toString().endsWith(BLANG_SOURCE_EXT)) { + try { + String content = Files.readString(path); + String oldImportStatementStart = IMPORT + templateName + DOT; + String newImportStatementStart = IMPORT + packageName + DOT; + if (content.contains(oldImportStatementStart)) { + content = content.replaceAll(oldImportStatementStart, newImportStatementStart); + Files.write(path, content.getBytes(StandardCharsets.UTF_8)); + } + } catch (IOException e) { + throw new RuntimeException("Error while replacing template name in module import statements: " + path, e); + } + } + } + /** * Get the list of files and directories in a directory. * @@ -313,17 +332,26 @@ public static List getFilesInDirectory(Path directoryPath) { public static class Copy extends SimpleFileVisitor { private Path fromPath; private Path toPath; + private String templateName; + private String packageName; private StandardCopyOption copyOption; - public Copy(Path fromPath, Path toPath, StandardCopyOption copyOption) { + public Copy(Path fromPath, Path toPath, String templateName, String packageName, + StandardCopyOption copyOption) { this.fromPath = fromPath; this.toPath = toPath; + this.templateName = templateName; + this.packageName = packageName; this.copyOption = copyOption; } public Copy(Path fromPath, Path toPath) { - this(fromPath, toPath, StandardCopyOption.REPLACE_EXISTING); + this(fromPath, toPath, "", "", StandardCopyOption.REPLACE_EXISTING); + } + + public Copy(Path fromPath, Path toPath, String templateName, String packageName) { + this(fromPath, toPath, templateName, packageName, StandardCopyOption.REPLACE_EXISTING); } @Override @@ -342,6 +370,9 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.copy(file, toPath.resolve(fromPath.relativize(file).toString()), copyOption); + if (!packageName.equals("") && !templateName.equals("") && !packageName.equals(templateName)) { + replaceTemplateName(toPath.resolve(fromPath.relativize(file).toString()), templateName, packageName); + } return FileVisitResult.CONTINUE; } } diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectConstants.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectConstants.java index 440b41bda76c..87606533701a 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectConstants.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectConstants.java @@ -64,6 +64,7 @@ public class ProjectConstants { public static final String USER_DIR = "user.dir"; public static final String USER_NAME = "user.name"; + public static final String IMPORT = "import "; // Bala specific constants public static final String MODULES_ROOT = "modules"; From 78a62540399a594925f2daa2df89c91fe123f240 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Mon, 29 May 2023 17:03:36 +0530 Subject: [PATCH 10/70] Fix new lines and constants --- .../testorg/centralSample/1.0.0/any/bala.json | 2 +- .../centralSample/1.0.0/any/dependency-graph.json | 2 +- .../testorg/centralSample/1.0.0/any/package.json | 2 +- .../java/io/ballerina/projects/util/FileUtils.java | 11 ++++++----- .../io/ballerina/projects/util/ProjectConstants.java | 3 ++- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/bala.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/bala.json index fd0a01921270..812300d452d7 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/bala.json +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/bala.json @@ -1,4 +1,4 @@ { "bala_version": "2.0.0", "built_by": "WSO2" -} \ No newline at end of file +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/dependency-graph.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/dependency-graph.json index 3572d54adf1f..95053666f7d8 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/dependency-graph.json +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/dependency-graph.json @@ -100,4 +100,4 @@ ] } ] -} \ No newline at end of file +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/package.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/package.json index 44273abe37f2..4283bec1ff27 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/package.json +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/package.json @@ -10,4 +10,4 @@ "language_spec_version": "2022R4", "platform": "any", "template": true -} \ No newline at end of file +} diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java index b53e6c4b8862..ea36c756e85e 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java @@ -48,7 +48,8 @@ import static io.ballerina.projects.util.ProjectConstants.BLANG_SOURCE_EXT; import static io.ballerina.projects.util.ProjectConstants.COMPILER_PLUGIN_TOML; import static io.ballerina.projects.util.ProjectConstants.DOT; -import static io.ballerina.projects.util.ProjectConstants.IMPORT; +import static io.ballerina.projects.util.ProjectConstants.EMPTY_STRING; +import static io.ballerina.projects.util.ProjectConstants.IMPORT_PREFIX; import static io.ballerina.projects.util.ProjectConstants.MODULES_ROOT; import static io.ballerina.projects.util.ProjectConstants.RESOURCE_DIR_NAME; import static io.ballerina.projects.util.ProjectConstants.TEST_DIR_NAME; @@ -298,8 +299,8 @@ public static void replaceTemplateName(Path path, String templateName, String pa if (fileName.isPresent() && fileName.get().toString().endsWith(BLANG_SOURCE_EXT)) { try { String content = Files.readString(path); - String oldImportStatementStart = IMPORT + templateName + DOT; - String newImportStatementStart = IMPORT + packageName + DOT; + String oldImportStatementStart = IMPORT_PREFIX + templateName + DOT; + String newImportStatementStart = IMPORT_PREFIX + packageName + DOT; if (content.contains(oldImportStatementStart)) { content = content.replaceAll(oldImportStatementStart, newImportStatementStart); Files.write(path, content.getBytes(StandardCharsets.UTF_8)); @@ -347,7 +348,7 @@ public Copy(Path fromPath, Path toPath, String templateName, String packageName, } public Copy(Path fromPath, Path toPath) { - this(fromPath, toPath, "", "", StandardCopyOption.REPLACE_EXISTING); + this(fromPath, toPath, EMPTY_STRING, EMPTY_STRING, StandardCopyOption.REPLACE_EXISTING); } public Copy(Path fromPath, Path toPath, String templateName, String packageName) { @@ -370,7 +371,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.copy(file, toPath.resolve(fromPath.relativize(file).toString()), copyOption); - if (!packageName.equals("") && !templateName.equals("") && !packageName.equals(templateName)) { + if (!packageName.equals(EMPTY_STRING) && !templateName.equals(EMPTY_STRING) && !packageName.equals(templateName)) { replaceTemplateName(toPath.resolve(fromPath.relativize(file).toString()), templateName, packageName); } return FileVisitResult.CONTINUE; diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectConstants.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectConstants.java index 87606533701a..ce3c1ebdd50e 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectConstants.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectConstants.java @@ -64,7 +64,8 @@ public class ProjectConstants { public static final String USER_DIR = "user.dir"; public static final String USER_NAME = "user.name"; - public static final String IMPORT = "import "; + public static final String IMPORT_PREFIX = "import "; + public static final String EMPTY_STRING = ""; // Bala specific constants public static final String MODULES_ROOT = "modules"; From 3aeab3316496425fad1d68ffb8a2fe11e1ecd7b8 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Mon, 29 May 2023 18:31:11 +0530 Subject: [PATCH 11/70] Fix checkStyleMain bugs --- .../src/main/java/io/ballerina/projects/util/FileUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java index ea36c756e85e..4ffd771e5a2b 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/FileUtils.java @@ -371,7 +371,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.copy(file, toPath.resolve(fromPath.relativize(file).toString()), copyOption); - if (!packageName.equals(EMPTY_STRING) && !templateName.equals(EMPTY_STRING) && !packageName.equals(templateName)) { + if (!packageName.equals(EMPTY_STRING) && !templateName.equals(EMPTY_STRING) && + !packageName.equals(templateName)) { replaceTemplateName(toPath.resolve(fromPath.relativize(file).toString()), templateName, packageName); } return FileVisitResult.CONTINUE; From 24389355f0c221b4cf5ed02f6ad36366d2f9985a Mon Sep 17 00:00:00 2001 From: ShammiL Date: Wed, 21 Jun 2023 16:28:33 +0530 Subject: [PATCH 12/70] Improve replace module imports tests --- .../io/ballerina/cli/cmd/NewCommandTest.java | 10 +++- .../{1.0.0 => 1.0.2}/any/bala.json | 2 +- .../any/dependency-graph.json | 60 +++++++++---------- .../{1.0.0 => 1.0.2}/any/docs/Package.md | 0 .../docs/modules/centralSample.mod1/Module.md | 0 .../any/modules/centralSample.mod1/mod1.bal | 0 .../any/modules/centralSample/main.bal | 0 .../{1.0.0 => 1.0.2}/any/package.json | 8 +-- 8 files changed, 43 insertions(+), 37 deletions(-) rename cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/{1.0.0 => 1.0.2}/any/bala.json (96%) rename cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/{1.0.0 => 1.0.2}/any/dependency-graph.json (95%) rename cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/{1.0.0 => 1.0.2}/any/docs/Package.md (100%) rename cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/{1.0.0 => 1.0.2}/any/docs/modules/centralSample.mod1/Module.md (100%) rename cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/{1.0.0 => 1.0.2}/any/modules/centralSample.mod1/mod1.bal (100%) rename cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/{1.0.0 => 1.0.2}/any/modules/centralSample/main.bal (100%) rename cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/{1.0.0 => 1.0.2}/any/package.json (62%) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 5518ea7a22e7..78d9bb00dc9c 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -619,7 +619,7 @@ public void testNewCommandWithTemplateCentralPullWithVersion() throws IOExceptio @Test(description = "Test pulling a central template and replacing the template name in module imports") public void testNewCommandCentralTemplateReplaceImports() throws IOException { - String templateArg = "testorg/centralSample:1.0.0"; + String templateArg = "testorg/centralSample:1.0.2"; String packageName = "central_sample"; Path packageDir = tmpDir.resolve(packageName); String[] args = {packageDir.toString(), "-t", templateArg}; @@ -633,7 +633,7 @@ public void testNewCommandCentralTemplateReplaceImports() throws IOException { String expectedTomlContent = "[package]\n" + "org = \"testorg\"\n" + "name = \"" + packageName + "\"\n" + - "version = \"1.0.0\"\n" + + "version = \"1.0.2\"\n" + "export = [\"central_sample\"]\n" + "distribution = \"" + RepoUtils.getBallerinaShortVersion() + "\"\n\n" + "[build-options]\n" + @@ -645,6 +645,12 @@ public void testNewCommandCentralTemplateReplaceImports() throws IOException { Assert.assertTrue(mainContent.contains("import central_sample.mod1;")); Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.PACKAGE_MD_FILE_NAME))); Assert.assertTrue(readOutput().contains("Created new package")); + + BuildCommand buildCommand = new BuildCommand(packageDir, printStream, printStream, false); + new CommandLine(buildCommand).parseArgs(); + buildCommand.execute(); + String buildLog = readOutput(true); + Assert.assertTrue(buildLog.contains("Generating executable"));; } @Test diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/bala.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/bala.json similarity index 96% rename from cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/bala.json rename to cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/bala.json index 812300d452d7..fd0a01921270 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/bala.json +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/bala.json @@ -1,4 +1,4 @@ { "bala_version": "2.0.0", "built_by": "WSO2" -} +} \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/dependency-graph.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/dependency-graph.json similarity index 95% rename from cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/dependency-graph.json rename to cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/dependency-graph.json index 95053666f7d8..d41c343ba72b 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/dependency-graph.json +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/dependency-graph.json @@ -2,9 +2,17 @@ "packages": [ { "org": "ballerina", - "name": "lang.value", + "name": "jballerina.java", "version": "0.0.0", "transitive": false, + "dependencies": [], + "modules": [] + }, + { + "org": "ballerina", + "name": "io", + "version": "1.4.1", + "transitive": false, "dependencies": [ { "org": "ballerina", @@ -13,20 +21,11 @@ "transitive": false, "dependencies": [], "modules": [] - } - ], - "modules": [] - }, - { - "org": "testorg", - "name": "centralSample", - "version": "1.0.0", - "transitive": false, - "dependencies": [ + }, { "org": "ballerina", - "name": "io", - "version": "1.4.1", + "name": "lang.value", + "version": "0.0.0", "transitive": false, "dependencies": [], "modules": [] @@ -36,30 +35,31 @@ }, { "org": "ballerina", - "name": "jballerina.java", + "name": "lang.value", "version": "0.0.0", "transitive": false, - "dependencies": [], - "modules": [] - }, - { - "org": "ballerina", - "name": "io", - "version": "1.4.1", - "transitive": false, "dependencies": [ { "org": "ballerina", - "name": "lang.value", + "name": "jballerina.java", "version": "0.0.0", "transitive": false, "dependencies": [], "modules": [] - }, + } + ], + "modules": [] + }, + { + "org": "testorg", + "name": "centralSample", + "version": "1.0.2", + "transitive": false, + "dependencies": [ { "org": "ballerina", - "name": "jballerina.java", - "version": "0.0.0", + "name": "io", + "version": "1.4.1", "transitive": false, "dependencies": [], "modules": [] @@ -72,13 +72,13 @@ { "org": "testorg", "package_name": "centralSample", - "version": "1.0.0", + "version": "1.0.2", "module_name": "centralSample", "dependencies": [ { "org": "testorg", "package_name": "centralSample", - "version": "1.0.0", + "version": "1.0.2", "module_name": "centralSample.mod1", "dependencies": [] } @@ -87,7 +87,7 @@ { "org": "testorg", "package_name": "centralSample", - "version": "1.0.0", + "version": "1.0.2", "module_name": "centralSample.mod1", "dependencies": [ { @@ -100,4 +100,4 @@ ] } ] -} +} \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/Package.md b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/docs/Package.md similarity index 100% rename from cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/Package.md rename to cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/docs/Package.md diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/modules/centralSample.mod1/Module.md b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/docs/modules/centralSample.mod1/Module.md similarity index 100% rename from cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/docs/modules/centralSample.mod1/Module.md rename to cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/docs/modules/centralSample.mod1/Module.md diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample.mod1/mod1.bal b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample.mod1/mod1.bal similarity index 100% rename from cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample.mod1/mod1.bal rename to cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample.mod1/mod1.bal diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample/main.bal b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample/main.bal similarity index 100% rename from cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/modules/centralSample/main.bal rename to cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample/main.bal diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/package.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/package.json similarity index 62% rename from cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/package.json rename to cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/package.json index 4283bec1ff27..29a3ca7a8b80 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.0/any/package.json +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/package.json @@ -1,13 +1,13 @@ { "organization": "testorg", "name": "centralSample", - "version": "1.0.0", + "version": "1.0.2", "export": [ "centralSample" ], - "ballerina_version": "2201.6.0-SNAPSHOT", + "ballerina_version": "2201.7.0-SNAPSHOT", "implementation_vendor": "WSO2", - "language_spec_version": "2022R4", + "language_spec_version": "2023R1", "platform": "any", "template": true -} +} \ No newline at end of file From 79e8b79b793a7bb68b5fb7e1cd42e2d87104207b Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 22 Jun 2023 11:53:59 +0530 Subject: [PATCH 13/70] Print diagnostic info to console. --- .../src/main/java/io/ballerina/cli/task/CompileTask.java | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java index 40c2439584ee..ba937a7071aa 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java @@ -208,6 +208,7 @@ public void execute(Project project) { boolean hasErrors = false; for (Diagnostic d : diagnostics) { if (d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) { + err.println(d); hasErrors = true; } if (d.diagnosticInfo().code() == null || !d.diagnosticInfo().code().equals( From 46e2071266d26969920081a84190e42f1e79ce98 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 22 Jun 2023 12:32:07 +0530 Subject: [PATCH 14/70] Catch BLauncher exception in tests --- .../src/main/java/io/ballerina/cli/task/CompileTask.java | 1 - .../src/test/java/io/ballerina/cli/cmd/NewCommandTest.java | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java index ba937a7071aa..40c2439584ee 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java @@ -208,7 +208,6 @@ public void execute(Project project) { boolean hasErrors = false; for (Diagnostic d : diagnostics) { if (d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) { - err.println(d); hasErrors = true; } if (d.diagnosticInfo().code() == null || !d.diagnosticInfo().code().equals( diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 78d9bb00dc9c..d1dea0de91a6 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -18,6 +18,7 @@ package io.ballerina.cli.cmd; +import io.ballerina.cli.launcher.BLauncherException; import io.ballerina.projects.util.FileUtils; import io.ballerina.projects.util.ProjectConstants; import io.ballerina.projects.util.ProjectUtils; @@ -648,7 +649,11 @@ public void testNewCommandCentralTemplateReplaceImports() throws IOException { BuildCommand buildCommand = new BuildCommand(packageDir, printStream, printStream, false); new CommandLine(buildCommand).parseArgs(); - buildCommand.execute(); + try { + buildCommand.execute(); + } catch (BLauncherException e) { + e.printStackTrace(); + } String buildLog = readOutput(true); Assert.assertTrue(buildLog.contains("Generating executable"));; } From 0d618e1369adb7b666efaff4b2b176558c5fc068 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 22 Jun 2023 14:08:31 +0530 Subject: [PATCH 15/70] Print BLauncher exception in tests --- .../src/test/java/io/ballerina/cli/cmd/NewCommandTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index d1dea0de91a6..37b0e3b0cb71 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -652,7 +652,7 @@ public void testNewCommandCentralTemplateReplaceImports() throws IOException { try { buildCommand.execute(); } catch (BLauncherException e) { - e.printStackTrace(); + printStream.println(e.getStackTrace()); } String buildLog = readOutput(true); Assert.assertTrue(buildLog.contains("Generating executable"));; From ad1fa0fa379830d10e98306bbacd5ff39966b69f Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 22 Jun 2023 12:32:07 +0530 Subject: [PATCH 16/70] Catch BLauncher exception in tests --- .../src/main/java/io/ballerina/cli/task/CompileTask.java | 1 - .../src/test/java/io/ballerina/cli/cmd/NewCommandTest.java | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java index ba937a7071aa..40c2439584ee 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java @@ -208,7 +208,6 @@ public void execute(Project project) { boolean hasErrors = false; for (Diagnostic d : diagnostics) { if (d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) { - err.println(d); hasErrors = true; } if (d.diagnosticInfo().code() == null || !d.diagnosticInfo().code().equals( diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 78d9bb00dc9c..37b0e3b0cb71 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -18,6 +18,7 @@ package io.ballerina.cli.cmd; +import io.ballerina.cli.launcher.BLauncherException; import io.ballerina.projects.util.FileUtils; import io.ballerina.projects.util.ProjectConstants; import io.ballerina.projects.util.ProjectUtils; @@ -648,7 +649,11 @@ public void testNewCommandCentralTemplateReplaceImports() throws IOException { BuildCommand buildCommand = new BuildCommand(packageDir, printStream, printStream, false); new CommandLine(buildCommand).parseArgs(); - buildCommand.execute(); + try { + buildCommand.execute(); + } catch (BLauncherException e) { + printStream.println(e.getStackTrace()); + } String buildLog = readOutput(true); Assert.assertTrue(buildLog.contains("Generating executable"));; } From 5a3fa798a3173324cc1529333867f13e2346bed9 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 22 Jun 2023 14:45:01 +0530 Subject: [PATCH 17/70] Print BLauncher exception in tests --- .../src/test/java/io/ballerina/cli/cmd/NewCommandTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 37b0e3b0cb71..86a79dc1a6fb 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -653,6 +653,8 @@ public void testNewCommandCentralTemplateReplaceImports() throws IOException { buildCommand.execute(); } catch (BLauncherException e) { printStream.println(e.getStackTrace()); + printStream.println(e.getMessage()); + printStream.println(e.getDetailedMessages()); } String buildLog = readOutput(true); Assert.assertTrue(buildLog.contains("Generating executable"));; From 4db7eb5b53dd0d8210c5e7d621bdafb1153ba3f0 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Tue, 27 Jun 2023 14:32:11 +0530 Subject: [PATCH 18/70] Correct error msgs for map constants negative case --- .../semantics/analyzer/ConstantAnalyzer.java | 2 ++ .../analyzer/ConstantTypeChecker.java | 26 ++++++++++++++----- .../semantics/analyzer/TypeChecker.java | 7 +---- .../compiler/semantics/analyzer/Types.java | 4 +++ .../test/types/constant/ConstantTypeTest.java | 2 +- .../constant/MapConstantNegativeTest.java | 17 +++++++----- .../test/types/constant/MapConstantTest.java | 5 ++++ .../constant/SimpleConstantNegativeTest.java | 2 ++ .../types/constant/map-literal-constant.bal | 18 +++++++++++++ .../types/constant/map_constant_negative.bal | 4 +++ .../simple-literal-constant-negative.bal | 3 +++ 11 files changed, 71 insertions(+), 19 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantAnalyzer.java index b938b5793943..c6f1882099b6 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantAnalyzer.java @@ -152,6 +152,8 @@ public void visit(BLangUnaryExpr unaryExpr) { case NOT: analyzeExpr(unaryExpr.expr); return; + default: + dlog.error(unaryExpr.pos, DiagnosticErrorCode.EXPRESSION_IS_NOT_A_CONSTANT_EXPRESSION); } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java index 54e1d523d06c..7d92b032ffbb 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java @@ -27,6 +27,7 @@ import org.ballerinalang.model.tree.NodeKind; import org.ballerinalang.model.tree.OperatorKind; import org.ballerinalang.model.tree.expressions.RecordLiteralNode; +import org.ballerinalang.model.types.TypeKind; import org.ballerinalang.util.diagnostic.DiagnosticErrorCode; import org.wso2.ballerinalang.compiler.diagnostic.BLangDiagnosticLog; import org.wso2.ballerinalang.compiler.parser.BLangAnonymousModelHelper; @@ -549,7 +550,24 @@ private void reportIncompatibleMappingConstructorError(BLangRecordLiteral mappin if (expType.tag != TypeTags.UNION) { dlog.error(mappingConstructorExpr.pos, DiagnosticErrorCode.MAPPING_CONSTRUCTOR_COMPATIBLE_TYPE_NOT_FOUND, expType); + return; + } + + BUnionType unionType = (BUnionType) expType; + BType[] memberTypes = types.getAllTypes(unionType, true).toArray(new BType[0]); + + // By this point, we know there aren't any types to which we can assign the mapping constructor. If this is + // case where there is at least one type with which we can use mapping constructors, but this particular + // mapping constructor is incompatible, we give an incompatible mapping constructor error. + for (BType bType : memberTypes) { + if (types.isMappingConstructorCompatibleType(bType)) { + dlog.error(mappingConstructorExpr.pos, DiagnosticErrorCode.INCOMPATIBLE_MAPPING_CONSTRUCTOR, unionType); + return; + } } + + dlog.error(mappingConstructorExpr.pos, + DiagnosticErrorCode.MAPPING_CONSTRUCTOR_COMPATIBLE_TYPE_NOT_FOUND, unionType); } private BType checkMappingConstructorCompatibilityForUnionType(BType expType, BLangRecordLiteral mappingConstructor, @@ -560,12 +578,8 @@ private BType checkMappingConstructorCompatibilityForUnionType(BType expType, BL this.dlog.mute(); List compatibleTypes = new ArrayList<>(); - boolean erroredExpType = false; for (BType memberType : ((BUnionType) expType).getMemberTypes()) { if (memberType == symTable.semanticError) { - if (!erroredExpType) { - erroredExpType = true; - } continue; } @@ -591,7 +605,7 @@ private BType checkMappingConstructorCompatibilityForUnionType(BType expType, BL } if (compatibleTypes.isEmpty()) { - dlog.error(mappingConstructor.pos, DiagnosticErrorCode.INCOMPATIBLE_TYPES, mappingConstructor, expType); + reportIncompatibleMappingConstructorError(mappingConstructor, expType); return symTable.semanticError; } else if (compatibleTypes.size() != 1) { dlog.error(mappingConstructor.pos, DiagnosticErrorCode.AMBIGUOUS_TYPES, expType); @@ -2055,7 +2069,7 @@ private boolean addFields(LinkedHashMap fields, BType keyValueTy BRecordTypeSymbol recordSymbol) { Name fieldName = Names.fromString(key); if (fields.containsKey(key)) { - dlog.error(pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_RECORD_LITERAL, keyValueType.getKind().typeName(), + dlog.error(pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_RECORD_LITERAL, TypeKind.RECORD.typeName(), key); return false; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java index f6ec8686c430..f89c7fa4b0b5 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/TypeChecker.java @@ -2606,11 +2606,6 @@ private BType getMappingConstructorCompatibleNonUnionType(BType type, AnalyzerDa return symTable.semanticError; } - private boolean isMappingConstructorCompatibleType(BType type) { - return Types.getReferredType(type).tag == TypeTags.RECORD - || Types.getReferredType(type).tag == TypeTags.MAP; - } - private void reportIncompatibleMappingConstructorError(BLangRecordLiteral mappingConstructorExpr, BType expType, AnalyzerData data) { if (expType == symTable.semanticError) { @@ -2648,7 +2643,7 @@ private void reportIncompatibleMappingConstructorError(BLangRecordLiteral mappin // case where there is at least one type with which we can use mapping constructors, but this particular // mapping constructor is incompatible, we give an incompatible mapping constructor error. for (BType bType : memberTypes) { - if (isMappingConstructorCompatibleType(bType)) { + if (types.isMappingConstructorCompatibleType(bType)) { dlog.error(mappingConstructorExpr.pos, DiagnosticErrorCode.INCOMPATIBLE_MAPPING_CONSTRUCTOR, unionType); return; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java index c24a3ec6a61b..e45067bead89 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java @@ -7270,4 +7270,8 @@ public boolean isContainSubtypeOfInt(BType type) { return false; } } + + public boolean isMappingConstructorCompatibleType(BType type) { + return Types.getReferredType(type).tag == TypeTags.RECORD || Types.getReferredType(type).tag == TypeTags.MAP; + } } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/ConstantTypeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/ConstantTypeTest.java index a2511f127dd0..b02c0dcd34c3 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/ConstantTypeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/ConstantTypeTest.java @@ -73,7 +73,7 @@ public void constExpressionSemanticAnalysisNegative() { BAssertUtil.validateError(compileResult1, i++, "incompatible types: expected 'TYPE4', found '3'", 44, 16); BAssertUtil.validateError(compileResult1, i++, "incompatible types: expected 'TYPE5', found 'false'", 45, 16); BAssertUtil.validateError(compileResult1, i++, "incompatible types: expected 'TYPE6', found '\"12\"'", 46, 16); - BAssertUtil.validateError(compileResult1, i++, "invalid usage of finite literal: duplicate key 'b'", 63, 46); + BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'b'", 63, 46); BAssertUtil.validateError(compileResult1, i++, "incompatible types: expected 'TYPE12', found " + "'(record {| record {| record {| 1 a; |} b; |} a; (record {| (record {| 1 a; |} & readonly) a; " + "(record {| 2 b; 3 c; |} & readonly) CMI2; record {| 1 d; |} c; |} & readonly) b; |} & readonly)'", diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantNegativeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantNegativeTest.java index 48f2a3920019..2cfac2f4a8e1 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantNegativeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantNegativeTest.java @@ -39,10 +39,15 @@ public void testNegative() { validateError(compileResult, i++, "incompatible types: expected 'string', found 'other'", 20, 27); validateError(compileResult, i++, "expression is not a constant expression", 20, 38); validateError(compileResult, i++, "cannot update constant value", 34, 5); - validateError(compileResult, i++, "invalid usage of finite literal: duplicate key 'a'", 39, 34); + validateError(compileResult, i++, "invalid usage of record literal: duplicate key 'a'", 39, 34); validateError(compileResult, i++, "illegal cyclic reference '[B1]'", 46, 1); validateError(compileResult, i++, "cannot declare a constant with type 'A1', expected a subtype of 'anydata'" + " that is not 'never'", 46, 7); + validateError(compileResult, i++, "ambiguous type '(map|map)'", 48, 41); + validateError(compileResult, i++, "incompatible mapping constructor expression for type " + + "'(map|map)'", 49, 42); + validateError(compileResult, i++, "a type compatible with mapping constructor expressions not found in type" + + " '(int|float|string)'", 50, 34); Assert.assertEquals(compileResult.getErrorCount(), i); } @@ -51,11 +56,11 @@ public void constMapSpreadFieldNegative() { CompileResult compileResult1 = BCompileUtil.compile( "test-src/types/constant/constant_map_spread_field_negative.bal"); int i = 0; - BAssertUtil.validateError(compileResult1, i++, "invalid usage of finite literal: duplicate key 'b'", 21, 46); - BAssertUtil.validateError(compileResult1, i++, "invalid usage of finite literal: duplicate key 'b'", 22, 36); - BAssertUtil.validateError(compileResult1, i++, "invalid usage of finite literal: duplicate key 'b'", 23, 33); - BAssertUtil.validateError(compileResult1, i++, "invalid usage of finite literal: duplicate key 'b'", 24, 36); - BAssertUtil.validateError(compileResult1, i++, "invalid usage of finite literal: duplicate key 'c'", 24, 36); + BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'b'", 21, 46); + BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'b'", 22, 36); + BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'b'", 23, 33); + BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'b'", 24, 36); + BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'c'", 24, 36); Assert.assertEquals(compileResult1.getErrorCount(), i); } } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantTest.java index 15d9965ffe97..f965edab0484 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantTest.java @@ -271,6 +271,11 @@ public void testConstMapWithComputedField() { BRunUtil.invoke(compileResult, "testConstMapWithComputedField"); } + @Test + public void testUnionAsExpectedType() { + BRunUtil.invoke(compileResult, "testUnionAsExpectedType"); + } + @AfterClass public void tearDown() { compileResult = null; diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/SimpleConstantNegativeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/SimpleConstantNegativeTest.java index 611a4d29fdcc..32e9611cae8c 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/SimpleConstantNegativeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/SimpleConstantNegativeTest.java @@ -165,6 +165,8 @@ public void testNegative() { " is out of range for 'decimal'", 354, 20); BAssertUtil.validateError(compileResult, index++, "'5E+6413' is out of range for 'decimal'", 355, 20); BAssertUtil.validateError(compileResult, index++, "'int' range overflow", 357, 19); + BAssertUtil.validateError(compileResult, index++, "expression is not a constant expression", 360, 12); + BAssertUtil.validateError(compileResult, index++, "operator 'typeof' not defined for '1'", 360, 12); Assert.assertEquals(compileResult.getErrorCount(), index); } } diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/map-literal-constant.bal b/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/map-literal-constant.bal index c8830eae730e..ba293331515c 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/map-literal-constant.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/map-literal-constant.bal @@ -267,6 +267,24 @@ function testConstMapWithComputedField() { assertEqual(X.toString(), "{\"a\":\"A\",\"b\":\"B\"}"); } +const map|map mUnion1 = {a: 1, b: 2.0f}; +const map|map|map mUnion2 = {a: 1, b: 2.0, c: 3d}; + +function testUnionAsExpectedType() { + assertTrue(mUnion1["a"] is float); + assertTrue(mUnion1["b"] is float); + assertEqual(mUnion1.toString(), "{\"a\":1.0,\"b\":2.0}"); + + assertTrue(mUnion2["a"] is decimal); + assertTrue(mUnion2["b"] is decimal); + assertTrue(mUnion2["c"] is decimal); + assertEqual(mUnion2.toString(), "{\"a\":1,\"b\":2.0,\"c\":3}"); +} + +function assertTrue(boolean actual) { + assertEqual(actual, true); +} + function assertEqual(int|float|decimal|boolean|string actual, int|float|decimal|boolean|string expected) { if (actual != expected) { panic error(string `Assertion error: expected ${expected} found ${actual}`); diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/map_constant_negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/map_constant_negative.bal index b3fa8466d614..969829889d51 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/map_constant_negative.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/map_constant_negative.bal @@ -44,3 +44,7 @@ type A1 record {| |}; const A1 B1 = {b : ()}; + +const map|map mUnion1 = {a: 1, b: 2.0}; // Ambiguous type. +const map|map mUnion2 = {a: 1, b: 2}; // Incompatible type. +const int|float|string mUnion3 = {a: 1, b: 2}; // Incompatible type. diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/simple-literal-constant-negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/simple-literal-constant-negative.bal index 604d53ad73b9..60440a136b30 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/simple-literal-constant-negative.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/constant/simple-literal-constant-negative.bal @@ -355,3 +355,6 @@ const decimal d4 = -1E6144d - 9.999999999999999999999999999999999E6144d; const decimal d5 = 1E614d / 2E-5800d; const int ANS13 = -int:MIN_VALUE; + +const int var1 = 1; +const T1 = typeof var1; From 5f1a349a91057e0d63e20ca3fb7b8b6d5bb0a24e Mon Sep 17 00:00:00 2001 From: ShammiL Date: Fri, 19 May 2023 15:04:14 +0530 Subject: [PATCH 19/70] Add support for tool template for bal new command --- .../io/ballerina/cli/cmd/CommandUtil.java | 94 ++++++++++++++++++- .../io/ballerina/cli/cmd/PackCommand.java | 3 +- .../create_cmd_templates/tool/tool/.keep | 0 .../new_cmd_defaults/manifest-tool.toml | 4 + .../io/ballerina/cli/cmd/NewCommandTest.java | 45 +++++++++ .../io/ballerina/projects/BalaWriter.java | 11 ++- .../projects/JBallerinaBalaWriter.java | 8 ++ .../ballerina/projects/util/ProjectUtils.java | 11 ++- 8 files changed, 168 insertions(+), 8 deletions(-) create mode 100644 cli/ballerina-cli/src/main/resources/create_cmd_templates/tool/tool/.keep create mode 100644 cli/ballerina-cli/src/main/resources/new_cmd_defaults/manifest-tool.toml diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java index 4b4925a48f54..1e5bbe34d376 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java @@ -30,6 +30,7 @@ import io.ballerina.projects.SemanticVersion; import io.ballerina.projects.Settings; import io.ballerina.projects.bala.BalaProject; +import io.ballerina.projects.internal.bala.BalToolJson; import io.ballerina.projects.internal.bala.DependencyGraphJson; import io.ballerina.projects.internal.bala.ModuleDependency; import io.ballerina.projects.internal.bala.PackageJson; @@ -74,9 +75,12 @@ import java.util.stream.Stream; import static io.ballerina.cli.launcher.LauncherUtils.createLauncherException; +import static io.ballerina.projects.util.ProjectConstants.BAL_TOOL_TOML; import static io.ballerina.projects.util.ProjectConstants.DEPENDENCIES_TOML; import static io.ballerina.projects.util.ProjectConstants.DEPENDENCY_GRAPH_JSON; +import static io.ballerina.projects.util.ProjectConstants.LIB_DIR; import static io.ballerina.projects.util.ProjectConstants.PACKAGE_JSON; +import static io.ballerina.projects.util.ProjectConstants.TOOL_DIR; import static io.ballerina.projects.util.ProjectUtils.deleteDirectory; import static io.ballerina.projects.util.ProjectUtils.getAccessTokenOfCLI; import static io.ballerina.projects.util.ProjectUtils.guessPkgName; @@ -199,8 +203,10 @@ private static void addModules(Path balaPath, Path projectPath, String packageNa Gson gson = new Gson(); Path packageJsonPath = balaPath.resolve(PACKAGE_JSON); Path dependencyGraphJsonPath = balaPath.resolve(DEPENDENCY_GRAPH_JSON); + Path balToolJsonPath = balaPath.resolve(TOOL_DIR).resolve(ProjectConstants.BAL_TOOL_JSON); PackageJson templatePackageJson = null; DependencyGraphJson templateDependencyGraphJson = null; + BalToolJson templateBalToolJson = null; try (InputStream inputStream = new FileInputStream(String.valueOf(packageJsonPath))) { Reader fileReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); @@ -226,6 +232,19 @@ private static void addModules(Path balaPath, Path projectPath, String packageNa } } + if (balToolJsonPath.toFile().exists()) { + try (InputStream inputStream = new FileInputStream(String.valueOf(balToolJsonPath))) { + Reader fileReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); + templateBalToolJson = gson.fromJson(fileReader, BalToolJson.class); + } catch (IOException e) { + printError(errStream, + "Error while reading the bal-tool json file: " + e.getMessage(), + null, + false); + getRuntime().exit(1); + } + } + if (!templatePackageJson.getTemplate()) { throw createLauncherException("unable to create the package: " + "specified package is not a template"); @@ -244,6 +263,14 @@ private static void addModules(Path balaPath, Path projectPath, String packageNa writeDependenciesToml(projectPath, templateDependencyGraphJson, templatePackageJson); } + if (balToolJsonPath.toFile().exists()) { + // Create BalTool.toml and copy dependency jars + Path balToolToml = projectPath.resolve(BAL_TOOL_TOML); + Files.createFile(balToolToml); + writeBalToolToml(balToolToml, templateBalToolJson, packageName); + copyToolDependencies(projectPath, balaPath.resolve(TOOL_DIR).resolve(LIBS_DIR)); + } + // Create Package.md Path packageMDFilePath = balaPath.resolve("docs") .resolve(ProjectConstants.PACKAGE_MD_FILE_NAME); @@ -571,6 +598,40 @@ public static void writeDependenciesToml(Path projectPath, DependencyGraphJson t Files.writeString(depsTomlPath, pkgDesc.toString(), StandardOpenOption.APPEND); } + /** + * Write to BalTool.toml file. + * + * @param balToolTomlPath path to BalTool.toml + * @param balToolJson Bal-tool.json content + */ + public static void writeBalToolToml(Path balToolTomlPath, BalToolJson balToolJson, String packageName) + throws IOException { + Files.writeString(balToolTomlPath, "[tool]", StandardOpenOption.APPEND); + Files.writeString(balToolTomlPath, "\nid = \"" + packageName + "\"\n", + StandardOpenOption.APPEND); + + List dependencyPaths = balToolJson.dependencyPaths(); + StringBuilder dependenciesContent = new StringBuilder(); + for (String dependencyPath: dependencyPaths) { + dependenciesContent.append("\n[[dependency]]\n").append("path = \"").append(dependencyPath).append("\"\n"); + } + Files.writeString(balToolTomlPath, dependenciesContent.toString(), StandardOpenOption.APPEND); + } + + /** + * Copy dependency jars to new package from template package. + * + * @param projectPath path to new project + * @param toolsLibPath Path to /tool/libs directory containing dependencies + */ + public static void copyToolDependencies(Path projectPath, Path toolsLibPath) throws IOException { + Path toolDirectory = projectPath.resolve(TOOL_DIR); + Files.createDirectory(toolDirectory); + Files.createDirectory(toolDirectory.resolve(LIBS_DIR)); + Files.walkFileTree(toolsLibPath, new FileUtils.Copy(toolsLibPath, toolDirectory.resolve(LIBS_DIR))); + + } + /** * Get formatted dependencies array content for Dependencies.toml dependency. * @@ -750,11 +811,13 @@ public static void initPackageByTemplate(Path path, String packageName, String t // - .devcontainer.json applyTemplate(path, template, balFilesExist); - if (template.equalsIgnoreCase("lib")) { + if (template.equalsIgnoreCase(LIB_DIR)) { initLibPackage(path, packageName); Path source = path.resolve("lib.bal"); Files.move(source, source.resolveSibling(guessPkgName(packageName, template) + ".bal"), StandardCopyOption.REPLACE_EXISTING); + } else if (template.equalsIgnoreCase(TOOL_DIR)) { + initToolPackage(path, packageName); } else { initPackage(path, packageName); } @@ -894,6 +957,33 @@ private static void initLibPackage(Path path, String packageName) throws IOExcep write(path.resolve(ProjectConstants.PACKAGE_MD_FILE_NAME), packageMd.getBytes(StandardCharsets.UTF_8)); } + /** + * Initialize a new ballerina tool package in the given path. + * + * @param path Project path + * @param packageName package name + * @throws IOException If any IO exception occurred + */ + private static void initToolPackage(Path path, String packageName) throws IOException { + Path ballerinaToml = path.resolve(ProjectConstants.BALLERINA_TOML); + Files.createFile(ballerinaToml); + + String defaultManifest = FileUtils.readFileAsString(NEW_CMD_DEFAULTS + "/" + "manifest-app.toml"); + defaultManifest = defaultManifest + .replaceAll(ORG_NAME, ProjectUtils.guessOrgName()) + .replaceAll(PKG_NAME, guessPkgName(packageName, TOOL_DIR)) + .replaceAll(DIST_VERSION, RepoUtils.getBallerinaShortVersion()); + Files.write(ballerinaToml, defaultManifest.getBytes(StandardCharsets.UTF_8)); + + Path balToolToml = path.resolve(ProjectConstants.BAL_TOOL_TOML); + Files.createFile(balToolToml); + + String balToolManifest = FileUtils.readFileAsString(NEW_CMD_DEFAULTS + "/" + "manifest-tool.toml"); + balToolManifest = balToolManifest.replaceAll(TOOL_ID, guessPkgName(packageName, TOOL_DIR)); + + write(balToolToml, balToolManifest.getBytes(StandardCharsets.UTF_8)); + } + protected static PackageVersion findLatest(List packageVersions) { if (packageVersions.isEmpty()) { return null; @@ -1002,7 +1092,7 @@ public static String checkTemplateFilesExists(String template, Path packagePath) * @param packagePath given path */ public static String checkPackageFilesExists(Path packagePath) { - String[] packageFiles = {DEPENDENCIES_TOML, ProjectConstants.PACKAGE_MD_FILE_NAME, + String[] packageFiles = {DEPENDENCIES_TOML, BAL_TOOL_TOML, ProjectConstants.PACKAGE_MD_FILE_NAME, ProjectConstants.MODULE_MD_FILE_NAME, ProjectConstants.MODULES_ROOT, ProjectConstants.TEST_DIR_NAME}; String existingFiles = ""; diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/PackCommand.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/PackCommand.java index f9b64dbdad66..e950cad858ab 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/PackCommand.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/PackCommand.java @@ -160,7 +160,8 @@ public void execute() { } // If project is empty - if (ProjectUtils.isProjectEmpty(project) && project.currentPackage().compilerPluginToml().isEmpty()) { + if (ProjectUtils.isProjectEmpty(project) && project.currentPackage().compilerPluginToml().isEmpty() && + project.currentPackage().balToolToml().isEmpty()) { CommandUtil.printError(this.errStream, "package is empty. Please add at least one .bal file.", null, false); CommandUtil.exitError(this.exitWhenFinish); diff --git a/cli/ballerina-cli/src/main/resources/create_cmd_templates/tool/tool/.keep b/cli/ballerina-cli/src/main/resources/create_cmd_templates/tool/tool/.keep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/cli/ballerina-cli/src/main/resources/new_cmd_defaults/manifest-tool.toml b/cli/ballerina-cli/src/main/resources/new_cmd_defaults/manifest-tool.toml new file mode 100644 index 000000000000..24cb508c8375 --- /dev/null +++ b/cli/ballerina-cli/src/main/resources/new_cmd_defaults/manifest-tool.toml @@ -0,0 +1,4 @@ +[tool] +id = "TOOL_ID" + +[[dependency]] diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 8220e39c313b..293dd53a0c56 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -503,6 +503,51 @@ public void testNewCommandWithLib() throws IOException { Assert.assertTrue(readOutput().contains("Created new package")); } + @Test(description = "Test new command with tool template") + public void testNewCommandWithTool() throws IOException { + System.setProperty(USER_NAME, "testuserorg"); + Path packageDir = tmpDir.resolve("tool_sample"); + String[] args = {packageDir.toString(), "-t", "tool"}; + NewCommand newCommand = new NewCommand(printStream, false); + new CommandLine(newCommand).parseArgs(args); + newCommand.execute(); + // Check with spec + // project_name/ + // - Ballerina.toml + // - BalTool.toml + // - Package.md + // - tool + // - .gitignore <- git ignore file + + Assert.assertTrue(Files.exists(packageDir)); + Assert.assertTrue(Files.isDirectory(packageDir)); + Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.BALLERINA_TOML))); + String packageName = Paths.get(args[0]).getFileName().toString(); + String tomlContent = Files.readString( + packageDir.resolve(ProjectConstants.BALLERINA_TOML), StandardCharsets.UTF_8); + String expectedContent = "[package]\n" + + "org = \"testuserorg\"\n" + + "name = \"" + packageName + "\"\n" + + "version = \"0.1.0\"\n" + + "distribution = \"" + RepoUtils.getBallerinaShortVersion() + "\"\n\n" + + "[build-options]\n" + + "observabilityIncluded = true\n"; + Assert.assertEquals(tomlContent.trim(), expectedContent.trim()); + + Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.BAL_TOOL_TOML))); + String toolTomlContent = Files.readString( + packageDir.resolve(ProjectConstants.BAL_TOOL_TOML), StandardCharsets.UTF_8); + String expectedToolTomlContent = "[tool]\n" + + "id = \"" + packageName + "\"\n\n" + + "[[dependency]]\n"; + Assert.assertTrue(toolTomlContent.contains(expectedToolTomlContent)); + + Assert.assertTrue(Files.notExists(packageDir.resolve(ProjectConstants.PACKAGE_MD_FILE_NAME))); + Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.TOOL_DIR))); + + Assert.assertTrue(readOutput().contains("Created new package")); + } + @Test(description = "Test new command with invalid project name", dataProvider = "invalidProjectNames") public void testNewCommandWithInvalidProjectName(String projectName, String derivedPkgName) throws IOException { // Test if no arguments was passed in diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java index 3279b84b839b..33006fc8f346 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java @@ -265,7 +265,9 @@ private void addPackageDoc(ZipOutputStream balaOutputStream, Path packageSourceD } private void addPackageSource(ZipOutputStream balaOutputStream) throws IOException { - + // add default module directory + createDirectoryInZipFile(balaOutputStream, String.valueOf(Paths.get(MODULES_ROOT, + this.packageContext.packageName().toString()))); // add module sources for (ModuleId moduleId : this.packageContext.moduleIds()) { Module module = this.packageContext.project().currentPackage().module(moduleId); @@ -448,6 +450,13 @@ protected void putDirectoryToZipFile(Path sourceDir, Path pathInZipFile, ZipOutp } } + protected void createDirectoryInZipFile(ZipOutputStream zipOutputStream, String directoryName) + throws IOException { + ZipEntry directoryEntry = new ZipEntry(directoryName + File.separator); + zipOutputStream.putNextEntry(directoryEntry); + zipOutputStream.closeEntry(); + } + protected abstract Optional addPlatformLibs(ZipOutputStream balaOutputStream) throws IOException; diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java index 820aadbf9e9e..cc54f2a7c962 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java @@ -117,6 +117,10 @@ protected void addCompilerPlugin(ZipOutputStream balaOutputStream) throws IOExce List compilerPluginLibPaths = new ArrayList<>(); List compilerPluginDependencies = this.compilerPluginToml.get().getCompilerPluginDependencies(); + if (compilerPluginDependencies.get(0) == null) { + throw new ProjectException("No dependencies found in CompilerPlugin.toml file"); + } + if (!compilerPluginDependencies.isEmpty()) { // Iterate through compiler plugin dependencies and add them to bala @@ -166,6 +170,10 @@ protected void addBalTool(ZipOutputStream balaOutputStream) throws IOException { List balToolLibPaths = new ArrayList<>(); List balToolDependencies = this.balToolToml.get().getBalToolDependencies(); + if (balToolDependencies.get(0) == null) { + throw new ProjectException("No dependencies found in BalTool.toml file"); + } + if (!balToolDependencies.isEmpty()) { // Iterate through bal tool dependencies and add them to bala // organization would be diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectUtils.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectUtils.java index ef6fa1a53ad1..0b38d6b166f4 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectUtils.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/util/ProjectUtils.java @@ -111,6 +111,7 @@ import static io.ballerina.projects.util.ProjectConstants.TARGET_DIR_NAME; import static io.ballerina.projects.util.ProjectConstants.TEST_CORE_JAR_PREFIX; import static io.ballerina.projects.util.ProjectConstants.TEST_RUNTIME_JAR_PREFIX; +import static io.ballerina.projects.util.ProjectConstants.TOOL_DIR; import static io.ballerina.projects.util.ProjectConstants.USER_NAME; /** @@ -366,11 +367,13 @@ public static String guessPkgName(String packageName, String template) { packageName = packageName.replaceAll("[^a-zA-Z0-9_.]", "_"); } - // if package name is starting with numeric character, prepend `app` / `lib` + // if package name is starting with numeric character, prepend `app` / `lib` / `tool` if (packageName.matches("[0-9].*")) { - if (template.equalsIgnoreCase("lib")) { - packageName = "lib" + packageName; - } else { + if (template.equalsIgnoreCase(LIB_DIR)) { + packageName = LIB_DIR + packageName; + } else if (template.equalsIgnoreCase(TOOL_DIR)) { + packageName = TOOL_DIR + packageName; + } else { packageName = "app" + packageName; } } From fb50cd7414d2882f4965fc11b890e5b8f04d523c Mon Sep 17 00:00:00 2001 From: ShammiL Date: Fri, 19 May 2023 15:04:14 +0530 Subject: [PATCH 20/70] Add support for tool template for bal new command --- .../src/main/java/io/ballerina/cli/cmd/CommandUtil.java | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java index 1e5bbe34d376..703018dd07cb 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java @@ -100,6 +100,7 @@ public class CommandUtil { public static final String ORG_NAME = "ORG_NAME"; public static final String PKG_NAME = "PKG_NAME"; public static final String DIST_VERSION = "DIST_VERSION"; + public static final String TOOL_ID = "TOOL_ID"; public static final String USER_HOME = "user.home"; public static final String GITIGNORE = "gitignore"; public static final String DEVCONTAINER = "devcontainer"; From b9f03f4048bb752692be3729e8abec07b42aacc5 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 25 May 2023 09:10:34 +0530 Subject: [PATCH 21/70] Include tests for new and pack commands --- .../io/ballerina/cli/cmd/NewCommandTest.java | 40 +++++++++++++++++++ .../io/ballerina/cli/cmd/PackCommandTest.java | 16 ++++++++ .../testorg/toolProject/1.0.0/any/bala.json | 4 ++ .../1.0.0/any/dependency-graph.json | 21 ++++++++++ .../toolProject/1.0.0/any/docs/Package.md | 1 + .../toolProject/1.0.0/any/package.json | 13 ++++++ .../toolProject/1.0.0/any/tool/bal-tool.json | 6 +++ .../any/tool/libs/platform-io-1.3.0-java.txt | 1 + .../unix/compile-empty-project-with-tool.txt | 5 +++ .../compile-empty-project-with-tool.txt | 5 +++ .../emptyProjectWithTool/BalTool.toml | 5 +++ .../emptyProjectWithTool/Ballerina.toml | 4 ++ .../tool/libs/platform-io-1.3.0-java.txt | 1 + 13 files changed, 122 insertions(+) create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/bala.json create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/dependency-graph.json create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/docs/Package.md create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/package.json create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/libs/platform-io-1.3.0-java.txt create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/compile-empty-project-with-tool.txt create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/compile-empty-project-with-tool.txt create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/BalTool.toml create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/Ballerina.toml create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/tool/libs/platform-io-1.3.0-java.txt diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 293dd53a0c56..0703e4687a78 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -43,6 +43,7 @@ import static io.ballerina.cli.cmd.CommandOutputUtils.getOutput; import static io.ballerina.cli.cmd.CommandOutputUtils.readFileAsString; +import static io.ballerina.projects.util.ProjectConstants.TOOL_DIR; import static io.ballerina.projects.util.ProjectConstants.USER_NAME; /** @@ -575,6 +576,45 @@ public void testNewCommandWithInvalidTemplate() throws IOException { Assert.assertTrue(readOutput().contains("invalid package name provided")); } + @Test(description = "Test new command by pulling a central tool template") + public void testNewCommandWithToolTemplateCentral() throws IOException { + String templateArg = "testorg/toolProject:1.0.0"; + String packageName = "sample_tool_template"; + Path packageDir = tmpDir.resolve(packageName); + String[] args = {packageDir.toString(), "-t", templateArg}; + NewCommand newCommand = new NewCommand(printStream, false, homeCache); + new CommandLine(newCommand).parseArgs(args); + newCommand.execute(); + + Assert.assertTrue(Files.exists(packageDir)); + + Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.BALLERINA_TOML))); + String expectedTomlContent = "[package]\n" + + "org = \"testorg\"\n" + + "name = \"" + packageName + "\"\n" + + "version = \"1.0.0\"\n" + + "export = [\"sample_tool_template\"]\n" + + "distribution = \"" + RepoUtils.getBallerinaShortVersion() + "\"\n\n" + + "[build-options]\n" + + "observabilityIncluded = true\n"; + Assert.assertEquals( + readFileAsString(packageDir.resolve(ProjectConstants.BALLERINA_TOML)), expectedTomlContent); + + String toolTomlContent = Files.readString( + packageDir.resolve(ProjectConstants.BAL_TOOL_TOML), StandardCharsets.UTF_8); + String expectedToolTomlContent = "[tool]\n" + + "id = \"" + packageName + "\"\n\n" + + "[[dependency]]\n" + + "path = \"tool/libs/platform-io-1.3.0-java.txt\"\n"; + Assert.assertTrue(toolTomlContent.contains(expectedToolTomlContent)); + + Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.PACKAGE_MD_FILE_NAME))); + Path dependencyPath = packageDir.resolve(TOOL_DIR).resolve("libs") + .resolve("platform-io-1.3.0-java.txt"); + Assert.assertTrue(Files.exists(dependencyPath)); + Assert.assertTrue(readOutput().contains("Created new package")); + } + @Test(description = "Test new command with central template in the local cache") public void testNewCommandWithTemplateInLocalCache() throws IOException { // Test if no arguments was passed in diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java index 223a2a4b25e5..a1ac2ad6b129 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java @@ -179,6 +179,22 @@ public void testPackEmptyProjectWithCompilerPlugin() throws IOException { getOutput("compile-empty-project-with-compiler-plugin.txt")); } + @Test(description = "Pack an empty package as a tool") + public void testPackEmptyProjectWithTool() throws IOException { + Path projectPath = this.testResources.resolve("emptyProjectWithTool"); + System.setProperty("user.dir", projectPath.toString()); + + PackCommand packCommand = new PackCommand(projectPath, printStream, printStream, false, true); + new CommandLine(packCommand).parseArgs(); + packCommand.execute(); + String buildLog = readOutput(true); + + Assert.assertTrue(projectPath.resolve("target").resolve("bala") + .resolve("wso2-emptyProjWithTool-any-0.1.0.bala").toFile().exists()); + Assert.assertEquals(buildLog.replaceAll("\r", ""), + getOutput("compile-empty-project-with-tool.txt")); + } + @Test(description = "Pack an empty package with tests only") public void testPackEmptyProjectWithTestsOnly() { Path projectPath = this.testResources.resolve("emptyProjectWithTestsOnly"); diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/bala.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/bala.json new file mode 100644 index 000000000000..fd0a01921270 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/bala.json @@ -0,0 +1,4 @@ +{ + "bala_version": "2.0.0", + "built_by": "WSO2" +} \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/dependency-graph.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/dependency-graph.json new file mode 100644 index 000000000000..b450609de0b9 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/dependency-graph.json @@ -0,0 +1,21 @@ +{ + "packages": [ + { + "org": "testorg", + "name": "toolProject", + "version": "1.0.0", + "transitive": false, + "dependencies": [], + "modules": [] + } + ], + "modules": [ + { + "org": "testorg", + "package_name": "toolProject", + "version": "1.0.0", + "module_name": "toolProject", + "dependencies": [] + } + ] +} \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/docs/Package.md b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/docs/Package.md new file mode 100644 index 000000000000..bd9518b902e3 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/docs/Package.md @@ -0,0 +1 @@ +test tool package \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/package.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/package.json new file mode 100644 index 000000000000..a85d22308cdf --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/package.json @@ -0,0 +1,13 @@ +{ + "organization": "testorg", + "name": "toolProject", + "version": "1.0.0", + "export": [ + "toolProject" + ], + "ballerina_version": "2201.6.0-SNAPSHOT", + "implementation_vendor": "WSO2", + "language_spec_version": "2022R4", + "platform": "any", + "template": true +} \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json new file mode 100644 index 000000000000..ec156c768d95 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json @@ -0,0 +1,6 @@ +{ + "tool_id": "toolProject", + "dependency_paths": [ + "tool/libs/platform-io-1.3.0-java.txt" + ] +} \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/libs/platform-io-1.3.0-java.txt b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/libs/platform-io-1.3.0-java.txt new file mode 100644 index 000000000000..b4704b3b8db3 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/libs/platform-io-1.3.0-java.txt @@ -0,0 +1 @@ +// Testing \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/compile-empty-project-with-tool.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/compile-empty-project-with-tool.txt new file mode 100644 index 000000000000..4ccf26d2e611 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/compile-empty-project-with-tool.txt @@ -0,0 +1,5 @@ +Compiling source + wso2/emptyProjWithTool:0.1.0 + +Creating bala + target/bala/wso2-emptyProjWithTool-any-0.1.0.bala diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/compile-empty-project-with-tool.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/compile-empty-project-with-tool.txt new file mode 100644 index 000000000000..8cf4014a6668 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/compile-empty-project-with-tool.txt @@ -0,0 +1,5 @@ +Compiling source + wso2/emptyProjWithTool:0.1.0 + +Creating bala + target\bala\wso2-emptyProjWithTool-any-0.1.0.bala diff --git a/cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/BalTool.toml b/cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/BalTool.toml new file mode 100644 index 000000000000..62e2ccf4bc77 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/BalTool.toml @@ -0,0 +1,5 @@ +[tool] +id = "emptyProjWithTool" + +[[dependency]] +path = "tool/libs/platform-io-1.3.0-java.txt" diff --git a/cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/Ballerina.toml b/cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/Ballerina.toml new file mode 100644 index 000000000000..23cceb232759 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "wso2" +name = "emptyProjWithTool" +version = "0.1.0" diff --git a/cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/tool/libs/platform-io-1.3.0-java.txt b/cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/tool/libs/platform-io-1.3.0-java.txt new file mode 100644 index 000000000000..b4704b3b8db3 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/emptyProjectWithTool/tool/libs/platform-io-1.3.0-java.txt @@ -0,0 +1 @@ +// Testing \ No newline at end of file From e31a1dcc31ff9930586ff1f0b90c5eaff62fabba Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 25 May 2023 10:08:25 +0530 Subject: [PATCH 22/70] Add missing files --- .../testorg/toolProject/1.0.0/any/modules/toolProject/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/modules/toolProject/.keep diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/modules/toolProject/.keep b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/modules/toolProject/.keep new file mode 100644 index 000000000000..e69de29bb2d1 From 250767512bf9ab46a68268868dfec9f734ea3225 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Fri, 16 Jun 2023 09:29:51 +0530 Subject: [PATCH 23/70] Generate empty bal file in default module for tools --- .../testorg/toolProject/1.0.0/any/bala.json | 2 +- .../toolProject/1.0.0/any/dependency-graph.json | 2 +- .../testorg/toolProject/1.0.0/any/package.json | 2 +- .../toolProject/1.0.0/any/tool/bal-tool.json | 2 +- .../java/io/ballerina/projects/BalaWriter.java | 16 ++++++++++++++++ 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/bala.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/bala.json index fd0a01921270..812300d452d7 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/bala.json +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/bala.json @@ -1,4 +1,4 @@ { "bala_version": "2.0.0", "built_by": "WSO2" -} \ No newline at end of file +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/dependency-graph.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/dependency-graph.json index b450609de0b9..cab49c6804b6 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/dependency-graph.json +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/dependency-graph.json @@ -18,4 +18,4 @@ "dependencies": [] } ] -} \ No newline at end of file +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/package.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/package.json index a85d22308cdf..6e27c3910f41 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/package.json +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/package.json @@ -10,4 +10,4 @@ "language_spec_version": "2022R4", "platform": "any", "template": true -} \ No newline at end of file +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json index ec156c768d95..f9f6a3142e5c 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json @@ -3,4 +3,4 @@ "dependency_paths": [ "tool/libs/platform-io-1.3.0-java.txt" ] -} \ No newline at end of file +} diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java index 33006fc8f346..7b18b4e39604 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java @@ -33,6 +33,8 @@ import io.ballerina.projects.internal.model.Dependency; import io.ballerina.projects.util.ProjectConstants; import io.ballerina.projects.util.ProjectUtils; +import io.ballerina.tools.text.TextDocument; +import io.ballerina.tools.text.TextDocuments; import org.apache.commons.compress.utils.IOUtils; import org.ballerinalang.compiler.BLangCompilerException; import org.wso2.ballerinalang.util.RepoUtils; @@ -74,6 +76,7 @@ public abstract class BalaWriter { private static final String BLANG_SOURCE_EXT = ".bal"; protected static final String PLATFORM = "platform"; protected static final String PATH = "path"; + private static final String MAIN_BAL = "main.bal"; // Set the target as any for default bala. protected String target = "any"; @@ -280,6 +283,19 @@ private void addPackageSource(ZipOutputStream balaOutputStream) throws IOExcepti putZipEntry(balaOutputStream, resourcePath, new ByteArrayInputStream(resource.content())); } + // Generate empty bal file for default module in tools + if (module.isDefaultModule() && !packageContext.balToolTomlContext().isEmpty() && + module.documentIds().isEmpty()) { + String emptyBalContent = "// AUTO-GENERATED FILE.\n" + + "\n" + + "// This file is auto-generated by Ballerina for managing modules in packages.\n"; + + TextDocument emptyBalTextDocument = TextDocuments.from(emptyBalContent); + DocumentId documentId = DocumentId.create(MAIN_BAL, moduleId); + DocumentConfig documentConfig = DocumentConfig.from(documentId, emptyBalTextDocument.toString(), + MAIN_BAL); + module = module.modify().addDocument(documentConfig).apply(); + } // only add .bal files of module for (DocumentId docId : module.documentIds()) { From 867db9c7347aa41b24ac308ce0e7f509894bdb51 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Fri, 16 Jun 2023 11:23:54 +0530 Subject: [PATCH 24/70] Fix test errors --- .../src/test/java/io/ballerina/cli/cmd/NewCommandTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 0703e4687a78..882ccf7daf88 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -594,7 +594,7 @@ public void testNewCommandWithToolTemplateCentral() throws IOException { "name = \"" + packageName + "\"\n" + "version = \"1.0.0\"\n" + "export = [\"sample_tool_template\"]\n" + - "distribution = \"" + RepoUtils.getBallerinaShortVersion() + "\"\n\n" + + "distribution = \"2201.6.0-SNAPSHOT\"\n\n" + "[build-options]\n" + "observabilityIncluded = true\n"; Assert.assertEquals( From 1a1c14f3f5eea6757bee38a3384fe2a2f3da8534 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Mon, 26 Jun 2023 22:47:00 +0530 Subject: [PATCH 25/70] Remove empty directory --- .../main/java/io/ballerina/projects/BalaWriter.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java index 7b18b4e39604..1d9b332d940f 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java @@ -268,9 +268,6 @@ private void addPackageDoc(ZipOutputStream balaOutputStream, Path packageSourceD } private void addPackageSource(ZipOutputStream balaOutputStream) throws IOException { - // add default module directory - createDirectoryInZipFile(balaOutputStream, String.valueOf(Paths.get(MODULES_ROOT, - this.packageContext.packageName().toString()))); // add module sources for (ModuleId moduleId : this.packageContext.moduleIds()) { Module module = this.packageContext.project().currentPackage().module(moduleId); @@ -466,13 +463,6 @@ protected void putDirectoryToZipFile(Path sourceDir, Path pathInZipFile, ZipOutp } } - protected void createDirectoryInZipFile(ZipOutputStream zipOutputStream, String directoryName) - throws IOException { - ZipEntry directoryEntry = new ZipEntry(directoryName + File.separator); - zipOutputStream.putNextEntry(directoryEntry); - zipOutputStream.closeEntry(); - } - protected abstract Optional addPlatformLibs(ZipOutputStream balaOutputStream) throws IOException; From 0c0e75109ffe737a74d55dd42108c0c28f1bddf3 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Tue, 27 Jun 2023 23:37:36 +0530 Subject: [PATCH 26/70] Add BalaWriter test --- .../projects/test/TestBalaWriter.java | 60 ++++++++++++++++++ .../balawriter/projectTool/.devcontainer.json | 4 ++ .../balawriter/projectTool/.gitignore | 3 + .../balawriter/projectTool/BalTool.toml | 5 ++ .../balawriter/projectTool/Ballerina.toml | 10 +++ .../balawriter/projectTool/Package.md | 1 + .../tool/libs/ballerina-io-1.4.0-java.jar | Bin 0 -> 2330 bytes 7 files changed, 83 insertions(+) create mode 100644 project-api/project-api-test/src/test/resources/balawriter/projectTool/.devcontainer.json create mode 100644 project-api/project-api-test/src/test/resources/balawriter/projectTool/.gitignore create mode 100644 project-api/project-api-test/src/test/resources/balawriter/projectTool/BalTool.toml create mode 100644 project-api/project-api-test/src/test/resources/balawriter/projectTool/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/balawriter/projectTool/Package.md create mode 100644 project-api/project-api-test/src/test/resources/balawriter/projectTool/tool/libs/ballerina-io-1.4.0-java.jar diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBalaWriter.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBalaWriter.java index 838fdd215377..a2519422f42a 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBalaWriter.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBalaWriter.java @@ -346,6 +346,66 @@ public void testBalaWriterWithMinimalBalProject(ITestContext ctx) throws IOExcep Assert.assertTrue(defaultModuleSrcPath.resolve(Paths.get("main.bal")).toFile().exists()); } + @Test + public void testBalaWriterWithToolProject(ITestContext ctx) throws IOException { + Gson gson = new Gson(); + Path projectPath = BALA_WRITER_RESOURCES.resolve("projectTool"); + ctx.getCurrentXmlTest().addParameter(PACKAGE_PATH, String.valueOf(projectPath)); + Project project = TestUtils.loadBuildProject(projectPath); + + PackageCompilation packageCompilation = project.currentPackage().getCompilation(); + Target target = new Target(project.sourceRoot()); + + JBallerinaBackend jBallerinaBackend = JBallerinaBackend.from(packageCompilation, JvmTarget.JAVA_11); + jBallerinaBackend.emit(JBallerinaBackend.OutputType.BALA, target.getBalaPath()); + + // invoke write bala method + jBallerinaBackend.emit(JBallerinaBackend.OutputType.BALA, target.getBalaPath()); + + // unzip bala + TestUtils.unzip(String.valueOf(target.getBalaPath().resolve("foo-tool_test-any-1.0.1.bala")), + String.valueOf(balaExportPath)); + + // bala.json + Path balaJsonPath = balaExportPath.resolve("bala.json"); + Assert.assertTrue(balaJsonPath.toFile().exists()); + + try (FileReader reader = new FileReader(String.valueOf(balaJsonPath))) { + BalaJson balaJson = gson.fromJson(reader, BalaJson.class); + Assert.assertEquals(balaJson.getBala_version(), "2.0.0"); + Assert.assertEquals(balaJson.getBuilt_by(), "WSO2"); + } + + // package.json + Path packageJsonPath = balaExportPath.resolve("package.json"); + Assert.assertTrue(packageJsonPath.toFile().exists()); + + try (FileReader reader = new FileReader(String.valueOf(packageJsonPath))) { + PackageJson packageJson = gson.fromJson(reader, PackageJson.class); + Assert.assertEquals(packageJson.getOrganization(), "foo"); + Assert.assertEquals(packageJson.getName(), "tool_test"); + Assert.assertEquals(packageJson.getVersion(), "1.0.1"); + } + + // bal-tool.json + Path balToolJsonPath = balaExportPath.resolve("tool").resolve("bal-tool.json"); + try (FileReader reader = new FileReader(String.valueOf(balToolJsonPath))) { + BalToolJson balToolJson = gson.fromJson(reader, BalToolJson.class); + Assert.assertEquals(balToolJson.toolId(), "tool_test"); + Assert.assertEquals(balToolJson.dependencyPaths().size(), 1); + } + + // module sources + Path defaultModuleSrcPath = balaExportPath.resolve("modules").resolve("tool_test"); + Assert.assertTrue(defaultModuleSrcPath.toFile().exists()); + Path mainFilePath = defaultModuleSrcPath.resolve(Paths.get("main.bal")); + Assert.assertTrue(mainFilePath.toFile().exists()); + String expectedMainContent = "// AUTO-GENERATED FILE.\n" + + "\n" + + "// This file is auto-generated by Ballerina for managing modules in packages.\n"; + Assert.assertEquals(Files.readString(mainFilePath), expectedMainContent); + } + @Test public void testBalaWriterWithTwoDirectDependencies(ITestContext ctx) throws IOException { Gson gson = new Gson(); diff --git a/project-api/project-api-test/src/test/resources/balawriter/projectTool/.devcontainer.json b/project-api/project-api-test/src/test/resources/balawriter/projectTool/.devcontainer.json new file mode 100644 index 000000000000..cb6489ba7f7f --- /dev/null +++ b/project-api/project-api-test/src/test/resources/balawriter/projectTool/.devcontainer.json @@ -0,0 +1,4 @@ +{ + "image": "ballerina/ballerina-devcontainer:2201.7.0-SNAPSHOT", + "extensions": ["WSO2.ballerina"], +} diff --git a/project-api/project-api-test/src/test/resources/balawriter/projectTool/.gitignore b/project-api/project-api-test/src/test/resources/balawriter/projectTool/.gitignore new file mode 100644 index 000000000000..7512ebe2325f --- /dev/null +++ b/project-api/project-api-test/src/test/resources/balawriter/projectTool/.gitignore @@ -0,0 +1,3 @@ +target +generated +Config.toml diff --git a/project-api/project-api-test/src/test/resources/balawriter/projectTool/BalTool.toml b/project-api/project-api-test/src/test/resources/balawriter/projectTool/BalTool.toml new file mode 100644 index 000000000000..27f144123863 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/balawriter/projectTool/BalTool.toml @@ -0,0 +1,5 @@ +[tool] +id = "tool_test" + +[[dependency]] +path = "tool/libs/ballerina-io-1.4.0-java.jar" diff --git a/project-api/project-api-test/src/test/resources/balawriter/projectTool/Ballerina.toml b/project-api/project-api-test/src/test/resources/balawriter/projectTool/Ballerina.toml new file mode 100644 index 000000000000..af02acdfbb74 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/balawriter/projectTool/Ballerina.toml @@ -0,0 +1,10 @@ +[package] +org = "foo" +name = "tool_test" +version = "1.0.1" +export = ["tool_test"] +distribution = "2201.6.0-SNAPSHOT" +template = true + +[build-options] +observabilityIncluded = true diff --git a/project-api/project-api-test/src/test/resources/balawriter/projectTool/Package.md b/project-api/project-api-test/src/test/resources/balawriter/projectTool/Package.md new file mode 100644 index 000000000000..47f5c5be3422 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/balawriter/projectTool/Package.md @@ -0,0 +1 @@ +test tool bala \ No newline at end of file diff --git a/project-api/project-api-test/src/test/resources/balawriter/projectTool/tool/libs/ballerina-io-1.4.0-java.jar b/project-api/project-api-test/src/test/resources/balawriter/projectTool/tool/libs/ballerina-io-1.4.0-java.jar new file mode 100644 index 0000000000000000000000000000000000000000..022673679f6e9776e8d1bc1816d9bc318261bb62 GIT binary patch literal 2330 zcmaKtc|6qlAIHa$Ge-^9ICHEJBiG87D;ehux(D2mq3Y@46dZ(rZX=llJ9ACK4T{r-Hv??0Ya<}9pS000LEKo*a; z1JK3HxY5N)Ur-|(17%YfM1=)l^_8(El6ggxZUE7jz+NNN0A>m?vbF(3Av9=zpM|+H zc#y|jS&`WPqW0YJA@wOLh046I7(Kt+m7Tu#f``FTm~VRSwQ>K?=7~anW93S}sf9ZL z0NkMaB)+y9|5Mx$?duEoI}i3m!7-RbJD(7sk#IyY@{y^Ce6c`pQR8*y7GXujJc1Ci zs!oJ#mhYmtAPag~gd1;sq#$6`?6wmXv8EDhJRLO`x;@LjlQgNLTPu16ipAH`h|RPK zbZYBP_%?eKa8ox9Y;kmzmw2=asDAmO`;^CF<;)~cyH4`}m!ugpG5eWJbILC+@iRI+ zlOsctbp{3e#%DWIn9_^gk0qJ1XdP4%t~x+2%cZiaV@u8(<*7#+f_tU<#Is!uybe%k zy_HF%8l@wZTuVQ@VVLg~Tu`1=99^iJX~Z5>8u^pw1apP8Z8*OP^hDCyrTX=7D)-WJ z`-%eRx{7?fbE!#5T7%dt(L>gz@iND<%QaI67g)2^XhR`pN6yj^j)_1}A^YjJWLItU z2W(q?H^s@FWGG^&9S6F%)SdT60Xq>i(m6=F0}G3taD-`}OwpX;k}WAR{)AogzD62& zDadsZc}$^o-Z97$_(M1~i_ApULuuO`m*!twyesHUeE6fs*n`*}r=q-O?i2%{$=rBB zomY|9TZ}kKtlwF~J>;=c24R^GDN2kPYx>b<{ME>Ds8d$HU#W+3-jPD9+NG4W%nBae zTM&DJ2MD?3C)Y7GPWI?D+vA|i)h+iUJGAirZ6=CGyKT*0Z;~NRLq{n2vF{fyQ;0EjHtI9O%>^R09rKlOoR=JsYqRJgNM`jo65cKvA;&Q&;?+Tqu-WYo|`gmy?(MMCV#>bXeI z7c0Gl2;$<*RCa=eob{?cRQ#jdIUi#6&?lcBSs$g;!7U5OA5e?~POGD(G!kK1+NOne zYHwD6zQ48nm*s5@eu7n}bgk_VP6>Do<;esth|h?Imi7FzD6F%B(sX^>2TrfEWV@cg zj$Y02MLFN!Y%}>M*KG#R#g`4GYOtVtTo8dn?fI#KBi6-Ke`ikPuG<1DQ()1MaD1#t z(0Ci)DMy`>Yhewu4GomS6S}Zz30-(1YWT2EG*r;IYIo`6uFHT8ywW)Z2X0~wp8ra+Pr1V0QY|HP&wYy9(@Yn|52aQ+bShvA# z8Q6Bxonw5ChkixQFi|qtevjjjl^GDirm@Y&^60kR*}9xM@e#268nwkerV$!FH!7hA zHxJEvgD@$+?{RBjFj}wrld4Y48xE-!-<)W!6)AfsOL zjL?mMm=BHpS4}Pu+FV{6Tp%q5=hh1I%7itn>@03O@%ODj`E{;YzXP1_h#H7mPr92? zND?VXs_ZpJX-lG~0(0Ium{`946qNAH{<@pe4Y$=NqP!2NxnRny^%;CS&Jw6v6BnzB zr;-!?OpcINqVRgGoywu9I^LgEcq*Q7MV)4a4U=!Tvh|N%RqkluZ0*w8y!jwFCoq6Z zo`&1u`PHvWq7S;!-fu6Y(uGV2E1>h3HOU`cnCB0*MBDz)>i~C8Qsmjsqn4 zv;{oK&(j-m$mkI>z;CJ)?Ib|#J1N0C8 Pz)rsgm;eBcfBxm)PRxuy literal 0 HcmV?d00001 From 39da71580203ac47ec6920fc7099791ae8fdeea4 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Tue, 27 Jun 2023 23:38:14 +0530 Subject: [PATCH 27/70] Fix bad sad error in tool packing --- .../projects/JBallerinaBalaWriter.java | 65 +++++++++---------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java index cc54f2a7c962..4e6803453283 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java @@ -170,46 +170,43 @@ protected void addBalTool(ZipOutputStream balaOutputStream) throws IOException { List balToolLibPaths = new ArrayList<>(); List balToolDependencies = this.balToolToml.get().getBalToolDependencies(); - if (balToolDependencies.get(0) == null) { + if (balToolDependencies.isEmpty()) { throw new ProjectException("No dependencies found in BalTool.toml file"); } - - if (!balToolDependencies.isEmpty()) { - // Iterate through bal tool dependencies and add them to bala - // organization would be - // -- Bala Root - // - tool/ - // - libs - // - java-library1.jar - // - java-library2.jar - - - // Iterate jars and create directories for each target - for (String balToolLib : balToolDependencies) { - Path libPath = this.packageContext.project().sourceRoot().resolve(balToolLib); - // null check is added for spot bug with the toml validation filename cannot be null - String fileName = Optional.ofNullable(libPath.getFileName()) - .map(p -> p.toString()).orElse("annon"); - Path entryPath = Paths.get(TOOL).resolve(LIBS).resolve(fileName); - // create a zip entry for each file - putZipEntry(balaOutputStream, entryPath, new FileInputStream(libPath.toString())); - balToolLibPaths.add(entryPath.toString()); - } + // Iterate through bal tool dependencies and add them to bala + // organization would be + // -- Bala Root + // - tool/ + // - libs + // - java-library1.jar + // - java-library2.jar + + + // Iterate jars and create directories for each target + for (String balToolLib : balToolDependencies) { + Path libPath = this.packageContext.project().sourceRoot().resolve(balToolLib); + // null check is added for spot bug with the toml validation filename cannot be null + String fileName = Optional.ofNullable(libPath.getFileName()) + .map(p -> p.toString()).orElse("annon"); + Path entryPath = Paths.get(TOOL).resolve(LIBS).resolve(fileName); + // create a zip entry for each file + putZipEntry(balaOutputStream, entryPath, new FileInputStream(libPath.toString())); + balToolLibPaths.add(entryPath.toString()); } - BalToolJson balToolJson = new BalToolJson(this.balToolToml.get().tool().getId(), balToolLibPaths); + BalToolJson balToolJson = new BalToolJson(this.balToolToml.get().tool().getId(), balToolLibPaths); - // Remove fields with empty values from `BalTool.json` - Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Collection.class, new JsonCollectionsAdaptor()) - .registerTypeHierarchyAdapter(String.class, new JsonStringsAdaptor()).setPrettyPrinting().create(); + // Remove fields with empty values from `BalTool.json` + Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Collection.class, new JsonCollectionsAdaptor()) + .registerTypeHierarchyAdapter(String.class, new JsonStringsAdaptor()).setPrettyPrinting().create(); - try { - putZipEntry(balaOutputStream, Paths.get(TOOL, BAL_TOOL_JSON), - new ByteArrayInputStream( - gson.toJson(balToolJson).getBytes(Charset.defaultCharset()))); - } catch (IOException e) { - throw new ProjectException("Failed to write '" + BAL_TOOL_JSON + "' file: " + e.getMessage(), e); - } + try { + putZipEntry(balaOutputStream, Paths.get(TOOL, BAL_TOOL_JSON), + new ByteArrayInputStream( + gson.toJson(balToolJson).getBytes(Charset.defaultCharset()))); + } catch (IOException e) { + throw new ProjectException("Failed to write '" + BAL_TOOL_JSON + "' file: " + e.getMessage(), e); + } } } From 9e4a680191f7218e992a91ca4ec59f8ac4cdc912 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Wed, 28 Jun 2023 11:34:08 +0530 Subject: [PATCH 28/70] Add logs and set user directory --- .../java/io/ballerina/cli/cmd/NewCommandTest.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 86a79dc1a6fb..89cfbda1e426 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -22,6 +22,9 @@ import io.ballerina.projects.util.FileUtils; import io.ballerina.projects.util.ProjectConstants; import io.ballerina.projects.util.ProjectUtils; +import org.ballerinalang.test.BCompileUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -56,6 +59,8 @@ public class NewCommandTest extends BaseCommandTest { Path testResources; Path centralCache; + private static final Logger logger = LoggerFactory.getLogger(BCompileUtil.class); + @DataProvider(name = "invalidProjectNames") public Object[][] provideInvalidProjectNames() { return new Object[][] { @@ -646,15 +651,17 @@ public void testNewCommandCentralTemplateReplaceImports() throws IOException { Assert.assertTrue(mainContent.contains("import central_sample.mod1;")); Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.PACKAGE_MD_FILE_NAME))); Assert.assertTrue(readOutput().contains("Created new package")); - + logger.info(System.getProperty("user.dir")); + System.setProperty("user.dir", packageDir.toString()); + logger.info(System.getProperty("user.dir")); BuildCommand buildCommand = new BuildCommand(packageDir, printStream, printStream, false); new CommandLine(buildCommand).parseArgs(); try { buildCommand.execute(); } catch (BLauncherException e) { - printStream.println(e.getStackTrace()); - printStream.println(e.getMessage()); - printStream.println(e.getDetailedMessages()); + logger.error(e.getStackTrace().toString()); + logger.error(e.getMessage()); + logger.error(e.getDetailedMessages().toString()); } String buildLog = readOutput(true); Assert.assertTrue(buildLog.contains("Generating executable"));; From f2368237fbbe2d681c26f3eaf862836e8f1519f2 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Wed, 28 Jun 2023 15:04:29 +0530 Subject: [PATCH 29/70] Change workflow for manual triggering --- .github/workflows/daily_build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/daily_build.yml b/.github/workflows/daily_build.yml index 482d1b471a9c..72ddf3962ffc 100644 --- a/.github/workflows/daily_build.yml +++ b/.github/workflows/daily_build.yml @@ -1,6 +1,7 @@ name: Ballerina daily build on: + workflow_dispatch: schedule: - cron: '30 18 * * *' # 00:00 in LK time (GMT+5:30) - cron: '30 6 * * *' # 12:00 in LK time (GMT+5:30) From 843933c92f88094ea19da3d96c60130d7e528361 Mon Sep 17 00:00:00 2001 From: Tharik Kanaka Date: Tue, 4 Jul 2023 15:03:01 +0530 Subject: [PATCH 30/70] Remove unused dependencies --- distribution/zip/jballerina-tools/build.gradle | 4 ---- gradle.properties | 16 ---------------- gradle/javaLibsProject.gradle | 16 ---------------- 3 files changed, 36 deletions(-) diff --git a/distribution/zip/jballerina-tools/build.gradle b/distribution/zip/jballerina-tools/build.gradle index 92062e14da8f..ccd9d233e728 100644 --- a/distribution/zip/jballerina-tools/build.gradle +++ b/distribution/zip/jballerina-tools/build.gradle @@ -67,7 +67,6 @@ configurations { } dependencies { - dist "org.bytedeco:javacpp:${project.bytedecoJavacppVersion}" dist "com.fasterxml.jackson.core:jackson-databind:${project.jacksonDatabindVersion}" dist "com.fasterxml.jackson.core:jackson-core:${project.jacksonCoreVersion}" dist "com.fasterxml.jackson.core:jackson-annotations:${project.jacksonAnnotationsVersion}" @@ -81,9 +80,6 @@ dependencies { dist "org.apache.commons:commons-text:${project.apacheCommonsTextVersion}" dist "com.github.spullara.mustache.java:compiler:${project.spullaraMustacheCompilerVersion}" - // Following dependencies are required for kraal library - dist "org.jetbrains.kotlin:kotlin-stdlib:${project.jetbrainsKotlinStdlibVersion}" - dist "org.jetbrains.kotlin:kotlin-stdlib-common:${project.jetbrainsKotlinStdlibCommonVersion}" dist "org.ow2.asm:asm:${project.ow2AsmVersion}" dist "org.ow2.asm:asm-analysis:${project.ow2AsmAnalysisVersion}" dist "org.ow2.asm:asm-tree:${project.ow2AsmTreeVersion}" diff --git a/gradle.properties b/gradle.properties index db62e6cdb3de..3e90019400cd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -18,8 +18,6 @@ apacheCommonsCompressVersion=1.21 apacheCommonsLang3Version=3.12.0 apacheCommonsTextVersion=1.10.0 apacheGeronimoStaxVersion=1.0.1 -apacheGeronimoActivationVersion=1.1 -apacheGeronimoJsonVersion=1.0-alpha-1 apacheMavenPluginAnnotationsVersion=3.6.0 apacheMavenPluginApiVersion=3.6.0 apacheMavenProviderVersion=3.6.3 @@ -40,15 +38,9 @@ atomikosTransactionsApiVersion=5.0.8 atomikosTransactionsJdbcVersion=5.0.8 atomikosTransactionsVersion=5.0.8 awaitilityVersion=3.1.6 -balMessagingBrokerAuthVersion=0.970.0 -balMessagingBrokerCommonVersion=0.970.5 -balMessagingBrokerCoordinationVersion=0.970.0 -balMessagingBrokerCoreVersion=0.970.5 balMessagingBrokerAmqpVersion=0.970.5 -balMessagingBrokerRestRunnerVersion=0.970.0 bitbucketCowwocVersion=1.2 bouncycastleVersion=1.61 -bytedecoJavacppVersion=1.4.2 bytedecoJavacppPlatformVersion=6.0.1-1.4.2 checkStyleToolVersion=7.8.2 chewiebugGcviewerVersion=1.36 @@ -60,7 +52,6 @@ commonsCodecVersion=1.14 commonsIoVersion=2.7 commonsLoggingVersion=1.1.1 commonsCollectionsVersion=3.2.2 -dropwizardMetricsCoreVersion=4.1.7 drongoldTaskTreeVersion=1.3.1 eclipseLsp4jVersion=0.15.0 eclipseLsp4jJsonrpcVersion=0.15.0 @@ -75,7 +66,6 @@ guavaVersion=30.0-jre guruNidiGraphvizVersion=0.18.1 harbbyGradleServiceloaderVersion=1.1.5 hdrHistogramVersion=2.1.11 -hikariCPVersion=3.3.1 hsqldbVersion=2.4.1 jacksonDatabindVersion=2.14.0 jacksonDataformatYamlVersion=2.13.2 @@ -92,11 +82,6 @@ javaTuples=1.2 javaxTransactionApiVersion=1.3 javaxMailVersion=1.6.2 javaxWsRsApi=2.1.1 -jaxenVersion=1.1.6 -jbossLoggingVersion=3.3.1.Final -jcraftJzlibVersion=1.1.3 -jetbrainsKotlinStdlibVersion=1.6.0 -jetbrainsKotlinStdlibCommonVersion=1.6.0 junitVersion=4.8.2 jknackHandlebarsVersion=4.0.6 jlineVersion=3.11.0 @@ -157,5 +142,4 @@ wso2SecurevaultVersion=1.0.0-wso2v2 wso2TransportHttpVersion=6.3.11 wso2TransportLocalFileSystemVersion=6.0.55 wso2StaxonCoreVersion=1.2.0.wso2v2 -wso2CommonsPoolVersion=1.5.6.wso2v1 zafarkhajaJsemverVersion=0.9.0 diff --git a/gradle/javaLibsProject.gradle b/gradle/javaLibsProject.gradle index 66b31237c09e..30a2eadd3687 100644 --- a/gradle/javaLibsProject.gradle +++ b/gradle/javaLibsProject.gradle @@ -28,8 +28,6 @@ dependencies { dist "com.squareup.okio:okio:${project.squareupOkioVersion}" dist "io.jaegertracing:jaeger-core:${project.jaegerCoreVersion}" dist "io.jaegertracing:jaeger-thrift:${project.jaegerThriftVersion}" - dist "com.zaxxer:HikariCP:${project.hikariCPVersion}" - dist "io.dropwizard.metrics:metrics-core:${project.dropwizardMetricsCoreVersion}" dist "javax.transaction:javax.transaction-api:${project.javaxTransactionApiVersion}" dist "org.quartz-scheduler:quartz-jobs:${project.quartzSchedulerJobsVersion}" dist "org.quartz-scheduler:quartz:${project.quartzSchedulerVersion}" @@ -38,37 +36,23 @@ dependencies { dist "org.wso2.transport.file:org.wso2.transport.local-file-system:${project.wso2TransportLocalFileSystemVersion}" dist "org.wso2.transport.http:org.wso2.transport.http.netty:${project.wso2TransportHttpVersion}" dist "info.picocli:picocli:${project.picocliVersion}" - dist "io.ballerina.messaging:broker-auth:${project.balMessagingBrokerAuthVersion}" - dist "io.ballerina.messaging:broker-common:${project.balMessagingBrokerCommonVersion}" - dist "io.ballerina.messaging:broker-coordination:${project.balMessagingBrokerCoordinationVersion}" - dist "io.ballerina.messaging:broker-core:${project.balMessagingBrokerCoreVersion}" - dist "io.ballerina.messaging:broker-rest-runner:${project.balMessagingBrokerRestRunnerVersion}" - dist "org.apache.geronimo.specs:geronimo-activation_1.1_spec:${project.apacheGeronimoActivationVersion}" dist "org.apache.geronimo.specs:geronimo-stax-api_1.0_spec:${project.apacheGeronimoStaxVersion}" dist "com.google.code.gson:gson:${project.gsonVersion}" dist "com.github.jknack:handlebars:${project.jknackHandlebarsVersion}" dist "com.sun.mail:javax.mail:${project.javaxMailVersion}" - dist "jaxen:jaxen:${project.jaxenVersion}" dist "io.netty:netty-buffer:${project.nettyBufferVersion}" - dist "io.netty:netty-codec-http2:${project.nettyCodecHttp2Version}" - dist "io.netty:netty-codec-http:${project.nettyCodecHttpVersion}" - dist "io.netty:netty-codec:${project.nettyCodecVersion}" dist "io.netty:netty-common:${project.nettyCommonVersion}" dist "io.netty:netty-handler-proxy:${project.nettyHandlerProxyVersion}" dist "io.netty:netty-handler:${project.nettyHandlerVersion}" dist "io.netty:netty-resolver:${project.nettyResolverVersion}" dist "io.netty:netty-transport:${project.nettyTransportVersion}" - dist "commons-pool.wso2:commons-pool:${project.wso2CommonsPoolVersion}" dist "org.wso2.carbon.messaging:org.wso2.carbon.messaging:${project.wso2CarbonMessagingVersion}" dist "org.wso2.carbon.metrics:org.wso2.carbon.metrics.core:${project.wso2CarbonMetricsVersion}" dist "com.sun.mail:javax.mail:${project.javaxMailVersion}" dist "org.yaml:snakeyaml:${project.snakeyamlVersion}" dist "org.wso2.staxon:staxon-core:${project.wso2StaxonCoreVersion}" - dist "com.jcraft:jzlib:${project.jcraftJzlibVersion}" dist "commons-beanutils:commons-beanutils:${project.commonsBeanutilsVersion}" - dist "org.jboss.logging:jboss-logging:${project.jbossLoggingVersion}" dist "commons-collections:commons-collections:${project.commonsCollectionsVersion}" - dist "org.apache.geronimo.specs:geronimo-json_1.0_spec:${project.apacheGeronimoJsonVersion}" dist "io.netty:netty-transport-native-epoll:${project.nettyTransportNativeEpollVersion}" dist "io.netty:netty-transport-native-kqueue:${project.nettyTransportNativeKqueueVersion}" dist "org.apache.ws.commons.axiom:axiom-api:${project.apacheCommonsAxiomApiVersion}" From 6b4a6dd41a7ae9865aaf690aa3408cc40f86e722 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Wed, 28 Jun 2023 12:10:30 +0530 Subject: [PATCH 31/70] Remove dependency imports in test resources --- .../java/io/ballerina/cli/cmd/NewCommandTest.java | 15 +++++---------- .../1.0.2/any/modules/centralSample.mod1/mod1.bal | 8 ++------ .../1.0.2/any/modules/centralSample/main.bal | 2 +- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 89cfbda1e426..7f1346ed1640 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -22,9 +22,6 @@ import io.ballerina.projects.util.FileUtils; import io.ballerina.projects.util.ProjectConstants; import io.ballerina.projects.util.ProjectUtils; -import org.ballerinalang.test.BCompileUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -35,6 +32,7 @@ import java.io.File; import java.io.IOException; +import java.io.PrintStream; import java.nio.charset.StandardCharsets; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -58,8 +56,7 @@ public class NewCommandTest extends BaseCommandTest { Path testResources; Path centralCache; - - private static final Logger logger = LoggerFactory.getLogger(BCompileUtil.class); + private PrintStream errStream = System.err; @DataProvider(name = "invalidProjectNames") public Object[][] provideInvalidProjectNames() { @@ -651,17 +648,15 @@ public void testNewCommandCentralTemplateReplaceImports() throws IOException { Assert.assertTrue(mainContent.contains("import central_sample.mod1;")); Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.PACKAGE_MD_FILE_NAME))); Assert.assertTrue(readOutput().contains("Created new package")); - logger.info(System.getProperty("user.dir")); + errStream.println("setting user directory"); System.setProperty("user.dir", packageDir.toString()); - logger.info(System.getProperty("user.dir")); + errStream.println(System.getProperty("user.dir")); BuildCommand buildCommand = new BuildCommand(packageDir, printStream, printStream, false); new CommandLine(buildCommand).parseArgs(); try { buildCommand.execute(); } catch (BLauncherException e) { - logger.error(e.getStackTrace().toString()); - logger.error(e.getMessage()); - logger.error(e.getDetailedMessages().toString()); + errStream.println(e.getDetailedMessages().toString()); } String buildLog = readOutput(true); Assert.assertTrue(buildLog.contains("Generating executable"));; diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample.mod1/mod1.bal b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample.mod1/mod1.bal index 286b25df4812..126bd5947d20 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample.mod1/mod1.bal +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample.mod1/mod1.bal @@ -1,10 +1,6 @@ -import ballerina/io; # Returns the string `Hello` with the input string name. # # + name - name as a string -public function hello(string name) { - if !(name is "") { - io:println("Hello, " + name); - } - io:println("Hello, World!"); +public function hello(string name) returns string{ + return "Hello " + name; } diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample/main.bal b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample/main.bal index 142eb8fee68d..5a2141c2d03f 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample/main.bal +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/centralSample/1.0.2/any/modules/centralSample/main.bal @@ -1,5 +1,5 @@ import centralSample.mod1; public function main() { - mod1:hello("Ballerina"); + string msg = mod1:hello("Ballerina"); } From 6b83814243052df8e8ae89f5f63bbd086f081de5 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 6 Jul 2023 00:06:51 +0530 Subject: [PATCH 32/70] Revert workflow changes --- .github/workflows/daily_build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/daily_build.yml b/.github/workflows/daily_build.yml index 72ddf3962ffc..482d1b471a9c 100644 --- a/.github/workflows/daily_build.yml +++ b/.github/workflows/daily_build.yml @@ -1,7 +1,6 @@ name: Ballerina daily build on: - workflow_dispatch: schedule: - cron: '30 18 * * *' # 00:00 in LK time (GMT+5:30) - cron: '30 6 * * *' # 12:00 in LK time (GMT+5:30) From 1b5ed93776bded61b6ad7ca3c642693732e566f5 Mon Sep 17 00:00:00 2001 From: Tharik Kanaka Date: Thu, 6 Jul 2023 10:42:33 +0530 Subject: [PATCH 33/70] Remove netty dependencies --- gradle/javaLibsProject.gradle | 2 -- gradle/javaProject.gradle | 10 ---------- 2 files changed, 12 deletions(-) diff --git a/gradle/javaLibsProject.gradle b/gradle/javaLibsProject.gradle index 30a2eadd3687..b1614293ad79 100644 --- a/gradle/javaLibsProject.gradle +++ b/gradle/javaLibsProject.gradle @@ -42,8 +42,6 @@ dependencies { dist "com.sun.mail:javax.mail:${project.javaxMailVersion}" dist "io.netty:netty-buffer:${project.nettyBufferVersion}" dist "io.netty:netty-common:${project.nettyCommonVersion}" - dist "io.netty:netty-handler-proxy:${project.nettyHandlerProxyVersion}" - dist "io.netty:netty-handler:${project.nettyHandlerVersion}" dist "io.netty:netty-resolver:${project.nettyResolverVersion}" dist "io.netty:netty-transport:${project.nettyTransportVersion}" dist "org.wso2.carbon.messaging:org.wso2.carbon.messaging:${project.wso2CarbonMessagingVersion}" diff --git a/gradle/javaProject.gradle b/gradle/javaProject.gradle index 7cc35254b6fa..d9460eaa94ce 100644 --- a/gradle/javaProject.gradle +++ b/gradle/javaProject.gradle @@ -48,7 +48,6 @@ dependencies { implementation "com.squareup.okio:okio:${project.squareupOkioVersion}" implementation "io.jaegertracing:jaeger-core:${project.jaegerCoreVersion}" implementation "io.jaegertracing:jaeger-thrift:${project.jaegerThriftVersion}" - implementation "com.zaxxer:HikariCP:${project.hikariCPVersion}" implementation "com.github.spullara.mustache.java:compiler:${project.mustacheJavaCompilerVersion}" implementation "org.bitbucket.cowwoc:diff-match-patch:${project.bitbucketCowwocVersion}" implementation "guru.nidi:graphviz-java:${project.guruNidiGraphvizVersion}" @@ -98,17 +97,8 @@ dependencies { implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${project.jacksonDatatypeJsr310Version}" implementation "info.picocli:picocli:${project.picocliVersion}" - implementation "io.ballerina.messaging:broker-common:${project.balMessagingBrokerCommonVersion}" - implementation "io.ballerina.messaging:broker-core:${project.balMessagingBrokerCoreVersion}" implementation "io.ballerina.messaging:broker-amqp:${project.balMessagingBrokerAmqpVersion}" - implementation "io.dropwizard.metrics:metrics-core:${project.dropwizardMetricsCoreVersion}" - implementation "io.netty:netty-codec:${project.nettyCodecVersion}" implementation "io.netty:netty-buffer:${project.nettyBufferVersion}" - implementation "io.netty:netty-common:${project.nettyCommonVersion}" - implementation "io.netty:netty-codec-http:${project.nettyCodecHttpVersion}" - implementation "io.netty:netty-codec-http2:${project.nettyCodecHttp2Version}" - implementation "io.netty:netty-handler:${project.nettyHandlerVersion}" - implementation "io.netty:netty-transport:${project.nettyTransportVersion}" implementation "io.opentelemetry:opentelemetry-api:${project.openTelemetryApiVersion}" implementation "io.opentelemetry:opentelemetry-sdk-trace:${project.openTelemetrySdkTraceVersion}" implementation "io.opentelemetry:opentelemetry-sdk-testing:${project.openTelemetrySdkTestingVersion}" From bfcd883ee50e6260df118d96d74f382e45a7b06c Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 6 Jul 2023 15:21:16 +0530 Subject: [PATCH 34/70] Remove logs --- .../src/test/java/io/ballerina/cli/cmd/NewCommandTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 7f1346ed1640..f1b458f75ec3 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -648,9 +648,8 @@ public void testNewCommandCentralTemplateReplaceImports() throws IOException { Assert.assertTrue(mainContent.contains("import central_sample.mod1;")); Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.PACKAGE_MD_FILE_NAME))); Assert.assertTrue(readOutput().contains("Created new package")); - errStream.println("setting user directory"); + System.setProperty("user.dir", packageDir.toString()); - errStream.println(System.getProperty("user.dir")); BuildCommand buildCommand = new BuildCommand(packageDir, printStream, printStream, false); new CommandLine(buildCommand).parseArgs(); try { @@ -659,7 +658,7 @@ public void testNewCommandCentralTemplateReplaceImports() throws IOException { errStream.println(e.getDetailedMessages().toString()); } String buildLog = readOutput(true); - Assert.assertTrue(buildLog.contains("Generating executable"));; + Assert.assertTrue(buildLog.contains("Generating executable")); } @Test From 17a928ff0d7c2fc21ec9788f1becd33126221a27 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Mon, 10 Jul 2023 12:31:44 +0530 Subject: [PATCH 35/70] Reduce creating type defns for const annotations --- .../compiler/bir/writer/BIRWriterUtils.java | 19 +++++++++----- .../analyzer/ConstantValueResolver.java | 25 +++++++++++++++---- .../annotations/DisplayAnnotationTest.java | 2 +- .../test/bala/annotation/AnnotationTests.java | 7 +----- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java index d9727b0f949e..0e718d0d80db 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java @@ -220,17 +220,22 @@ public static BIRNode.BIRAnnotationAttachment createBIRAnnotationAttachment( } public static BIRNode.ConstValue getBIRConstantVal(BLangConstantValue constValue) { - int tag = constValue.type.tag; + BType refferedType = Types.getReferredType(constValue.type); + int tag = refferedType.tag; + boolean isIntersection = false; if (tag == TypeTags.INTERSECTION) { - constValue.type = ((BIntersectionType) constValue.type).effectiveType; - tag = constValue.type.tag; + refferedType = ((BIntersectionType) refferedType).effectiveType; + tag = refferedType.tag; + isIntersection = true; } if (tag == TypeTags.RECORD) { Map mapConstVal = new HashMap<>(); ((Map) constValue.value) .forEach((key, value) -> mapConstVal.put(key, getBIRConstantVal(value))); - return new BIRNode.ConstValue(mapConstVal, ((BRecordType) constValue.type).getIntersectionType().get()); + return isIntersection ? + new BIRNode.ConstValue(mapConstVal, ((BRecordType) refferedType).getIntersectionType().get()) + : new BIRNode.ConstValue(mapConstVal, refferedType); } if (tag == TypeTags.TUPLE) { @@ -239,9 +244,11 @@ public static BIRNode.ConstValue getBIRConstantVal(BLangConstantValue constValue for (int exprIndex = 0; exprIndex < constantValueList.size(); exprIndex++) { tupleConstVal[exprIndex] = getBIRConstantVal(constantValueList.get(exprIndex)); } - return new BIRNode.ConstValue(tupleConstVal, ((BTupleType) constValue.type).getIntersectionType().get()); + return isIntersection ? + new BIRNode.ConstValue(tupleConstVal, ((BTupleType) refferedType).getIntersectionType().get()) + : new BIRNode.ConstValue(tupleConstVal, refferedType); } - return new BIRNode.ConstValue(constValue.value, constValue.type); + return new BIRNode.ConstValue(constValue.value, refferedType); } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java index f0c68289294c..d92673835337 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java @@ -85,6 +85,7 @@ import java.util.Stack; import java.util.function.BiFunction; +import static org.ballerinalang.model.symbols.SymbolOrigin.SOURCE; import static org.ballerinalang.model.symbols.SymbolOrigin.VIRTUAL; /** @@ -666,8 +667,8 @@ private void updateConstantType(BConstantSymbol symbol, BLangExpression expr, Sy return; } - if (resolvedType.getKind() == TypeKind.INTERSECTION && isListOrMapping(type.tag)) { - expr.setBType(((BIntersectionType) resolvedType).effectiveType); + if (resolvedType.getKind() == TypeKind.RECORD && isListOrMapping(type.tag)) { + expr.setBType(resolvedType); symbol.type = resolvedType; symbol.literalType = resolvedType; symbol.value.type = resolvedType; @@ -845,9 +846,23 @@ private BType createRecordType(BLangExpression expr, BConstantSymbol constantSym } createTypeDefinition(recordType, pos, env); - BIntersectionType intersectionType = ImmutableTypeCloner.getImmutableIntersectionType(pos, types, - recordType, env, symTable, anonymousModelHelper, names, new HashSet<>()); - return intersectionType; + updateRecordFields(recordType, pos, env); + recordType.tsymbol.flags |= Flags.READONLY; + recordType.flags |= Flags.READONLY; + return recordType; + } + + private void updateRecordFields(BRecordType recordType, Location pos, SymbolEnv env) { + BTypeSymbol structureSymbol = recordType.tsymbol; + for (BField field : recordType.fields.values()) { + field.type = ImmutableTypeCloner.getImmutableType(pos, types, field.type, env, + pkgID, env.scope.owner, symTable, anonymousModelHelper, names, + new HashSet<>()); + Name fieldName = field.symbol.name; + field.symbol = new BVarSymbol(field.symbol.flags | Flags.READONLY, fieldName, + pkgID, field.type, structureSymbol, pos, SOURCE); + structureSymbol.scope.define(fieldName, field.symbol); + } } private boolean populateRecordFields(BLangExpression expr, BConstantSymbol constantSymbol, Location pos, diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/DisplayAnnotationTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/DisplayAnnotationTest.java index adb7d514e159..7b97ddcd707a 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/DisplayAnnotationTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/DisplayAnnotationTest.java @@ -90,7 +90,7 @@ public void testDisplayAnnotOnObjectAndMemberFunction() { @Test public void testDisplayAnnotOnRecord() { - TypeDefinition typeDefinition = result.getAST().getTypeDefinitions().get(23); + TypeDefinition typeDefinition = result.getAST().getTypeDefinitions().get(13); List annot = typeDefinition.getAnnotationAttachments(); Assert.assertEquals(annot.size(), 1); Assert.assertEquals(annot.get(0).getExpression().toString(), diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/annotation/AnnotationTests.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/annotation/AnnotationTests.java index 68c04f5fc95c..38597644ff0f 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/annotation/AnnotationTests.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/annotation/AnnotationTests.java @@ -278,10 +278,7 @@ public void testParamAnnotAttachmentsViaBir() { Assert.assertEquals(mapValue.size(), 1); Assert.assertEquals(mapValue.get("i").value, 1L); BType type = constAttachmentSymbol.attachmentValueSymbol.type; - Assert.assertEquals(type.tag, TypeTags.INTERSECTION); - BIntersectionType intersectionType = (BIntersectionType) type; - BType effectiveType = intersectionType.effectiveType; - Assert.assertEquals(effectiveType.tag, TypeTags.RECORD); + Assert.assertEquals(type.tag, TypeTags.RECORD); params = detachMethod.symbol.params; annotationAttachmentSymbols = params.get(0).getAnnotations(); @@ -303,8 +300,6 @@ public void testParamAnnotAttachmentsViaBir() { Assert.assertEquals(mapValue.size(), 1); Assert.assertTrue(members.remove(mapValue.get("i").value)); type = constAttachmentSymbol.attachmentValueSymbol.type; - Assert.assertEquals(type.tag, TypeTags.INTERSECTION); - type = ((BIntersectionType) type).effectiveType; Assert.assertEquals(type.tag, TypeTags.RECORD); } } From eb1b9fd899991475f8029c2cfbeaecab3e8be016 Mon Sep 17 00:00:00 2001 From: Kanushka Gayan Date: Wed, 5 Jul 2023 17:42:37 +0530 Subject: [PATCH 36/70] Fix class level visible endpoint missing issue --- .../diagramutil/SyntaxTreeMapGenerator.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/misc/diagram-util/src/main/java/org/ballerinalang/diagramutil/SyntaxTreeMapGenerator.java b/misc/diagram-util/src/main/java/org/ballerinalang/diagramutil/SyntaxTreeMapGenerator.java index 1b8db081de75..2e362365665e 100644 --- a/misc/diagram-util/src/main/java/org/ballerinalang/diagramutil/SyntaxTreeMapGenerator.java +++ b/misc/diagram-util/src/main/java/org/ballerinalang/diagramutil/SyntaxTreeMapGenerator.java @@ -297,9 +297,14 @@ protected JsonElement transformSyntaxNode(Node node) { nodeJson.add("typeData", symbolJson); - if ((node.kind() == SyntaxKind.BLOCK_STATEMENT || node.kind() == SyntaxKind.FUNCTION_BODY_BLOCK || - node.kind() == SyntaxKind.SERVICE_DECLARATION) && (this.visibleEpsForEachBlock.size() > 0 || - this.visibleEpsForModule.size() > 0)) { + boolean isBlockNode = node.kind() == SyntaxKind.BLOCK_STATEMENT + || node.kind() == SyntaxKind.FUNCTION_BODY_BLOCK + || node.kind() == SyntaxKind.SERVICE_DECLARATION; + boolean hasVisibleEps = this.visibleEpsForEachBlock.size() > 0 + || this.visibleEpsForClass.size() > 0 + || this.visibleEpsForModule.size() > 0; + + if (isBlockNode && hasVisibleEps) { JsonArray blockEndpoints = new JsonArray(); // Add module level endpoints From 4094aede4ca6bee599e620c1a07d4a7ed40433cf Mon Sep 17 00:00:00 2001 From: Kanushka Gayan Date: Wed, 5 Jul 2023 17:43:11 +0530 Subject: [PATCH 37/70] Add test cases to capture class level endpoints --- .../diagramutil/SyntaxTreeGenTest.java | 37 +++++++++++++++++++ .../resources/classEndpoint/Ballerina.toml | 4 ++ .../src/test/resources/classEndpoint/main.bal | 15 ++++++++ 3 files changed, 56 insertions(+) create mode 100644 misc/diagram-util/src/test/resources/classEndpoint/Ballerina.toml create mode 100644 misc/diagram-util/src/test/resources/classEndpoint/main.bal diff --git a/misc/diagram-util/src/test/java/org/ballerinalang/diagramutil/SyntaxTreeGenTest.java b/misc/diagram-util/src/test/java/org/ballerinalang/diagramutil/SyntaxTreeGenTest.java index 8ac5e6fa8f0a..5d8e377628a9 100644 --- a/misc/diagram-util/src/test/java/org/ballerinalang/diagramutil/SyntaxTreeGenTest.java +++ b/misc/diagram-util/src/test/java/org/ballerinalang/diagramutil/SyntaxTreeGenTest.java @@ -45,6 +45,7 @@ public class SyntaxTreeGenTest { private final Path externalClientInitOnly = TestUtil.RES_DIR.resolve("externalClientInitOnly"); private final Path multiLevelEndpoints = TestUtil.RES_DIR.resolve("multiLevelEndpoints"); private final Path endpointOrder = TestUtil.RES_DIR.resolve("endpointOrder"); + private final Path classEndpoint = TestUtil.RES_DIR.resolve("classEndpoint"); @Test(description = "Generate ST for empty bal file.") public void testEmptyBalST() throws IOException { @@ -689,6 +690,42 @@ public void testVisibleEndpointOrder() throws IOException { "http", "http", "2.8.0", 74, 74, false, true, true, false); } + @Test(description = "Test visible endpoint defined in Class/Service level") + public void testClassLevelVisibleEndpoint() throws IOException { + Path inputFile = TestUtil.createTempProject(classEndpoint); + + BuildProject project = BuildProject.load(inputFile); + Optional optionalModuleId = project.currentPackage().moduleIds().stream().findFirst(); + if (optionalModuleId.isEmpty()) { + Assert.fail("Failed to retrieve the module ID"); + } + ModuleId moduleId = optionalModuleId.get(); + Module module = project.currentPackage().module(moduleId); + PackageCompilation packageCompilation = project.currentPackage().getCompilation(); + SemanticModel semanticModel = packageCompilation.getSemanticModel(moduleId); + Optional optionalDocumentId = module.documentIds().stream() + .filter(documentId -> module.document(documentId).name().equals("main.bal")).findFirst(); + if (optionalDocumentId.isEmpty()) { + Assert.fail("Failed to retrieve the document ID"); + } + DocumentId documentId = optionalDocumentId.get(); + Document document = module.document(documentId); + JsonElement stJson = DiagramUtil.getSyntaxTreeJSON(document, semanticModel); + Assert.assertTrue(stJson.isJsonObject()); + + Assert.assertEquals(stJson.getAsJsonObject().get("kind").getAsString(), "ModulePart"); + JsonArray members = stJson.getAsJsonObject().get("members").getAsJsonArray(); + + // Verify user service + JsonObject userService = members.get(0).getAsJsonObject(); + JsonObject userPostFunction = userService.get("members").getAsJsonArray().get(2).getAsJsonObject(); + JsonArray userPostFunctionVEp = userPostFunction.get("functionBody").getAsJsonObject().get("VisibleEndpoints") + .getAsJsonArray(); + Assert.assertEquals(userPostFunctionVEp.size(), 1); + checkClientVisibleEndpoints(userPostFunctionVEp.get(0).getAsJsonObject(), "httpEpS10", "Client", "ballerina", + "http", "http", "2.8.0", 4, 4, false, true, true, false); + } + private void checkClientVisibleEndpoints(JsonObject ep, String varName, String typeName, String orgName, String packageName, String moduleName, String version, int startLine, int endLine, boolean isModuleVar, boolean isExternal, boolean isClassField, diff --git a/misc/diagram-util/src/test/resources/classEndpoint/Ballerina.toml b/misc/diagram-util/src/test/resources/classEndpoint/Ballerina.toml new file mode 100644 index 000000000000..8e9606d5b30b --- /dev/null +++ b/misc/diagram-util/src/test/resources/classEndpoint/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "gayanOrg" +name = "EndpintOrder" +version = "0.1.0" diff --git a/misc/diagram-util/src/test/resources/classEndpoint/main.bal b/misc/diagram-util/src/test/resources/classEndpoint/main.bal new file mode 100644 index 000000000000..75fcf853ffee --- /dev/null +++ b/misc/diagram-util/src/test/resources/classEndpoint/main.bal @@ -0,0 +1,15 @@ +import ballerina/http; + +service /user on new http:Listener(9090) { + + http:Client httpEpS10; + + function init() returns error? { + self.httpEpS10 = check new (url = ""); + } + + resource function post .() returns error? { + + } +} + From b0d1106a81025c682e5bed5fab042f4de3f086cb Mon Sep 17 00:00:00 2001 From: malinthar Date: Mon, 10 Jul 2023 17:47:17 +0530 Subject: [PATCH 38/70] Improve compiler plugin completion manager --- .../ballerina/projects/CompletionManager.java | 97 +++++++++++++++---- .../plugins/LanguageServerExtensionTests.java | 81 +++++++++++++++- .../Ballerina.toml | 4 - .../Ballerina.toml | 4 + .../main.bal | 0 .../Ballerina.toml | 4 + .../main.bal | 12 +++ .../Ballerina.toml | 4 + .../main.bal | 10 ++ .../Ballerina.toml | 4 + .../main.bal | 11 +++ .../Ballerina.toml | 4 + .../main.bal | 7 ++ .../Ballerina.toml | 4 + .../main.bal | 8 ++ 15 files changed, 228 insertions(+), 26 deletions(-) delete mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_1/Ballerina.toml rename project-api/project-api-test/src/test/resources/compiler_plugin_tests/{package_plugin_user_with_completions => package_plugin_user_with_completions_1}/main.bal (100%) create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_2/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_2/main.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_3/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_3/main.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_4/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_4/main.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_5/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_5/main.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_6/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_6/main.bal diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CompletionManager.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CompletionManager.java index 879f6ea30546..e07073431b17 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CompletionManager.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CompletionManager.java @@ -22,8 +22,13 @@ import io.ballerina.compiler.api.symbols.TypeReferenceTypeSymbol; import io.ballerina.compiler.api.symbols.TypeSymbol; import io.ballerina.compiler.api.symbols.UnionTypeSymbol; +import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode; +import io.ballerina.compiler.syntax.tree.MethodDeclarationNode; import io.ballerina.compiler.syntax.tree.Node; +import io.ballerina.compiler.syntax.tree.NodeVisitor; +import io.ballerina.compiler.syntax.tree.ObjectFieldNode; import io.ballerina.compiler.syntax.tree.ServiceDeclarationNode; +import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode; import io.ballerina.compiler.syntax.tree.SyntaxKind; import io.ballerina.projects.plugins.completion.CompletionContext; import io.ballerina.projects.plugins.completion.CompletionException; @@ -165,9 +170,12 @@ private TypeSymbol getRawType(TypeSymbol typeDescriptor) { return typeDescriptor; } - private boolean isInServiceBodyNodeContext(CompletionContext context, Node referenceNode) { + private boolean isInServiceBodyNodeContext(CompletionContext context, + Node referenceNode) { Optional cursorPosition = context.cursorPosition(); - if (referenceNode.kind() != SyntaxKind.SERVICE_DECLARATION || cursorPosition.isEmpty()) { + Node nodeAtCursor = context.nodeAtCursor(); + if (referenceNode.kind() != SyntaxKind.SERVICE_DECLARATION + || cursorPosition.isEmpty()) { return false; } @@ -184,22 +192,9 @@ private boolean isInServiceBodyNodeContext(CompletionContext context, Node refer return false; } - /* Covers - service on new http:Listener(9090) { - - r - - resource function get test(http:Caller caller, http:Request req) { - - } - } - */ - return serviceDeclarationNode.members().stream() - .filter(member -> member.kind() == SyntaxKind.RESOURCE_ACCESSOR_DEFINITION - || member.kind() == SyntaxKind.FUNCTION_DEFINITION) - .noneMatch(member -> member.lineRange().startLine().line() <= cursorLine - && cursorLine <= member.lineRange().endLine().line()); - + ServiceDeclarationContextValidator validator = new ServiceDeclarationContextValidator(context); + validator.visitNode(nodeAtCursor); + return validator.isValidContext(); } /** @@ -225,4 +220,70 @@ public CompilerPluginInfo compilerPluginInfo() { } } + /** + * Visitor to validate the completion context. + * + */ + static class ServiceDeclarationContextValidator extends NodeVisitor { + + private CompletionContext context; + private boolean isValidContext = false; + + public ServiceDeclarationContextValidator(CompletionContext context) { + this.context = context; + } + + @Override + public void visit(ServiceDeclarationNode serviceDeclarationNode) { + isValidContext = true; + } + + @Override + public void visit(SimpleNameReferenceNode simpleNameReferenceNode) { + simpleNameReferenceNode.parent().accept(this); + } + + @Override + public void visit(ObjectFieldNode objectFieldNode) { + int cursorPosition = context.cursorPosInTree(); + isValidContext = objectFieldNode.textRange().startOffset() <= cursorPosition + && cursorPosition <= objectFieldNode.textRange().endOffset() + && objectFieldNode.fieldName().isMissing() + && objectFieldNode.equalsToken().isEmpty(); + } + + @Override + public void visit(FunctionDefinitionNode functionDefinitionNode) { + int cursorLine = context.cursorPosition().get().line(); + isValidContext = cursorLine < functionDefinitionNode.lineRange().startLine().line() + || functionDefinitionNode.lineRange().endLine().line() < cursorLine; + } + + @Override + public void visit(MethodDeclarationNode methodDeclarationNode) { + int cursorLine = context.cursorPosition().get().line(); + isValidContext = cursorLine < methodDeclarationNode.lineRange().startLine().line() + || methodDeclarationNode.lineRange().endLine().line() < cursorLine; + } + + @Override + protected void visitSyntaxNode(Node node) { + //Do nothing + } + + public Boolean isValidContext() { + return this.isValidContext; + } + + public void visitNode(Node node) { + if (node.kind() == SyntaxKind.LIST) { + if (node.parent() != null) { + node.parent().accept(this); + } + } else { + node.accept(this); + } + } + } + } diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/LanguageServerExtensionTests.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/LanguageServerExtensionTests.java index 55ad658fb2d3..32810faa8eb8 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/LanguageServerExtensionTests.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/LanguageServerExtensionTests.java @@ -19,6 +19,10 @@ import io.ballerina.compiler.api.symbols.TypeSymbol; import io.ballerina.compiler.syntax.tree.ModulePartNode; import io.ballerina.compiler.syntax.tree.Node; +import io.ballerina.compiler.syntax.tree.NodeList; +import io.ballerina.compiler.syntax.tree.ObjectFieldNode; +import io.ballerina.compiler.syntax.tree.ServiceDeclarationNode; +import io.ballerina.compiler.syntax.tree.SyntaxKind; import io.ballerina.compiler.syntax.tree.SyntaxTree; import io.ballerina.projects.CodeActionManager; import io.ballerina.projects.CodeActionResult; @@ -53,6 +57,7 @@ import org.mockito.Mockito; import org.testng.Assert; import org.testng.annotations.BeforeSuite; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import org.wso2.ballerinalang.compiler.diagnostic.BLangDiagnostic; import org.wso2.ballerinalang.compiler.diagnostic.BLangDiagnosticLocation; @@ -148,9 +153,9 @@ public void testOneCompilerPluginWithOneCodeAction() { })); } - @Test - public void testOneCompilerPluginWithOneCompletionProvider() { - String path = RESOURCE_DIRECTORY.resolve("package_plugin_user_with_completions").toString(); + @Test(dataProvider = "completion-data-provider") + public void testOneCompilerPluginWithOneCompletionProvider(String sourceDir, int line, int offset) { + String path = RESOURCE_DIRECTORY.resolve(sourceDir).toString(); CompileResult result = BCompileUtil.compileAndCacheBala(path); Project project = result.project(); PackageCompilation packageCompilation = project.currentPackage().getCompilation(); @@ -163,7 +168,7 @@ public void testOneCompilerPluginWithOneCompletionProvider() { //Get the service declaration node Node nodeAtCursor = ((ModulePartNode) document.syntaxTree().rootNode()).members().get(1); - LinePosition cursorPos = LinePosition.from(5, 5); + LinePosition cursorPos = LinePosition.from(line, offset); int cursorPositionInTree = document.textDocument().textPositionFrom(cursorPos); CompletionContext completionContext = CompletionContextImpl.from(filePath.toUri().toString(), filePath, cursorPos, cursorPositionInTree, nodeAtCursor, document, @@ -190,4 +195,72 @@ public void testOneCompilerPluginWithOneCompletionProvider() { CompletionUtil.LINE_BREAK) && edit.range().startOffset() == nodeAtCursor.textRange().startOffset() && edit.range().length() == 0); } + + @Test + public void testOneCompilerPluginWithOneCompletionProviderNegative1() { + String path = RESOURCE_DIRECTORY.resolve("package_plugin_user_with_completions_5").toString(); + CompileResult result = BCompileUtil.compileAndCacheBala(path); + Project project = result.project(); + PackageCompilation packageCompilation = project.currentPackage().getCompilation(); + CompletionManager completionManager = packageCompilation.getCompletionManager(); + Path filePath = Paths.get(path, "main.bal"); + DocumentId documentId = project.documentId(filePath); + Module module = project.currentPackage().module(documentId.moduleId()); + Document document = module.document(documentId); + + //Select list constructor node as the node at cursor + Node serviceNode = ((ModulePartNode) document.syntaxTree().rootNode()).members().get(1); + Assert.assertSame(serviceNode.kind(), SyntaxKind.SERVICE_DECLARATION); + NodeList members = ((ServiceDeclarationNode) serviceNode).members(); + + Assert.assertTrue(!members.isEmpty() && members.get(0).kind() == SyntaxKind.OBJECT_FIELD + && ((ObjectFieldNode) members.get(0)).expression().isPresent()); + Node nodeAtCursor = ((ObjectFieldNode) members.get(0)).expression().get(); + + LinePosition cursorPos = LinePosition.from(5, 20); + int cursorPositionInTree = document.textDocument().textPositionFrom(cursorPos); + CompletionContext completionContext = CompletionContextImpl.from(filePath.toUri().toString(), + filePath, cursorPos, cursorPositionInTree, nodeAtCursor, document, + module.getCompilation().getSemanticModel()); + + CompletionResult completionResult = completionManager.completions(completionContext); + Assert.assertTrue(completionResult.getCompletionItems().isEmpty()); + } + + @Test + public void testOneCompilerPluginWithOneCompletionProviderNegative2() { + String path = RESOURCE_DIRECTORY.resolve("package_plugin_user_with_completions_6").toString(); + CompileResult result = BCompileUtil.compileAndCacheBala(path); + Project project = result.project(); + PackageCompilation packageCompilation = project.currentPackage().getCompilation(); + CompletionManager completionManager = packageCompilation.getCompletionManager(); + Path filePath = Paths.get(path, "main.bal"); + DocumentId documentId = project.documentId(filePath); + Module module = project.currentPackage().module(documentId.moduleId()); + Document document = module.document(documentId); + + //Select list constructor node as the node at cursor + Node serviceNode = ((ModulePartNode) document.syntaxTree().rootNode()).members().get(1); + + LinePosition cursorPos = LinePosition.from(4, 21); + int cursorPositionInTree = document.textDocument().textPositionFrom(cursorPos); + CompletionContext completionContext = CompletionContextImpl.from(filePath.toUri().toString(), + filePath, cursorPos, cursorPositionInTree, serviceNode, document, + module.getCompilation().getSemanticModel()); + + CompletionResult completionResult = completionManager.completions(completionContext); + Assert.assertTrue(completionResult.getCompletionItems().isEmpty()); + } + + + @DataProvider(name = "completion-data-provider") + public Object[][] completionDataProvider() { + return new Object[][]{ + {"package_plugin_user_with_completions_1", 5, 5}, + {"package_plugin_user_with_completions_2", 7, 3}, + {"package_plugin_user_with_completions_3", 5, 4}, + {"package_plugin_user_with_completions_4", 9, 3} + }; + } + } diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions/Ballerina.toml deleted file mode 100644 index 1bbd3b74c3bf..000000000000 --- a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "lstest" -name = "package_plugin_user_with_completions" -version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_1/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_1/Ballerina.toml new file mode 100644 index 000000000000..2185956ea369 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_1/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "lstest" +name = "package_plugin_user_with_completions_1" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_1/main.bal similarity index 100% rename from project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions/main.bal rename to project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_1/main.bal diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_2/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_2/Ballerina.toml new file mode 100644 index 000000000000..2b13a16d7959 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_2/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "lstest" +name = "package_plugin_user_with_completions_2" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_2/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_2/main.bal new file mode 100644 index 000000000000..8595ca64be86 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_2/main.bal @@ -0,0 +1,12 @@ +import lstest/package_comp_plugin_with_completions as foo; + +public listener listener1 = new foo:Listener(9090); + +service on listener1 { + int[] a = []; + + + remote function name() { + + } +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_3/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_3/Ballerina.toml new file mode 100644 index 000000000000..16f1310923a5 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_3/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "lstest" +name = "package_plugin_user_with_completions_3" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_3/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_3/main.bal new file mode 100644 index 000000000000..2aba674dc311 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_3/main.bal @@ -0,0 +1,10 @@ +import lstest/package_comp_plugin_with_completions as foo; + +public listener listener1 = new foo:Listener(9090); + +service on listener1 { + r + remote function name() { + + } +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_4/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_4/Ballerina.toml new file mode 100644 index 000000000000..be5ad1c0c0aa --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_4/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "lstest" +name = "package_plugin_user_with_completions_4" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_4/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_4/main.bal new file mode 100644 index 000000000000..6826842c80a0 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_4/main.bal @@ -0,0 +1,11 @@ +import lstest/package_comp_plugin_with_completions as foo; + +public listener listener1 = new foo:Listener(9090); + +service on listener1 { + remote function name() { + + } + + +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_5/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_5/Ballerina.toml new file mode 100644 index 000000000000..2b13a16d7959 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_5/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "lstest" +name = "package_plugin_user_with_completions_2" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_5/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_5/main.bal new file mode 100644 index 000000000000..a7686f0176b5 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_5/main.bal @@ -0,0 +1,7 @@ +import lstest/package_comp_plugin_with_completions as foo; + +public listener listener1 = new foo:Listener(9090); + +service on listener1 { + string[] arr = [] +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_6/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_6/Ballerina.toml new file mode 100644 index 000000000000..f47b940cbd6d --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_6/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "lstest" +name = "package_plugin_user_with_completions_6" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_6/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_6/main.bal new file mode 100644 index 000000000000..909bef75acb0 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/package_plugin_user_with_completions_6/main.bal @@ -0,0 +1,8 @@ +import lstest/package_comp_plugin_with_completions as foo; + +public listener listener1 = new foo:Listener(9090); + +service on listener1 { + +} + From 9158364945cea1ddb32112fa58e7b89d909b0887 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Tue, 11 Jul 2023 08:41:01 +0530 Subject: [PATCH 39/70] Update failing tests --- .../ConstAnnotationAttachmentSymbolTest.java | 16 +++++----------- .../api/test/symbols/ConstDeclSymbolTest.java | 16 +++++----------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/symbols/ConstAnnotationAttachmentSymbolTest.java b/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/symbols/ConstAnnotationAttachmentSymbolTest.java index 82cbdace2ddb..d32ac10fa8eb 100644 --- a/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/symbols/ConstAnnotationAttachmentSymbolTest.java +++ b/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/symbols/ConstAnnotationAttachmentSymbolTest.java @@ -21,7 +21,6 @@ import io.ballerina.compiler.api.SemanticModel; import io.ballerina.compiler.api.impl.values.BallerinaConstantValue; import io.ballerina.compiler.api.symbols.AnnotationAttachmentSymbol; -import io.ballerina.compiler.api.symbols.IntersectionTypeSymbol; import io.ballerina.compiler.api.symbols.MemberTypeSymbol; import io.ballerina.compiler.api.symbols.RecordTypeSymbol; import io.ballerina.compiler.api.symbols.Symbol; @@ -80,14 +79,11 @@ public void testValuesInConstantAnnotationAttachment() { ConstantValue constVal = annotAttachment.attachmentValue().get(); // Test type-descriptor - assertEquals(constVal.valueType().typeKind(), TypeDescKind.INTERSECTION); - assertEquals(((IntersectionTypeSymbol) constVal.valueType()).memberTypeDescriptors().get(0).typeKind(), - TypeDescKind.RECORD); - assertEquals(((IntersectionTypeSymbol) constVal.valueType()).memberTypeDescriptors().get(1).typeKind(), - TypeDescKind.READONLY); + assertEquals(constVal.valueType().typeKind(), TypeDescKind.RECORD); RecordTypeSymbol recTypeSymbol = - (RecordTypeSymbol) ((IntersectionTypeSymbol) constVal.valueType()).memberTypeDescriptors().get(0); - assertEquals(recTypeSymbol.signature(), "record {|1 id; record {|1 a; 2 b;|} perm;|}"); + (RecordTypeSymbol) constVal.valueType(); + assertEquals(recTypeSymbol.signature(), "record {|readonly 1 id; readonly record " + + "{|readonly 1 a; readonly 2 b;|} perm;|}"); // Test const value assertTrue(constVal.value() instanceof HashMap); @@ -101,9 +97,7 @@ public void testValuesInConstantAnnotationAttachment() { assertTrue(valueMap.get("perm") instanceof BallerinaConstantValue); BallerinaConstantValue permValue = (BallerinaConstantValue) valueMap.get("perm"); - assertEquals(permValue.valueType().typeKind(), TypeDescKind.INTERSECTION); - assertEquals(((IntersectionTypeSymbol) permValue.valueType()).effectiveTypeDescriptor().typeKind(), - TypeDescKind.RECORD); + assertEquals(permValue.valueType().typeKind(), TypeDescKind.RECORD); assertTrue(permValue.value() instanceof HashMap); HashMap permMap = (HashMap) permValue.value(); assertEquals(((BallerinaConstantValue) permMap.get("a")).value(), 1L); diff --git a/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/symbols/ConstDeclSymbolTest.java b/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/symbols/ConstDeclSymbolTest.java index f4bb23475198..40d797784ea1 100644 --- a/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/symbols/ConstDeclSymbolTest.java +++ b/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/symbols/ConstDeclSymbolTest.java @@ -24,7 +24,6 @@ import io.ballerina.compiler.api.symbols.AnnotationSymbol; import io.ballerina.compiler.api.symbols.ConstantSymbol; import io.ballerina.compiler.api.symbols.Documentation; -import io.ballerina.compiler.api.symbols.IntersectionTypeSymbol; import io.ballerina.compiler.api.symbols.Qualifier; import io.ballerina.compiler.api.symbols.RecordTypeSymbol; import io.ballerina.compiler.api.symbols.Symbol; @@ -160,14 +159,11 @@ public void testValuesInConstantAnnotationAttachment() { ConstantValue constVal = annotAttachment.attachmentValue().get(); // Test type-descriptor - assertEquals(constVal.valueType().typeKind(), TypeDescKind.INTERSECTION); - assertEquals(((IntersectionTypeSymbol) constVal.valueType()).memberTypeDescriptors().get(0).typeKind(), - TypeDescKind.RECORD); - assertEquals(((IntersectionTypeSymbol) constVal.valueType()).memberTypeDescriptors().get(1).typeKind(), - TypeDescKind.READONLY); + assertEquals(constVal.valueType().typeKind(), TypeDescKind.RECORD); RecordTypeSymbol recTypeSymbol = - (RecordTypeSymbol) ((IntersectionTypeSymbol) constVal.valueType()).memberTypeDescriptors().get(0); - assertEquals(recTypeSymbol.signature(), "record {|1 id; record {|1 a; 2 b;|} perm;|}"); + (RecordTypeSymbol) constVal.valueType(); + assertEquals(recTypeSymbol.signature(), "record {|readonly 1 id; readonly record " + + "{|readonly 1 a; readonly 2 b;|} perm;|}"); // Test const value assertTrue(constVal.value() instanceof HashMap); @@ -182,9 +178,7 @@ public void testValuesInConstantAnnotationAttachment() { assertTrue(valueMap.get("perm") instanceof BallerinaConstantValue); BallerinaConstantValue permValue = (BallerinaConstantValue) valueMap.get("perm"); - assertEquals(permValue.valueType().typeKind(), TypeDescKind.INTERSECTION); - assertEquals(((IntersectionTypeSymbol) permValue.valueType()).effectiveTypeDescriptor().typeKind(), - TypeDescKind.RECORD); + assertEquals(permValue.valueType().typeKind(), TypeDescKind.RECORD); assertTrue(permValue.value() instanceof HashMap); HashMap permMap = (HashMap) permValue.value(); assertEquals(((BallerinaConstantValue) permMap.get("a")).value(), 1L); From ca9413d98c3a2fbb4ce9519f348e0e17c8148703 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Tue, 11 Jul 2023 12:41:02 +0530 Subject: [PATCH 40/70] Fix SpotBugs failure --- .../compiler/bir/writer/BIRWriterUtils.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java index 0e718d0d80db..546fd9e02e3e 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java @@ -220,12 +220,12 @@ public static BIRNode.BIRAnnotationAttachment createBIRAnnotationAttachment( } public static BIRNode.ConstValue getBIRConstantVal(BLangConstantValue constValue) { - BType refferedType = Types.getReferredType(constValue.type); - int tag = refferedType.tag; + BType constValType = constValue.type; + int tag = constValue.type.tag; boolean isIntersection = false; - if (tag == TypeTags.INTERSECTION) { - refferedType = ((BIntersectionType) refferedType).effectiveType; - tag = refferedType.tag; + if (constValType.tag == TypeTags.INTERSECTION) { + constValType = ((BIntersectionType) constValType).effectiveType; + tag = constValType.tag; isIntersection = true; } @@ -234,8 +234,8 @@ public static BIRNode.ConstValue getBIRConstantVal(BLangConstantValue constValue ((Map) constValue.value) .forEach((key, value) -> mapConstVal.put(key, getBIRConstantVal(value))); return isIntersection ? - new BIRNode.ConstValue(mapConstVal, ((BRecordType) refferedType).getIntersectionType().get()) - : new BIRNode.ConstValue(mapConstVal, refferedType); + new BIRNode.ConstValue(mapConstVal, ((BRecordType) constValType).getIntersectionType().get()) + : new BIRNode.ConstValue(mapConstVal, constValType); } if (tag == TypeTags.TUPLE) { @@ -245,10 +245,10 @@ public static BIRNode.ConstValue getBIRConstantVal(BLangConstantValue constValue tupleConstVal[exprIndex] = getBIRConstantVal(constantValueList.get(exprIndex)); } return isIntersection ? - new BIRNode.ConstValue(tupleConstVal, ((BTupleType) refferedType).getIntersectionType().get()) - : new BIRNode.ConstValue(tupleConstVal, refferedType); + new BIRNode.ConstValue(tupleConstVal, ((BTupleType) constValType).getIntersectionType().get()) + : new BIRNode.ConstValue(tupleConstVal, constValType); } - return new BIRNode.ConstValue(constValue.value, refferedType); + return new BIRNode.ConstValue(constValue.value, constValType); } } From 87301b8f7529038d99392160f32867a40fbe73bf Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Tue, 11 Jul 2023 12:51:14 +0530 Subject: [PATCH 41/70] Remove typeDefinition for source annotation in jar --- .../compiler/bir/codegen/JvmPackageGen.java | 5 +++++ .../semantics/analyzer/ConstantValueResolver.java | 9 +++++++-- .../semantics/analyzer/SymbolResolver.java | 14 ++++++++++++-- .../java/org/wso2/ballerinalang/util/Flags.java | 1 + 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java index 319e80eb2d28..fe2ca9923003 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java @@ -785,6 +785,7 @@ CompiledJarFile generate(BIRPackage module, boolean isEntry) { // generate the shutdown listener class. new ShutDownListenerGen().generateShutdownSignalListener(moduleInitClass, jarEntries); + removeSourceAnnotationTypeDefs(module.typeDefs); // desugar the record init function rewriteRecordInits(module.typeDefs); @@ -811,6 +812,10 @@ CompiledJarFile generate(BIRPackage module, boolean isEntry) { return new CompiledJarFile(getModuleLevelClassName(module.packageID, MODULE_INIT_CLASS_NAME, "."), jarEntries); } + private void removeSourceAnnotationTypeDefs(List typeDefs) { + typeDefs.removeIf(def -> Symbols.isFlagOn(def.flags, Flags.SOURCE_ANNOTATION)); + } + private BIRFunction getMainFunction(BIRPackage module) { BIRFunction mainFunc = null; if (module.packageID.skipTests) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java index d92673835337..29bd869a23c0 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java @@ -565,12 +565,13 @@ private BLangConstantValue calculateBooleanComplement(BLangConstantValue value) BLangConstantValue constructBLangConstantValueWithExactType(BLangExpression expression, BConstantSymbol constantSymbol, SymbolEnv env) { - return constructBLangConstantValueWithExactType(expression, constantSymbol, env, new Stack<>()); + return constructBLangConstantValueWithExactType(expression, constantSymbol, env, new Stack<>(), false); } BLangConstantValue constructBLangConstantValueWithExactType(BLangExpression expression, BConstantSymbol constantSymbol, SymbolEnv env, - Stack anonTypeNameSuffixes) { + Stack anonTypeNameSuffixes, + boolean isSourceOnlyAnon) { BLangConstantValue value = constructBLangConstantValue(expression); constantSymbol.value = value; @@ -580,6 +581,10 @@ BLangConstantValue constructBLangConstantValueWithExactType(BLangExpression expr this.anonTypeNameSuffixes = anonTypeNameSuffixes; updateConstantType(constantSymbol, expression, env); + BLangTypeDefinition typeDefinition = createdTypeDefinitions.get(constantSymbol.type.tsymbol); + if (isSourceOnlyAnon && typeDefinition != null) { + typeDefinition.symbol.flags |= Flags.SOURCE_ANNOTATION; + } return value; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolResolver.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolResolver.java index 0cb70e0aad9a..85693a7b62c4 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolResolver.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolResolver.java @@ -20,6 +20,7 @@ import io.ballerina.tools.diagnostics.DiagnosticCode; import io.ballerina.tools.diagnostics.Location; import org.ballerinalang.model.TreeBuilder; +import org.ballerinalang.model.elements.AttachPoint; import org.ballerinalang.model.elements.Flag; import org.ballerinalang.model.elements.PackageID; import org.ballerinalang.model.symbols.SymbolKind; @@ -2552,7 +2553,7 @@ public void populateAnnotationAttachmentSymbol(BLangAnnotationAttachment annotat attachedType = Types.getReferredType(attachedType); attachedType = attachedType.tag == TypeTags.ARRAY ? ((BArrayType) attachedType).eType : attachedType; } - + boolean isSourceOnlyAnon = isSourceAnonOnly(annotationSymbol.points); BConstantSymbol constantSymbol = new BConstantSymbol(0, Names.EMPTY, Names.EMPTY, env.enclPkg.packageID, attachedType, attachedType, env.scope.owner, annotationAttachment.pos, VIRTUAL); @@ -2575,7 +2576,7 @@ public void populateAnnotationAttachmentSymbol(BLangAnnotationAttachment annotat } } else { constAnnotationValue = constantValueResolver.constructBLangConstantValueWithExactType(expr, constantSymbol, - env, anonTypeNameSuffixes); + env, anonTypeNameSuffixes, isSourceOnlyAnon); } constantSymbol.type = constAnnotationValue.type; @@ -2588,6 +2589,15 @@ public void populateAnnotationAttachmentSymbol(BLangAnnotationAttachment annotat constantSymbol); } + private boolean isSourceAnonOnly(Set attachPoints) { + for (AttachPoint attachPoint : attachPoints) { + if (!attachPoint.source) { + return false; + } + } + return true; + } + public Set getConfigVarSymbolsIncludingImportedModules(BPackageSymbol packageSymbol) { Set configVars = new HashSet<>(); populateConfigurableVars(packageSymbol, configVars); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/util/Flags.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/util/Flags.java index ccc9cf1dc1c9..a1dfdd6d0b99 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/util/Flags.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/util/Flags.java @@ -82,6 +82,7 @@ public class Flags { public static final long ENUM_MEMBER = INFER << 1; // 41 public static final long QUERY_LAMBDA = ENUM_MEMBER << 1; // 42 public static final long EFFECTIVE_TYPE_DEF = QUERY_LAMBDA << 1; // 43 + public static final long SOURCE_ANNOTATION = EFFECTIVE_TYPE_DEF << 1; // 44 public static long asMask(Set flagSet) { long mask = 0; From db56c15ef5d9ac36d4f31b6c72dab6b0985cde36 Mon Sep 17 00:00:00 2001 From: Fathima Dilhasha Date: Tue, 11 Jul 2023 16:56:32 +0530 Subject: [PATCH 42/70] Proioritize the use of graalvmCompatible property in Ballerina.toml --- .../src/main/java/io/ballerina/projects/BalaWriter.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java index 3279b84b839b..bf516709bbc9 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java @@ -190,10 +190,8 @@ private void addPackageJson(ZipOutputStream balaOutputStream, Optional Date: Wed, 12 Jul 2023 07:40:32 +0530 Subject: [PATCH 43/70] Improve error msg based on review suggestions --- .../util/diagnostic/DiagnosticErrorCode.java | 2 +- .../semantics/analyzer/CodeAnalyzer.java | 4 +-- .../analyzer/ConstantTypeChecker.java | 6 ++-- .../compiler/semantics/analyzer/Types.java | 3 +- .../src/main/resources/compiler.properties | 4 +-- ...otationExpressionCodeAnalysisNegative.java | 2 +- .../MappingConstructorExprTest.java | 32 +++++++++---------- .../test/record/RecordDefNegativeTest.java | 6 ++-- .../test/types/constant/ConstantTypeTest.java | 2 +- .../constant/MapConstantNegativeTest.java | 12 +++---- .../map/MapInitializerExprNegativeTest.java | 6 ++-- 11 files changed, 41 insertions(+), 38 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java index 8319d56f1ce0..59cf113480c3 100644 --- a/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java +++ b/compiler/ballerina-lang/src/main/java/org/ballerinalang/util/diagnostic/DiagnosticErrorCode.java @@ -331,7 +331,7 @@ public enum DiagnosticErrorCode implements DiagnosticCode { INVALID_RECORD_LITERAL("BCE2575", "invalid.record.literal"), INVALID_FIELD_IN_RECORD_BINDING_PATTERN("BCE2576", "invalid.field.in.record.binding.pattern"), INVALID_RECORD_LITERAL_BINDING_PATTERN("BCE2577", "invalid.record.literal.in.binding.pattern"), - DUPLICATE_KEY_IN_RECORD_LITERAL("BCE2578", "duplicate.key.in.record.literal"), + DUPLICATE_KEY_IN_MAPPING_CONSTRUCTOR("BCE2578", "duplicate.key.in.mapping.constructor"), DUPLICATE_KEY_IN_TABLE_LITERAL("BCE2579", "duplicate.key.in.table.literal"), DUPLICATE_KEY_IN_RECORD_LITERAL_SPREAD_OP("BCE2580", "duplicate.key.in.record.literal.spread.op"), POSSIBLE_DUPLICATE_OF_FIELD_SPECIFIED_VIA_SPREAD_OP( diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java index 22547e0ef872..1aa515723573 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/CodeAnalyzer.java @@ -2356,7 +2356,7 @@ public void visit(BLangRecordLiteral recordLiteral, AnalyzerData data) { String name = ((BLangSimpleVarRef) keyExpr).variableName.value; String unescapedName = Utils.unescapeJava(name); if (names.contains(unescapedName)) { - this.dlog.error(keyExpr.pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_RECORD_LITERAL, + this.dlog.error(keyExpr.pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_MAPPING_CONSTRUCTOR, Types.getReferredType(recordLiteral.expectedType).getKind().typeName(), unescapedName); } else if (inclusiveTypeSpreadField != null && !neverTypedKeys.contains(unescapedName)) { @@ -2374,7 +2374,7 @@ public void visit(BLangRecordLiteral recordLiteral, AnalyzerData data) { } else if (keyExpr.getKind() == NodeKind.LITERAL || keyExpr.getKind() == NodeKind.NUMERIC_LITERAL) { Object name = ((BLangLiteral) keyExpr).value; if (names.contains(name)) { - this.dlog.error(keyExpr.pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_RECORD_LITERAL, + this.dlog.error(keyExpr.pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_MAPPING_CONSTRUCTOR, Types.getReferredType(recordLiteral.parent.getBType()) .getKind().typeName(), name); } else if (inclusiveTypeSpreadField != null && !neverTypedKeys.contains(name)) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java index fe718ac94d95..20801e1c33bb 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java @@ -539,6 +539,9 @@ private BType checkMappingConstructorCompatibility(BType expType, BLangRecordLit } } + /** + * This method is similar to reportIncompatibleMappingConstructorError method in TypeChecker.java. + */ private void reportIncompatibleMappingConstructorError(BLangRecordLiteral mappingConstructorExpr, BType expType) { if (expType == symTable.semanticError) { return; @@ -2022,8 +2025,7 @@ private boolean addFields(LinkedHashMap fields, BType keyValueTy BRecordTypeSymbol recordSymbol) { Name fieldName = Names.fromString(key); if (fields.containsKey(key)) { - dlog.error(pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_RECORD_LITERAL, TypeKind.RECORD.typeName(), - key); + dlog.error(pos, DiagnosticErrorCode.DUPLICATE_KEY_IN_MAPPING_CONSTRUCTOR, TypeKind.RECORD.typeName(), key); return false; } long flags = recordSymbol.flags | Flags.REQUIRED; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java index 9b0a0548d5a4..b50335700f31 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java @@ -7282,6 +7282,7 @@ public boolean isContainSubtypeOfInt(BType type) { } public boolean isMappingConstructorCompatibleType(BType type) { - return Types.getReferredType(type).tag == TypeTags.RECORD || Types.getReferredType(type).tag == TypeTags.MAP; + int tag = getReferredType(type).tag; + return tag == TypeTags.RECORD || tag == TypeTags.MAP; } } diff --git a/compiler/ballerina-lang/src/main/resources/compiler.properties b/compiler/ballerina-lang/src/main/resources/compiler.properties index 180201655d73..6f1afc4226e4 100644 --- a/compiler/ballerina-lang/src/main/resources/compiler.properties +++ b/compiler/ballerina-lang/src/main/resources/compiler.properties @@ -1172,8 +1172,8 @@ error.invalid.any.var.def=\ error.invalid.record.literal=\ invalid usage of record literal with type ''{0}'' -error.duplicate.key.in.record.literal=\ - invalid usage of {0} literal: duplicate key ''{1}'' +error.duplicate.key.in.mapping.constructor=\ + invalid {0} constructor: duplicate key ''{1}'' error.duplicate.key.in.table.literal=\ duplicate key found in table row key(''{0}'') : ''{1}'' diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationExpressionCodeAnalysisNegative.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationExpressionCodeAnalysisNegative.java index e5c7b8a96b40..03845db2fa72 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationExpressionCodeAnalysisNegative.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnnotationExpressionCodeAnalysisNegative.java @@ -46,7 +46,7 @@ public void testAttachmentExpression() { "keys, expected a string literal or an expression", 26, 5); BAssertUtil.validateError(compileResult, i++, "invalid usage of record literal: duplicate key 'i' via spread " + "operator '...fl'", 49, 8); - BAssertUtil.validateError(compileResult, i++, "invalid usage of record literal: duplicate key 's'", 50, 5); + BAssertUtil.validateError(compileResult, i++, "invalid record constructor: duplicate key 's'", 50, 5); Assert.assertEquals(compileResult.getErrorCount(), i); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/expressions/mappingconstructor/MappingConstructorExprTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/expressions/mappingconstructor/MappingConstructorExprTest.java index 9fc5014b65d5..09380f3d2537 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/expressions/mappingconstructor/MappingConstructorExprTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/expressions/mappingconstructor/MappingConstructorExprTest.java @@ -107,10 +107,10 @@ public void testVarNameFieldCodeAnalysisNegative() { CompileResult result = BCompileUtil.compile( "test-src/expressions/mappingconstructor/var_name_field_code_analysis_negative.bal"); Assert.assertEquals(result.getErrorCount(), 4); - validateError(result, 0, "invalid usage of record literal: duplicate key 's'", 26, 17); - validateError(result, 1, "invalid usage of record literal: duplicate key 'i'", 26, 23); - validateError(result, 2, "invalid usage of map literal: duplicate key 'i'", 27, 34); - validateError(result, 3, "invalid usage of map literal: duplicate key 'i'", 27, 42); + validateError(result, 0, "invalid record constructor: duplicate key 's'", 26, 17); + validateError(result, 1, "invalid record constructor: duplicate key 'i'", 26, 23); + validateError(result, 2, "invalid map constructor: duplicate key 'i'", 27, 34); + validateError(result, 3, "invalid map constructor: duplicate key 'i'", 27, 42); } @Test(enabled = false) @@ -174,14 +174,14 @@ public void testSpreadOpFieldCodeAnalysisNegative() { int i = 0; validateError(result, i++, "invalid usage of record literal: duplicate key 'i' via spread operator '...f'", 30, 31); - validateError(result, i++, "invalid usage of record literal: duplicate key 's'", 30, 34); + validateError(result, i++, "invalid record constructor: duplicate key 's'", 30, 34); validateError(result, i++, "invalid usage of map literal: duplicate key 's' via spread operator " + "'...b'", 31, 47); - validateError(result, i++, "invalid usage of map literal: duplicate key 'f'", 31, 50); - validateError(result, i++, "invalid usage of map literal: duplicate key 'i'", 31, 58); + validateError(result, i++, "invalid map constructor: duplicate key 'f'", 31, 50); + validateError(result, i++, "invalid map constructor: duplicate key 'i'", 31, 58); validateError(result, i++, "invalid usage of map literal: duplicate key 's' via spread operator " + "'... {s: hi,i: 1}'", 32, 38); - validateError(result, i++, "invalid usage of map literal: duplicate key 'i'", 32, 63); + validateError(result, i++, "invalid map constructor: duplicate key 'i'", 32, 63); validateError(result, i++, "invalid usage of map literal: duplicate key 'i' via spread " + "operator '...alpha'", 41, 27); validateError(result, i++, "invalid usage of mapping constructor expression: spread field " + @@ -400,16 +400,16 @@ public void testDuplicateFieldWithEscapeSequence() { "test-src/expressions/mappingconstructor/mapping_constructor_duplicate_fields.bal"); int index = 0; - validateError(compileResult, index++, "invalid usage of map literal: duplicate key 'a\\'", 23, 29); - validateError(compileResult, index++, "invalid usage of map literal: duplicate key 'a\\'", 24, 31); - validateError(compileResult, index++, "invalid usage of map literal: duplicate key 'a\\'", 26, 33); - validateError(compileResult, index++, "invalid usage of map literal: duplicate key 'a\\'", 27, 33); - validateError(compileResult, index++, "invalid usage of map literal: duplicate key 'a\\'", 29, 29); - validateError(compileResult, index++, "invalid usage of map literal: duplicate key 'a{'", 30, 29); - validateError(compileResult, index++, "invalid usage of map literal: duplicate key 'field['", 33, 31); + validateError(compileResult, index++, "invalid map constructor: duplicate key 'a\\'", 23, 29); + validateError(compileResult, index++, "invalid map constructor: duplicate key 'a\\'", 24, 31); + validateError(compileResult, index++, "invalid map constructor: duplicate key 'a\\'", 26, 33); + validateError(compileResult, index++, "invalid map constructor: duplicate key 'a\\'", 27, 33); + validateError(compileResult, index++, "invalid map constructor: duplicate key 'a\\'", 29, 29); + validateError(compileResult, index++, "invalid map constructor: duplicate key 'a{'", 30, 29); + validateError(compileResult, index++, "invalid map constructor: duplicate key 'field['", 33, 31); validateError(compileResult, index++, "invalid usage of map literal: duplicate key 'field[' via " + "spread operator '...recVar1'", 34, 37); - validateError(compileResult, index++, "invalid usage of map literal: duplicate key 'field('", 37, 28); + validateError(compileResult, index++, "invalid map constructor: duplicate key 'field('", 37, 28); Assert.assertEquals(compileResult.getErrorCount(), index); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/RecordDefNegativeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/RecordDefNegativeTest.java index cf927f38de66..63659e0b4894 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/RecordDefNegativeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/RecordDefNegativeTest.java @@ -37,12 +37,12 @@ public class RecordDefNegativeTest { public void duplicateKeyTest() { CompileResult compileResult = BCompileUtil.compile("test-src/record/negative/duplicate-key-negative.bal"); int errorIndex = 0; - BAssertUtil.validateError(compileResult, errorIndex++, "invalid usage of record literal: duplicate key 'name'", + BAssertUtil.validateError(compileResult, errorIndex++, "invalid record constructor: duplicate key 'name'", 26, 10); BAssertUtil.validateWarning(compileResult, errorIndex++, "unused variable 'hi'", 31, 5); - BAssertUtil.validateError(compileResult, errorIndex++, "invalid usage of record literal: duplicate key 'name'", + BAssertUtil.validateError(compileResult, errorIndex++, "invalid record constructor: duplicate key 'name'", 35, 9); - BAssertUtil.validateError(compileResult, errorIndex++, "invalid usage of record literal: duplicate key 'name'", + BAssertUtil.validateError(compileResult, errorIndex++, "invalid record constructor: duplicate key 'name'", 43, 10); Assert.assertEquals(compileResult.getDiagnostics().length, errorIndex); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/ConstantTypeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/ConstantTypeTest.java index b02c0dcd34c3..5ce5aa0192f1 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/ConstantTypeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/ConstantTypeTest.java @@ -73,7 +73,7 @@ public void constExpressionSemanticAnalysisNegative() { BAssertUtil.validateError(compileResult1, i++, "incompatible types: expected 'TYPE4', found '3'", 44, 16); BAssertUtil.validateError(compileResult1, i++, "incompatible types: expected 'TYPE5', found 'false'", 45, 16); BAssertUtil.validateError(compileResult1, i++, "incompatible types: expected 'TYPE6', found '\"12\"'", 46, 16); - BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'b'", 63, 46); + BAssertUtil.validateError(compileResult1, i++, "invalid record constructor: duplicate key 'b'", 63, 46); BAssertUtil.validateError(compileResult1, i++, "incompatible types: expected 'TYPE12', found " + "'(record {| record {| record {| 1 a; |} b; |} a; (record {| (record {| 1 a; |} & readonly) a; " + "(record {| 2 b; 3 c; |} & readonly) CMI2; record {| 1 d; |} c; |} & readonly) b; |} & readonly)'", diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantNegativeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantNegativeTest.java index 2313805a82a0..29abb5557b22 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantNegativeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/constant/MapConstantNegativeTest.java @@ -39,7 +39,7 @@ public void testNegative() { validateError(compileResult, i++, "incompatible types: expected 'string', found 'other'", 20, 27); validateError(compileResult, i++, "expression is not a constant expression", 20, 38); validateError(compileResult, i++, "cannot update constant value", 34, 5); - validateError(compileResult, i++, "invalid usage of record literal: duplicate key 'a'", 39, 34); + validateError(compileResult, i++, "invalid record constructor: duplicate key 'a'", 39, 34); validateError(compileResult, i++, "illegal cyclic reference '[B1]'", 46, 1); validateError(compileResult, i++, "ambiguous type '(map|map)'", 48, 41); validateError(compileResult, i++, "incompatible mapping constructor expression for type " + @@ -54,11 +54,11 @@ public void constMapSpreadFieldNegative() { CompileResult compileResult1 = BCompileUtil.compile( "test-src/types/constant/constant_map_spread_field_negative.bal"); int i = 0; - BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'b'", 21, 46); - BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'b'", 22, 36); - BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'b'", 23, 33); - BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'b'", 24, 36); - BAssertUtil.validateError(compileResult1, i++, "invalid usage of record literal: duplicate key 'c'", 24, 36); + BAssertUtil.validateError(compileResult1, i++, "invalid record constructor: duplicate key 'b'", 21, 46); + BAssertUtil.validateError(compileResult1, i++, "invalid record constructor: duplicate key 'b'", 22, 36); + BAssertUtil.validateError(compileResult1, i++, "invalid record constructor: duplicate key 'b'", 23, 33); + BAssertUtil.validateError(compileResult1, i++, "invalid record constructor: duplicate key 'b'", 24, 36); + BAssertUtil.validateError(compileResult1, i++, "invalid record constructor: duplicate key 'c'", 24, 36); Assert.assertEquals(compileResult1.getErrorCount(), i); } } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/map/MapInitializerExprNegativeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/map/MapInitializerExprNegativeTest.java index b7117623635b..cba0af704001 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/map/MapInitializerExprNegativeTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/map/MapInitializerExprNegativeTest.java @@ -41,17 +41,17 @@ public void setup() { @Test(description = "Test map initializer expression with duplicated keys") public void mapInitWithDuplicatedKeysTest() { - BAssertUtil.validateError(compileResult, 0, "invalid usage of map literal: duplicate key 'key'", 2, 40); + BAssertUtil.validateError(compileResult, 0, "invalid map constructor: duplicate key 'key'", 2, 40); } @Test(description = "Test map initializer expression with duplicated keys when one key is a string literal") public void mapInitWithDuplicatedKeysOneStringKeyTest() { - BAssertUtil.validateError(compileResult, 1, "invalid usage of map literal: duplicate key 'key'", 7, 40); + BAssertUtil.validateError(compileResult, 1, "invalid map constructor: duplicate key 'key'", 7, 40); } @Test(description = "Test map initializer expression with duplicated keys when both keys are string literals") public void mapInitWithDuplicatedKeysBothStringKeysTest() { - BAssertUtil.validateError(compileResult, 2, "invalid usage of map literal: duplicate key 'key'", 12, 42); + BAssertUtil.validateError(compileResult, 2, "invalid map constructor: duplicate key 'key'", 12, 42); } @AfterClass From 54b8f31f1c7de8abb0cecbc841d61ebdf8f556ed Mon Sep 17 00:00:00 2001 From: malinthar Date: Wed, 12 Jul 2023 10:01:39 +0530 Subject: [PATCH 44/70] Address review suggestions --- .../src/main/java/io/ballerina/projects/CompletionManager.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CompletionManager.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CompletionManager.java index e07073431b17..ba53edad7e5f 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CompletionManager.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CompletionManager.java @@ -173,7 +173,6 @@ private TypeSymbol getRawType(TypeSymbol typeDescriptor) { private boolean isInServiceBodyNodeContext(CompletionContext context, Node referenceNode) { Optional cursorPosition = context.cursorPosition(); - Node nodeAtCursor = context.nodeAtCursor(); if (referenceNode.kind() != SyntaxKind.SERVICE_DECLARATION || cursorPosition.isEmpty()) { return false; @@ -193,7 +192,7 @@ private boolean isInServiceBodyNodeContext(CompletionContext context, } ServiceDeclarationContextValidator validator = new ServiceDeclarationContextValidator(context); - validator.visitNode(nodeAtCursor); + validator.visitNode(context.nodeAtCursor()); return validator.isValidContext(); } From ae1ad75ebabca1a7f2acc2e385d47bfed07ba101 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Wed, 12 Jul 2023 11:05:21 +0530 Subject: [PATCH 45/70] Fix BIRSpecTest failures --- docs/bir-spec/src/main/resources/kaitai/bir.ksy | 1 + .../test/java/org/ballerinalang/birspec/BIRTestUtils.java | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/bir-spec/src/main/resources/kaitai/bir.ksy b/docs/bir-spec/src/main/resources/kaitai/bir.ksy index 08b726b9034b..0fd03ee4748d 100644 --- a/docs/bir-spec/src/main/resources/kaitai/bir.ksy +++ b/docs/bir-spec/src/main/resources/kaitai/bir.ksy @@ -723,6 +723,7 @@ types: 'type_tag_enum::type_tag_decimal': decimal_constant_info 'type_tag_enum::type_tag_boolean': boolean_constant_info 'type_tag_enum::type_tag_nil': nil_constant_info + 'type_tag_enum::type_tag_record': map_constant_info 'type_tag_enum::type_tag_intersection': intersection_constant_info instances: type: diff --git a/docs/bir-spec/src/test/java/org/ballerinalang/birspec/BIRTestUtils.java b/docs/bir-spec/src/test/java/org/ballerinalang/birspec/BIRTestUtils.java index 34ccdcdf18c1..1c0b93339202 100644 --- a/docs/bir-spec/src/test/java/org/ballerinalang/birspec/BIRTestUtils.java +++ b/docs/bir-spec/src/test/java/org/ballerinalang/birspec/BIRTestUtils.java @@ -742,8 +742,13 @@ private static void assertConstantValue(Bir.ConstantValue actualConstantValue, O Assert.assertEquals(booleanConstantInfo.valueBooleanConstant() == 1, expectedValue); break; case TYPE_TAG_RECORD: - Bir.MapConstantInfo actualMapConst = + Bir.MapConstantInfo actualMapConst; + if (constantValueInfo instanceof Bir.MapConstantInfo) { + actualMapConst = (Bir.MapConstantInfo) constantValueInfo; + } else { + actualMapConst = (Bir.MapConstantInfo) ((Bir.IntersectionConstantInfo) constantValueInfo).constantValueInfo(); + } Map expectedMapConst = (Map) expectedValue; Assert.assertEquals(actualMapConst.mapConstantSize(), expectedMapConst.size()); break; From 862134c9fea6ff2e4e574a7e9c68248b898b8abd Mon Sep 17 00:00:00 2001 From: Tharik Kanaka Date: Wed, 12 Jul 2023 11:26:00 +0530 Subject: [PATCH 46/70] Include kotlin dependency --- distribution/zip/jballerina-tools/build.gradle | 2 ++ gradle.properties | 2 ++ 2 files changed, 4 insertions(+) diff --git a/distribution/zip/jballerina-tools/build.gradle b/distribution/zip/jballerina-tools/build.gradle index ccd9d233e728..d765898d1703 100644 --- a/distribution/zip/jballerina-tools/build.gradle +++ b/distribution/zip/jballerina-tools/build.gradle @@ -80,6 +80,8 @@ dependencies { dist "org.apache.commons:commons-text:${project.apacheCommonsTextVersion}" dist "com.github.spullara.mustache.java:compiler:${project.spullaraMustacheCompilerVersion}" + dist "org.jetbrains.kotlin:kotlin-stdlib:${project.jetbrainsKotlinStdlibVersion}" + dist "org.jetbrains.kotlin:kotlin-stdlib-common:${project.jetbrainsKotlinStdlibCommonVersion}" dist "org.ow2.asm:asm:${project.ow2AsmVersion}" dist "org.ow2.asm:asm-analysis:${project.ow2AsmAnalysisVersion}" dist "org.ow2.asm:asm-tree:${project.ow2AsmTreeVersion}" diff --git a/gradle.properties b/gradle.properties index 3e90019400cd..dc9858a5db37 100644 --- a/gradle.properties +++ b/gradle.properties @@ -82,6 +82,8 @@ javaTuples=1.2 javaxTransactionApiVersion=1.3 javaxMailVersion=1.6.2 javaxWsRsApi=2.1.1 +jetbrainsKotlinStdlibVersion=1.6.0 +jetbrainsKotlinStdlibCommonVersion=1.6.0 junitVersion=4.8.2 jknackHandlebarsVersion=4.0.6 jlineVersion=3.11.0 From e6260300ee876d0e370ba9fae0b01c501a4483be Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Wed, 12 Jul 2023 14:49:46 +0530 Subject: [PATCH 47/70] Remove unwanted changes to fix list const tests --- .../ballerinalang/compiler/bir/writer/BIRWriterUtils.java | 4 +--- .../ballerinalang/test/bala/annotation/AnnotationTests.java | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java index 546fd9e02e3e..94a39e5f0df5 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRWriterUtils.java @@ -244,9 +244,7 @@ public static BIRNode.ConstValue getBIRConstantVal(BLangConstantValue constValue for (int exprIndex = 0; exprIndex < constantValueList.size(); exprIndex++) { tupleConstVal[exprIndex] = getBIRConstantVal(constantValueList.get(exprIndex)); } - return isIntersection ? - new BIRNode.ConstValue(tupleConstVal, ((BTupleType) constValType).getIntersectionType().get()) - : new BIRNode.ConstValue(tupleConstVal, constValType); + return new BIRNode.ConstValue(tupleConstVal, ((BTupleType) constValType).getIntersectionType().get()); } return new BIRNode.ConstValue(constValue.value, constValType); diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/annotation/AnnotationTests.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/annotation/AnnotationTests.java index 38597644ff0f..28eee157944c 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/annotation/AnnotationTests.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/annotation/AnnotationTests.java @@ -37,7 +37,6 @@ import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeDefinitionSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol; import org.wso2.ballerinalang.compiler.semantics.model.types.BField; -import org.wso2.ballerinalang.compiler.semantics.model.types.BIntersectionType; import org.wso2.ballerinalang.compiler.semantics.model.types.BRecordType; import org.wso2.ballerinalang.compiler.semantics.model.types.BTupleMember; import org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType; From 4aa5977a474893e5213e7c231ad60c6da8691e6c Mon Sep 17 00:00:00 2001 From: ShammiL Date: Wed, 12 Jul 2023 15:40:26 +0530 Subject: [PATCH 48/70] Add build and pack to tool tests --- .../io/ballerina/cli/cmd/CommandUtil.java | 3 +- .../create_cmd_templates/tool/main.bal | 3 + .../io/ballerina/cli/cmd/NewCommandTest.java | 64 +++++++++++++++++-- .../1.0.0/any/modules/toolProject/main.bal | 3 + .../toolProject/1.0.0/any/tool/bal-tool.json | 2 +- .../libs/platform-io-1.3.0-java.jar} | 0 .../any/tool/libs/platform-io-1.3.0-java.txt | 1 - .../unix/build-tool-template.txt | 5 ++ .../unix/pack-central-tool.txt | 5 ++ .../unix/pack-tool-template.txt | 5 ++ .../windows/build-tool-template.txt | 5 ++ .../windows/pack-central-tool.txt | 5 ++ .../windows/pack-tool-template.txt | 5 ++ .../test-jar-files/tool-test.jar | 0 .../io/ballerina/projects/BalaWriter.java | 2 +- 15 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 cli/ballerina-cli/src/main/resources/create_cmd_templates/tool/main.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/modules/toolProject/main.bal rename cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/{modules/toolProject/.keep => tool/libs/platform-io-1.3.0-java.jar} (100%) delete mode 100644 cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/libs/platform-io-1.3.0-java.txt create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-template.txt create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/pack-central-tool.txt create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/pack-tool-template.txt create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-template.txt create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-central-tool.txt create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-tool-template.txt create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/test-jar-files/tool-test.jar diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java index 703018dd07cb..eeb82016f86a 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CommandUtil.java @@ -75,6 +75,7 @@ import java.util.stream.Stream; import static io.ballerina.cli.launcher.LauncherUtils.createLauncherException; +import static io.ballerina.projects.util.ProjectConstants.BAL_TOOL_JSON; import static io.ballerina.projects.util.ProjectConstants.BAL_TOOL_TOML; import static io.ballerina.projects.util.ProjectConstants.DEPENDENCIES_TOML; import static io.ballerina.projects.util.ProjectConstants.DEPENDENCY_GRAPH_JSON; @@ -239,7 +240,7 @@ private static void addModules(Path balaPath, Path projectPath, String packageNa templateBalToolJson = gson.fromJson(fileReader, BalToolJson.class); } catch (IOException e) { printError(errStream, - "Error while reading the bal-tool json file: " + e.getMessage(), + "Error while reading the " + BAL_TOOL_JSON + " file: " + e.getMessage(), null, false); getRuntime().exit(1); diff --git a/cli/ballerina-cli/src/main/resources/create_cmd_templates/tool/main.bal b/cli/ballerina-cli/src/main/resources/create_cmd_templates/tool/main.bal new file mode 100644 index 000000000000..a6f89ee470ad --- /dev/null +++ b/cli/ballerina-cli/src/main/resources/create_cmd_templates/tool/main.bal @@ -0,0 +1,3 @@ +// AUTO-GENERATED FILE. + +// This file is auto-generated by Ballerina for managing modules in packages. diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index 882ccf7daf88..b0c08dccabcf 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -18,6 +18,7 @@ package io.ballerina.cli.cmd; +import io.ballerina.cli.launcher.BLauncherException; import io.ballerina.projects.util.FileUtils; import io.ballerina.projects.util.ProjectConstants; import io.ballerina.projects.util.ProjectUtils; @@ -31,6 +32,7 @@ import java.io.File; import java.io.IOException; +import java.io.PrintStream; import java.nio.charset.StandardCharsets; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -55,6 +57,7 @@ public class NewCommandTest extends BaseCommandTest { Path testResources; Path centralCache; + private PrintStream errStream = System.err; @DataProvider(name = "invalidProjectNames") public Object[][] provideInvalidProjectNames() { @@ -547,6 +550,36 @@ public void testNewCommandWithTool() throws IOException { Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.TOOL_DIR))); Assert.assertTrue(readOutput().contains("Created new package")); + + // Test building the package + System.setProperty("user.dir", packageDir.toString()); + BuildCommand buildCommand = new BuildCommand(packageDir, printStream, printStream, false); + new CommandLine(buildCommand).parseArgs(); + try { + buildCommand.execute(); + } catch (BLauncherException e) { + errStream.println(e.getDetailedMessages().toString()); + } + String buildLog = readOutput(true); + Assert.assertEquals(buildLog.replaceAll("\r", ""), getOutput("build-tool-template.txt")); + Assert.assertTrue(packageDir.resolve("target").resolve("bin").resolve("tool_sample.jar") + .toFile().exists()); + + // Test packing the package + Files.copy(testResources.resolve("test-jar-files").resolve("tool-test.jar"), + packageDir.resolve(TOOL_DIR).resolve("tool-test.jar")); + String newToolTomlContent = toolTomlContent + "path = \"tool" + File.separator + "tool-test.jar\"\n"; + Files.writeString(packageDir.resolve(ProjectConstants.BAL_TOOL_TOML), newToolTomlContent); + System.setProperty("user.dir", packageDir.toString()); + PackCommand packCommand = new PackCommand(packageDir, printStream, printStream, false, true); + new CommandLine(packCommand).parseArgs(); + packCommand.execute(); + String packBuildLog = readOutput(true); + + Assert.assertEquals(packBuildLog.replaceAll("\r", ""), getOutput("pack-tool-template.txt")); + Assert.assertTrue( + packageDir.resolve("target").resolve("bala") + .resolve("testuserorg-tool_sample-any-0.1.0.bala").toFile().exists()); } @Test(description = "Test new command with invalid project name", dataProvider = "invalidProjectNames") @@ -585,9 +618,7 @@ public void testNewCommandWithToolTemplateCentral() throws IOException { NewCommand newCommand = new NewCommand(printStream, false, homeCache); new CommandLine(newCommand).parseArgs(args); newCommand.execute(); - Assert.assertTrue(Files.exists(packageDir)); - Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.BALLERINA_TOML))); String expectedTomlContent = "[package]\n" + "org = \"testorg\"\n" + @@ -605,14 +636,39 @@ public void testNewCommandWithToolTemplateCentral() throws IOException { String expectedToolTomlContent = "[tool]\n" + "id = \"" + packageName + "\"\n\n" + "[[dependency]]\n" + - "path = \"tool/libs/platform-io-1.3.0-java.txt\"\n"; + "path = \"tool/libs/platform-io-1.3.0-java.jar\"\n"; Assert.assertTrue(toolTomlContent.contains(expectedToolTomlContent)); Assert.assertTrue(Files.exists(packageDir.resolve(ProjectConstants.PACKAGE_MD_FILE_NAME))); Path dependencyPath = packageDir.resolve(TOOL_DIR).resolve("libs") - .resolve("platform-io-1.3.0-java.txt"); + .resolve("platform-io-1.3.0-java.jar"); Assert.assertTrue(Files.exists(dependencyPath)); Assert.assertTrue(readOutput().contains("Created new package")); + + // Test building the package + System.setProperty("user.dir", packageDir.toString()); + BuildCommand buildCommand = new BuildCommand(packageDir, printStream, printStream, false); + new CommandLine(buildCommand).parseArgs(); + try { + buildCommand.execute(); + } catch (BLauncherException e) { + errStream.println(e.getDetailedMessages().toString()); + } + String buildLog = readOutput(true); + Assert.assertTrue(buildLog.contains("Generating executable")); + Assert.assertTrue(packageDir.resolve("target").resolve("bin").resolve("sample_tool_template.jar") + .toFile().exists()); + +// Test packing the package + System.setProperty("user.dir", packageDir.toString()); + PackCommand packCommand = new PackCommand(packageDir, printStream, printStream, false, true); + new CommandLine(packCommand).parseArgs(); + packCommand.execute(); + String packBuildLog = readOutput(true); + Assert.assertEquals(packBuildLog.replaceAll("\r", ""), getOutput("pack-central-tool.txt")); + Assert.assertTrue( + packageDir.resolve("target").resolve("bala") + .resolve("testorg-sample_tool_template-any-1.0.0.bala").toFile().exists()); } @Test(description = "Test new command with central template in the local cache") diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/modules/toolProject/main.bal b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/modules/toolProject/main.bal new file mode 100644 index 000000000000..a6f89ee470ad --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/modules/toolProject/main.bal @@ -0,0 +1,3 @@ +// AUTO-GENERATED FILE. + +// This file is auto-generated by Ballerina for managing modules in packages. diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json index f9f6a3142e5c..29b032893daf 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json +++ b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/bal-tool.json @@ -1,6 +1,6 @@ { "tool_id": "toolProject", "dependency_paths": [ - "tool/libs/platform-io-1.3.0-java.txt" + "tool/libs/platform-io-1.3.0-java.jar" ] } diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/modules/toolProject/.keep b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/libs/platform-io-1.3.0-java.jar similarity index 100% rename from cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/modules/toolProject/.keep rename to cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/libs/platform-io-1.3.0-java.jar diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/libs/platform-io-1.3.0-java.txt b/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/libs/platform-io-1.3.0-java.txt deleted file mode 100644 index b4704b3b8db3..000000000000 --- a/cli/ballerina-cli/src/test/resources/test-resources/balacache-template/testorg/toolProject/1.0.0/any/tool/libs/platform-io-1.3.0-java.txt +++ /dev/null @@ -1 +0,0 @@ -// Testing \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-template.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-template.txt new file mode 100644 index 000000000000..cffd3602bad0 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-template.txt @@ -0,0 +1,5 @@ +Compiling source + testuserorg/tool_sample:0.1.0 + +Generating executable + target/bin/tool_sample.jar diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/pack-central-tool.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/pack-central-tool.txt new file mode 100644 index 000000000000..8a732223a047 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/pack-central-tool.txt @@ -0,0 +1,5 @@ +Compiling source + testorg/sample_tool_template:1.0.0 + +Creating bala + target/bala/testorg-sample_tool_template-any-1.0.0.bala diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/pack-tool-template.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/pack-tool-template.txt new file mode 100644 index 000000000000..0763517bacdb --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/pack-tool-template.txt @@ -0,0 +1,5 @@ +Compiling source + testuserorg/tool_sample:0.1.0 + +Creating bala + target/bala/testuserorg-tool_sample-any-0.1.0.bala diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-template.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-template.txt new file mode 100644 index 000000000000..2f7791e8ed77 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-template.txt @@ -0,0 +1,5 @@ +Compiling source + testuserorg\tool_sample:0.1.0 + +Generating executable + target\bin\tool_sample.jar diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-central-tool.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-central-tool.txt new file mode 100644 index 000000000000..1f87e697b36b --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-central-tool.txt @@ -0,0 +1,5 @@ +Compiling source + testorg\sample_tool_template:1.0.0 + +Creating bala + target\bala\testorg-sample_tool_template-any-1.0.0.bala diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-tool-template.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-tool-template.txt new file mode 100644 index 000000000000..a365b7461e16 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-tool-template.txt @@ -0,0 +1,5 @@ +Compiling source + testuserorg\tool_sample:0.1.0 + +Creating bala + target/bala\testuserorg-tool_sample-any-0.1.0.bala diff --git a/cli/ballerina-cli/src/test/resources/test-resources/test-jar-files/tool-test.jar b/cli/ballerina-cli/src/test/resources/test-resources/test-jar-files/tool-test.jar new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java index 1d9b332d940f..43265dbdc444 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/BalaWriter.java @@ -285,7 +285,7 @@ private void addPackageSource(ZipOutputStream balaOutputStream) throws IOExcepti module.documentIds().isEmpty()) { String emptyBalContent = "// AUTO-GENERATED FILE.\n" + "\n" + - "// This file is auto-generated by Ballerina for managing modules in packages.\n"; + "// This file is auto-generated by Ballerina for packages with empty default modules. \n"; TextDocument emptyBalTextDocument = TextDocuments.from(emptyBalContent); DocumentId documentId = DocumentId.create(MAIN_BAL, moduleId); From d54f3e2ee6bb4f993bd5b8f59ef2c81f443684c2 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 13 Jul 2023 00:11:12 +0530 Subject: [PATCH 49/70] Fix test failures --- .../src/test/java/io/ballerina/cli/cmd/NewCommandTest.java | 2 +- .../command-outputs/windows/build-tool-template.txt | 2 +- .../command-outputs/windows/pack-central-tool.txt | 2 +- .../command-outputs/windows/pack-tool-template.txt | 4 ++-- .../test/java/io/ballerina/projects/test/TestBalaWriter.java | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index b0c08dccabcf..b36f3765cecf 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -568,7 +568,7 @@ public void testNewCommandWithTool() throws IOException { // Test packing the package Files.copy(testResources.resolve("test-jar-files").resolve("tool-test.jar"), packageDir.resolve(TOOL_DIR).resolve("tool-test.jar")); - String newToolTomlContent = toolTomlContent + "path = \"tool" + File.separator + "tool-test.jar\"\n"; + String newToolTomlContent = toolTomlContent + "path = \"tool/tool-test.jar\"\n"; Files.writeString(packageDir.resolve(ProjectConstants.BAL_TOOL_TOML), newToolTomlContent); System.setProperty("user.dir", packageDir.toString()); PackCommand packCommand = new PackCommand(packageDir, printStream, printStream, false, true); diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-template.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-template.txt index 2f7791e8ed77..de88c6ec3ba1 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-template.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-template.txt @@ -1,5 +1,5 @@ Compiling source - testuserorg\tool_sample:0.1.0 + testuserorg/tool_sample:0.1.0 Generating executable target\bin\tool_sample.jar diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-central-tool.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-central-tool.txt index 1f87e697b36b..e412738583e8 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-central-tool.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-central-tool.txt @@ -1,5 +1,5 @@ Compiling source - testorg\sample_tool_template:1.0.0 + testorg/sample_tool_template:1.0.0 Creating bala target\bala\testorg-sample_tool_template-any-1.0.0.bala diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-tool-template.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-tool-template.txt index a365b7461e16..22eb8c858d2c 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-tool-template.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/pack-tool-template.txt @@ -1,5 +1,5 @@ Compiling source - testuserorg\tool_sample:0.1.0 + testuserorg/tool_sample:0.1.0 Creating bala - target/bala\testuserorg-tool_sample-any-0.1.0.bala + target\bala\testuserorg-tool_sample-any-0.1.0.bala diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBalaWriter.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBalaWriter.java index a2519422f42a..9d25b5798877 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBalaWriter.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBalaWriter.java @@ -402,7 +402,7 @@ public void testBalaWriterWithToolProject(ITestContext ctx) throws IOException { Assert.assertTrue(mainFilePath.toFile().exists()); String expectedMainContent = "// AUTO-GENERATED FILE.\n" + "\n" + - "// This file is auto-generated by Ballerina for managing modules in packages.\n"; + "// This file is auto-generated by Ballerina for packages with empty default modules. \n"; Assert.assertEquals(Files.readString(mainFilePath), expectedMainContent); } From c6e9721c1a9efe4eca66031669b2a432a72fcdff Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Thu, 13 Jul 2023 13:23:03 +0530 Subject: [PATCH 50/70] Address review suggestions --- .../analyzer/ConstantValueResolver.java | 7 +++--- .../org/wso2/ballerinalang/util/Flags.java | 2 +- .../test/typedefs/TypeDefinitionsTest.java | 5 ++++ .../test-src/typedefs/type-definitions.bal | 23 +++++++++++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java index 29bd869a23c0..ee749c1dc32b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantValueResolver.java @@ -82,6 +82,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Stack; import java.util.function.BiFunction; @@ -581,10 +582,8 @@ BLangConstantValue constructBLangConstantValueWithExactType(BLangExpression expr this.anonTypeNameSuffixes = anonTypeNameSuffixes; updateConstantType(constantSymbol, expression, env); - BLangTypeDefinition typeDefinition = createdTypeDefinitions.get(constantSymbol.type.tsymbol); - if (isSourceOnlyAnon && typeDefinition != null) { - typeDefinition.symbol.flags |= Flags.SOURCE_ANNOTATION; - } + Optional.ofNullable(isSourceOnlyAnon ? createdTypeDefinitions.get(constantSymbol.type.tsymbol) : null) + .ifPresent(typeDefinition -> typeDefinition.symbol.flags |= Flags.SOURCE_ANNOTATION); return value; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/util/Flags.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/util/Flags.java index a1dfdd6d0b99..25fa804b1e70 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/util/Flags.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/util/Flags.java @@ -82,7 +82,7 @@ public class Flags { public static final long ENUM_MEMBER = INFER << 1; // 41 public static final long QUERY_LAMBDA = ENUM_MEMBER << 1; // 42 public static final long EFFECTIVE_TYPE_DEF = QUERY_LAMBDA << 1; // 43 - public static final long SOURCE_ANNOTATION = EFFECTIVE_TYPE_DEF << 1; // 44 + public static final long SOURCE_ANNOTATION = EFFECTIVE_TYPE_DEF << 1; // 44 public static long asMask(Set flagSet) { long mask = 0; diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/typedefs/TypeDefinitionsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/typedefs/TypeDefinitionsTest.java index 96004c500cc4..8197c30d0c56 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/typedefs/TypeDefinitionsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/typedefs/TypeDefinitionsTest.java @@ -184,6 +184,11 @@ public void testRecordTypeResolvingWithTypeInclusion() { BRunUtil.invoke(recordFieldRes, "testRecordTypeResolvingWithTypeInclusion"); } + @Test + public void testAnnotWithRecordTypeDefinition() { + BRunUtil.invoke(compileResult, "testAnnotWithRecordTypeDefinition"); + } + @AfterClass public void tearDown() { compileResult = null; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/typedefs/type-definitions.bal b/tests/jballerina-unit-test/src/test/resources/test-src/typedefs/type-definitions.bal index c0b3d373e828..009e273f612d 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/typedefs/type-definitions.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/typedefs/type-definitions.bal @@ -338,6 +338,29 @@ public function returnTupleWithSingletonType2() returns X2 { return ["x", 2, ()]; } +type Data record {| + string value; +|}; + +const annotation Data dataAnon on type; + +@dataAnon { + value: "T1" +} +type Person2 record {| + string name; +|}; + +function testAnnotWithRecordTypeDefinition() { + Person2 foo = {name: "James"}; + typedesc t = typeof foo; + Data? annon = t.@dataAnon; + assertEquality("{\"value\":\"T1\"}", annon.toString()); + + Data data = {value: "T2"}; + assertEquality("{\"value\":\"T2\"}" , data.toString()); +} + function assertTrue(any|error actual) { assertEquality(true, actual); } From a06dae67abe48a59fdffdf6970bad38cf9c5b1ba Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 13 Jul 2023 15:31:06 +0530 Subject: [PATCH 51/70] Fix version error --- .../src/test/java/io/ballerina/cli/cmd/NewCommandTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java index f1b458f75ec3..f3f4334d7e10 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/NewCommandTest.java @@ -638,7 +638,7 @@ public void testNewCommandCentralTemplateReplaceImports() throws IOException { "name = \"" + packageName + "\"\n" + "version = \"1.0.2\"\n" + "export = [\"central_sample\"]\n" + - "distribution = \"" + RepoUtils.getBallerinaShortVersion() + "\"\n\n" + + "distribution = \"2201.7.0-SNAPSHOT\"\n\n" + "[build-options]\n" + "observabilityIncluded = true\n"; Assert.assertEquals( From 65b2f7d10ffe618085bc539103be49c38a6cbbea Mon Sep 17 00:00:00 2001 From: Thevakumar-Luheerathan Date: Thu, 13 Jul 2023 17:43:53 +0530 Subject: [PATCH 52/70] Add in-built compiler plugin modifier support --- cli/ballerina-cli/build.gradle | 16 ++ .../io/ballerina/cli/cmd/PackCommandTest.java | 56 ++++++ .../io/ballerina/cli/cmd/RunCommandTest.java | 60 +++++++ .../Ballerina.toml | 4 + .../log_creator_combined_plugin/main.bal | 6 + .../Ballerina.toml | 4 + .../CompilerPlugin.toml | 5 + .../another.bal | 3 + .../main.bal | 8 + .../tests/main_tests.bal | 2 + .../Ballerina.toml | 4 + .../CompilerPlugin.toml | 5 + .../another.bal | 3 + .../main.bal | 8 + .../tests/main_tests.bal | 2 + .../Ballerina.toml | 4 + .../CompilerPlugin.toml | 5 + .../another.bal | 3 + .../main.bal | 8 + .../tests/main_tests.bal | 2 + .../projects/CodeModifierManager.java | 22 ++- project-api/project-api-test/build.gradle | 7 + .../test/plugins/CompilerPluginTests.java | 108 +++++++++++- .../Ballerina.toml | 4 + .../in_built_plugin_code_modify/another.bal | 3 + .../in_built_plugin_code_modify/main.bal | 8 + .../tests/main_tests.bal | 2 + .../Ballerina.toml | 4 + .../log_creator_combined_plugin/main.bal | 6 + .../Ballerina.toml | 4 + .../CompilerPlugin.toml | 5 + .../another.bal | 3 + .../main.bal | 8 + .../tests/main_tests.bal | 2 + .../Ballerina.toml | 4 + .../CompilerPlugin.toml | 5 + .../another.bal | 3 + .../main.bal | 8 + .../tests/main_tests.bal | 2 + .../Ballerina.toml | 4 + .../CompilerPlugin.toml | 5 + .../another.bal | 3 + .../main.bal | 8 + .../tests/main_tests.bal | 2 + .../build.gradle | 39 +++++ .../codemodify/InBuiltCodeModifierPlugin.java | 36 ++++ .../codemodify/InsertFuncCodeModifier.java | 162 ++++++++++++++++++ .../src/main/java/module-info.java | 8 + ....ballerina.projects.plugins.CompilerPlugin | 1 + ....ballerina.projects.plugins.CompilerPlugin | 1 + .../build.gradle | 37 ++++ .../LogCodeAnalyzerInBuiltPlugin.java | 81 +++++++++ .../src/main/java/module-info.java | 7 + ....ballerina.projects.plugins.CompilerPlugin | 1 + .../build.gradle | 37 ++++ .../LogCodeGeneratorInBuiltPlugin.java | 83 +++++++++ .../src/main/java/module-info.java | 7 + ....ballerina.projects.plugins.CompilerPlugin | 1 + .../build.gradle | 37 ++++ .../LogCodeModifierInBuiltPlugin.java | 80 +++++++++ .../src/main/java/module-info.java | 7 + ....ballerina.projects.plugins.CompilerPlugin | 1 + .../build.gradle | 37 ++++ .../analyzer/LogCodeAnalyzerPkgPlugin.java | 80 +++++++++ .../src/main/java/module-info.java | 7 + .../build.gradle | 37 ++++ .../generator/LogCodeGeneratorPkgPlugin.java | 82 +++++++++ .../src/main/java/module-info.java | 7 + .../build.gradle | 37 ++++ .../modifier/LogCodeModifierPkgPlugin.java | 82 +++++++++ .../src/main/java/module-info.java | 7 + settings.gradle | 14 ++ 72 files changed, 1448 insertions(+), 6 deletions(-) create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_combined_plugin/Ballerina.toml create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_combined_plugin/main.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/Ballerina.toml create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/CompilerPlugin.toml create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/another.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/main.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/tests/main_tests.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/Ballerina.toml create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/CompilerPlugin.toml create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/another.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/main.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/tests/main_tests.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/Ballerina.toml create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/CompilerPlugin.toml create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/another.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/main.bal create mode 100644 cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/tests/main_tests.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/another.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/main.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/tests/main_tests.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_combined_plugin/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_combined_plugin/main.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/CompilerPlugin.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/another.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/main.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/tests/main_tests.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/CompilerPlugin.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/another.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/main.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/tests/main_tests.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/CompilerPlugin.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/another.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/main.bal create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/tests/main_tests.bal create mode 100644 project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle create mode 100644 project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java create mode 100644 project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java create mode 100644 project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/module-info.java create mode 100644 project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin create mode 100644 project-api/test-artifacts/init-function-codegen-compiler-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin create mode 100644 project-api/test-artifacts/log-creator-in-built-code-analyzer/build.gradle create mode 100644 project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java create mode 100644 project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/module-info.java create mode 100644 project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin create mode 100644 project-api/test-artifacts/log-creator-in-built-code-generator/build.gradle create mode 100644 project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java create mode 100644 project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/module-info.java create mode 100644 project-api/test-artifacts/log-creator-in-built-code-generator/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin create mode 100644 project-api/test-artifacts/log-creator-in-built-code-modifier/build.gradle create mode 100644 project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java create mode 100644 project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/module-info.java create mode 100644 project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/build.gradle create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/module-info.java create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-generator/build.gradle create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/module-info.java create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-modifier/build.gradle create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/module-info.java diff --git a/cli/ballerina-cli/build.gradle b/cli/ballerina-cli/build.gradle index 0ef247ef767f..b4c34817de13 100644 --- a/cli/ballerina-cli/build.gradle +++ b/cli/ballerina-cli/build.gradle @@ -22,6 +22,9 @@ configurations { testCompile.exclude group: 'org.slf4j', module: 'slf4j-log4j12' testCompile.exclude group: 'org.slf4j', module: 'slf4j-simple' testCompile.exclude group: 'org.ops4j.pax.logging', module: 'pax-logging-api' + compilerPluginJar { + transitive false + } distributionBala distributionBirJar balRt @@ -62,6 +65,13 @@ dependencies { testRt project(':testerina:testerina-runtime') testCore project(':testerina:testerina-core') testRuntime project(':project-api-test-artifact:simple-code-gen-plugin-with-resource-gen') + testRuntime project(':project-api-test-artifact:log-creator-in-built-code-modifier') + testRuntime project(':project-api-test-artifact:log-creator-in-built-code-generator') + testRuntime project(':project-api-test-artifact:log-creator-in-built-code-analyzer') + + compilerPluginJar project(':project-api-test-artifact:log-creator-pkg-provided-code-modifier') + compilerPluginJar project(':project-api-test-artifact:log-creator-pkg-provided-code-generator') + compilerPluginJar project(':project-api-test-artifact:log-creator-pkg-provided-code-analyzer') } task createTestDistributionCache(type: Copy) { @@ -70,6 +80,11 @@ task createTestDistributionCache(type: Copy) { into "$buildDir/repo" } +task copyCompilerPluginJars(type: Copy) { + from configurations.compilerPluginJar + into "$buildDir/compiler-plugin-jars" +} + task createTestBre(type: Copy) { from configurations.balRt from configurations.testRt @@ -81,6 +96,7 @@ task createTestBre(type: Copy) { test { dependsOn createTestDistributionCache dependsOn createTestBre + dependsOn copyCompilerPluginJars systemProperty "ballerina.home", "$buildDir" diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java index f2ff2c238a31..753d4dad2191 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java @@ -4,6 +4,7 @@ import io.ballerina.projects.util.ProjectUtils; import org.ballerinalang.test.BCompileUtil; import org.testng.Assert; +import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; @@ -32,6 +33,9 @@ public class PackCommandTest extends BaseCommandTest { private static final String VALID_PROJECT = "validApplicationProject"; private Path testResources; + static Path logFile = Paths.get("./src/test/resources/compiler_plugin_tests/" + + "log_creator_combined_plugin/compiler-plugin.txt"); + @BeforeClass public void setup() throws IOException { super.setup(); @@ -44,6 +48,8 @@ public void setup() throws IOException { } catch (URISyntaxException e) { Assert.fail("error loading resources"); } + Files.createDirectories(logFile.getParent()); + Files.writeString(logFile, ""); } @Test(description = "Test package command") @@ -80,6 +86,48 @@ public void testPackProject() throws IOException { .resolve("foo-winery-0.1.0.jar").toFile().exists()); } + @Test(description = "Pack a ballerina project with the engagement of all type of compiler plugins") + public void testRunBalProjectWithAllCompilerPlugins() throws IOException { + Path compilerPluginPath = Paths.get("./src/test/resources/test-resources/compiler-plugins"); + BCompileUtil.compileAndCacheBala(compilerPluginPath.resolve("log_creator_pkg_provided_code_analyzer_im") + .toAbsolutePath().toString()); + BCompileUtil.compileAndCacheBala(compilerPluginPath.resolve("log_creator_pkg_provided_code_generator_im") + .toAbsolutePath().toString()); + BCompileUtil.compileAndCacheBala(compilerPluginPath.resolve("log_creator_pkg_provided_code_modifier_im") + .toAbsolutePath().toString()); + + Path projectPath = this.testResources.resolve("compiler-plugins").resolve("log_creator_combined_plugin"); + System.setProperty("user.dir", projectPath.toString()); + PackCommand packCommand = new PackCommand(projectPath, printStream, printStream, false, true); + new CommandLine(packCommand).parseArgs(); + packCommand.execute(); + String logFileContent = Files.readString(logFile); + Assert.assertTrue(logFileContent.contains("pkg-provided-syntax-node-analysis-analyzer"), + "Package provided syntax node analysis from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-syntax-node-analysis-analyzer"), + "In-Built syntax node analysis from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-source-analyzer"), + "Package provided source analyzer from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-source-analyzer"), + "In-Built source analyzer from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-syntax-node-analysis-generator"), + "Package provided syntax node analysis from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-syntax-node-analysis-generator"), + "In-Built syntax node analysis from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-source-generator"), + "Package provided source generator from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-source-generator"), + "In-Built source generator from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-syntax-node-analysis-modifier"), + "In-Built syntax node analysis from code modifier has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-source-modifier"), + "In-Built source modifier from code modifier has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-syntax-node-analysis-modifier"), + "Package provided syntax node analysis from code modifier has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-source-modifier"), + "Package provided source modifier from code modifier has failed to run"); + } + @Test(description = "Pack an application package") public void testPackApplicationPackage() { Path projectPath = this.testResources.resolve("validApplicationProject"); @@ -368,4 +416,12 @@ public void testPackProjectWithPlatformLibs() throws IOException { String buildLog = readOutput(true); Assert.assertTrue(buildLog.contains("WARNING: Package is not verified with GraalVM.")); } + + @AfterClass + public void cleanUp() throws IOException { + Files.deleteIfExists(logFile); + Files.deleteIfExists(logFile.getParent()); + Files.deleteIfExists(logFile.getParent().getParent()); + + } } diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java index ae3d1532967a..94d724f5c5d2 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java @@ -9,6 +9,7 @@ import org.apache.commons.io.filefilter.WildcardFileFilter; import org.ballerinalang.test.BCompileUtil; import org.testng.Assert; +import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import picocli.CommandLine; @@ -35,6 +36,8 @@ public class RunCommandTest extends BaseCommandTest { private Path testResources; private Path testDistCacheDirectory; private ProjectEnvironmentBuilder projectEnvironmentBuilder; + static Path logFile = Paths.get("./src/test/resources/compiler_plugin_tests/" + + "log_creator_combined_plugin/compiler-plugin.txt"); @BeforeClass public void setup() throws IOException { @@ -53,6 +56,8 @@ public void setup() throws IOException { } catch (URISyntaxException e) { Assert.fail("error loading resources"); } + Files.createDirectories(logFile.getParent()); + Files.writeString(logFile, ""); } @Test(description = "Run a valid ballerina file") @@ -250,6 +255,53 @@ public void testRunWithCustomTarget() { } } + @Test(description = "Run a ballerina project with the engagement of all type of compiler plugins") + public void testRunBalProjectWithAllCompilerPlugins() throws IOException { + Path logFile = Paths.get("./src/test/resources/compiler_plugin_tests/" + + "log_creator_combined_plugin/compiler-plugin.txt"); + Files.createDirectories(logFile.getParent()); + Files.writeString(logFile, ""); + Path compilerPluginPath = Paths.get("./src/test/resources/test-resources").resolve("compiler-plugins"); + BCompileUtil.compileAndCacheBala(compilerPluginPath.resolve("log_creator_pkg_provided_code_analyzer_im"), + testDistCacheDirectory, projectEnvironmentBuilder); + BCompileUtil.compileAndCacheBala(compilerPluginPath.resolve("log_creator_pkg_provided_code_generator_im"), + testDistCacheDirectory, projectEnvironmentBuilder); + BCompileUtil.compileAndCacheBala(compilerPluginPath.resolve("log_creator_pkg_provided_code_modifier_im"), + testDistCacheDirectory, projectEnvironmentBuilder); + + Path projectPath = this.testResources.resolve("compiler-plugins").resolve("log_creator_combined_plugin"); + System.setProperty("user.dir", projectPath.toString()); + RunCommand runCommand = new RunCommand(projectPath, printStream, false); + new CommandLine(runCommand).parseArgs(); + runCommand.execute(); + String logFileContent = Files.readString(logFile); + Assert.assertTrue(logFileContent.contains("pkg-provided-syntax-node-analysis-analyzer"), + "Package provided syntax node analysis from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-syntax-node-analysis-analyzer"), + "In-Built syntax node analysis from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-source-analyzer"), + "Package provided source analyzer from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-source-analyzer"), + "In-Built source analyzer from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-syntax-node-analysis-generator"), + "Package provided syntax node analysis from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-syntax-node-analysis-generator"), + "In-Built syntax node analysis from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-source-generator"), + "Package provided source generator from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-source-generator"), + "In-Built source generator from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-syntax-node-analysis-modifier"), + "In-Built syntax node analysis from code modifier has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-source-modifier"), + "In-Built source modifier from code modifier has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-syntax-node-analysis-modifier"), + "Package provided syntax node analysis from code modifier has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-source-modifier"), + "Package provided source modifier from code modifier has failed to run"); + + } + @Test(description = "Run a valid ballerina project with invalid argument") public void testNoClassDefProject() { Path projectPath = this.testResources.resolve("noClassDefProject"); @@ -323,4 +375,12 @@ public void testRunEmptyPackage() throws IOException { String buildLog = readOutput(true); Assert.assertEquals(buildLog.replaceAll("\r", ""), getOutput("build-empty-package.txt")); } + + @AfterClass + public void cleanUp() throws IOException { + Files.deleteIfExists(logFile); + Files.deleteIfExists(logFile.getParent()); + Files.deleteIfExists(logFile.getParent().getParent()); + + } } diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_combined_plugin/Ballerina.toml b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_combined_plugin/Ballerina.toml new file mode 100644 index 000000000000..a28fcf926ca4 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_combined_plugin/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "luhee" +name = "log_creator_combined_plugin" +version = "0.1.0" diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_combined_plugin/main.bal b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_combined_plugin/main.bal new file mode 100644 index 000000000000..c6ac59829c3d --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_combined_plugin/main.bal @@ -0,0 +1,6 @@ +import luhee/log_creator_pkg_provided_code_modifier_im as _; +import luhee/log_creator_pkg_provided_code_generator_im as _; +import luhee/log_creator_pkg_provided_code_analyzer_im as _; +public function main() { +} + diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/Ballerina.toml b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/Ballerina.toml new file mode 100644 index 000000000000..b4fc4a43cc32 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "luhee" +name = "log_creator_pkg_provided_code_analyzer_im" +version = "0.1.0" diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/CompilerPlugin.toml b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/CompilerPlugin.toml new file mode 100644 index 000000000000..563227e91b83 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/CompilerPlugin.toml @@ -0,0 +1,5 @@ +[plugin] +class = "io.luhee.plugins.pkg.analyzer.LogCodeAnalyzerPkgPlugin" + +[[dependency]] +path = "../../../../../../build/compiler-plugin-jars/log-creator-pkg-provided-code-analyzer-1.0.0.jar" diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/another.bal b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/another.bal new file mode 100644 index 000000000000..fce726cd81ad --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/another.bal @@ -0,0 +1,3 @@ + +function yoo() { +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/main.bal b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/main.bal new file mode 100644 index 000000000000..a59a9f1ff8ca --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/main.bal @@ -0,0 +1,8 @@ +public function main() { +} + +function foo() { +} + +function bar() { +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/tests/main_tests.bal b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/tests/main_tests.bal new file mode 100644 index 000000000000..764b65dd22c9 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_analyzer_im/tests/main_tests.bal @@ -0,0 +1,2 @@ +public function testRunMain() { +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/Ballerina.toml b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/Ballerina.toml new file mode 100644 index 000000000000..318289d634c1 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "luhee" +name = "log_creator_pkg_provided_code_generator_im" +version = "0.1.0" diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/CompilerPlugin.toml b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/CompilerPlugin.toml new file mode 100644 index 000000000000..c3783aa36295 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/CompilerPlugin.toml @@ -0,0 +1,5 @@ +[plugin] +class = "io.luhee.plugins.pkg.generator.LogCodeGeneratorPkgPlugin" + +[[dependency]] +path = "../../../../../../build/compiler-plugin-jars/log-creator-pkg-provided-code-generator-1.0.0.jar" diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/another.bal b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/another.bal new file mode 100644 index 000000000000..fce726cd81ad --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/another.bal @@ -0,0 +1,3 @@ + +function yoo() { +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/main.bal b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/main.bal new file mode 100644 index 000000000000..a59a9f1ff8ca --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/main.bal @@ -0,0 +1,8 @@ +public function main() { +} + +function foo() { +} + +function bar() { +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/tests/main_tests.bal b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/tests/main_tests.bal new file mode 100644 index 000000000000..764b65dd22c9 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_generator_im/tests/main_tests.bal @@ -0,0 +1,2 @@ +public function testRunMain() { +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/Ballerina.toml b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/Ballerina.toml new file mode 100644 index 000000000000..73107eb24c9d --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "luhee" +name = "log_creator_pkg_provided_code_modifier_im" +version = "0.1.0" diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/CompilerPlugin.toml b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/CompilerPlugin.toml new file mode 100644 index 000000000000..df46866e937a --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/CompilerPlugin.toml @@ -0,0 +1,5 @@ +[plugin] +class = "io.luhee.plugins.pkg.modifier.LogCodeModifierPkgPlugin" + +[[dependency]] +path = "../../../../../../build/compiler-plugin-jars/log-creator-pkg-provided-code-modifier-1.0.0.jar" diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/another.bal b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/another.bal new file mode 100644 index 000000000000..fce726cd81ad --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/another.bal @@ -0,0 +1,3 @@ + +function yoo() { +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/main.bal b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/main.bal new file mode 100644 index 000000000000..a59a9f1ff8ca --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/main.bal @@ -0,0 +1,8 @@ +public function main() { +} + +function foo() { +} + +function bar() { +} diff --git a/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/tests/main_tests.bal b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/tests/main_tests.bal new file mode 100644 index 000000000000..764b65dd22c9 --- /dev/null +++ b/cli/ballerina-cli/src/test/resources/test-resources/compiler-plugins/log_creator_pkg_provided_code_modifier_im/tests/main_tests.bal @@ -0,0 +1,2 @@ +public function testRunMain() { +} diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CodeModifierManager.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CodeModifierManager.java index 4747e6c152e7..9ccd10179ed3 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CodeModifierManager.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/CodeModifierManager.java @@ -102,21 +102,35 @@ private Map> getSourceModifierTaskBy return descriptorListMap; } + private List getInBuiltSourceModifierTask() { + List inBuiltSourceModifierTasks = new ArrayList<>(); + for (Map.Entry> codeModifierListEntry : + codeModifierTasks.sourceModTaskMap.entrySet()) { + for (SourceModifierTask sourceModifierTask : codeModifierListEntry.getValue()) { + if (sourceModifierTask.compilerPluginInfo.kind().equals(CompilerPluginKind.BUILT_IN)) { + inBuiltSourceModifierTasks.add(sourceModifierTask); + } + } + + } + return inBuiltSourceModifierTasks; + } + private void runSourceModifierTasks(CodeModifierTaskResultBuilder resultBuilder) { + runSourceModifierTask(getInBuiltSourceModifierTask(), resultBuilder); Map> sourceModifierTaskByPackage = getSourceModifierTaskByPackage(); for (Map.Entry> packageDescriptorListEntry : sourceModifierTaskByPackage.entrySet()) { Map> pkgSyntaxNodeAnalysisTaskMap = populatePkgSyntaxNodeTaskMap(packageDescriptorListEntry.getKey()); - runSourceModifierTask(packageDescriptorListEntry.getValue(), pkgSyntaxNodeAnalysisTaskMap, resultBuilder); + List analysisTaskDiagnostics = runSyntaxNodeAnalysisTasks(pkgSyntaxNodeAnalysisTaskMap); + resultBuilder.addDiagnostics(analysisTaskDiagnostics); + runSourceModifierTask(packageDescriptorListEntry.getValue(), resultBuilder); } } private void runSourceModifierTask(List sourceModifierTasks, - Map> syntaxNodeAnalysisTasks, CodeModifierTaskResultBuilder resultBuilder) { - List analysisTaskDiagnostics = runSyntaxNodeAnalysisTasks(syntaxNodeAnalysisTasks); - resultBuilder.addDiagnostics(analysisTaskDiagnostics); for (SourceModifierTask sourceModifierTask : sourceModifierTasks) { SourceModifierContextImpl sourceModifyContext = new SourceModifierContextImpl(currentPackage, compilation); diff --git a/project-api/project-api-test/build.gradle b/project-api/project-api-test/build.gradle index 367916f98003..b82fdb42d5a3 100644 --- a/project-api/project-api-test/build.gradle +++ b/project-api/project-api-test/build.gradle @@ -48,6 +48,10 @@ dependencies { testRuntime project(':ballerina-runtime') testRuntime project(':project-api-test-artifact:logging-file-appender-plugin') testRuntime project(':compiler-plugins:package-semantic-analyzer') + testRuntime project(':project-api-test-artifact:in-built-code-modifier-plugin') + testRuntime project(':project-api-test-artifact:log-creator-in-built-code-modifier') + testRuntime project(':project-api-test-artifact:log-creator-in-built-code-generator') + testRuntime project(':project-api-test-artifact:log-creator-in-built-code-analyzer') compilerPluginJar project(':project-api-test-artifact:event-logger-compiler-plugin') compilerPluginJar project(':project-api-test-artifact:compiler-plugin-with-one-dependency') @@ -63,6 +67,9 @@ dependencies { compilerPluginJar project(':compiler-plugins:package-semantic-analyzer') compilerPluginJar project(':project-api-test-artifact:init-function-diagnostic-compiler-plugin') compilerPluginJar project(':project-api-test-artifact:compiler-plugin-with-analyzer-generator-modifier') + compilerPluginJar project(':project-api-test-artifact:log-creator-pkg-provided-code-modifier') + compilerPluginJar project(':project-api-test-artifact:log-creator-pkg-provided-code-generator') + compilerPluginJar project(':project-api-test-artifact:log-creator-pkg-provided-code-analyzer') testRuntime project(":ballerina-lang-test") balRt project(':ballerina-rt') diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java index 1138549032b0..6f900ca78198 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java @@ -40,6 +40,7 @@ import org.ballerinalang.test.BRunUtil; import org.ballerinalang.test.CompileResult; import org.testng.Assert; +import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; @@ -97,6 +98,12 @@ public void init() { "compiler_plugin_tests/immutable_type_definition_with_code_modifier_test/defns"); BCompileUtil.compileAndCacheBala( "compiler_plugin_tests/package_comp_plugin_with_analyzer_generator_modifier"); + BCompileUtil.compileAndCacheBala( + "compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im"); + BCompileUtil.compileAndCacheBala( + "compiler_plugin_tests/log_creator_pkg_provided_code_generator_im"); + BCompileUtil.compileAndCacheBala( + "compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im"); } @Test @@ -362,12 +369,102 @@ public void testCompilerPluginCodeModifyBasic() { DiagnosticResult diagnosticResult = compilation.diagnosticResult(); diagnosticResult.diagnostics().forEach(OUT::println); Assert.assertFalse(diagnosticResult.hasErrors(), "Unexpected errors in compilation"); - Assert.assertEquals(diagnosticResult.diagnosticCount(), 8, "Unexpected compilation diagnostics"); + Assert.assertEquals(diagnosticResult.diagnosticCount(), 11, "Unexpected compilation diagnostics"); // Check direct package dependencies count is 1 Assert.assertEquals(newPackage.packageDependencies().size(), 1, "Unexpected number of dependencies"); } + @Test(description = "Test loading in-built code modifier by default") + public void testCompilerPluginCodeModifyInBuilt() { + Package currentPackage = loadPackage("in_built_plugin_code_modify"); + // Check the document count in the current package + Assert.assertEquals(currentPackage.getDefaultModule().documentIds().size(), 2); + + // Running the compilation + currentPackage.getCompilation(); + + + // Running the code generation + CodeModifierResult codeModifierResult = currentPackage.runCodeModifierPlugins(); + + // Compiling the new package + Project project = currentPackage.project(); + Package newPackage = codeModifierResult.updatedPackage().orElse(null); + Assert.assertNotNull(newPackage, "Cannot be null, because there exist code modifiers"); + Assert.assertSame(newPackage.project(), project); + Assert.assertSame(newPackage, project.currentPackage()); + + // Modified source files + Assert.assertEquals(newPackage.getDefaultModule().documentIds().size(), 2); + for (DocumentId documentId : newPackage.getDefaultModule().documentIds()) { + Document document = newPackage.getDefaultModule().document(documentId); + // The code generator adds specific function to the end of every source file. + String specificFunction = "public function inBuiltFuncCodeModifier" + + document.name().replace(".bal", "").replace("/", "_") + + "(string params) returns error? {\n}"; + Assert.assertTrue(document.syntaxTree().toSourceCode().contains(specificFunction)); + } + + // Modified test source files + Assert.assertEquals(newPackage.getDefaultModule().testDocumentIds().size(), 1); + for (DocumentId documentId : newPackage.getDefaultModule().testDocumentIds()) { + Document document = newPackage.getDefaultModule().document(documentId); + // The code generator adds specific function to the end of every source file. + String specificFunction = "public function inBuiltFuncCodeModifier" + + document.name().replace(".bal", "").replace("/", "_").replace("-", "_") + + "(string params) returns error? {\n}"; + Assert.assertTrue(document.syntaxTree().toSourceCode().contains(specificFunction)); + } + + PackageCompilation compilation = newPackage.getCompilation(); + // Check whether there are any diagnostics + DiagnosticResult diagnosticResult = compilation.diagnosticResult(); + diagnosticResult.diagnostics().forEach(OUT::println); + Assert.assertFalse(diagnosticResult.hasErrors(), "Unexpected errors in compilation"); + Assert.assertEquals(diagnosticResult.diagnosticCount(), 0, "Unexpected compilation diagnostics"); + + // Check direct package dependencies count is 1 + Assert.assertEquals(newPackage.packageDependencies().size(), 0, "Unexpected number of dependencies"); + } + + @Test(description = "Test a combination of in-built and package provided compiler plugins") + public void testCombinationOfCompilerPlugins() throws IOException { + Path logFile = Paths.get("./src/test/resources/compiler_plugin_tests/" + + "log_creator_combined_plugin/compiler-plugin.txt"); + Files.writeString(logFile, ""); + Package currentPackage = loadPackage("log_creator_combined_plugin"); + currentPackage.getCompilation(); + CodeGeneratorResult codeGeneratorResult = currentPackage.runCodeGeneratorPlugins(); + CodeModifierResult codeModifierResult = currentPackage.runCodeModifierPlugins(); + String logFileContent = Files.readString(logFile); + Assert.assertTrue(logFileContent.contains("pkg-provided-syntax-node-analysis-analyzer"), + "Package provided syntax node analysis from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-syntax-node-analysis-analyzer"), + "In-Built syntax node analysis from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-source-analyzer"), + "Package provided source analyzer from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-source-analyzer"), + "In-Built source analyzer from code analyzer has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-syntax-node-analysis-generator"), + "Package provided syntax node analysis from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-syntax-node-analysis-generator"), + "In-Built syntax node analysis from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-source-generator"), + "Package provided source generator from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-source-generator"), + "In-Built source generator from code generator has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-syntax-node-analysis-modifier"), + "In-Built syntax node analysis from code modifier has failed to run"); + Assert.assertTrue(logFileContent.contains("in-built-source-modifier"), + "In-Built source modifier from code modifier has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-syntax-node-analysis-modifier"), + "Package provided syntax node analysis from code modifier has failed to run"); + Assert.assertTrue(logFileContent.contains("pkg-provided-source-modifier"), + "Package provided source modifier from code modifier has failed to run"); + Files.delete(logFile); + } + @Test(description = "Test basic single bal file code modify using code modifier plugin") public void testCompilerPluginSingleBalFileCodeModifyBasic() { Path projectDirPath = RESOURCE_DIRECTORY.resolve("single_bal_plugin_code_modify_user_1").resolve("main.bal"); @@ -409,7 +506,7 @@ public void testCompilerPluginSingleBalFileCodeModifyBasic() { DiagnosticResult diagnosticResult = compilation.diagnosticResult(); diagnosticResult.diagnostics().forEach(OUT::println); Assert.assertFalse(diagnosticResult.hasErrors(), "Unexpected errors in compilation"); - Assert.assertEquals(diagnosticResult.diagnosticCount(), 4, "Unexpected compilation diagnostics"); + Assert.assertEquals(diagnosticResult.diagnosticCount(), 5, "Unexpected compilation diagnostics"); // Check direct package dependencies count is 1 Assert.assertEquals(newPackage.packageDependencies().size(), 1, "Unexpected number of dependencies"); @@ -617,4 +714,11 @@ private void releaseLock() { "Error while deleting the lock file: " + PLUGIN_LOCK_FILE_PATH + " " + e.getMessage()); } } + + @AfterClass + private void cleanup() throws IOException { + Path logFile = Paths.get("./src/test/resources/compiler_plugin_tests/" + + "log_creator_combined_plugin/compiler-plugin.txt"); + Files.delete(logFile); + } } diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/Ballerina.toml new file mode 100644 index 000000000000..46a8b235f7c3 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "luhee" +name = "in_built_plugin_code_modify" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/another.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/another.bal new file mode 100644 index 000000000000..fce726cd81ad --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/another.bal @@ -0,0 +1,3 @@ + +function yoo() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/main.bal new file mode 100644 index 000000000000..a59a9f1ff8ca --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/main.bal @@ -0,0 +1,8 @@ +public function main() { +} + +function foo() { +} + +function bar() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/tests/main_tests.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/tests/main_tests.bal new file mode 100644 index 000000000000..764b65dd22c9 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/in_built_plugin_code_modify/tests/main_tests.bal @@ -0,0 +1,2 @@ +public function testRunMain() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_combined_plugin/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_combined_plugin/Ballerina.toml new file mode 100644 index 000000000000..a28fcf926ca4 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_combined_plugin/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "luhee" +name = "log_creator_combined_plugin" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_combined_plugin/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_combined_plugin/main.bal new file mode 100644 index 000000000000..c6ac59829c3d --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_combined_plugin/main.bal @@ -0,0 +1,6 @@ +import luhee/log_creator_pkg_provided_code_modifier_im as _; +import luhee/log_creator_pkg_provided_code_generator_im as _; +import luhee/log_creator_pkg_provided_code_analyzer_im as _; +public function main() { +} + diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/Ballerina.toml new file mode 100644 index 000000000000..b4fc4a43cc32 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "luhee" +name = "log_creator_pkg_provided_code_analyzer_im" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/CompilerPlugin.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/CompilerPlugin.toml new file mode 100644 index 000000000000..41817d1b3425 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/CompilerPlugin.toml @@ -0,0 +1,5 @@ +[plugin] +class = "io.luhee.plugins.pkg.analyzer.LogCodeAnalyzerPkgPlugin" + +[[dependency]] +path = "../../../../../build/compiler-plugin-jars/log-creator-pkg-provided-code-analyzer-1.0.0.jar" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/another.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/another.bal new file mode 100644 index 000000000000..fce726cd81ad --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/another.bal @@ -0,0 +1,3 @@ + +function yoo() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/main.bal new file mode 100644 index 000000000000..a59a9f1ff8ca --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/main.bal @@ -0,0 +1,8 @@ +public function main() { +} + +function foo() { +} + +function bar() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/tests/main_tests.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/tests/main_tests.bal new file mode 100644 index 000000000000..764b65dd22c9 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_analyzer_im/tests/main_tests.bal @@ -0,0 +1,2 @@ +public function testRunMain() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/Ballerina.toml new file mode 100644 index 000000000000..318289d634c1 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "luhee" +name = "log_creator_pkg_provided_code_generator_im" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/CompilerPlugin.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/CompilerPlugin.toml new file mode 100644 index 000000000000..957511fcfe4e --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/CompilerPlugin.toml @@ -0,0 +1,5 @@ +[plugin] +class = "io.luhee.plugins.pkg.generator.LogCodeGeneratorPkgPlugin" + +[[dependency]] +path = "../../../../../build/compiler-plugin-jars/log-creator-pkg-provided-code-generator-1.0.0.jar" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/another.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/another.bal new file mode 100644 index 000000000000..fce726cd81ad --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/another.bal @@ -0,0 +1,3 @@ + +function yoo() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/main.bal new file mode 100644 index 000000000000..a59a9f1ff8ca --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/main.bal @@ -0,0 +1,8 @@ +public function main() { +} + +function foo() { +} + +function bar() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/tests/main_tests.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/tests/main_tests.bal new file mode 100644 index 000000000000..764b65dd22c9 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_generator_im/tests/main_tests.bal @@ -0,0 +1,2 @@ +public function testRunMain() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/Ballerina.toml new file mode 100644 index 000000000000..73107eb24c9d --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "luhee" +name = "log_creator_pkg_provided_code_modifier_im" +version = "0.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/CompilerPlugin.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/CompilerPlugin.toml new file mode 100644 index 000000000000..e25606e19382 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/CompilerPlugin.toml @@ -0,0 +1,5 @@ +[plugin] +class = "io.luhee.plugins.pkg.modifier.LogCodeModifierPkgPlugin" + +[[dependency]] +path = "../../../../../build/compiler-plugin-jars/log-creator-pkg-provided-code-modifier-1.0.0.jar" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/another.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/another.bal new file mode 100644 index 000000000000..fce726cd81ad --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/another.bal @@ -0,0 +1,3 @@ + +function yoo() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/main.bal new file mode 100644 index 000000000000..a59a9f1ff8ca --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/main.bal @@ -0,0 +1,8 @@ +public function main() { +} + +function foo() { +} + +function bar() { +} diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/tests/main_tests.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/tests/main_tests.bal new file mode 100644 index 000000000000..764b65dd22c9 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/log_creator_pkg_provided_code_modifier_im/tests/main_tests.bal @@ -0,0 +1,2 @@ +public function testRunMain() { +} diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle b/project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle new file mode 100644 index 000000000000..68ac0874ead5 --- /dev/null +++ b/project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +apply from: "$rootDir/gradle/javaProject.gradle" + +description = 'Compiler Plugin Tests - In Built Code Modify Init Function' +version = '1.0.0' + +dependencies { + implementation project(':ballerina-lang') + implementation project(':ballerina-parser') + implementation project(':ballerina-tools-api') + implementation project(':project-api-test-artifact:diagnostic-utils-lib') +} + +ext.moduleName = 'compiler.plugin.test.init.func.inbuilt.codemodify' + +compileJava { + doFirst { + options.compilerArgs = [ + '--module-path', classpath.asPath, + ] + classpath = files() + } +} diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java new file mode 100644 index 000000000000..3db7efab821e --- /dev/null +++ b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.samjs.plugins.init.codemodify; + + +import io.ballerina.projects.plugins.CompilerPlugin; +import io.ballerina.projects.plugins.CompilerPluginContext; + + +/** + * A sample {@code CompilerPlugin} that modifies source files. + * + * @since 2201.0.3 + */ +public class InBuiltCodeModifierPlugin extends CompilerPlugin { + + @Override + public void init(CompilerPluginContext pluginContext) { + pluginContext.addCodeModifier(new InsertFuncCodeModifier()); + } +} diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java new file mode 100644 index 000000000000..83f46ba6cc9a --- /dev/null +++ b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.samjs.plugins.init.codemodify; + +import io.ballerina.compiler.syntax.tree.FunctionBodyBlockNode; +import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode; +import io.ballerina.compiler.syntax.tree.FunctionSignatureNode; +import io.ballerina.compiler.syntax.tree.MinutiaeList; +import io.ballerina.compiler.syntax.tree.ModuleMemberDeclarationNode; +import io.ballerina.compiler.syntax.tree.ModulePartNode; +import io.ballerina.compiler.syntax.tree.NodeList; +import io.ballerina.compiler.syntax.tree.OptionalTypeDescriptorNode; +import io.ballerina.compiler.syntax.tree.RequiredParameterNode; +import io.ballerina.compiler.syntax.tree.ReturnTypeDescriptorNode; +import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode; +import io.ballerina.compiler.syntax.tree.SyntaxKind; +import io.ballerina.compiler.syntax.tree.SyntaxTree; +import io.ballerina.compiler.syntax.tree.Token; +import io.ballerina.projects.Document; +import io.ballerina.projects.DocumentId; +import io.ballerina.projects.Module; +import io.ballerina.projects.ModuleId; +import io.ballerina.projects.plugins.CodeModifier; +import io.ballerina.projects.plugins.CodeModifierContext; + +import java.util.ArrayList; +import java.util.List; + +import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createEmptyMinutiaeList; +import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createEmptyNodeList; +import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createIdentifierToken; +import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createMinutiaeList; +import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createNodeList; +import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createSeparatedNodeList; +import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createToken; +import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createWhitespaceMinutiae; +import static io.ballerina.compiler.syntax.tree.NodeFactory.createFunctionBodyBlockNode; +import static io.ballerina.compiler.syntax.tree.NodeFactory.createFunctionDefinitionNode; +import static io.ballerina.compiler.syntax.tree.NodeFactory.createFunctionSignatureNode; +import static io.ballerina.compiler.syntax.tree.NodeFactory.createOptionalTypeDescriptorNode; +import static io.ballerina.compiler.syntax.tree.NodeFactory.createParameterizedTypeDescriptorNode; +import static io.ballerina.compiler.syntax.tree.NodeFactory.createRequiredParameterNode; +import static io.ballerina.compiler.syntax.tree.NodeFactory.createReturnTypeDescriptorNode; +import static io.ballerina.compiler.syntax.tree.NodeFactory.createSimpleNameReferenceNode; + +/** + * A {@code CodeModifier} implementation that modify each bal file by adding a specific function to the end. + * + * @since 2201.0.3 + */ +public class InsertFuncCodeModifier extends CodeModifier { + + @Override + public void init(CodeModifierContext modifierContext) { + modifierContext.addSourceModifierTask(sourceModifierContext -> { + // Add new function to every bal file + for (ModuleId moduleId : sourceModifierContext.currentPackage().moduleIds()) { + Module module = sourceModifierContext.currentPackage().module(moduleId); + for (DocumentId documentId : module.documentIds()) { + Document document = module.document(documentId); + ModulePartNode rootNode = document.syntaxTree().rootNode(); + NodeList newMembers = + rootNode.members().add(createFunctionDefNode(document)); + ModulePartNode newModulePart = + rootNode.modify(rootNode.imports(), newMembers, rootNode.eofToken()); + SyntaxTree updatedSyntaxTree = document.syntaxTree().modifyWith(newModulePart); + sourceModifierContext.modifySourceFile(updatedSyntaxTree.textDocument(), documentId); + } + } + // Add new function to every test bal file + for (ModuleId moduleId : sourceModifierContext.currentPackage().moduleIds()) { + Module module = sourceModifierContext.currentPackage().module(moduleId); + for (DocumentId documentId : module.testDocumentIds()) { + Document document = module.document(documentId); + ModulePartNode rootNode = document.syntaxTree().rootNode(); + NodeList newMembers = + rootNode.members().add(createFunctionDefNode(document)); + ModulePartNode newModulePart = + rootNode.modify(rootNode.imports(), newMembers, rootNode.eofToken()); + SyntaxTree updatedSyntaxTree = document.syntaxTree().modifyWith(newModulePart); + sourceModifierContext.modifyTestSourceFile(updatedSyntaxTree.textDocument(), documentId); + } + } + }); + } + + private static FunctionDefinitionNode createFunctionDefNode(Document document) { + List qualifierList = new ArrayList<>(); + Token publicToken = createToken(SyntaxKind.PUBLIC_KEYWORD, generateMinutiaeListWithTwoNewline(), + generateMinutiaeListWithWhitespace()); + qualifierList.add(publicToken); + + SimpleNameReferenceNode simpleNameRefNode = createSimpleNameReferenceNode( + createIdentifierToken("string", createEmptyMinutiaeList(), + generateMinutiaeListWithWhitespace())); + + RequiredParameterNode requiredParameterNode = + createRequiredParameterNode(createEmptyNodeList(), simpleNameRefNode, + createIdentifierToken("params")); + + OptionalTypeDescriptorNode optionalErrorTypeDescriptorNode = + createOptionalTypeDescriptorNode( + createParameterizedTypeDescriptorNode(SyntaxKind.ERROR_TYPE_DESC, + createToken(SyntaxKind.ERROR_KEYWORD), null), + createToken(SyntaxKind.QUESTION_MARK_TOKEN, createEmptyMinutiaeList(), + generateMinutiaeListWithWhitespace())); + + ReturnTypeDescriptorNode returnTypeDescriptorNode = + createReturnTypeDescriptorNode(createToken(SyntaxKind.RETURNS_KEYWORD, + createEmptyMinutiaeList(), generateMinutiaeListWithWhitespace()), + createEmptyNodeList(), optionalErrorTypeDescriptorNode); + + FunctionSignatureNode functionSignatureNode = + createFunctionSignatureNode(createToken(SyntaxKind.OPEN_PAREN_TOKEN), + createSeparatedNodeList(requiredParameterNode), + createToken(SyntaxKind.CLOSE_PAREN_TOKEN, + createEmptyMinutiaeList(), generateMinutiaeListWithWhitespace()), + returnTypeDescriptorNode); + + FunctionBodyBlockNode emptyFunctionBodyNode = + createFunctionBodyBlockNode( + createToken(SyntaxKind.OPEN_BRACE_TOKEN, createEmptyMinutiaeList(), + generateMinutiaeListWithNewline()), null, + createEmptyNodeList(), createToken(SyntaxKind.CLOSE_BRACE_TOKEN), null); + + return createFunctionDefinitionNode( + SyntaxKind.FUNCTION_DEFINITION, null, createNodeList(qualifierList), + createToken(SyntaxKind.FUNCTION_KEYWORD, createEmptyMinutiaeList(), + generateMinutiaeListWithWhitespace()), + createIdentifierToken("inBuiltFuncCodeModifier" + + document.name().replace(".bal", "").replace("/", "_") + .replace("-", "_")), + createEmptyNodeList(), functionSignatureNode, emptyFunctionBodyNode); + } + + private static MinutiaeList generateMinutiaeListWithWhitespace() { + return createMinutiaeList(createWhitespaceMinutiae(" ")); + } + + private static MinutiaeList generateMinutiaeListWithNewline() { + return createMinutiaeList(createWhitespaceMinutiae("\n")); + } + + private static MinutiaeList generateMinutiaeListWithTwoNewline() { + return createMinutiaeList(createWhitespaceMinutiae("\n\n")); + } +} diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/module-info.java b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/module-info.java new file mode 100644 index 000000000000..9485c202e88b --- /dev/null +++ b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/module-info.java @@ -0,0 +1,8 @@ +module compiler.plugin.test.init.func.inbuilt.codemodify { + requires io.ballerina.lang; + requires io.ballerina.parser; + requires io.ballerina.tools.api; + requires compiler.plugin.test.diagnostic.utils.lib; + + exports io.samjs.plugins.init.codemodify; +} diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin new file mode 100644 index 000000000000..d62e3606eed1 --- /dev/null +++ b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin @@ -0,0 +1 @@ +io.samjs.plugins.init.codemodify.InBuiltCodeModifierPlugin \ No newline at end of file diff --git a/project-api/test-artifacts/init-function-codegen-compiler-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin b/project-api/test-artifacts/init-function-codegen-compiler-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin new file mode 100644 index 000000000000..e7781f37ce62 --- /dev/null +++ b/project-api/test-artifacts/init-function-codegen-compiler-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin @@ -0,0 +1 @@ +io.samjs.plugins.init.codegen.CodegenFunctionPlugin \ No newline at end of file diff --git a/project-api/test-artifacts/log-creator-in-built-code-analyzer/build.gradle b/project-api/test-artifacts/log-creator-in-built-code-analyzer/build.gradle new file mode 100644 index 000000000000..ae79a83c033e --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-analyzer/build.gradle @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +apply from: "$rootDir/gradle/javaProject.gradle" + +description = 'Compiler Plugin Tests - Plugin with inbuilt code analyzer' +version = '1.0.0' + +dependencies { + implementation project(':ballerina-lang') + implementation project(':ballerina-parser') + implementation project(':ballerina-tools-api') +} + +ext.moduleName = 'compiler.plugin.with.inbuilt.code.analyzer' + +compileJava { + doFirst { + options.compilerArgs = [ + '--module-path', classpath.asPath, + ] + classpath = files() + } +} diff --git a/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java new file mode 100644 index 000000000000..321295e611e7 --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.luhee.plugins.inbuilt.analyzer; + +import io.ballerina.compiler.syntax.tree.SyntaxKind; +import io.ballerina.projects.plugins.AnalysisTask; +import io.ballerina.projects.plugins.CodeAnalysisContext; +import io.ballerina.projects.plugins.CodeAnalyzer; +import io.ballerina.projects.plugins.CodeModifier; +import io.ballerina.projects.plugins.CodeModifierContext; +import io.ballerina.projects.plugins.CompilerPlugin; +import io.ballerina.projects.plugins.CompilerPluginContext; +import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; + + +/*** + * A in-built code analyzer which adds a log statement to the beginning of the file. + */ +public class LogCodeAnalyzerInBuiltPlugin extends CompilerPlugin { + static String filePath = "./src/test/resources/compiler_plugin_tests/" + + "log_creator_combined_plugin/compiler-plugin.txt"; + + @Override + public void init(CompilerPluginContext pluginContext) { + + pluginContext.addCodeAnalyzer(new LogCodeAnalyzer()); + } + + public static class LogCodeAnalyzer extends CodeAnalyzer { + + @Override + public void init(CodeAnalysisContext analysisContext) { + analysisContext.addCompilationAnalysisTask(compilationAnalysisContext -> { + appendToOutputFile(filePath, "source-analyzer"); + }); + + analysisContext.addSyntaxNodeAnalysisTask(new LogSyntaxNodeAnalysis(), SyntaxKind.FUNCTION_DEFINITION); + } + } + + public static class LogSyntaxNodeAnalysis implements AnalysisTask { + + @Override + public void perform(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) { + appendToOutputFile(filePath, "syntax-node-analysis-analyzer"); + } + } + + private static void appendToOutputFile(String filePath, String content) { + File outputFile = new File(filePath); + try (FileOutputStream fileStream = new FileOutputStream(outputFile, true); + Writer writer = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8)) { + writer.write("in-built-" + content + "\n"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } +} diff --git a/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/module-info.java new file mode 100644 index 000000000000..8720e575a944 --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/module-info.java @@ -0,0 +1,7 @@ +module compiler.plugin.with.inbuilt.code.analyzer { + requires io.ballerina.lang; + requires io.ballerina.parser; + requires io.ballerina.tools.api; + + exports io.luhee.plugins.inbuilt.analyzer; +} \ No newline at end of file diff --git a/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin new file mode 100644 index 000000000000..72bf4ebfcf23 --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin @@ -0,0 +1 @@ +io.luhee.plugins.inbuilt.analyzer.LogCodeAnalyzerInBuiltPlugin \ No newline at end of file diff --git a/project-api/test-artifacts/log-creator-in-built-code-generator/build.gradle b/project-api/test-artifacts/log-creator-in-built-code-generator/build.gradle new file mode 100644 index 000000000000..6290ab699c46 --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-generator/build.gradle @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +apply from: "$rootDir/gradle/javaProject.gradle" + +description = 'Compiler Plugin Tests - Plugin with inbuilt code generator' +version = '1.0.0' + +dependencies { + implementation project(':ballerina-lang') + implementation project(':ballerina-parser') + implementation project(':ballerina-tools-api') +} + +ext.moduleName = 'compiler.plugin.with.inbuilt.code.generator' + +compileJava { + doFirst { + options.compilerArgs = [ + '--module-path', classpath.asPath, + ] + classpath = files() + } +} diff --git a/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java new file mode 100644 index 000000000000..e0933899dec2 --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.luhee.plugins.inbuilt.generator; + +import io.ballerina.compiler.syntax.tree.SyntaxKind; +import io.ballerina.projects.plugins.AnalysisTask; +import io.ballerina.projects.plugins.CodeGenerator; +import io.ballerina.projects.plugins.CodeGeneratorContext; +import io.ballerina.projects.plugins.CodeModifier; +import io.ballerina.projects.plugins.CodeModifierContext; +import io.ballerina.projects.plugins.CompilerPlugin; +import io.ballerina.projects.plugins.CompilerPluginContext; +import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; + + +/*** + * A in-built code generator which adds a log statement to the beginning of the file. + */ +public class LogCodeGeneratorInBuiltPlugin extends CompilerPlugin { + static String filePath = "./src/test/resources/compiler_plugin_tests/" + + "log_creator_combined_plugin/compiler-plugin.txt"; + + @Override + public void init(CompilerPluginContext pluginContext) { + pluginContext.addCodeGenerator(new LogCodeGenerator()); + } + + + + public static class LogCodeGenerator extends CodeGenerator { + @Override + public void init(CodeGeneratorContext generatorContext) { + generatorContext.addSourceGeneratorTask(sourceGeneratorContext -> { + appendToOutputFile(filePath, "source-generator"); + }); + + generatorContext.addSyntaxNodeAnalysisTask(new LogSyntaxNodeAnalysis(), SyntaxKind.FUNCTION_DEFINITION); + } + + } + + public static class LogSyntaxNodeAnalysis implements AnalysisTask { + + @Override + public void perform(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) { + appendToOutputFile(filePath, "syntax-node-analysis-generator"); + } + } + + + private static void appendToOutputFile(String filePath, String content) { + File outputFile = new File(filePath); + try (FileOutputStream fileStream = new FileOutputStream(outputFile, true); + Writer writer = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8)) { + writer.write("in-built-" + content + "\n"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } +} diff --git a/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/module-info.java new file mode 100644 index 000000000000..4a8c3933a596 --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/module-info.java @@ -0,0 +1,7 @@ +module compiler.plugin.with.inbuilt.code.generator { + requires io.ballerina.lang; + requires io.ballerina.parser; + requires io.ballerina.tools.api; + + exports io.luhee.plugins.inbuilt.generator; +} \ No newline at end of file diff --git a/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin new file mode 100644 index 000000000000..74c34ef064ed --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin @@ -0,0 +1 @@ +io.luhee.plugins.inbuilt.generator.LogCodeGeneratorInBuiltPlugin \ No newline at end of file diff --git a/project-api/test-artifacts/log-creator-in-built-code-modifier/build.gradle b/project-api/test-artifacts/log-creator-in-built-code-modifier/build.gradle new file mode 100644 index 000000000000..458a3620c541 --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-modifier/build.gradle @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +apply from: "$rootDir/gradle/javaProject.gradle" + +description = 'Compiler Plugin Tests - Plugin with inbuilt code modifier' +version = '1.0.0' + +dependencies { + implementation project(':ballerina-lang') + implementation project(':ballerina-parser') + implementation project(':ballerina-tools-api') +} + +ext.moduleName = 'compiler.plugin.with.inbuilt.code.modifier' + +compileJava { + doFirst { + options.compilerArgs = [ + '--module-path', classpath.asPath, + ] + classpath = files() + } +} diff --git a/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java new file mode 100644 index 000000000000..dd4144f657fe --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.luhee.plugins.inbuilt.modifier; + +import io.ballerina.compiler.syntax.tree.SyntaxKind; +import io.ballerina.projects.plugins.AnalysisTask; +import io.ballerina.projects.plugins.CodeModifier; +import io.ballerina.projects.plugins.CodeModifierContext; +import io.ballerina.projects.plugins.CompilerPlugin; +import io.ballerina.projects.plugins.CompilerPluginContext; +import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; + + +/*** + * A in-built code modifier which adds a log statement to the beginning of the file. + */ +public class LogCodeModifierInBuiltPlugin extends CompilerPlugin { + static String filePath = "./src/test/resources/compiler_plugin_tests/" + + "log_creator_combined_plugin/compiler-plugin.txt"; + + @Override + public void init(CompilerPluginContext pluginContext) { + pluginContext.addCodeModifier(new LogCodeModifier()); + } + + + + public static class LogCodeModifier extends CodeModifier { + @Override + public void init(CodeModifierContext modifierContext) { + modifierContext.addSourceModifierTask(sourceGeneratorContext -> { + appendToOutputFile(filePath, "source-modifier"); + }); + + modifierContext.addSyntaxNodeAnalysisTask(new LogSyntaxNodeAnalysis(), SyntaxKind.FUNCTION_DEFINITION); + } + + } + + public static class LogSyntaxNodeAnalysis implements AnalysisTask { + + @Override + public void perform(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) { + appendToOutputFile(filePath, "syntax-node-analysis-modifier"); + } + } + + private static void appendToOutputFile(String filePath, String content) { + File outputFile = new File(filePath); + try (FileOutputStream fileStream = new FileOutputStream(outputFile, true); + Writer writer = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8)) { + writer.write("in-built-" + content + "\n"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } +} diff --git a/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/module-info.java new file mode 100644 index 000000000000..633d320462fa --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/module-info.java @@ -0,0 +1,7 @@ +module compiler.plugin.with.inbuilt.code.modifier { + requires io.ballerina.lang; + requires io.ballerina.parser; + requires io.ballerina.tools.api; + + exports io.luhee.plugins.inbuilt.modifier; +} \ No newline at end of file diff --git a/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin new file mode 100644 index 000000000000..eb431f064d72 --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin @@ -0,0 +1 @@ +io.luhee.plugins.inbuilt.modifier.LogCodeModifierInBuiltPlugin \ No newline at end of file diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/build.gradle b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/build.gradle new file mode 100644 index 000000000000..4c2ea37bcc96 --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/build.gradle @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +apply from: "$rootDir/gradle/javaProject.gradle" + +description = 'Compiler Plugin Tests - Plugin with package provided code analyzer' +version = '1.0.0' + +dependencies { + implementation project(':ballerina-lang') + implementation project(':ballerina-parser') + implementation project(':ballerina-tools-api') +} + +ext.moduleName = 'compiler.plugin.with.pkg.code.analyzer' + +compileJava { + doFirst { + options.compilerArgs = [ + '--module-path', classpath.asPath, + ] + classpath = files() + } +} diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java new file mode 100644 index 000000000000..53473d3434dc --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.luhee.plugins.pkg.analyzer; + + +import io.ballerina.compiler.syntax.tree.SyntaxKind; +import io.ballerina.projects.plugins.AnalysisTask; +import io.ballerina.projects.plugins.CodeAnalysisContext; +import io.ballerina.projects.plugins.CodeAnalyzer; +import io.ballerina.projects.plugins.CompilerPlugin; +import io.ballerina.projects.plugins.CompilerPluginContext; +import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; + + +/*** + * A package provided code analyzer which adds a log statement to the beginning of the file. + */ +public class LogCodeAnalyzerPkgPlugin extends CompilerPlugin { + + static String filePath = "./src/test/resources/compiler_plugin_tests/" + + "log_creator_combined_plugin/compiler-plugin.txt"; + + @Override + public void init(CompilerPluginContext pluginContext) { + pluginContext.addCodeAnalyzer(new LogCodeAnalyzer()); + } + + + + public static class LogCodeAnalyzer extends CodeAnalyzer { + @Override + public void init(CodeAnalysisContext analysisContext) { + analysisContext.addCompilationAnalysisTask(sourceGeneratorContext -> { + appendToOutputFile(filePath, "source-analyzer"); + }); + + analysisContext.addSyntaxNodeAnalysisTask(new LogSyntaxNodeAnalysis(), SyntaxKind.FUNCTION_DEFINITION); + } + + } + + public static class LogSyntaxNodeAnalysis implements AnalysisTask { + + @Override + public void perform(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) { + appendToOutputFile(filePath, "syntax-node-analysis-analyzer"); + } + } + + private static void appendToOutputFile(String filePath, String content) { + File outputFile = new File(filePath); + try (FileOutputStream fileStream = new FileOutputStream(outputFile, true); + Writer writer = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8)) { + writer.write("pkg-provided-" + content + "\n"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } +} diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/module-info.java new file mode 100644 index 000000000000..2a833160a25e --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/module-info.java @@ -0,0 +1,7 @@ +module compiler.plugin.with.pkg.code.analyzer { + requires io.ballerina.lang; + requires io.ballerina.parser; + requires io.ballerina.tools.api; + + exports io.luhee.plugins.pkg.analyzer; +} \ No newline at end of file diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-generator/build.gradle b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/build.gradle new file mode 100644 index 000000000000..37c8fe10a587 --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/build.gradle @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +apply from: "$rootDir/gradle/javaProject.gradle" + +description = 'Compiler Plugin Tests - Plugin with package provided code generator' +version = '1.0.0' + +dependencies { + implementation project(':ballerina-lang') + implementation project(':ballerina-parser') + implementation project(':ballerina-tools-api') +} + +ext.moduleName = 'compiler.plugin.with.pkg.code.generator' + +compileJava { + doFirst { + options.compilerArgs = [ + '--module-path', classpath.asPath, + ] + classpath = files() + } +} diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java new file mode 100644 index 000000000000..942678722ec8 --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.luhee.plugins.pkg.generator; + +import io.ballerina.compiler.syntax.tree.SyntaxKind; +import io.ballerina.projects.plugins.AnalysisTask; +import io.ballerina.projects.plugins.CodeGenerator; +import io.ballerina.projects.plugins.CodeGeneratorContext; +import io.ballerina.projects.plugins.CompilerPlugin; +import io.ballerina.projects.plugins.CompilerPluginContext; +import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; + + +/*** + * A package provided code generator which adds a log statement to the beginning of the file. + */ +public class LogCodeGeneratorPkgPlugin extends CompilerPlugin { + + static String filePath = "./src/test/resources/compiler_plugin_tests/" + + "log_creator_combined_plugin/compiler-plugin.txt"; + + @Override + public void init(CompilerPluginContext pluginContext) { + pluginContext.addCodeGenerator(new LogCodeGenerator()); + } + + + + public static class LogCodeGenerator extends CodeGenerator { + @Override + public void init(CodeGeneratorContext generatorContext) { + generatorContext.addSourceGeneratorTask(sourceGeneratorContext -> { + appendToOutputFile(filePath, "source-generator"); + }); + + generatorContext.addSyntaxNodeAnalysisTask(new LogSyntaxNodeAnalysis(), SyntaxKind.FUNCTION_DEFINITION); + } + + } + + public static class LogSyntaxNodeAnalysis implements AnalysisTask { + + @Override + public void perform(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) { + appendToOutputFile(filePath, "syntax-node-analysis-generator"); + } + } + + + private static void appendToOutputFile(String filePath, String content) { + File outputFile = new File(filePath); + try (FileOutputStream fileStream = new FileOutputStream(outputFile, true); + Writer writer = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8)) { + writer.write("pkg-provided-" + content + "\n"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } +} diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/module-info.java new file mode 100644 index 000000000000..c10dff2972da --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/module-info.java @@ -0,0 +1,7 @@ +module compiler.plugin.with.pkg.code.generator { + requires io.ballerina.lang; + requires io.ballerina.parser; + requires io.ballerina.tools.api; + + exports io.luhee.plugins.pkg.generator; +} \ No newline at end of file diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/build.gradle b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/build.gradle new file mode 100644 index 000000000000..1efd15783047 --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/build.gradle @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +apply from: "$rootDir/gradle/javaProject.gradle" + +description = 'Compiler Plugin Tests - Plugin with package provided code modifier' +version = '1.0.0' + +dependencies { + implementation project(':ballerina-lang') + implementation project(':ballerina-parser') + implementation project(':ballerina-tools-api') +} + +ext.moduleName = 'compiler.plugin.with.pkg.code.modifier' + +compileJava { + doFirst { + options.compilerArgs = [ + '--module-path', classpath.asPath, + ] + classpath = files() + } +} diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java new file mode 100644 index 000000000000..40f4421252ef --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.luhee.plugins.pkg.modifier; + + +import io.ballerina.compiler.syntax.tree.SyntaxKind; +import io.ballerina.projects.plugins.AnalysisTask; +import io.ballerina.projects.plugins.CodeModifier; +import io.ballerina.projects.plugins.CodeModifierContext; +import io.ballerina.projects.plugins.CompilerPlugin; +import io.ballerina.projects.plugins.CompilerPluginContext; +import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; + + +/*** + * A package provided code modifier which adds a log statement to the beginning of the file. + */ +public class LogCodeModifierPkgPlugin extends CompilerPlugin { + + static String filePath = "./src/test/resources/compiler_plugin_tests/" + + "log_creator_combined_plugin/compiler-plugin.txt"; + + @Override + public void init(CompilerPluginContext pluginContext) { + pluginContext.addCodeModifier(new LogCodeModifier()); + } + + + + public static class LogCodeModifier extends CodeModifier { + @Override + public void init(CodeModifierContext modifierContext) { + modifierContext.addSourceModifierTask(sourceGeneratorContext -> { + appendToOutputFile(filePath, "source-modifier"); + }); + + modifierContext.addSyntaxNodeAnalysisTask(new LogSyntaxNodeAnalysis(), SyntaxKind.FUNCTION_DEFINITION); + } + + } + + public static class LogSyntaxNodeAnalysis implements AnalysisTask { + + @Override + public void perform(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) { + appendToOutputFile(filePath, "syntax-node-analysis-modifier"); + } + } + + private static void appendToOutputFile(String filePath, String content) { + File outputFile = new File(filePath); + try (FileOutputStream fileStream = new FileOutputStream(outputFile, true); + Writer writer = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8)) { + writer.write("pkg-provided-" + content + "\n"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } +} diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/module-info.java new file mode 100644 index 000000000000..df669a0da5a7 --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/module-info.java @@ -0,0 +1,7 @@ +module compiler.plugin.with.pkg.code.modifier { + requires io.ballerina.lang; + requires io.ballerina.parser; + requires io.ballerina.tools.api; + + exports io.luhee.plugins.pkg.modifier; +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index b68373d3d4dc..799b2305d961 100644 --- a/settings.gradle +++ b/settings.gradle @@ -126,6 +126,13 @@ include(':project-api-test-artifact:bad-sad-compiler-plugin') include(':project-api-test-artifact:logging-file-appender-plugin') include(':project-api-test-artifact:init-function-codegen-compiler-plugin') include(':project-api-test-artifact:init-function-code-modify-compiler-plugin') +include(':project-api-test-artifact:in-built-code-modifier-plugin') +include(':project-api-test-artifact:log-creator-in-built-code-modifier') +include(':project-api-test-artifact:log-creator-in-built-code-generator') +include(':project-api-test-artifact:log-creator-in-built-code-analyzer') +include(':project-api-test-artifact:log-creator-pkg-provided-code-modifier') +include(':project-api-test-artifact:log-creator-pkg-provided-code-generator') +include(':project-api-test-artifact:log-creator-pkg-provided-code-analyzer') include(':project-api-test-artifact:simple-code-gen-plugin-with-resource-gen') include(':project-api-test-artifact:init-function-diagnostic-compiler-plugin') include(':project-api-test-artifact:compiler-plugin-with-analyzer-generator-modifier') @@ -152,6 +159,13 @@ project(':project-api-test-artifact:bad-sad-compiler-plugin').projectDir = file( project(':project-api-test-artifact:logging-file-appender-plugin').projectDir = file('project-api/test-artifacts/logging-file-appender-plugin') project(':project-api-test-artifact:init-function-codegen-compiler-plugin').projectDir = file('project-api/test-artifacts/init-function-codegen-compiler-plugin') project(':project-api-test-artifact:init-function-code-modify-compiler-plugin').projectDir = file('project-api/test-artifacts/init-function-code-modify-compiler-plugin') +project(':project-api-test-artifact:in-built-code-modifier-plugin').projectDir = file('project-api/test-artifacts/in-built-code-modifier-plugin') +project(':project-api-test-artifact:log-creator-in-built-code-modifier').projectDir = file('project-api/test-artifacts/log-creator-in-built-code-modifier') +project(':project-api-test-artifact:log-creator-in-built-code-generator').projectDir = file('project-api/test-artifacts/log-creator-in-built-code-generator') +project(':project-api-test-artifact:log-creator-in-built-code-analyzer').projectDir = file('project-api/test-artifacts/log-creator-in-built-code-analyzer') +project(':project-api-test-artifact:log-creator-pkg-provided-code-modifier').projectDir = file('project-api/test-artifacts/log-creator-pkg-provided-code-modifier') +project(':project-api-test-artifact:log-creator-pkg-provided-code-generator').projectDir = file('project-api/test-artifacts/log-creator-pkg-provided-code-generator') +project(':project-api-test-artifact:log-creator-pkg-provided-code-analyzer').projectDir = file('project-api/test-artifacts/log-creator-pkg-provided-code-analyzer') project(':project-api-test-artifact:simple-code-gen-plugin-with-resource-gen').projectDir = file('project-api/test-artifacts/simple-code-gen-plugin-with-resource-gen') project(':project-api-test-artifact:init-function-diagnostic-compiler-plugin').projectDir = file('project-api/test-artifacts/init-function-diagnostic-compiler-plugin') project(':project-api-test-artifact:compiler-plugin-with-analyzer-generator-modifier').projectDir = file('project-api/test-artifacts/compiler-plugin-with-analyzer-generator-modifier') From 924aae5d7cd02573b32673214b8a2a5369205398 Mon Sep 17 00:00:00 2001 From: Thevakumar-Luheerathan Date: Thu, 13 Jul 2023 18:32:59 +0530 Subject: [PATCH 53/70] Fix build failure --- .../build.gradle | 2 +- .../codemodify/InBuiltCodeModifierPlugin.java | 2 +- .../codemodify/InsertFuncCodeModifier.java | 3 +- ....ballerina.projects.plugins.CompilerPlugin | 2 +- .../spotbugs-exclude.xml | 29 +++++++++++++++++++ .../LogCodeAnalyzerInBuiltPlugin.java | 11 ++++--- .../src/main/java/module-info.java | 2 +- ....ballerina.projects.plugins.CompilerPlugin | 2 +- .../spotbugs-exclude.xml | 29 +++++++++++++++++++ .../LogCodeGeneratorInBuiltPlugin.java | 13 +++++---- .../src/main/java/module-info.java | 2 +- ....ballerina.projects.plugins.CompilerPlugin | 2 +- .../spotbugs-exclude.xml | 29 +++++++++++++++++++ .../LogCodeModifierInBuiltPlugin.java | 10 +++++-- .../src/main/java/module-info.java | 2 +- ....ballerina.projects.plugins.CompilerPlugin | 2 +- .../spotbugs-exclude.xml | 29 +++++++++++++++++++ .../analyzer/LogCodeAnalyzerPkgPlugin.java | 7 ++++- .../src/main/java/module-info.java | 2 +- .../spotbugs-exclude.xml | 29 +++++++++++++++++++ .../generator/LogCodeGeneratorPkgPlugin.java | 10 +++++-- .../src/main/java/module-info.java | 2 +- .../spotbugs-exclude.xml | 29 +++++++++++++++++++ .../modifier/LogCodeModifierPkgPlugin.java | 10 +++++-- .../src/main/java/module-info.java | 2 +- 25 files changed, 228 insertions(+), 34 deletions(-) create mode 100644 project-api/test-artifacts/log-creator-in-built-code-analyzer/spotbugs-exclude.xml create mode 100644 project-api/test-artifacts/log-creator-in-built-code-generator/spotbugs-exclude.xml create mode 100644 project-api/test-artifacts/log-creator-in-built-code-modifier/spotbugs-exclude.xml create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/spotbugs-exclude.xml create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-generator/spotbugs-exclude.xml create mode 100644 project-api/test-artifacts/log-creator-pkg-provided-code-modifier/spotbugs-exclude.xml diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle b/project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle index 68ac0874ead5..7d0867c1c1be 100644 --- a/project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle +++ b/project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2023, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java index 3db7efab821e..202dd6ff068a 100644 --- a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java +++ b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2023, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java index 83f46ba6cc9a..af9c36558187 100644 --- a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java +++ b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2023, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -61,7 +61,6 @@ /** * A {@code CodeModifier} implementation that modify each bal file by adding a specific function to the end. * - * @since 2201.0.3 */ public class InsertFuncCodeModifier extends CodeModifier { diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin index d62e3606eed1..a1f7806ab14d 100644 --- a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin +++ b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin @@ -1 +1 @@ -io.samjs.plugins.init.codemodify.InBuiltCodeModifierPlugin \ No newline at end of file +io.samjs.plugins.init.codemodify.InBuiltCodeModifierPlugin diff --git a/project-api/test-artifacts/log-creator-in-built-code-analyzer/spotbugs-exclude.xml b/project-api/test-artifacts/log-creator-in-built-code-analyzer/spotbugs-exclude.xml new file mode 100644 index 000000000000..949b2fee8182 --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-analyzer/spotbugs-exclude.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + diff --git a/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java index 321295e611e7..6eedf27e1379 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java +++ b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java @@ -19,8 +19,6 @@ import io.ballerina.projects.plugins.AnalysisTask; import io.ballerina.projects.plugins.CodeAnalysisContext; import io.ballerina.projects.plugins.CodeAnalyzer; -import io.ballerina.projects.plugins.CodeModifier; -import io.ballerina.projects.plugins.CodeModifierContext; import io.ballerina.projects.plugins.CompilerPlugin; import io.ballerina.projects.plugins.CompilerPluginContext; import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; @@ -31,8 +29,6 @@ import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; -import java.nio.file.Path; -import java.nio.file.Paths; /*** @@ -48,6 +44,10 @@ public void init(CompilerPluginContext pluginContext) { pluginContext.addCodeAnalyzer(new LogCodeAnalyzer()); } + /*** + * A in-built code analyzer which adds a log statement to the beginning of the file. + */ + public static class LogCodeAnalyzer extends CodeAnalyzer { @Override @@ -60,6 +60,9 @@ public void init(CodeAnalysisContext analysisContext) { } } + /*** + * A in-built code analyzer which adds a log statement to the beginning of the file. + */ public static class LogSyntaxNodeAnalysis implements AnalysisTask { @Override diff --git a/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/module-info.java index 8720e575a944..f2189331f0f1 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/module-info.java +++ b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/module-info.java @@ -4,4 +4,4 @@ requires io.ballerina.tools.api; exports io.luhee.plugins.inbuilt.analyzer; -} \ No newline at end of file +} diff --git a/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin index 72bf4ebfcf23..6295dfa02ef0 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin +++ b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin @@ -1 +1 @@ -io.luhee.plugins.inbuilt.analyzer.LogCodeAnalyzerInBuiltPlugin \ No newline at end of file +io.luhee.plugins.inbuilt.analyzer.LogCodeAnalyzerInBuiltPlugin diff --git a/project-api/test-artifacts/log-creator-in-built-code-generator/spotbugs-exclude.xml b/project-api/test-artifacts/log-creator-in-built-code-generator/spotbugs-exclude.xml new file mode 100644 index 000000000000..1a06c437ca55 --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-generator/spotbugs-exclude.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + diff --git a/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java index e0933899dec2..c74e081d3e89 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java +++ b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java @@ -19,8 +19,6 @@ import io.ballerina.projects.plugins.AnalysisTask; import io.ballerina.projects.plugins.CodeGenerator; import io.ballerina.projects.plugins.CodeGeneratorContext; -import io.ballerina.projects.plugins.CodeModifier; -import io.ballerina.projects.plugins.CodeModifierContext; import io.ballerina.projects.plugins.CompilerPlugin; import io.ballerina.projects.plugins.CompilerPluginContext; import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; @@ -31,8 +29,7 @@ import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; -import java.nio.file.Path; -import java.nio.file.Paths; + /*** @@ -47,8 +44,9 @@ public void init(CompilerPluginContext pluginContext) { pluginContext.addCodeGenerator(new LogCodeGenerator()); } - - + /*** + * A in-built code generator which adds a log statement to the beginning of the file. + */ public static class LogCodeGenerator extends CodeGenerator { @Override public void init(CodeGeneratorContext generatorContext) { @@ -61,6 +59,9 @@ public void init(CodeGeneratorContext generatorContext) { } + /*** + * A in-built code generator which adds a log statement to the beginning of the file. + */ public static class LogSyntaxNodeAnalysis implements AnalysisTask { @Override diff --git a/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/module-info.java index 4a8c3933a596..8622c9504ad6 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/module-info.java +++ b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/module-info.java @@ -4,4 +4,4 @@ requires io.ballerina.tools.api; exports io.luhee.plugins.inbuilt.generator; -} \ No newline at end of file +} diff --git a/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin index 74c34ef064ed..1593122a849e 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin +++ b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin @@ -1 +1 @@ -io.luhee.plugins.inbuilt.generator.LogCodeGeneratorInBuiltPlugin \ No newline at end of file +io.luhee.plugins.inbuilt.generator.LogCodeGeneratorInBuiltPlugin diff --git a/project-api/test-artifacts/log-creator-in-built-code-modifier/spotbugs-exclude.xml b/project-api/test-artifacts/log-creator-in-built-code-modifier/spotbugs-exclude.xml new file mode 100644 index 000000000000..6d99d415d30d --- /dev/null +++ b/project-api/test-artifacts/log-creator-in-built-code-modifier/spotbugs-exclude.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + diff --git a/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java index dd4144f657fe..28a3ef35e519 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java +++ b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java @@ -29,8 +29,7 @@ import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; -import java.nio.file.Path; -import java.nio.file.Paths; + /*** @@ -46,7 +45,9 @@ public void init(CompilerPluginContext pluginContext) { } - + /*** + * A in-built code modifier which adds a log statement to the beginning of the file. + */ public static class LogCodeModifier extends CodeModifier { @Override public void init(CodeModifierContext modifierContext) { @@ -59,6 +60,9 @@ public void init(CodeModifierContext modifierContext) { } + /*** + * A in-built code modifier which adds a log statement to the beginning of the file. + */ public static class LogSyntaxNodeAnalysis implements AnalysisTask { @Override diff --git a/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/module-info.java index 633d320462fa..c1cd47abe73e 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/module-info.java +++ b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/module-info.java @@ -4,4 +4,4 @@ requires io.ballerina.tools.api; exports io.luhee.plugins.inbuilt.modifier; -} \ No newline at end of file +} diff --git a/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin index eb431f064d72..17628769f199 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin +++ b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin @@ -1 +1 @@ -io.luhee.plugins.inbuilt.modifier.LogCodeModifierInBuiltPlugin \ No newline at end of file +io.luhee.plugins.inbuilt.modifier.LogCodeModifierInBuiltPlugin diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/spotbugs-exclude.xml b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/spotbugs-exclude.xml new file mode 100644 index 000000000000..78861bab4c1d --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/spotbugs-exclude.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java index 53473d3434dc..7ec7ac7b0c04 100644 --- a/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java @@ -46,7 +46,9 @@ public void init(CompilerPluginContext pluginContext) { } - + /*** + * A package provided code analyzer which adds a log statement to the beginning of the file. + */ public static class LogCodeAnalyzer extends CodeAnalyzer { @Override public void init(CodeAnalysisContext analysisContext) { @@ -59,6 +61,9 @@ public void init(CodeAnalysisContext analysisContext) { } + /*** + * A package provided code analyzer which adds a log statement to the beginning of the file. + */ public static class LogSyntaxNodeAnalysis implements AnalysisTask { @Override diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/module-info.java index 2a833160a25e..8283baacb85a 100644 --- a/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/module-info.java +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/module-info.java @@ -4,4 +4,4 @@ requires io.ballerina.tools.api; exports io.luhee.plugins.pkg.analyzer; -} \ No newline at end of file +} diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-generator/spotbugs-exclude.xml b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/spotbugs-exclude.xml new file mode 100644 index 000000000000..78cbb6ee6d88 --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/spotbugs-exclude.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java index 942678722ec8..45200368510a 100644 --- a/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java @@ -29,8 +29,7 @@ import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; -import java.nio.file.Path; -import java.nio.file.Paths; + /*** @@ -47,7 +46,9 @@ public void init(CompilerPluginContext pluginContext) { } - + /*** + * A package provided code generator which adds a log statement to the beginning of the file. + */ public static class LogCodeGenerator extends CodeGenerator { @Override public void init(CodeGeneratorContext generatorContext) { @@ -60,6 +61,9 @@ public void init(CodeGeneratorContext generatorContext) { } + /*** + * A package provided code generator which adds a log statement to the beginning of the file. + */ public static class LogSyntaxNodeAnalysis implements AnalysisTask { @Override diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/module-info.java index c10dff2972da..8cb3fec5d33e 100644 --- a/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/module-info.java +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/module-info.java @@ -4,4 +4,4 @@ requires io.ballerina.tools.api; exports io.luhee.plugins.pkg.generator; -} \ No newline at end of file +} diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/spotbugs-exclude.xml b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/spotbugs-exclude.xml new file mode 100644 index 000000000000..96d2d647b3a4 --- /dev/null +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/spotbugs-exclude.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java index 40f4421252ef..676475cd5ef4 100644 --- a/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java @@ -30,8 +30,7 @@ import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; -import java.nio.file.Path; -import java.nio.file.Paths; + /*** @@ -48,7 +47,9 @@ public void init(CompilerPluginContext pluginContext) { } - + /*** + * A package provided code modifier which adds a log statement to the beginning of the file. + */ public static class LogCodeModifier extends CodeModifier { @Override public void init(CodeModifierContext modifierContext) { @@ -61,6 +62,9 @@ public void init(CodeModifierContext modifierContext) { } + /*** + * A package provided code modifier which adds a log statement to the beginning of the file. + */ public static class LogSyntaxNodeAnalysis implements AnalysisTask { @Override diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/module-info.java b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/module-info.java index df669a0da5a7..b4a995d196b0 100644 --- a/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/module-info.java +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/module-info.java @@ -4,4 +4,4 @@ requires io.ballerina.tools.api; exports io.luhee.plugins.pkg.modifier; -} \ No newline at end of file +} From aa3460a3613b94421be3e974020f55e5a3b90806 Mon Sep 17 00:00:00 2001 From: Thevakumar-Luheerathan Date: Thu, 13 Jul 2023 19:12:43 +0530 Subject: [PATCH 54/70] Fix BuildCommandTest failure --- .../java/io/ballerina/cli/cmd/RunCommandTest.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java index 94d724f5c5d2..f4348c210d79 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java @@ -9,8 +9,9 @@ import org.apache.commons.io.filefilter.WildcardFileFilter; import org.ballerinalang.test.BCompileUtil; import org.testng.Assert; -import org.testng.annotations.AfterClass; +import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; import picocli.CommandLine; @@ -39,6 +40,11 @@ public class RunCommandTest extends BaseCommandTest { static Path logFile = Paths.get("./src/test/resources/compiler_plugin_tests/" + "log_creator_combined_plugin/compiler-plugin.txt"); + @BeforeSuite + public void setupSuite() throws IOException { + Files.createDirectories(logFile.getParent()); + Files.writeString(logFile, ""); + } @BeforeClass public void setup() throws IOException { super.setup(); @@ -56,8 +62,6 @@ public void setup() throws IOException { } catch (URISyntaxException e) { Assert.fail("error loading resources"); } - Files.createDirectories(logFile.getParent()); - Files.writeString(logFile, ""); } @Test(description = "Run a valid ballerina file") @@ -376,7 +380,7 @@ public void testRunEmptyPackage() throws IOException { Assert.assertEquals(buildLog.replaceAll("\r", ""), getOutput("build-empty-package.txt")); } - @AfterClass + @AfterSuite public void cleanUp() throws IOException { Files.deleteIfExists(logFile); Files.deleteIfExists(logFile.getParent()); From be9949a52729eb2105a2b4a35bfde5acbef0a722 Mon Sep 17 00:00:00 2001 From: Thevakumar-Luheerathan Date: Fri, 14 Jul 2023 14:16:11 +0530 Subject: [PATCH 55/70] Fix compiler plugin negative test failure --- project-api/project-api-test/build.gradle | 1 - .../test/plugins/CompilerPluginTests.java | 61 +------ .../build.gradle | 39 ----- .../codemodify/InBuiltCodeModifierPlugin.java | 36 ---- .../codemodify/InsertFuncCodeModifier.java | 161 ------------------ .../src/main/java/module-info.java | 8 - ....ballerina.projects.plugins.CompilerPlugin | 1 - settings.gradle | 2 - 8 files changed, 4 insertions(+), 305 deletions(-) delete mode 100644 project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle delete mode 100644 project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java delete mode 100644 project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java delete mode 100644 project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/module-info.java delete mode 100644 project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin diff --git a/project-api/project-api-test/build.gradle b/project-api/project-api-test/build.gradle index b82fdb42d5a3..c2a7cf181682 100644 --- a/project-api/project-api-test/build.gradle +++ b/project-api/project-api-test/build.gradle @@ -48,7 +48,6 @@ dependencies { testRuntime project(':ballerina-runtime') testRuntime project(':project-api-test-artifact:logging-file-appender-plugin') testRuntime project(':compiler-plugins:package-semantic-analyzer') - testRuntime project(':project-api-test-artifact:in-built-code-modifier-plugin') testRuntime project(':project-api-test-artifact:log-creator-in-built-code-modifier') testRuntime project(':project-api-test-artifact:log-creator-in-built-code-generator') testRuntime project(':project-api-test-artifact:log-creator-in-built-code-analyzer') diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java index 6f900ca78198..ada61679a122 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginTests.java @@ -40,7 +40,7 @@ import org.ballerinalang.test.BRunUtil; import org.ballerinalang.test.CompileResult; import org.testng.Assert; -import org.testng.annotations.AfterClass; +import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; @@ -369,65 +369,12 @@ public void testCompilerPluginCodeModifyBasic() { DiagnosticResult diagnosticResult = compilation.diagnosticResult(); diagnosticResult.diagnostics().forEach(OUT::println); Assert.assertFalse(diagnosticResult.hasErrors(), "Unexpected errors in compilation"); - Assert.assertEquals(diagnosticResult.diagnosticCount(), 11, "Unexpected compilation diagnostics"); + Assert.assertEquals(diagnosticResult.diagnosticCount(), 8, "Unexpected compilation diagnostics"); // Check direct package dependencies count is 1 Assert.assertEquals(newPackage.packageDependencies().size(), 1, "Unexpected number of dependencies"); } - @Test(description = "Test loading in-built code modifier by default") - public void testCompilerPluginCodeModifyInBuilt() { - Package currentPackage = loadPackage("in_built_plugin_code_modify"); - // Check the document count in the current package - Assert.assertEquals(currentPackage.getDefaultModule().documentIds().size(), 2); - - // Running the compilation - currentPackage.getCompilation(); - - - // Running the code generation - CodeModifierResult codeModifierResult = currentPackage.runCodeModifierPlugins(); - - // Compiling the new package - Project project = currentPackage.project(); - Package newPackage = codeModifierResult.updatedPackage().orElse(null); - Assert.assertNotNull(newPackage, "Cannot be null, because there exist code modifiers"); - Assert.assertSame(newPackage.project(), project); - Assert.assertSame(newPackage, project.currentPackage()); - - // Modified source files - Assert.assertEquals(newPackage.getDefaultModule().documentIds().size(), 2); - for (DocumentId documentId : newPackage.getDefaultModule().documentIds()) { - Document document = newPackage.getDefaultModule().document(documentId); - // The code generator adds specific function to the end of every source file. - String specificFunction = "public function inBuiltFuncCodeModifier" - + document.name().replace(".bal", "").replace("/", "_") - + "(string params) returns error? {\n}"; - Assert.assertTrue(document.syntaxTree().toSourceCode().contains(specificFunction)); - } - - // Modified test source files - Assert.assertEquals(newPackage.getDefaultModule().testDocumentIds().size(), 1); - for (DocumentId documentId : newPackage.getDefaultModule().testDocumentIds()) { - Document document = newPackage.getDefaultModule().document(documentId); - // The code generator adds specific function to the end of every source file. - String specificFunction = "public function inBuiltFuncCodeModifier" - + document.name().replace(".bal", "").replace("/", "_").replace("-", "_") - + "(string params) returns error? {\n}"; - Assert.assertTrue(document.syntaxTree().toSourceCode().contains(specificFunction)); - } - - PackageCompilation compilation = newPackage.getCompilation(); - // Check whether there are any diagnostics - DiagnosticResult diagnosticResult = compilation.diagnosticResult(); - diagnosticResult.diagnostics().forEach(OUT::println); - Assert.assertFalse(diagnosticResult.hasErrors(), "Unexpected errors in compilation"); - Assert.assertEquals(diagnosticResult.diagnosticCount(), 0, "Unexpected compilation diagnostics"); - - // Check direct package dependencies count is 1 - Assert.assertEquals(newPackage.packageDependencies().size(), 0, "Unexpected number of dependencies"); - } - @Test(description = "Test a combination of in-built and package provided compiler plugins") public void testCombinationOfCompilerPlugins() throws IOException { Path logFile = Paths.get("./src/test/resources/compiler_plugin_tests/" + @@ -506,7 +453,7 @@ public void testCompilerPluginSingleBalFileCodeModifyBasic() { DiagnosticResult diagnosticResult = compilation.diagnosticResult(); diagnosticResult.diagnostics().forEach(OUT::println); Assert.assertFalse(diagnosticResult.hasErrors(), "Unexpected errors in compilation"); - Assert.assertEquals(diagnosticResult.diagnosticCount(), 5, "Unexpected compilation diagnostics"); + Assert.assertEquals(diagnosticResult.diagnosticCount(), 4, "Unexpected compilation diagnostics"); // Check direct package dependencies count is 1 Assert.assertEquals(newPackage.packageDependencies().size(), 1, "Unexpected number of dependencies"); @@ -715,7 +662,7 @@ private void releaseLock() { } } - @AfterClass + @AfterSuite private void cleanup() throws IOException { Path logFile = Paths.get("./src/test/resources/compiler_plugin_tests/" + "log_creator_combined_plugin/compiler-plugin.txt"); diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle b/project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle deleted file mode 100644 index 7d0867c1c1be..000000000000 --- a/project-api/test-artifacts/in-built-code-modifier-plugin/build.gradle +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2023, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -apply from: "$rootDir/gradle/javaProject.gradle" - -description = 'Compiler Plugin Tests - In Built Code Modify Init Function' -version = '1.0.0' - -dependencies { - implementation project(':ballerina-lang') - implementation project(':ballerina-parser') - implementation project(':ballerina-tools-api') - implementation project(':project-api-test-artifact:diagnostic-utils-lib') -} - -ext.moduleName = 'compiler.plugin.test.init.func.inbuilt.codemodify' - -compileJava { - doFirst { - options.compilerArgs = [ - '--module-path', classpath.asPath, - ] - classpath = files() - } -} diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java deleted file mode 100644 index 202dd6ff068a..000000000000 --- a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InBuiltCodeModifierPlugin.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2023, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package io.samjs.plugins.init.codemodify; - - -import io.ballerina.projects.plugins.CompilerPlugin; -import io.ballerina.projects.plugins.CompilerPluginContext; - - -/** - * A sample {@code CompilerPlugin} that modifies source files. - * - * @since 2201.0.3 - */ -public class InBuiltCodeModifierPlugin extends CompilerPlugin { - - @Override - public void init(CompilerPluginContext pluginContext) { - pluginContext.addCodeModifier(new InsertFuncCodeModifier()); - } -} diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java deleted file mode 100644 index af9c36558187..000000000000 --- a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/io/samjs/plugins/init/codemodify/InsertFuncCodeModifier.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (c) 2023, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package io.samjs.plugins.init.codemodify; - -import io.ballerina.compiler.syntax.tree.FunctionBodyBlockNode; -import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode; -import io.ballerina.compiler.syntax.tree.FunctionSignatureNode; -import io.ballerina.compiler.syntax.tree.MinutiaeList; -import io.ballerina.compiler.syntax.tree.ModuleMemberDeclarationNode; -import io.ballerina.compiler.syntax.tree.ModulePartNode; -import io.ballerina.compiler.syntax.tree.NodeList; -import io.ballerina.compiler.syntax.tree.OptionalTypeDescriptorNode; -import io.ballerina.compiler.syntax.tree.RequiredParameterNode; -import io.ballerina.compiler.syntax.tree.ReturnTypeDescriptorNode; -import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode; -import io.ballerina.compiler.syntax.tree.SyntaxKind; -import io.ballerina.compiler.syntax.tree.SyntaxTree; -import io.ballerina.compiler.syntax.tree.Token; -import io.ballerina.projects.Document; -import io.ballerina.projects.DocumentId; -import io.ballerina.projects.Module; -import io.ballerina.projects.ModuleId; -import io.ballerina.projects.plugins.CodeModifier; -import io.ballerina.projects.plugins.CodeModifierContext; - -import java.util.ArrayList; -import java.util.List; - -import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createEmptyMinutiaeList; -import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createEmptyNodeList; -import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createIdentifierToken; -import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createMinutiaeList; -import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createNodeList; -import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createSeparatedNodeList; -import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createToken; -import static io.ballerina.compiler.syntax.tree.AbstractNodeFactory.createWhitespaceMinutiae; -import static io.ballerina.compiler.syntax.tree.NodeFactory.createFunctionBodyBlockNode; -import static io.ballerina.compiler.syntax.tree.NodeFactory.createFunctionDefinitionNode; -import static io.ballerina.compiler.syntax.tree.NodeFactory.createFunctionSignatureNode; -import static io.ballerina.compiler.syntax.tree.NodeFactory.createOptionalTypeDescriptorNode; -import static io.ballerina.compiler.syntax.tree.NodeFactory.createParameterizedTypeDescriptorNode; -import static io.ballerina.compiler.syntax.tree.NodeFactory.createRequiredParameterNode; -import static io.ballerina.compiler.syntax.tree.NodeFactory.createReturnTypeDescriptorNode; -import static io.ballerina.compiler.syntax.tree.NodeFactory.createSimpleNameReferenceNode; - -/** - * A {@code CodeModifier} implementation that modify each bal file by adding a specific function to the end. - * - */ -public class InsertFuncCodeModifier extends CodeModifier { - - @Override - public void init(CodeModifierContext modifierContext) { - modifierContext.addSourceModifierTask(sourceModifierContext -> { - // Add new function to every bal file - for (ModuleId moduleId : sourceModifierContext.currentPackage().moduleIds()) { - Module module = sourceModifierContext.currentPackage().module(moduleId); - for (DocumentId documentId : module.documentIds()) { - Document document = module.document(documentId); - ModulePartNode rootNode = document.syntaxTree().rootNode(); - NodeList newMembers = - rootNode.members().add(createFunctionDefNode(document)); - ModulePartNode newModulePart = - rootNode.modify(rootNode.imports(), newMembers, rootNode.eofToken()); - SyntaxTree updatedSyntaxTree = document.syntaxTree().modifyWith(newModulePart); - sourceModifierContext.modifySourceFile(updatedSyntaxTree.textDocument(), documentId); - } - } - // Add new function to every test bal file - for (ModuleId moduleId : sourceModifierContext.currentPackage().moduleIds()) { - Module module = sourceModifierContext.currentPackage().module(moduleId); - for (DocumentId documentId : module.testDocumentIds()) { - Document document = module.document(documentId); - ModulePartNode rootNode = document.syntaxTree().rootNode(); - NodeList newMembers = - rootNode.members().add(createFunctionDefNode(document)); - ModulePartNode newModulePart = - rootNode.modify(rootNode.imports(), newMembers, rootNode.eofToken()); - SyntaxTree updatedSyntaxTree = document.syntaxTree().modifyWith(newModulePart); - sourceModifierContext.modifyTestSourceFile(updatedSyntaxTree.textDocument(), documentId); - } - } - }); - } - - private static FunctionDefinitionNode createFunctionDefNode(Document document) { - List qualifierList = new ArrayList<>(); - Token publicToken = createToken(SyntaxKind.PUBLIC_KEYWORD, generateMinutiaeListWithTwoNewline(), - generateMinutiaeListWithWhitespace()); - qualifierList.add(publicToken); - - SimpleNameReferenceNode simpleNameRefNode = createSimpleNameReferenceNode( - createIdentifierToken("string", createEmptyMinutiaeList(), - generateMinutiaeListWithWhitespace())); - - RequiredParameterNode requiredParameterNode = - createRequiredParameterNode(createEmptyNodeList(), simpleNameRefNode, - createIdentifierToken("params")); - - OptionalTypeDescriptorNode optionalErrorTypeDescriptorNode = - createOptionalTypeDescriptorNode( - createParameterizedTypeDescriptorNode(SyntaxKind.ERROR_TYPE_DESC, - createToken(SyntaxKind.ERROR_KEYWORD), null), - createToken(SyntaxKind.QUESTION_MARK_TOKEN, createEmptyMinutiaeList(), - generateMinutiaeListWithWhitespace())); - - ReturnTypeDescriptorNode returnTypeDescriptorNode = - createReturnTypeDescriptorNode(createToken(SyntaxKind.RETURNS_KEYWORD, - createEmptyMinutiaeList(), generateMinutiaeListWithWhitespace()), - createEmptyNodeList(), optionalErrorTypeDescriptorNode); - - FunctionSignatureNode functionSignatureNode = - createFunctionSignatureNode(createToken(SyntaxKind.OPEN_PAREN_TOKEN), - createSeparatedNodeList(requiredParameterNode), - createToken(SyntaxKind.CLOSE_PAREN_TOKEN, - createEmptyMinutiaeList(), generateMinutiaeListWithWhitespace()), - returnTypeDescriptorNode); - - FunctionBodyBlockNode emptyFunctionBodyNode = - createFunctionBodyBlockNode( - createToken(SyntaxKind.OPEN_BRACE_TOKEN, createEmptyMinutiaeList(), - generateMinutiaeListWithNewline()), null, - createEmptyNodeList(), createToken(SyntaxKind.CLOSE_BRACE_TOKEN), null); - - return createFunctionDefinitionNode( - SyntaxKind.FUNCTION_DEFINITION, null, createNodeList(qualifierList), - createToken(SyntaxKind.FUNCTION_KEYWORD, createEmptyMinutiaeList(), - generateMinutiaeListWithWhitespace()), - createIdentifierToken("inBuiltFuncCodeModifier" - + document.name().replace(".bal", "").replace("/", "_") - .replace("-", "_")), - createEmptyNodeList(), functionSignatureNode, emptyFunctionBodyNode); - } - - private static MinutiaeList generateMinutiaeListWithWhitespace() { - return createMinutiaeList(createWhitespaceMinutiae(" ")); - } - - private static MinutiaeList generateMinutiaeListWithNewline() { - return createMinutiaeList(createWhitespaceMinutiae("\n")); - } - - private static MinutiaeList generateMinutiaeListWithTwoNewline() { - return createMinutiaeList(createWhitespaceMinutiae("\n\n")); - } -} diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/module-info.java b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/module-info.java deleted file mode 100644 index 9485c202e88b..000000000000 --- a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/java/module-info.java +++ /dev/null @@ -1,8 +0,0 @@ -module compiler.plugin.test.init.func.inbuilt.codemodify { - requires io.ballerina.lang; - requires io.ballerina.parser; - requires io.ballerina.tools.api; - requires compiler.plugin.test.diagnostic.utils.lib; - - exports io.samjs.plugins.init.codemodify; -} diff --git a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin b/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin deleted file mode 100644 index a1f7806ab14d..000000000000 --- a/project-api/test-artifacts/in-built-code-modifier-plugin/src/main/resources/META-INF/services/io.ballerina.projects.plugins.CompilerPlugin +++ /dev/null @@ -1 +0,0 @@ -io.samjs.plugins.init.codemodify.InBuiltCodeModifierPlugin diff --git a/settings.gradle b/settings.gradle index 799b2305d961..35108684fe77 100644 --- a/settings.gradle +++ b/settings.gradle @@ -126,7 +126,6 @@ include(':project-api-test-artifact:bad-sad-compiler-plugin') include(':project-api-test-artifact:logging-file-appender-plugin') include(':project-api-test-artifact:init-function-codegen-compiler-plugin') include(':project-api-test-artifact:init-function-code-modify-compiler-plugin') -include(':project-api-test-artifact:in-built-code-modifier-plugin') include(':project-api-test-artifact:log-creator-in-built-code-modifier') include(':project-api-test-artifact:log-creator-in-built-code-generator') include(':project-api-test-artifact:log-creator-in-built-code-analyzer') @@ -159,7 +158,6 @@ project(':project-api-test-artifact:bad-sad-compiler-plugin').projectDir = file( project(':project-api-test-artifact:logging-file-appender-plugin').projectDir = file('project-api/test-artifacts/logging-file-appender-plugin') project(':project-api-test-artifact:init-function-codegen-compiler-plugin').projectDir = file('project-api/test-artifacts/init-function-codegen-compiler-plugin') project(':project-api-test-artifact:init-function-code-modify-compiler-plugin').projectDir = file('project-api/test-artifacts/init-function-code-modify-compiler-plugin') -project(':project-api-test-artifact:in-built-code-modifier-plugin').projectDir = file('project-api/test-artifacts/in-built-code-modifier-plugin') project(':project-api-test-artifact:log-creator-in-built-code-modifier').projectDir = file('project-api/test-artifacts/log-creator-in-built-code-modifier') project(':project-api-test-artifact:log-creator-in-built-code-generator').projectDir = file('project-api/test-artifacts/log-creator-in-built-code-generator') project(':project-api-test-artifact:log-creator-in-built-code-analyzer').projectDir = file('project-api/test-artifacts/log-creator-in-built-code-analyzer') From aa2ca9a13d60ed38754c81b268c5a82e32fc7fb2 Mon Sep 17 00:00:00 2001 From: Chathuranga Jayanath Date: Sat, 15 Jul 2023 00:00:18 +0530 Subject: [PATCH 56/70] incorporate character limits in lines --- .../resources/cli-help/ballerina-graphql.help | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help index 430dbe82e032..6c5c31c04925 100644 --- a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help +++ b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help @@ -26,7 +26,8 @@ DESCRIPTION OPTIONS - -i, --input + -i, --input This is mandatory input. The given GraphQL config file which is configured with GraphQL schemas (SDL) and queries, will generate the Ballerina GraphQL client sources. The given Ballerina GraphQL service file will generate the GraphQL schema (SDL) file @@ -42,13 +43,15 @@ OPTIONS If this base path is not specified, schemas will be generated for each of the GraphQL services in the input file. -m, --mode - This mode is used to identify the operation mode. It can be `client`, `schema`, or `service`. The `client` argument - indicates the Ballerina client source code generation, the `schema` argument indicates the GraphQL schema - generation, and the `service` argument indicates the Ballerina GraphQL service source code generation. Mode is only - mandatory for Ballerina GraphQL service source code generation. + This mode is used to identify the operation mode. It can be `client`, `schema`, or + `service`. The `client` argument indicates the Ballerina client source code + generation, the `schema` argument indicates the GraphQL schema generation, and the + `service` argument indicates the Ballerina GraphQL service source code generation. + Mode is only mandatory for Ballerina GraphQL service source code generation. -r, --use-records-for-objects - This flag is used without an argument. It is used only in the Ballerina GraphQL service generation. It will make - the Ballerina CLI tool to use record types for GraphQL object types whenever possible. + This flag is used without an argument. It is used only in the Ballerina GraphQL + service generation. It will make the Ballerina CLI tool to use record types for + GraphQL object types whenever possible. EXAMPLES Generate Ballerina Graphql clients using a GraphQL config file (`graphql.config.yaml`). @@ -64,5 +67,6 @@ EXAMPLES Generate a Ballerina GraphQL service using a GraphQL schema file (`schema.graphql`). $ bal graphql -i schema.graphql -m service -o ./output_path - Generate a Ballerina GraphQL service using a GraphQL schema file (`schema.graphql`) including record types whenever possible. + Generate a Ballerina GraphQL service using a GraphQL schema file (`schema.graphql`) + including record types whenever possible. $ bal graphql -i schema.graphql -m service -o ./output_path -r From f78e5a1afde6c01ec8a6dcd294794b7d4c775569 Mon Sep 17 00:00:00 2001 From: Chathuranga Jayanath Date: Sat, 15 Jul 2023 00:05:47 +0530 Subject: [PATCH 57/70] update description of mode flag --- .../src/main/resources/cli-help/ballerina-graphql.help | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help index 6c5c31c04925..9224928d3bb8 100644 --- a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help +++ b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-graphql.help @@ -47,7 +47,8 @@ OPTIONS `service`. The `client` argument indicates the Ballerina client source code generation, the `schema` argument indicates the GraphQL schema generation, and the `service` argument indicates the Ballerina GraphQL service source code generation. - Mode is only mandatory for Ballerina GraphQL service source code generation. + If the `mode` flag is not specified, the `graphql` tool will infer the mode from the + `input` file extension. -r, --use-records-for-objects This flag is used without an argument. It is used only in the Ballerina GraphQL service generation. It will make the Ballerina CLI tool to use record types for From c3d93bc15fd91534fe3477925eda2727a9561f6f Mon Sep 17 00:00:00 2001 From: Thevakumar-Luheerathan Date: Tue, 18 Jul 2023 11:49:26 +0530 Subject: [PATCH 58/70] Address the review --- .../java/io/ballerina/cli/cmd/PackCommandTest.java | 1 - .../test/java/io/ballerina/cli/cmd/RunCommandTest.java | 3 +-- .../inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java | 8 +++----- .../generator/LogCodeGeneratorInBuiltPlugin.java | 6 +++--- .../inbuilt/modifier/LogCodeModifierInBuiltPlugin.java | 6 +++--- .../plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java | 8 +++----- .../pkg/generator/LogCodeGeneratorPkgPlugin.java | 9 +++------ .../plugins/pkg/modifier/LogCodeModifierPkgPlugin.java | 10 +++------- 8 files changed, 19 insertions(+), 32 deletions(-) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java index 753d4dad2191..7c63401b0fd2 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PackCommandTest.java @@ -422,6 +422,5 @@ public void cleanUp() throws IOException { Files.deleteIfExists(logFile); Files.deleteIfExists(logFile.getParent()); Files.deleteIfExists(logFile.getParent().getParent()); - } } diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java index f4348c210d79..3c2627f08ade 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/RunCommandTest.java @@ -45,6 +45,7 @@ public void setupSuite() throws IOException { Files.createDirectories(logFile.getParent()); Files.writeString(logFile, ""); } + @BeforeClass public void setup() throws IOException { super.setup(); @@ -303,7 +304,6 @@ public void testRunBalProjectWithAllCompilerPlugins() throws IOException { "Package provided syntax node analysis from code modifier has failed to run"); Assert.assertTrue(logFileContent.contains("pkg-provided-source-modifier"), "Package provided source modifier from code modifier has failed to run"); - } @Test(description = "Run a valid ballerina project with invalid argument") @@ -385,6 +385,5 @@ public void cleanUp() throws IOException { Files.deleteIfExists(logFile); Files.deleteIfExists(logFile.getParent()); Files.deleteIfExists(logFile.getParent().getParent()); - } } diff --git a/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java index 6eedf27e1379..7a081d819cb8 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java +++ b/project-api/test-artifacts/log-creator-in-built-code-analyzer/src/main/java/io/luhee/plugins/inbuilt/analyzer/LogCodeAnalyzerInBuiltPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * Copyright (c) 2023, WSO2 LLC. (http://wso2.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,9 +30,10 @@ import java.io.Writer; import java.nio.charset.StandardCharsets; - /*** * A in-built code analyzer which adds a log statement to the beginning of the file. + * + * @since 2.7.1 */ public class LogCodeAnalyzerInBuiltPlugin extends CompilerPlugin { static String filePath = "./src/test/resources/compiler_plugin_tests/" + @@ -40,14 +41,12 @@ public class LogCodeAnalyzerInBuiltPlugin extends CompilerPlugin { @Override public void init(CompilerPluginContext pluginContext) { - pluginContext.addCodeAnalyzer(new LogCodeAnalyzer()); } /*** * A in-built code analyzer which adds a log statement to the beginning of the file. */ - public static class LogCodeAnalyzer extends CodeAnalyzer { @Override @@ -79,6 +78,5 @@ private static void appendToOutputFile(String filePath, String content) { } catch (IOException e) { throw new RuntimeException(e); } - } } diff --git a/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java index c74e081d3e89..9d515d12e074 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java +++ b/project-api/test-artifacts/log-creator-in-built-code-generator/src/main/java/io/luhee/plugins/inbuilt/generator/LogCodeGeneratorInBuiltPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * Copyright (c) 2023, WSO2 LLC. (http://wso2.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,9 +31,10 @@ import java.nio.charset.StandardCharsets; - /*** * A in-built code generator which adds a log statement to the beginning of the file. + * + * @since 2.7.1 */ public class LogCodeGeneratorInBuiltPlugin extends CompilerPlugin { static String filePath = "./src/test/resources/compiler_plugin_tests/" + @@ -79,6 +80,5 @@ private static void appendToOutputFile(String filePath, String content) { } catch (IOException e) { throw new RuntimeException(e); } - } } diff --git a/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java index 28a3ef35e519..2a887fa014e3 100644 --- a/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java +++ b/project-api/test-artifacts/log-creator-in-built-code-modifier/src/main/java/io/luhee/plugins/inbuilt/modifier/LogCodeModifierInBuiltPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * Copyright (c) 2023, WSO2 LLC. (http://wso2.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,9 +31,10 @@ import java.nio.charset.StandardCharsets; - /*** * A in-built code modifier which adds a log statement to the beginning of the file. + * + * @since 2.7.1 */ public class LogCodeModifierInBuiltPlugin extends CompilerPlugin { static String filePath = "./src/test/resources/compiler_plugin_tests/" + @@ -79,6 +80,5 @@ private static void appendToOutputFile(String filePath, String content) { } catch (IOException e) { throw new RuntimeException(e); } - } } diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java index 7ec7ac7b0c04..3ac53bf04c3d 100644 --- a/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-analyzer/src/main/java/io/luhee/plugins/pkg/analyzer/LogCodeAnalyzerPkgPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * Copyright (c) 2023, WSO2 LLC. (http://wso2.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,8 @@ /*** * A package provided code analyzer which adds a log statement to the beginning of the file. + * + * @since 2.7.1 */ public class LogCodeAnalyzerPkgPlugin extends CompilerPlugin { @@ -45,7 +47,6 @@ public void init(CompilerPluginContext pluginContext) { pluginContext.addCodeAnalyzer(new LogCodeAnalyzer()); } - /*** * A package provided code analyzer which adds a log statement to the beginning of the file. */ @@ -58,14 +59,12 @@ public void init(CodeAnalysisContext analysisContext) { analysisContext.addSyntaxNodeAnalysisTask(new LogSyntaxNodeAnalysis(), SyntaxKind.FUNCTION_DEFINITION); } - } /*** * A package provided code analyzer which adds a log statement to the beginning of the file. */ public static class LogSyntaxNodeAnalysis implements AnalysisTask { - @Override public void perform(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) { appendToOutputFile(filePath, "syntax-node-analysis-analyzer"); @@ -80,6 +79,5 @@ private static void appendToOutputFile(String filePath, String content) { } catch (IOException e) { throw new RuntimeException(e); } - } } diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java index 45200368510a..c52682a56370 100644 --- a/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-generator/src/main/java/io/luhee/plugins/pkg/generator/LogCodeGeneratorPkgPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * Copyright (c) 2023, WSO2 LLC. (http://wso2.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,9 +31,10 @@ import java.nio.charset.StandardCharsets; - /*** * A package provided code generator which adds a log statement to the beginning of the file. + * + * @since 2.7.1 */ public class LogCodeGeneratorPkgPlugin extends CompilerPlugin { @@ -45,7 +46,6 @@ public void init(CompilerPluginContext pluginContext) { pluginContext.addCodeGenerator(new LogCodeGenerator()); } - /*** * A package provided code generator which adds a log statement to the beginning of the file. */ @@ -58,7 +58,6 @@ public void init(CodeGeneratorContext generatorContext) { generatorContext.addSyntaxNodeAnalysisTask(new LogSyntaxNodeAnalysis(), SyntaxKind.FUNCTION_DEFINITION); } - } /*** @@ -72,7 +71,6 @@ public void perform(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) { } } - private static void appendToOutputFile(String filePath, String content) { File outputFile = new File(filePath); try (FileOutputStream fileStream = new FileOutputStream(outputFile, true); @@ -81,6 +79,5 @@ private static void appendToOutputFile(String filePath, String content) { } catch (IOException e) { throw new RuntimeException(e); } - } } diff --git a/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java index 676475cd5ef4..87d62b80c2dd 100644 --- a/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java +++ b/project-api/test-artifacts/log-creator-pkg-provided-code-modifier/src/main/java/io/luhee/plugins/pkg/modifier/LogCodeModifierPkgPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved. + * Copyright (c) 2023, WSO2 LLC. (http://wso2.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,9 +32,10 @@ import java.nio.charset.StandardCharsets; - /*** * A package provided code modifier which adds a log statement to the beginning of the file. + * + * @since 2.7.1 */ public class LogCodeModifierPkgPlugin extends CompilerPlugin { @@ -46,7 +47,6 @@ public void init(CompilerPluginContext pluginContext) { pluginContext.addCodeModifier(new LogCodeModifier()); } - /*** * A package provided code modifier which adds a log statement to the beginning of the file. */ @@ -56,17 +56,14 @@ public void init(CodeModifierContext modifierContext) { modifierContext.addSourceModifierTask(sourceGeneratorContext -> { appendToOutputFile(filePath, "source-modifier"); }); - modifierContext.addSyntaxNodeAnalysisTask(new LogSyntaxNodeAnalysis(), SyntaxKind.FUNCTION_DEFINITION); } - } /*** * A package provided code modifier which adds a log statement to the beginning of the file. */ public static class LogSyntaxNodeAnalysis implements AnalysisTask { - @Override public void perform(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) { appendToOutputFile(filePath, "syntax-node-analysis-modifier"); @@ -81,6 +78,5 @@ private static void appendToOutputFile(String filePath, String content) { } catch (IOException e) { throw new RuntimeException(e); } - } } From 231c8103766f36d4118b16d9e7bfcdc17e8cac3d Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Tue, 18 Jul 2023 13:40:43 +0530 Subject: [PATCH 59/70] Fix match stmt not working in do clause --- .../wso2/ballerinalang/compiler/desugar/QueryDesugar.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/QueryDesugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/QueryDesugar.java index 7f78d6b261c1..270063952f8c 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/QueryDesugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/QueryDesugar.java @@ -149,6 +149,7 @@ import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLSequenceLiteral; import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangConstPattern; +import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangVarBindingPatternMatchPattern; import org.wso2.ballerinalang.compiler.tree.matchpatterns.BLangWildCardMatchPattern; import org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment; import org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt; @@ -2499,7 +2500,13 @@ public void visit(BLangMatchStatement matchStmt) { public void visit(BLangMatchClause matchClause) { matchClause.matchPatterns.forEach(this::acceptNode); this.acceptNode(matchClause.matchGuard); + matchClause.declaredVars.forEach((k, v) -> identifiers.putIfAbsent(k, v)); this.acceptNode(matchClause.blockStmt); + matchClause.declaredVars.forEach((k, v) -> identifiers.remove(k)); + } + + @Override + public void visit(BLangVarBindingPatternMatchPattern varBindingPattern) { } @Override From 9e87b89b16fbade84b79a42abe8a662a5abcc03e Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Tue, 18 Jul 2023 13:42:35 +0530 Subject: [PATCH 60/70] Add test for match stmt in do clause --- .../test/query/QueryActionOrExprTest.java | 1 + .../query/match-stmt-in-do-clause.bal | 75 ++++++++++++++++++- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryActionOrExprTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryActionOrExprTest.java index 04115e7cfa8e..4e796aa08afa 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryActionOrExprTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryActionOrExprTest.java @@ -196,6 +196,7 @@ public void testMatchStatementInsideDoClause() { BRunUtil.invoke(result, "testConstMatchPattern1"); BRunUtil.invoke(result, "testConstMatchPattern2"); + BRunUtil.invoke(result, "testBindingPatternsInMatchStatement"); } @AfterClass diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/query/match-stmt-in-do-clause.bal b/tests/jballerina-unit-test/src/test/resources/test-src/query/match-stmt-in-do-clause.bal index 9c4c6aefa832..04ed30d93fb9 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/query/match-stmt-in-do-clause.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/query/match-stmt-in-do-clause.bal @@ -118,13 +118,82 @@ function testConstMatchPattern2() { assertEquality([1, 2, "a", "x", "x"], result); } +function testBindingPatternsInMatchStatement() { + (anydata|error)[] expected = ["A", 1, 100, [1], [2, 3], [4, 5, 6, 7], [8, [9, 10]], + {a: 3, b: 20}, {t: {a: 3, b: 20}}, {a: 3, b: 20, c: 40, d: 500}, + error("Generic Error", code = 20)]; + (anydata|error)[] result = []; + from anydata|error item in expected + do { + int z = 100; + match item { + "A" => { + result.push("A"); + } + + 100 => { + result.push(z); + } + + var i if i is int => { + result.push(i); + } + + var [_] => { + result.push(item); + } + + var [i, j] if j is int => { + result.push([i, j]); + } + + var [i, [j, k]] => { + result.push([i, [j, k]]); + } + + var [i, j, ...rest] => { + result.push([i, j, ...rest]); + } + + var {a, b, ...rest} if rest.cloneReadOnly().length() > 0 => { + result.push({a, b, ...rest}); + } + + var {a, b} => { + result.push({a, b}); + } + + var {t: {a, b}} => { + result.push({t: {a, b}}); + } + + var error(ERROR, code = code) => { + result.push(error(ERROR, code = code)); + } + + _ if item is anydata => { + result.push(item); + } + } + }; + foreach int i in 0...result.length() - 1 { + assertEquality(expected[i], result[i]); + } +} + const ASSERTION_ERROR_REASON = "AssertionError"; -function assertEquality(anydata expected, anydata actual) { - if expected == actual { +function assertEquality(anydata|error expected, anydata|error actual) { + if expected is anydata && actual is anydata && expected == actual { + return; + } + + if expected is error && actual is error && expected.message() == actual.message(){ return; } + string expectedValAsString = expected is error ? expected.toString() : expected.toString(); + string actualValAsString = actual is error ? actual.toString() : actual.toString(); panic error(ASSERTION_ERROR_REASON, - message = "expected '" + expected.toString() + "', found '" + actual.toString() + "'"); + message = "expected '" + expectedValAsString + "', found '" + actualValAsString + "'"); } From 1f64b384b396fe1f1f20c2ee390927d37f7cb7cc Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Wed, 19 Jul 2023 10:41:52 +0530 Subject: [PATCH 61/70] Make shell experimental --- ballerina-shell/README.md | 5 ++++- .../modules/shell-cli/src/main/resources/command.header.txt | 4 ++++ .../modules/shell-cli/src/main/resources/command.help.txt | 3 +++ .../src/main/resources/cli-help/ballerina-help.help | 2 +- .../src/main/resources/cli-help/ballerina-shell.help | 2 +- 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/ballerina-shell/README.md b/ballerina-shell/README.md index df05cbffe30b..5238c030f333 100644 --- a/ballerina-shell/README.md +++ b/ballerina-shell/README.md @@ -1,4 +1,7 @@ -# Ballerina Shell +# Ballerina Shell [Experimental] + +Note: This is an experimental feature, which supports only a limited +set of functionality. A REPL program for the [ballerina language](https://github.com/ballerina-platform/ballerina-lang). Ballerina is an open source programming language and platform for cloud-era application programmers to easily write software that just works. diff --git a/ballerina-shell/modules/shell-cli/src/main/resources/command.header.txt b/ballerina-shell/modules/shell-cli/src/main/resources/command.header.txt index 0f33a8141c6b..0f65d6dbdc7c 100644 --- a/ballerina-shell/modules/shell-cli/src/main/resources/command.header.txt +++ b/ballerina-shell/modules/shell-cli/src/main/resources/command.header.txt @@ -1,2 +1,6 @@ Welcome to Ballerina Shell REPL. + +Note: This is an experimental feature, which supports only a limited +set of functionality. + Type /exit to exit and /help to list available commands. diff --git a/ballerina-shell/modules/shell-cli/src/main/resources/command.help.txt b/ballerina-shell/modules/shell-cli/src/main/resources/command.help.txt index 03a7e65dd06e..3964ccd3dabc 100644 --- a/ballerina-shell/modules/shell-cli/src/main/resources/command.help.txt +++ b/ballerina-shell/modules/shell-cli/src/main/resources/command.help.txt @@ -1,5 +1,8 @@ Ballerina Shell Help +Note: This is an experimental feature, which supports only a limited +set of functionality. + Type a Ballerina language expression, statement, or declaration. Or, type one of the following commands: diff --git a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help index c5f3aede690f..e1f81e06d8ed 100755 --- a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help +++ b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help @@ -47,5 +47,5 @@ COMMANDS asyncapi Generate the Ballerina sources for a given AsyncAPI definition persist Manage data persistence bindgen Generate the Ballerina bindings for Java APIs - shell Run Ballerina interactive REPL + shell Run Ballerina interactive REPL [experimental] version Print the Ballerina version diff --git a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-shell.help b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-shell.help index 20387b6e6f6a..1a947e1ed8d6 100755 --- a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-shell.help +++ b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-shell.help @@ -9,7 +9,7 @@ DESCRIPTION Run a REPL instance of Ballerina to enable users to execute small snippets of code. - Note: This is an experimental feeature, which supports only a limited + Note: This is an experimental feature, which supports only a limited set of functionality. Debug messages can be enabled using the '-d' option. From 9e4cc1aeed92b3949606bc58e1bb993a0e21cade Mon Sep 17 00:00:00 2001 From: Tharik Kanaka Date: Wed, 19 Jul 2023 14:21:49 +0530 Subject: [PATCH 62/70] Update Guava version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 2d64e50c72bf..c6a8b34359ae 100644 --- a/gradle.properties +++ b/gradle.properties @@ -62,7 +62,7 @@ githubSpotbugsVersion=1.6.10 githubJohnrengelmanShadowVersion=5.2.0 gradleBuildScanVersion=2.1 gsonVersion=2.8.9 -guavaVersion=30.0-jre +guavaVersion=32.1.1-jre guruNidiGraphvizVersion=0.18.1 harbbyGradleServiceloaderVersion=1.1.5 hdrHistogramVersion=2.1.11 From bf1e4645085535ef06fcffc22a51274788e43739 Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Wed, 19 Jul 2023 15:11:49 +0530 Subject: [PATCH 63/70] Update match clause identifer chaching for queries --- .../wso2/ballerinalang/compiler/desugar/QueryDesugar.java | 5 +++-- .../resources/test-src/query/match-stmt-in-do-clause.bal | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/QueryDesugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/QueryDesugar.java index 270063952f8c..35e431922615 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/QueryDesugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/QueryDesugar.java @@ -2500,9 +2500,10 @@ public void visit(BLangMatchStatement matchStmt) { public void visit(BLangMatchClause matchClause) { matchClause.matchPatterns.forEach(this::acceptNode); this.acceptNode(matchClause.matchGuard); - matchClause.declaredVars.forEach((k, v) -> identifiers.putIfAbsent(k, v)); + Map prevIdentifiers = new HashMap<>(identifiers); + identifiers.putAll(matchClause.declaredVars); this.acceptNode(matchClause.blockStmt); - matchClause.declaredVars.forEach((k, v) -> identifiers.remove(k)); + identifiers = prevIdentifiers; } @Override diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/query/match-stmt-in-do-clause.bal b/tests/jballerina-unit-test/src/test/resources/test-src/query/match-stmt-in-do-clause.bal index 04ed30d93fb9..ff5217dc0d62 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/query/match-stmt-in-do-clause.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/query/match-stmt-in-do-clause.bal @@ -118,8 +118,9 @@ function testConstMatchPattern2() { assertEquality([1, 2, "a", "x", "x"], result); } +const C = "C"; function testBindingPatternsInMatchStatement() { - (anydata|error)[] expected = ["A", 1, 100, [1], [2, 3], [4, 5, 6, 7], [8, [9, 10]], + (anydata|error)[] expected = ["A", "C", 1, 100, [1], [2, 3], [4, 5, 6, 7], [8, [9, 10]], {a: 3, b: 20}, {t: {a: 3, b: 20}}, {a: 3, b: 20, c: 40, d: 500}, error("Generic Error", code = 20)]; (anydata|error)[] result = []; @@ -131,6 +132,10 @@ function testBindingPatternsInMatchStatement() { result.push("A"); } + C => { + result.push("C"); + } + 100 => { result.push(z); } From ced055d56c9cce0cbf6636e11d927f83c0eb08ef Mon Sep 17 00:00:00 2001 From: Tharik Kanaka Date: Wed, 19 Jul 2023 15:24:57 +0530 Subject: [PATCH 64/70] Downgrade guava version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index c6a8b34359ae..e3cfa2268306 100644 --- a/gradle.properties +++ b/gradle.properties @@ -62,7 +62,7 @@ githubSpotbugsVersion=1.6.10 githubJohnrengelmanShadowVersion=5.2.0 gradleBuildScanVersion=2.1 gsonVersion=2.8.9 -guavaVersion=32.1.1-jre +guavaVersion=32.0.1-jre guruNidiGraphvizVersion=0.18.1 harbbyGradleServiceloaderVersion=1.1.5 hdrHistogramVersion=2.1.11 From 411b715f24c094beb2fa97f42460a8132941edd9 Mon Sep 17 00:00:00 2001 From: Dulaj Date: Wed, 19 Jul 2023 15:26:50 +0530 Subject: [PATCH 65/70] Set package as the enclosed-container when analyzing function params --- .../java/io/ballerina/compiler/api/impl/NodeFinder.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/NodeFinder.java b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/NodeFinder.java index 1866b5a3636e..0ccdceef4c29 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/NodeFinder.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/compiler/api/impl/NodeFinder.java @@ -302,12 +302,6 @@ public void visit(BLangXMLNS xmlnsNode) { @Override public void visit(BLangFunction funcNode) { lookupNodes(funcNode.annAttachments); - // Compare the target lookup pos with the function symbol pos to ensure that we are not looking for the - // container of the function. - if (!this.range.equals(funcNode.symbol.pos.lineRange())) { - this.enclosingContainer = funcNode; - } - lookupNodes(funcNode.requiredParams); lookupNode(funcNode.restParam); lookupNode(funcNode.returnTypeNode); From be7c29fd53cd8277af05cfea1271d9c5db593e6e Mon Sep 17 00:00:00 2001 From: Dulaj Date: Wed, 19 Jul 2023 15:27:00 +0530 Subject: [PATCH 66/70] Add tests --- .../allreferences/FindRefsInExprsTest.java | 18 ++++++++++++++++ .../find-all-ref/find_var_ref_in_exprs.bal | 21 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/allreferences/FindRefsInExprsTest.java b/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/allreferences/FindRefsInExprsTest.java index c51736dd4010..f2937f300469 100644 --- a/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/allreferences/FindRefsInExprsTest.java +++ b/tests/ballerina-compiler-api-test/src/test/java/io/ballerina/semantic/api/test/allreferences/FindRefsInExprsTest.java @@ -200,6 +200,24 @@ public Object[][] getLookupPositions() { {201, 7, location(201, 5, 9), List.of(location(201, 5, 9)) }, + // Parameters in named-args + {209, 10, location(215, 22, 24), + List.of(location(209, 10, 12), + location(215, 22, 24)) + }, + {209, 22, location(215, 37, 39), + List.of(location(209, 22, 24), + location(215, 37, 39)) + }, + {210, 10, location(218, 24, 29), + List.of(location(210, 10, 15), + location(211, 10, 15), + location(218, 24, 29)) + }, + {212, 16, location(223, 22, 24), + List.of(location(212, 16, 18), + location(223, 22, 24)) + } }; } diff --git a/tests/ballerina-compiler-api-test/src/test/resources/test-src/find-all-ref/find_var_ref_in_exprs.bal b/tests/ballerina-compiler-api-test/src/test/resources/test-src/find-all-ref/find_var_ref_in_exprs.bal index d48825ecc8be..3e6fe91c08d3 100644 --- a/tests/ballerina-compiler-api-test/src/test/resources/test-src/find-all-ref/find_var_ref_in_exprs.bal +++ b/tests/ballerina-compiler-api-test/src/test/resources/test-src/find-all-ref/find_var_ref_in_exprs.bal @@ -203,3 +203,24 @@ public function findRefsIn() { .reduce(function (int val1, int val2) => val1 + val2, 0); } + +function testRefsInsideFuncCall() { + string|int value = "Jam"; + + func1(s1 = "Sam", s2 = value); + func2(xFunc = func1); + func2(xFunc = func4); + func2(func3(s1 = "abc")); +} + +function func1(string s1, string|int s2) { +} + +function func2(function xFunc) { +} + +function (int) returns int func4 = a => a + a; + +function func3(string s1) returns function (int) returns int { + return func4; +} \ No newline at end of file From 53359182ae354a65dc5e8be306d2847f5a672ba3 Mon Sep 17 00:00:00 2001 From: Dulaj Dilshan Date: Wed, 19 Jul 2023 15:42:15 +0530 Subject: [PATCH 67/70] Update tests/ballerina-compiler-api-test/src/test/resources/test-src/find-all-ref/find_var_ref_in_exprs.bal Co-authored-by: Lakshan Weerasinghe <46857198+LakshanWeerasinghe@users.noreply.github.com> --- .../resources/test-src/find-all-ref/find_var_ref_in_exprs.bal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ballerina-compiler-api-test/src/test/resources/test-src/find-all-ref/find_var_ref_in_exprs.bal b/tests/ballerina-compiler-api-test/src/test/resources/test-src/find-all-ref/find_var_ref_in_exprs.bal index 3e6fe91c08d3..29791fcd579e 100644 --- a/tests/ballerina-compiler-api-test/src/test/resources/test-src/find-all-ref/find_var_ref_in_exprs.bal +++ b/tests/ballerina-compiler-api-test/src/test/resources/test-src/find-all-ref/find_var_ref_in_exprs.bal @@ -223,4 +223,4 @@ function (int) returns int func4 = a => a + a; function func3(string s1) returns function (int) returns int { return func4; -} \ No newline at end of file +} From a00c8971b43a2c4e31ebf96564656736c2a11cfc Mon Sep 17 00:00:00 2001 From: Dulaj Date: Wed, 19 Jul 2023 18:03:42 +0530 Subject: [PATCH 68/70] Add a rename test --- .../langserver/rename/ProjectRenameTest.java | 1 + .../rename_function_named_arg_result.json | 72 +++++++++++++++++++ .../resources/rename/sources/project/main.bal | 2 + .../project/modules/module2/module2.bal | 10 +++ 4 files changed, 85 insertions(+) create mode 100644 language-server/modules/langserver-core/src/test/resources/rename/expected/project/rename_function_named_arg_result.json diff --git a/language-server/modules/langserver-core/src/test/java/org/ballerinalang/langserver/rename/ProjectRenameTest.java b/language-server/modules/langserver-core/src/test/java/org/ballerinalang/langserver/rename/ProjectRenameTest.java index 6a8a66ad07dc..12c0e2b5d647 100644 --- a/language-server/modules/langserver-core/src/test/java/org/ballerinalang/langserver/rename/ProjectRenameTest.java +++ b/language-server/modules/langserver-core/src/test/java/org/ballerinalang/langserver/rename/ProjectRenameTest.java @@ -39,6 +39,7 @@ private Object[][] testDataProvider() { return new Object[][]{ {"rename_class_result.json", "Student"}, {"rename_function_result.json", "getStudents"}, + {"rename_function_named_arg_result.json", "firstName"}, {"rename_global_var_result.json", "path"}, {"rename_error_config1.json", "Mod2Error"}, {"rename_error_config2.json", "Mod2Error"}, diff --git a/language-server/modules/langserver-core/src/test/resources/rename/expected/project/rename_function_named_arg_result.json b/language-server/modules/langserver-core/src/test/resources/rename/expected/project/rename_function_named_arg_result.json new file mode 100644 index 000000000000..779ffaa7a078 --- /dev/null +++ b/language-server/modules/langserver-core/src/test/resources/rename/expected/project/rename_function_named_arg_result.json @@ -0,0 +1,72 @@ +{ + "source": { + "file": "main.bal" + }, + "position": { + "line": 16, + "character": 20 + }, + "prepareRename": { + "valid": true + }, + "result": { + "changes": { + "main.bal": [ + { + "range": { + "start": { + "line": 16, + "character": 20 + }, + "end": { + "line": 16, + "character": 25 + } + }, + "newText": "firstName" + } + ], + "modules/module2/module2.bal": [ + { + "range": { + "start": { + "line": 59, + "character": 31 + }, + "end": { + "line": 59, + "character": 36 + } + }, + "newText": "firstName" + }, + { + "range": { + "start": { + "line": 60, + "character": 11 + }, + "end": { + "line": 60, + "character": 16 + } + }, + "newText": "firstName" + }, + { + "range": { + "start": { + "line": 57, + "character": 4 + }, + "end": { + "line": 57, + "character": 9 + } + }, + "newText": "firstName" + } + ] + } + } +} diff --git a/language-server/modules/langserver-core/src/test/resources/rename/sources/project/main.bal b/language-server/modules/langserver-core/src/test/resources/rename/sources/project/main.bal index 23fb68a239d9..4c143350cad3 100644 --- a/language-server/modules/langserver-core/src/test/resources/rename/sources/project/main.bal +++ b/language-server/modules/langserver-core/src/test/resources/rename/sources/project/main.bal @@ -13,4 +13,6 @@ public function main() { } int localInt = module1:gInt; + + module2:setName(fname = "John", lname = "Doe"); } diff --git a/language-server/modules/langserver-core/src/test/resources/rename/sources/project/modules/module2/module2.bal b/language-server/modules/langserver-core/src/test/resources/rename/sources/project/modules/module2/module2.bal index 0e458e052242..1b66ee5b9f60 100644 --- a/language-server/modules/langserver-core/src/test/resources/rename/sources/project/modules/module2/module2.bal +++ b/language-server/modules/langserver-core/src/test/resources/rename/sources/project/modules/module2/module2.bal @@ -50,3 +50,13 @@ public function addPerson(Person p) { # Directory path public string directoryPath = "/"; + +string name = ""; + +# Set name. +# +# + fname - parameter description +# + lname - parameter description +public function setName(string fname, string lname) { + name = fname + " " + lname; +} From 70a0f75c30dab449fe15f919b8d6150190c7ace7 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Thu, 20 Jul 2023 11:45:42 +0530 Subject: [PATCH 69/70] Address review suggestions Co-authored-by: lochana-chathura <39232462+lochana-chathura@users.noreply.github.com> --- ballerina-shell/README.md | 2 +- .../src/main/resources/cli-help/ballerina-help.help | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ballerina-shell/README.md b/ballerina-shell/README.md index 5238c030f333..67cce916984d 100644 --- a/ballerina-shell/README.md +++ b/ballerina-shell/README.md @@ -1,4 +1,4 @@ -# Ballerina Shell [Experimental] +# Ballerina Shell Note: This is an experimental feature, which supports only a limited set of functionality. diff --git a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help index e1f81e06d8ed..285420498d3e 100755 --- a/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help +++ b/cli/ballerina-cli/src/main/resources/cli-help/ballerina-help.help @@ -47,5 +47,5 @@ COMMANDS asyncapi Generate the Ballerina sources for a given AsyncAPI definition persist Manage data persistence bindgen Generate the Ballerina bindings for Java APIs - shell Run Ballerina interactive REPL [experimental] + shell Run Ballerina interactive REPL [Experimental] version Print the Ballerina version From ba26effbbfcb2abab55b7f8763f814ef7bfe278f Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Thu, 20 Jul 2023 14:37:19 +0530 Subject: [PATCH 70/70] Add test for regexp langlib usage in query --- .../test/query/QueryActionOrExprTest.java | 5 ++- .../test-src/query/query_action_or_expr.bal | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryActionOrExprTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryActionOrExprTest.java index 04115e7cfa8e..dc0c349efa64 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryActionOrExprTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/query/QueryActionOrExprTest.java @@ -73,7 +73,10 @@ public Object[] dataToTestQueryActionOrExpr() { "testQueryActionOrExprWithClientResourceAccessAction", "testQueryActionOrExprWithGroupedClientResourceAccessAction", "testNestedQueryActionOrExprWithClientResourceAccessAction", - "testQueryActionWithQueryExpression" + "testQueryActionWithQueryExpression", + "testQueryActionWithRegexpLangLibs", + "testQueryExprWithRegExpLangLibs", + "testQueryActionWithInterpolationRegexpLangLibs" }; } diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/query/query_action_or_expr.bal b/tests/jballerina-unit-test/src/test/resources/test-src/query/query_action_or_expr.bal index 2f4ce8d35f42..ef474de2f722 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/query/query_action_or_expr.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/query/query_action_or_expr.bal @@ -915,6 +915,41 @@ function testQueryActionWithQueryExpression() { assertEquality([4, 25], res2); } + +function testQueryActionWithRegexpLangLibs() { + string[] res = []; + + from var item in ["a", "aab", "bc", "ac"] + do { + if re `a.*`.isFullMatch(item) { + res.push(item); + } + }; + + assertEquality(["a", "aab", "ac"], res); +} + +function testQueryExprWithRegExpLangLibs() { + string[] res = from var item in ["a", "aab", "bc", "ac"] + where re `a.*`.isFullMatch(item) + select item; + + assertEquality(["a", "aab", "ac"], res); +} + +function testQueryActionWithInterpolationRegexpLangLibs() { + string[] res = []; + string pattern = "a.*"; + from var item in ["aa", "aaab", "bc", "aac"] + do { + if re `a${pattern}`.isFullMatch(item) { + res.push(item); + } + }; + + assertEquality(["aa", "aaab", "aac"], res); +} + const ASSERTION_ERROR_REASON = "AssertionError"; function assertEquality(anydata expected, anydata actual) {