use of org.ballerinalang.model.elements.PackageID in project ballerina by ballerina-lang.
the class WorkspaceUtils method prepareCompilerContext.
private static CompilerContext prepareCompilerContext(String fileName, String source) {
CompilerContext context = new CompilerContext();
List<Name> names = new ArrayList<>();
names.add(new Name("."));
// Registering custom PackageRepository to provide ballerina content without a file in file-system
context.put(PackageRepository.class, new InMemoryPackageRepository(new PackageID(Names.ANON_ORG, names, new Name("0.0.0")), "", fileName, source.getBytes(StandardCharsets.UTF_8)));
return context;
}
use of org.ballerinalang.model.elements.PackageID in project ballerina by ballerina-lang.
the class CommandUtil method getCommandsByDiagnostic.
/**
* Get the command instances for a given diagnostic.
* @param diagnostic Diagnostic to get the command against
* @param params Code Action parameters
* @param lsPackageCache Lang Server Package cache
* @return {@link List} List of commands related to the given diagnostic
*/
public static List<Command> getCommandsByDiagnostic(Diagnostic diagnostic, CodeActionParams params, LSPackageCache lsPackageCache) {
String diagnosticMessage = diagnostic.getMessage();
List<Command> commands = new ArrayList<>();
if (isUndefinedPackage(diagnosticMessage)) {
String packageAlias = diagnosticMessage.substring(diagnosticMessage.indexOf("'") + 1, diagnosticMessage.lastIndexOf("'"));
LSDocument sourceDocument = new LSDocument(params.getTextDocument().getUri());
Path openedPath = CommonUtil.getPath(sourceDocument);
String sourceRoot = TextDocumentServiceUtil.getSourceRoot(openedPath);
sourceDocument.setSourceRoot(sourceRoot);
lsPackageCache.getPackageMap().entrySet().stream().filter(pkgEntry -> {
String fullPkgName = pkgEntry.getValue().packageID.orgName.getValue() + "/" + pkgEntry.getValue().packageID.getName().getValue();
return fullPkgName.endsWith("." + packageAlias) || fullPkgName.endsWith("/" + packageAlias);
}).forEach(pkgEntry -> {
PackageID packageID = pkgEntry.getValue().packageID;
String commandTitle = CommandConstants.IMPORT_PKG_TITLE + " " + packageID.getName().toString();
String fullPkgName = packageID.getOrgName() + "/" + packageID.getName().getValue();
CommandArgument pkgArgument = new CommandArgument(CommandConstants.ARG_KEY_PKG_NAME, fullPkgName);
CommandArgument docUriArgument = new CommandArgument(CommandConstants.ARG_KEY_DOC_URI, params.getTextDocument().getUri());
commands.add(new Command(commandTitle, CommandConstants.CMD_IMPORT_PACKAGE, new ArrayList<>(Arrays.asList(pkgArgument, docUriArgument))));
});
}
return commands;
}
use of org.ballerinalang.model.elements.PackageID in project ballerina by ballerina-lang.
the class ParserUtils method getAllPackages.
/**
* Get All Native Packages.
*
* @return {@link Map} Package name, package functions and connectors
*/
public static Map<String, ModelPackage> getAllPackages() {
final Map<String, ModelPackage> modelPackage = new HashMap<>();
// TODO: remove once the packerina api for package listing is available
final String[] packageNames = { "net.http", "net.http.authadaptor", "net.http.endpoints", "net.http.mock", "net.http.swagger", "net.uri", "mime", "net.websub", "net.websub.hub", "net.grpc", "auth", "auth.authz", "auth.authz.permissionstore", "auth.basic", "auth.jwtAuth", "auth.userstore", "auth.utils", "caching", "collections", "config", "data.sql", "file", "internal", "io", "jwt", "jwt.signature", "log", "math", "os", "reflect", "runtime", "security.crypto", "task", "time", "transactions.coordinator", "user", "util" };
try {
List<BLangPackage> builtInPackages = LSPackageLoader.getBuiltinPackages();
for (BLangPackage bLangPackage : builtInPackages) {
loadPackageMap(bLangPackage.packageID.getName().getValue(), bLangPackage, modelPackage);
}
CompilerContext context = CommonUtil.prepareTempCompilerContext();
for (String packageName : packageNames) {
PackageID packageID = new PackageID(new Name("ballerina"), new Name(packageName), new Name("0.0.0"));
BLangPackage bLangPackage = LSPackageLoader.getPackageById(context, packageID);
loadPackageMap(bLangPackage.packageID.getName().getValue(), bLangPackage, modelPackage);
}
} catch (Exception e) {
// Above catch is to fail safe composer front end due to core errors.
logger.warn("Error while loading packages");
}
return modelPackage;
}
use of org.ballerinalang.model.elements.PackageID in project ballerina by ballerina-lang.
the class PushUtils method pushPackages.
/**
* Push/Uploads packages to the central repository.
*
* @param packageName path of the package folder to be pushed
* @param installToRepo if it should be pushed to central or home
*/
public static void pushPackages(String packageName, String installToRepo) {
String accessToken = getAccessTokenOfCLI();
Manifest manifest = readManifestConfigurations();
if (manifest.getName() == null && manifest.getVersion() == null) {
throw new BLangCompilerException("An org-name and package version is required when pushing. " + "This is not specified in Ballerina.toml inside the project");
}
String orgName = manifest.getName();
String version = manifest.getVersion();
PackageID packageID = new PackageID(new Name(orgName), new Name(packageName), new Name(version));
Path prjDirPath = Paths.get(".").toAbsolutePath().normalize().resolve(ProjectDirConstants.DOT_BALLERINA_DIR_NAME);
// Get package path from project directory path
Path pkgPathFromPrjtDir = Paths.get(prjDirPath.toString(), "repo", Names.ANON_ORG.getValue(), packageName, Names.DEFAULT_VERSION.getValue(), packageName + ".zip");
if (installToRepo == null) {
if (accessToken == null) {
// TODO: get bal home location dynamically
throw new BLangCompilerException("Access token is missing in ~/ballerina_home/Settings.toml file.\n" + "Please visit https://central.ballerina.io/cli-token");
}
// Push package to central
String resourcePath = resolvePkgPathInRemoteRepo(packageID);
URI balxPath = URI.create(String.valueOf(PushUtils.class.getClassLoader().getResource("ballerina.push.balx")));
String msg = orgName + "/" + packageName + ":" + version + " [project repo -> central]";
ExecutorUtils.execute(balxPath, accessToken, resourcePath, pkgPathFromPrjtDir.toString(), msg);
} else {
if (!installToRepo.equals("home")) {
throw new BLangCompilerException("Unknown repository provided to push the package");
}
Path balHomeDir = HomeRepoUtils.createAndGetHomeReposPath();
Path targetDirectoryPath = Paths.get(balHomeDir.toString(), "repo", orgName, packageName, version, packageName + ".zip");
if (Files.exists(targetDirectoryPath)) {
throw new BLangCompilerException("Ballerina package exists in the home repository");
} else {
try {
Files.createDirectories(targetDirectoryPath);
Files.copy(pkgPathFromPrjtDir, targetDirectoryPath, StandardCopyOption.REPLACE_EXISTING);
outStream.println(orgName + "/" + packageName + ":" + version + " [project repo -> home repo]");
} catch (IOException e) {
throw new BLangCompilerException("Error occurred when creating directories in the home repository");
}
}
}
}
use of org.ballerinalang.model.elements.PackageID 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);
}
Aggregations