Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ina-lang into fix-isolated-in-init
  • Loading branch information
MaryamZi committed Jul 25, 2023
2 parents 81429c6 + 1b14109 commit e547324
Show file tree
Hide file tree
Showing 189 changed files with 2,941 additions and 238 deletions.
3 changes: 3 additions & 0 deletions ballerina-shell/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Ballerina Shell

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.

The Ballerina-shell tool is an interactive tool for learning the Ballerina programming language and prototyping Ballerina code. Ballerina-shell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results. Currently, the tool is run via the command line. Using Ballerina-shell, you can enter program statements one at a time and immediately see the result.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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:

Expand Down
16 changes: 16 additions & 0 deletions cli/ballerina-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -81,6 +96,7 @@ task createTestBre(type: Copy) {
test {
dependsOn createTestDistributionCache
dependsOn createTestBre
dependsOn copyCompilerPluginJars

systemProperty "ballerina.home", "$buildDir"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,9 +75,13 @@
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;
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;
Expand All @@ -96,6 +101,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";
Expand Down Expand Up @@ -199,8 +205,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);
Expand All @@ -226,6 +234,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");
Expand All @@ -244,6 +265,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);
Expand Down Expand Up @@ -275,7 +304,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);
Expand Down Expand Up @@ -571,6 +600,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<String> 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.
*
Expand Down Expand Up @@ -750,11 +813,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);
}
Expand Down Expand Up @@ -894,6 +959,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<PackageVersion> packageVersions) {
if (packageVersions.isEmpty()) {
return null;
Expand Down Expand Up @@ -1002,7 +1094,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 = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,6 +10,10 @@ SYNOPSIS
bal graphql [-i | --input] <ballerina-graphql-service-file-path>
[-o | --output] <output-location>
[-s | --service] <service-base-path>
bal graphql [-i | --input] <graphql-schema-file-path>
[-o | --output] <output-location>
[-m | --mode] <operation-mode>
[-r | --use-records-for-objects]


DESCRIPTION
Expand All @@ -21,11 +26,13 @@ DESCRIPTION


OPTIONS
-i, --input <graphql-configuration-file-path | ballerina-graphql-service-file-path>
-i, --input <graphql-configuration-file-path | ballerina-graphql-service-file-path |
graphql-schema-file-path>
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 <output-location>
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
Expand All @@ -35,6 +42,17 @@ 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`. 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.
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
GraphQL object types whenever possible.

EXAMPLES
Generate Ballerina Graphql clients using a GraphQL config file (`graphql.config.yaml`).
Expand All @@ -46,3 +64,10 @@ 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 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
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// AUTO-GENERATED FILE.

// This file is auto-generated by Ballerina for managing modules in packages.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[tool]
id = "TOOL_ID"

[[dependency]]
Loading

0 comments on commit e547324

Please sign in to comment.