Search in sources :

Example 21 with Manifest

use of org.ballerinalang.toml.model.Manifest in project ballerina by ballerina-lang.

the class InitCommand method execute.

@Override
public void execute() {
    PrintStream out = System.out;
    // Get source root path.
    Path projectPath = Paths.get(System.getProperty(USER_DIR));
    Scanner scanner = new Scanner(System.in, Charset.defaultCharset().name());
    try {
        Manifest manifest = null;
        if (helpFlag) {
            String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(parentCmdParser, "init");
            outStream.println(commandUsageInfo);
            return;
        }
        List<SrcFile> sourceFiles = new ArrayList<>();
        if (interactiveFlag) {
            // Check if Ballerina.toml file needs to be created.
            out.print("Create Ballerina.toml [yes/y, no/n]: (y) ");
            String createToml = scanner.nextLine().trim();
            if (createToml.equalsIgnoreCase("yes") || createToml.equalsIgnoreCase("y") || createToml.isEmpty()) {
                manifest = new Manifest();
                String defaultOrg = guessOrgName();
                // Get org name.
                out.print("Organization name: (" + defaultOrg + ") ");
                String orgName = scanner.nextLine().trim();
                manifest.setName(orgName.isEmpty() ? defaultOrg : orgName);
                String version;
                do {
                    out.print("Version: (" + DEFAULT_VERSION + ") ");
                    version = scanner.nextLine().trim();
                    version = version.isEmpty() ? DEFAULT_VERSION : version;
                } while (!validateVersion(out, version));
                manifest.setVersion(version);
            }
            String srcInput;
            boolean validInput = false;
            boolean first = true;
            do {
                if (first) {
                    out.print("Ballerina source [service/s, main/m]: (s) ");
                } else {
                    out.print("Ballerina source [service/s, main/m, finish/f]: (f) ");
                }
                srcInput = scanner.nextLine().trim();
                if (srcInput.equalsIgnoreCase("service") || srcInput.equalsIgnoreCase("s") || (first && srcInput.isEmpty())) {
                    out.print("Package for the service : (no package) ");
                    String packageName = scanner.nextLine().trim();
                    SrcFile srcFile = new SrcFile(packageName, SrcFile.SrcFileType.SERVICE);
                    sourceFiles.add(srcFile);
                } else if (srcInput.equalsIgnoreCase("main") || srcInput.equalsIgnoreCase("m")) {
                    out.print("Package for the main : (no package) ");
                    String packageName = scanner.nextLine().trim();
                    SrcFile srcFile = new SrcFile(packageName, SrcFile.SrcFileType.MAIN);
                    sourceFiles.add(srcFile);
                } else if (srcInput.isEmpty() || srcInput.equalsIgnoreCase("f")) {
                    validInput = true;
                } else {
                    out.println("Invalid input");
                }
                first = false;
            } while (!validInput);
            out.print("\n");
        } else {
            manifest = new Manifest();
            manifest.setName(guessOrgName());
            manifest.setVersion(DEFAULT_VERSION);
            if (isDirEmpty(projectPath)) {
                SrcFile srcFile = new SrcFile("", SrcFile.SrcFileType.SERVICE);
                sourceFiles.add(srcFile);
            }
        }
        InitHandler.initialize(projectPath, manifest, sourceFiles);
        out.println("Ballerina project initialized");
    } catch (IOException e) {
        out.println("Error occurred while creating project: " + e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) PrintStream(java.io.PrintStream) Scanner(java.util.Scanner) SrcFile(org.ballerinalang.packerina.init.models.SrcFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Manifest(org.ballerinalang.toml.model.Manifest)

Example 22 with Manifest

use of org.ballerinalang.toml.model.Manifest in project ballerina by ballerina-lang.

the class InitHandlerTest method testGeneratedSourceContent.

@Test(description = "Test if the generated source are runnable.", enabled = false)
public void testGeneratedSourceContent() throws IOException {
    Manifest manifest = new Manifest();
    manifest.setName("wso2");
    manifest.setVersion("1.0.0");
    SrcFile packageFile = new SrcFile("wso2_abc", SrcFile.SrcFileType.SERVICE);
    SrcFile mainFile = new SrcFile("main_runner", SrcFile.SrcFileType.MAIN);
    List<SrcFile> srcFiles = new ArrayList<>();
    srcFiles.add(packageFile);
    srcFiles.add(mainFile);
    InitHandler.initialize(tmpDir, manifest, srcFiles);
    Path tomlFile = tmpDir.resolve("Ballerina.toml");
    byte[] tomlFileBytes = Files.readAllBytes(tomlFile);
    String tomlFileContents = new String(tomlFileBytes, Charset.defaultCharset());
    Assert.assertTrue(tomlFileContents.contains("[project]"), "Project header missing in Ballerina.toml");
    Assert.assertTrue(tomlFileContents.contains("org-name = \"" + manifest.getName() + "\""), "Org-Name missing in Ballerina.toml");
    Assert.assertTrue(tomlFileContents.contains("version = \"" + manifest.getVersion() + "\""), "Version missing in Ballerina.toml");
    Path servicesBalFile = tmpDir.resolve(packageFile.getName()).resolve("services.bal");
    Path mainBalFile = tmpDir.resolve(mainFile.getName());
    Assert.assertTrue(Files.exists(servicesBalFile), "Package not generated.");
    Assert.assertTrue(Files.exists(mainBalFile), "Main file not generated.");
    CompileResult serviceFileCompileResult = BCompileUtil.compile(servicesBalFile.getParent().toString());
    Assert.assertFalse(serviceFileCompileResult.getDiagnostics().length > 0, "Errors found in the generated service files.");
    CompileResult mainFileCompileResult = BCompileUtil.compile(mainBalFile.toString());
    Assert.assertFalse(mainFileCompileResult.getDiagnostics().length > 0, "Errors found in the generated service files.");
}
Also used : Path(java.nio.file.Path) SrcFile(org.ballerinalang.packerina.init.models.SrcFile) ArrayList(java.util.ArrayList) CompileResult(org.ballerinalang.launcher.util.CompileResult) Manifest(org.ballerinalang.toml.model.Manifest) Test(org.testng.annotations.Test)

Example 23 with Manifest

use of org.ballerinalang.toml.model.Manifest in project ballerina by ballerina-lang.

the class ManifestProcessor method getManifest.

/**
 * Get the manifest object by passing the ballerina toml file.
 *
 * @param charStream toml file content as a char stream
 * @return manifest object
 */
private static Manifest getManifest(CharStream charStream) {
    Manifest manifest = new Manifest();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(new ManifestBuildListener(manifest), TomlProcessor.parseTomlContent(charStream));
    return manifest;
}
Also used : Manifest(org.ballerinalang.toml.model.Manifest) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 24 with Manifest

use of org.ballerinalang.toml.model.Manifest in project ballerina by ballerina-lang.

the class ManifestProcessorTest method testMultiplePatches.

@Test(description = "Multiple patches added to the patches section has an effect")
public void testMultiplePatches() throws IOException {
    Manifest manifest = ManifestProcessor.parseTomlContentFromString("[patches] \n " + "string-utils = {version = \"1.5.2\" } \n " + "jquery = { version = \"2.2.1\" } \n");
    Assert.assertEquals(manifest.getPatches().get(0).getPackageName(), "string-utils");
    Assert.assertEquals(manifest.getPatches().get(0).getVersion(), "1.5.2");
    Assert.assertEquals(manifest.getPatches().get(1).getPackageName(), "jquery");
    Assert.assertEquals(manifest.getPatches().get(1).getVersion(), "2.2.1");
}
Also used : Manifest(org.ballerinalang.toml.model.Manifest) Test(org.testng.annotations.Test)

Example 25 with Manifest

use of org.ballerinalang.toml.model.Manifest in project ballerina by ballerina-lang.

the class ManifestProcessorTest method testAttributeWithMultilineComments.

@Test(description = "Attribute with multiline comments doesn't have an effect")
public void testAttributeWithMultilineComments() throws IOException {
    Manifest manifest = ManifestProcessor.parseTomlContentFromString("[project] \n" + "# Name of the package \n #This is the package congif section \n org-name = \"foo/string\"");
    Assert.assertEquals(manifest.getName(), "foo/string");
}
Also used : Manifest(org.ballerinalang.toml.model.Manifest) Test(org.testng.annotations.Test)

Aggregations

Manifest (org.ballerinalang.toml.model.Manifest)29 Test (org.testng.annotations.Test)25 Path (java.nio.file.Path)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 SrcFile (org.ballerinalang.packerina.init.models.SrcFile)2 PrintStream (java.io.PrintStream)1 URI (java.net.URI)1 Scanner (java.util.Scanner)1 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)1 BLangCompilerException (org.ballerinalang.compiler.BLangCompilerException)1 CompileResult (org.ballerinalang.launcher.util.CompileResult)1 PackageID (org.ballerinalang.model.elements.PackageID)1 Name (org.wso2.ballerinalang.compiler.util.Name)1