Search in sources :

Example 1 with SrcFile

use of org.ballerinalang.packerina.init.models.SrcFile 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 2 with SrcFile

use of org.ballerinalang.packerina.init.models.SrcFile 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 3 with SrcFile

use of org.ballerinalang.packerina.init.models.SrcFile in project ballerina by ballerina-lang.

the class InitHandler method createSrcFolder.

/**
 * Create src/ folder.
 * @param projectPath The output path.
 * @param srcFiles The source files to be created.
 * @throws IOException If file write exception occurs.
 */
private static void createSrcFolder(Path projectPath, List<SrcFile> srcFiles) throws IOException {
    if (null != srcFiles && srcFiles.size() > 0) {
        for (SrcFile srcFile : srcFiles) {
            Path packagePath = projectPath.resolve(srcFile.getName());
            if (!Files.exists(packagePath)) {
                Files.createDirectory(packagePath);
            }
            Path srcFilePath = packagePath.resolve(srcFile.getSrcFileType().getFileName());
            if (!Files.exists(srcFilePath)) {
                Files.createFile(srcFilePath);
                writeContent(srcFilePath, srcFile.getContent());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) SrcFile(org.ballerinalang.packerina.init.models.SrcFile)

Aggregations

Path (java.nio.file.Path)3 SrcFile (org.ballerinalang.packerina.init.models.SrcFile)3 ArrayList (java.util.ArrayList)2 Manifest (org.ballerinalang.toml.model.Manifest)2 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Scanner (java.util.Scanner)1 CompileResult (org.ballerinalang.launcher.util.CompileResult)1 Test (org.testng.annotations.Test)1