Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Migrate filepath to jballerina #15914

Merged
merged 1 commit into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions stdlib/filepath/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,34 @@

apply from: "$rootDir/gradle/balNativeLibProject.gradle"

configurations.testCompileClasspath {
resolutionStrategy {
preferProjectModules()
}
}

dependencies {
implementation 'org.ballerinalang:ballerina-lang:0.992.0-m1'
baloCreat project(':lib-creator-milestone')
baloCreat project(':lib-creator')
implementation project(':ballerina-core')
// implementation project(':ballerina-lang')
// implementation project(':lib-creator')
implementation project(':ballerina-utils')
implementation project(':ballerina-io')
implementation project(':ballerina-runtime')

testCompile project(':ballerina-launcher')
testCompile 'org.testng:testng:6.13.1'
testCompile project(':ballerina-builtin')
testCompile project(':ballerina-runtime-api')
testCompile project(':ballerina-system')
testCompile project(':ballerina-logging')
testCompile project(':ballerina-log-api')
testCompile project(':ballerina-encoding')
testCompile project(':ballerina-internal')
testCompile project(':ballerina-reflect')
testCompile project(':ballerina-jvm')
testCompile 'org.slf4j:slf4j-jdk14:1.7.22'
testCompile project(path: ':ballerina-test-utils', configuration: 'shadow')

baloImplementation project(path: ':ballerina-system', configuration: 'baloImplementation')
baloImplementation project(path: ':ballerina-time', configuration: 'baloImplementation')
Expand All @@ -44,4 +55,11 @@ dependencies {
baloImplementation project(path: ':ballerina-runtime-api', configuration: 'baloImplementation')
}

createBalo {
jvmTarget = 'true'
}
description = 'Ballerina - File Path Implementation'

configurations.all {
resolutionStrategy.preferProjectModules()
}
12 changes: 6 additions & 6 deletions stdlib/filepath/src/main/ballerina/filepath/file_path.bal
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function parent(string path) returns string|error {
}
int offset;
string root;
(root, offset) = check getRoot(validatedPath);
[root, offset] = check getRoot(validatedPath);
if (len < offset) {
return root;
}
Expand All @@ -129,7 +129,7 @@ public function normalize(string path) returns string|error {

string root;
int offset;
(root, offset) = check getRoot(validatedPath);
[root, offset] = check getRoot(validatedPath);
string c0 = check charAt(path, 0);

int i = 0;
Expand Down Expand Up @@ -290,10 +290,10 @@ public function relative(string base, string target) returns string|error {
}
string baseRoot;
int baseOffset;
(baseRoot, baseOffset) = check getRoot(cleanBase);
[baseRoot, baseOffset] = check getRoot(cleanBase);
string targetRoot;
int targetOffset;
(targetRoot, targetOffset) = check getRoot(cleanTarget);
[targetRoot, targetOffset] = check getRoot(cleanTarget);
if (!isSamePath(baseRoot, targetRoot)) {
error err = error("{ballerina/filepath}RELATIVE_PATH_ERROR", { message: "Can't make: " + target
+ " relative to " + base});
Expand Down Expand Up @@ -375,7 +375,7 @@ function parse(string input) returns string|error {
if (isWindows) {
int offset = 0;
string root = "";
(root, offset) = check getRoot(input);
[root, offset] = check getRoot(input);
return root + check parseWindowsPath(input, offset);
} else {
int n = input.length();
Expand All @@ -396,7 +396,7 @@ function parse(string input) returns string|error {
}
}

function getRoot(string input) returns (string,int)|error {
function getRoot(string input) returns [string,int]|error {
if (isWindows) {
return getWindowsRoot(input);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ function buildUnixPath(string... parts) returns string|error {
return parse(finalPath);
}

function getUnixRoot(string input) returns (string, int)|error {
function getUnixRoot(string input) returns [string, int]|error {
int length = input.length();
int offset = 0;
string root = "";
if (length > 0 && isSlash(check charAt(input, 0))) {
root = pathSeparator;
offset = 1;
}
return (root, offset);
return [root, offset];
}

function getUnixOffsetIndex(string path) returns int[]|error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function buildWindowsPath(string... parts) returns string|error {
return normalizedHead + pathSeparator + normalizedTail;
}

function getWindowsRoot(string input) returns (string, int)|error {
function getWindowsRoot(string input) returns [string, int]|error {
int length = input.length();
int offset = 0;
string root = "";
Expand Down Expand Up @@ -178,7 +178,7 @@ function getWindowsRoot(string input) returns (string, int)|error {
root = "\\";
offset = 1;
}
return (root, offset);
return [root, offset];
}

function getWindowsOffsetIndex(string path) returns int[]|error {
Expand All @@ -189,7 +189,7 @@ function getWindowsOffsetIndex(string path) returns int[]|error {
offsetIndexes[count] = 0;
count = count + 1;
} else {
(_, index) = check getWindowsRoot(path);
[_, index] = check getWindowsRoot(path);
while(index < path.length()) {
string cn = check charAt(path, index);
if (cn == "/" || cn == "\\") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

package org.ballerinalang.stdlib.filepath;

import org.ballerinalang.model.types.BTypes;
import org.ballerinalang.model.values.BError;
import org.ballerinalang.model.values.BMap;
import org.ballerinalang.model.values.BString;
import org.ballerinalang.model.values.BValue;
import org.ballerinalang.jvm.types.BTypes;
import org.ballerinalang.jvm.values.ErrorValue;
import org.ballerinalang.jvm.values.MapValue;
import org.ballerinalang.jvm.values.MapValueImpl;

/**
* A utility class for OS Path related tasks.
Expand All @@ -35,42 +34,40 @@ public class Utils {
static final String UNKNOWN_REASON = "UNKNOWN";

/**
* Returns error record for input reason.
* Error type is generic ballerina error type. This utility to construct error struct from message.
* Returns error record for input reason. Error type is generic ballerina error type. This utility to construct
* error struct from message.
*
* @param reason Reason for creating the error object. If the reason is null, "UNKNOWN" sets by
* default.
* @param error Java throwable object to capture description of error struct. If throwable object is null,
* "Unknown Error" sets to message by default.
* @return Ballerina error object.
* @param reason Reason for creating the error object. If the reason is null, "UNKNOWN" sets by default.
* @param error Java throwable object to capture description of error struct. If throwable object is null, "Unknown
* Error" sets to message by default.
* @return Ballerina error object.
*/
public static BError getPathError(String reason, Throwable error) {
public static ErrorValue getPathError(String reason, Throwable error) {
String errorMsg = error != null && error.getMessage() != null ? error.getMessage() : UNKNOWN_MESSAGE;
return getPathError(reason, errorMsg);
}

/**
* Returns error record for input reason and details.
* Error type is generic ballerina error type. This utility to construct error struct from message.
* Returns error record for input reason and details. Error type is generic ballerina error type. This utility to
* construct error struct from message.
*
* @param reason Reason for creating the error object. If the reason is null, value "UNKNOWN" is set by
* default.
* @param details Java throwable object to capture description of error struct. If throwable object is null,
* "Unknown Error" is set to message by default.
* @return Ballerina error object.
* @param reason Reason for creating the error object. If the reason is null, value "UNKNOWN" is set by default.
* @param details Java throwable object to capture description of error struct. If throwable object is null,
* "Unknown Error" is set to message by default.
* @return Ballerina error object.
*/
public static BError getPathError(String reason, String details) {
BMap<String, BValue> refData = new BMap<>(BTypes.typeError.detailType);
public static ErrorValue getPathError(String reason, String details) {
MapValue<String, Object> refData = new MapValueImpl<>(BTypes.typeError.detailType);
if (reason != null) {
reason = Constants.ERROR_REASON_PREFIX + reason;
} else {
reason = Constants.ERROR_REASON_PREFIX + UNKNOWN_REASON;
}
if (details != null) {
refData.put("message", new BString(details));
refData.put("message", details);
} else {
refData.put("message", new BString(UNKNOWN_MESSAGE));
refData.put("message", UNKNOWN_MESSAGE);
}
return new BError(BTypes.typeError, reason, refData);
return new ErrorValue(BTypes.typeError, reason, refData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.ballerinalang.bre.Context;
import org.ballerinalang.bre.bvm.BlockingNativeCallableUnit;
import org.ballerinalang.model.values.BString;
import org.ballerinalang.jvm.Strand;
import org.ballerinalang.natives.annotations.BallerinaFunction;
import org.ballerinalang.stdlib.filepath.Constants;
import org.ballerinalang.stdlib.filepath.Utils;
Expand All @@ -43,12 +43,15 @@ public class Absolute extends BlockingNativeCallableUnit {

@Override
public void execute(Context context) {
String inputPath = context.getStringArgument(0);
}

public static Object absolute(Strand strand, String inputPath) {
try {
String absolutePath = FileSystems.getDefault().getPath(inputPath).toAbsolutePath().toString();
context.setReturnValues(new BString(absolutePath));
return absolutePath;
} catch (InvalidPathException ex) {
context.setReturnValues(Utils.getPathError("INVALID_PATH", ex));
return Utils.getPathError("INVALID_PATH", ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.ballerinalang.bre.Context;
import org.ballerinalang.bre.bvm.BlockingNativeCallableUnit;
import org.ballerinalang.model.values.BBoolean;
import org.ballerinalang.jvm.Strand;
import org.ballerinalang.natives.annotations.BallerinaFunction;
import org.ballerinalang.stdlib.filepath.Constants;
import org.ballerinalang.stdlib.filepath.Utils;
Expand Down Expand Up @@ -48,8 +48,9 @@ public class Matches extends BlockingNativeCallableUnit {

@Override
public void execute(Context context) {
String inputPath = context.getStringArgument(0);
String pattern = context.getStringArgument(1);
}

public static Object matches(Strand strand, String inputPath, String pattern) {
FileSystem fs = FileSystems.getDefault();
PathMatcher matcher;
try {
Expand All @@ -59,13 +60,11 @@ public void execute(Context context) {
matcher = fs.getPathMatcher(pattern);
}
} catch (PatternSyntaxException ex) {
context.setReturnValues(Utils.getPathError("INVALID_PATTERN", ex));
return;
return Utils.getPathError("INVALID_PATTERN", ex);
}
if (inputPath == null) {
context.setReturnValues(new BBoolean(Boolean.FALSE));
return;
return false;
}
context.setReturnValues(new BBoolean(matcher.matches(Paths.get(inputPath))));
return matcher.matches(Paths.get(inputPath));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.ballerinalang.bre.Context;
import org.ballerinalang.bre.bvm.BlockingNativeCallableUnit;
import org.ballerinalang.model.values.BString;
import org.ballerinalang.jvm.Strand;
import org.ballerinalang.natives.annotations.BallerinaFunction;
import org.ballerinalang.stdlib.filepath.Constants;
import org.ballerinalang.stdlib.filepath.Utils;
Expand All @@ -46,16 +46,19 @@ public class Resolve extends BlockingNativeCallableUnit {

@Override
public void execute(Context context) {
String inputPath = context.getStringArgument(0);
}

public static Object resolve(Strand strand, String inputPath) {
try {
Path realPath = Files.readSymbolicLink(Paths.get(inputPath).toAbsolutePath());
context.setReturnValues(new BString(realPath.toString()));
return realPath.toString();
} catch (NotLinkException ex) {
context.setReturnValues(Utils.getPathError("NOT_LINK_ERROR", ex));
return Utils.getPathError("NOT_LINK_ERROR", ex);
} catch (IOException ex) {
context.setReturnValues(Utils.getPathError("IO_ERROR", ex));
return Utils.getPathError("IO_ERROR", ex);
} catch (SecurityException ex) {
context.setReturnValues(Utils.getPathError("SECURITY_ERROR", ex));
return Utils.getPathError("SECURITY_ERROR", ex);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2019, 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 org.ballerinalang.stdlib.filepath;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

/**
* A test suit listener for jballerina test cases initialization.
*
* @since 0.995.0
*/
public class JBallerinaTestInitializer implements ITestListener {

private static Logger log = LoggerFactory.getLogger(JBallerinaTestInitializer.class);
private static final String ENABLE_JBALLERINA_TESTS = "enableJBallerinaTests";

@Override
public void onStart(ITestContext context) {
String property = context.getCurrentXmlTest().getParameter(ENABLE_JBALLERINA_TESTS);
if (property != null && Boolean.valueOf(property)) {
log.info("JBallerina tests initialized...");
System.setProperty(ENABLE_JBALLERINA_TESTS, "true");
}
}

@Override
public void onFinish(ITestContext context) {
String property = context.getCurrentXmlTest().getParameter(ENABLE_JBALLERINA_TESTS);
if (property != null && Boolean.valueOf(property)) {
log.info("JBallerina tests disabled...");
System.clearProperty(ENABLE_JBALLERINA_TESTS);
}
}

@Override
public void onTestStart(ITestResult result) {
//ignore
}

@Override
public void onTestSuccess(ITestResult result) {
//ignore
}

@Override
public void onTestFailure(ITestResult result) {
//ignore
}

@Override
public void onTestSkipped(ITestResult result) {
//ignore
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
//ignore
}
}
Loading