use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class UserRepositoryUtils method installSourcePackage.
public static void installSourcePackage(Path sourceRootPath, String packageStr) {
// First, we need to validate what is in their.
// Let's try to compile and see.
// If it won't compile, we won't install the package
Path packagePath = Paths.get(packageStr);
CompilerContext context = new CompilerContext();
CompilerOptions cOptions = CompilerOptions.getInstance(context);
cOptions.put(PROJECT_DIR, sourceRootPath.toString());
cOptions.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString());
cOptions.put(PRESERVE_WHITESPACE, "false");
// compile
Compiler compiler = Compiler.getInstance(context);
compiler.compile(packagePath.toString());
Path srcDirectoryPath = BLangPrograms.validateAndResolveSourcePath(sourceRootPath, packagePath);
Path targetDirectoryPath = initializeUserRepository().resolve(USER_REPO_ARTIFACTS_DIRNAME).resolve(USER_REPO_SRC_DIRNAME).resolve(packagePath);
try {
Files.list(srcDirectoryPath).filter(filePath -> !Files.isDirectory(filePath, LinkOption.NOFOLLOW_LINKS)).filter(filePath -> filePath.toString().endsWith(BLANG_SRC_FILE_SUFFIX)).forEach(filePath -> {
// Here we get the absolute path.
// Just get the file name and copy.
Path srcNamePath = filePath.getFileName();
Path targetFilePath = targetDirectoryPath.resolve(srcNamePath);
CopyOption[] options = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES, LinkOption.NOFOLLOW_LINKS };
try {
if (Files.exists(targetDirectoryPath, LinkOption.NOFOLLOW_LINKS) && !Files.isDirectory(targetDirectoryPath, LinkOption.NOFOLLOW_LINKS)) {
throw new RuntimeException("a file exists with the same name as the package name: " + targetDirectoryPath.toString());
} else if (!Files.exists(targetDirectoryPath, LinkOption.NOFOLLOW_LINKS)) {
Files.createDirectories(targetDirectoryPath);
}
Files.copy(filePath, targetFilePath, options);
} catch (IOException e) {
throw new RuntimeException("error installing package: " + packageStr + ": " + e.getMessage(), e);
}
});
} catch (IOException e) {
throw new RuntimeException("error installing package: " + packageStr + ": " + e.getMessage(), e);
}
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class PullCommand method execute.
@Override
public void execute() {
if (helpFlag) {
String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(parentCmdParser, "pull");
outStream.println(commandUsageInfo);
return;
}
if (argList == null || argList.size() == 0) {
throw new BLangCompilerException("no package given");
}
if (argList.size() > 1) {
throw new BLangCompilerException("too many arguments");
}
// Enable remote debugging
if (null != debugPort) {
System.setProperty(SYSTEM_PROP_BAL_DEBUG, debugPort);
}
String resourceName = argList.get(0);
String orgName;
String packageName;
String version;
// Get org-name
int orgNameIndex = resourceName.indexOf("/");
if (orgNameIndex != -1) {
orgName = resourceName.substring(0, orgNameIndex);
} else {
throw new BLangCompilerException("no package-name provided");
}
// Get package name
int packageNameIndex = resourceName.indexOf(":");
if (packageNameIndex != -1) {
// version is provided
packageName = resourceName.substring(orgNameIndex + 1, packageNameIndex);
version = resourceName.substring(packageNameIndex + 1, resourceName.length());
} else {
packageName = resourceName.substring(orgNameIndex + 1, resourceName.length());
version = "*";
}
URI baseURI = URI.create("https://staging.central.ballerina.io:9090/");
Repo remoteRepo = new RemoteRepo(baseURI);
PackageID packageID = new PackageID(new Name(orgName), new Name(packageName), new Name(version));
Patten patten = remoteRepo.calculate(packageID);
if (patten != Patten.NULL) {
Converter converter = remoteRepo.getConverterInstance();
patten.convertToSources(converter, packageID).collect(Collectors.toList());
} else {
outStream.println("couldn't find package " + patten);
}
Runtime.getRuntime().exit(0);
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class BCompileUtil method compile.
/**
* Compile and return the semantic errors.
*
* @param sourceRoot root path of the source packages
* @param packageName name of the package to compile
* @param compilerPhase Compiler phase
* @return Semantic errors
*/
public static CompileResult compile(String sourceRoot, String packageName, CompilerPhase compilerPhase) {
CompilerContext context = new CompilerContext();
CompilerOptions options = CompilerOptions.getInstance(context);
options.put(PROJECT_DIR, sourceRoot);
options.put(COMPILER_PHASE, compilerPhase.toString());
options.put(PRESERVE_WHITESPACE, "false");
CompileResult comResult = new CompileResult();
// catch errors
DiagnosticListener listener = comResult::addDiagnostic;
context.put(DiagnosticListener.class, listener);
// compile
Compiler compiler = Compiler.getInstance(context);
BLangPackage packageNode = compiler.compile(packageName);
comResult.setAST(packageNode);
if (comResult.getErrorCount() > 0 || CompilerPhase.CODE_GEN.compareTo(compilerPhase) > 0) {
return comResult;
}
CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(packageNode);
if (programFile != null) {
ProgramFile pFile = LauncherUtils.getExecutableProgram(programFile);
comResult.setProgFile(pFile);
if (pFile != null) {
boolean distributedTxEnabled = CompilerUtils.isDistributedTransactionsEnabled();
pFile.setDistributedTransactionEnabled(distributedTxEnabled);
}
}
return comResult;
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class BCompileUtil method getDiagnostics.
/**
* Used by IntelliJ IDEA plugin to provide semantic analyzing capability.
*
* @param classLoader a {@link ClassLoader} to be set as thread context class loader. This is used by {@link
* java.util.ServiceLoader}. Otherwise semantic analyzing capability providing wont work since it
* cant find core package.
* @param sourceRoot source root of a project
* @param fileName either the file name (if in project root) or the package name
* @return list of diagnostics
*/
public static List<Diagnostic> getDiagnostics(ClassLoader classLoader, String sourceRoot, String fileName) {
Thread.currentThread().setContextClassLoader(classLoader);
CompilerContext context = new CompilerContext();
CompilerOptions options = CompilerOptions.getInstance(context);
options.put(PROJECT_DIR, sourceRoot);
options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString());
options.put(PRESERVE_WHITESPACE, "false");
CompileResult comResult = new CompileResult();
// catch errors
DiagnosticListener listener = comResult::addDiagnostic;
context.put(DiagnosticListener.class, listener);
// compile
Compiler compiler = Compiler.getInstance(context);
BLangPackage entryPackageNode = compiler.compile(fileName);
CompiledBinaryFile.ProgramFile programFile = compiler.getExecutableProgram(entryPackageNode);
if (programFile != null) {
comResult.setProgFile(LauncherUtils.getExecutableProgram(programFile));
}
Diagnostic[] diagnostics = comResult.getDiagnostics();
return Arrays.stream(diagnostics).collect(Collectors.toList());
}
use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.PACKAGE in project ballerina by ballerina-lang.
the class BCompileUtil method compile.
/**
* Compile and return the semantic errors.
*
* @param obj this is to find the original callers location.
* @param sourceRoot root path of the source packages
* @param packageName name of the package to compile
* @return Semantic errors
*/
public static CompileResult compile(Object obj, String sourceRoot, String packageName) {
try {
String effectiveSource;
CodeSource codeSource = obj.getClass().getProtectionDomain().getCodeSource();
URL location = codeSource.getLocation();
URI locationUri = location.toURI();
Path pathLocation = Paths.get(locationUri);
String filePath = concatFileName(sourceRoot, pathLocation);
Path rootPath = Paths.get(filePath);
Path packagePath = Paths.get(packageName);
if (Files.isDirectory(packagePath)) {
String[] pkgParts = packageName.split("\\/");
List<Name> pkgNameComps = Arrays.stream(pkgParts).map(part -> {
if (part.equals("")) {
return Names.EMPTY;
} else if (part.equals("_")) {
return Names.EMPTY;
}
return new Name(part);
}).collect(Collectors.toList());
// TODO: orgName is anon, fix it.
PackageID pkgId = new PackageID(Names.ANON_ORG, pkgNameComps, Names.DEFAULT_VERSION);
effectiveSource = pkgId.getName().getValue();
return compile(rootPath.toString(), effectiveSource, CompilerPhase.CODE_GEN);
} else {
effectiveSource = packageName;
return compile(rootPath.toString(), effectiveSource, CompilerPhase.CODE_GEN, new FileSystemProjectDirectory(rootPath));
}
} catch (URISyntaxException e) {
throw new IllegalArgumentException("error while running test: " + e.getMessage());
}
}
Aggregations