@Test(description = "Build a ballerina project with non existing module.") public void testBuildBalProjectWithInvalidModule() throws IOException { Path sourceRoot = this.testResources.resolve("project-with-no-modules"); BuildCommand buildCommand = new BuildCommand(sourceRoot, printStream, printStream, false, true); new CommandLine(buildCommand).parse("xyz"); buildCommand.execute(); String buildLog = readOutput(true); Assert.assertEquals(buildLog.replaceAll("\r", ""), "ballerina: invalid Ballerina source path. It should either be a name of a module " + "in a Ballerina project or a file with a '.bal' extension. Use -a or --all " + "to build or compile all modules.\n" + "\n" + "USAGE:\n" + " ballerina build {<ballerina-file> | <module-name> | -a | --all}\n" + "\n" + "For more information try --help\n"); }
public void execute() { if (this.helpFlag) { String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(BUILD_COMMAND); this.errStream.println(commandUsageInfo); return; } String[] args; if (this.argList == null) { args = new String[0]; } else if (this.buildAll) { args = this.argList.toArray(new String[0]); } else { args = argList.subList(1, argList.size()).toArray(new String[0]); } if (!this.skipTests && this.debugPort != null) { System.setProperty(SYSTEM_PROP_BAL_DEBUG, this.debugPort); } String[] userArgs = LaunchUtils.getUserArgs(args, new HashMap<>()); if (userArgs.length > 0) { CommandUtil.printError(this.errStream, "too many arguments.", buildCmd, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!this.buildAll && (this.argList == null || this.argList.size() == 0)) { CommandUtil.printError(this.errStream, "'build' command requires a module name or a Ballerina file to build/compile. Use '-a' or " + "'--all' to build/compile all the modules of the project.", "ballerina build {<ballerina-file> | <module-name> | -a | --all}", false); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = null != this.sourceRoot ? Paths.get(this.sourceRoot).toAbsolutePath() : this.sourceRootPath; Path sourcePath = null; Path targetPath; if (this.buildAll) { if (null != this.output) { CommandUtil.printError(this.errStream, "'-o' and '--output' are only supported when building a single Ballerina " + "file.", "ballerina build -o <output-file> <ballerina-file> ", true); CommandUtil.exitError(this.exitWhenFinish); return; } if (!ProjectDirs.isProject(this.sourceRootPath)) { Path findRoot = ProjectDirs.findProjectRoot(this.sourceRootPath); if (null == findRoot) { CommandUtil.printError(this.errStream, "you are trying to build/compile a Ballerina project that does not have a " + "Ballerina.toml file.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = findRoot; } targetPath = this.sourceRootPath.resolve(ProjectDirConstants.TARGET_DIR_NAME); } else if (this.argList.get(0).endsWith(BLangConstants.BLANG_SRC_FILE_SUFFIX)) { if (coverage) { coverage = false; this.outStream.println("Code coverage is not yet supported with single bal files. Ignoring the flag " + "and continuing the test run..."); } if (this.compile) { CommandUtil.printError(this.errStream, "'-c' or '--compile' can only be used with modules.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } else { if (Paths.get(this.argList.get(0)).isAbsolute()) { sourcePath = Paths.get(this.argList.get(0)); } else { sourcePath = this.sourceRootPath.resolve(this.argList.get(0)); } this.sourceRootPath = sourcePath.getParent(); if (Files.notExists(sourcePath)) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' Ballerina file does not exist.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!Files.isRegularFile(sourcePath)) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' is not a Ballerina file. Check if it is a symlink or a shortcut.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } try { targetPath = Files.createTempDirectory("ballerina-build-" + System.nanoTime()); } catch (IOException e) { throw LauncherUtils.createLauncherException("Error occurred when creating the executable."); } } } else if (Files.exists( this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME).resolve(this.argList.get(0))) && Files.isDirectory( this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME) .resolve(this.argList.get(0)))) { if (null != this.output) { CommandUtil.printError(this.errStream, "'-o' and '--output' are only supported for building a single Ballerina " + "file.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!RepoUtils.isBallerinaProject(this.sourceRootPath)) { CommandUtil.printError(this.errStream, "you are trying to build/compile a module that is not inside a project.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (Paths.get(argList.get(0)).isAbsolute()) { CommandUtil.printError(this.errStream, "you are trying to build/compile a module giving the absolute path. You " + "only need to give the name of the module.", "ballerina build [-c] <module-name>", true); CommandUtil.exitError(this.exitWhenFinish); return; } String moduleName = argList.get(0); if (moduleName.endsWith("/")) { moduleName = moduleName.substring(0, moduleName.length() - 1); } sourcePath = Paths.get(moduleName); if (Files.notExists(this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME).resolve(sourcePath))) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' module does not exist.", "ballerina build [-c] <module-name>", true); CommandUtil.exitError(this.exitWhenFinish); return; } targetPath = this.sourceRootPath.resolve(ProjectDirConstants.TARGET_DIR_NAME); } else { CommandUtil.printError(this.errStream, "invalid Ballerina source path. It should either be a name of a module in a " + "Ballerina project or a file with a \'" + BLangConstants.BLANG_SRC_FILE_SUFFIX + "\' extension. Use -a or --all " + "to build or compile all modules.", "ballerina build {<ballerina-file> | <module-name> | -a | --all}", true); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = this.sourceRootPath.normalize(); sourcePath = sourcePath == null ? null : sourcePath.normalize(); targetPath = targetPath.normalize(); CompilerContext compilerContext = new CompilerContext(); CompilerOptions options = CompilerOptions.getInstance(compilerContext); options.put(PROJECT_DIR, this.sourceRootPath.toString()); options.put(DUMP_BIR, Boolean.toString(dumpBIR)); options.put(DUMP_BIR_FILE, dumpBIRFile); options.put(OFFLINE, Boolean.toString(this.offline)); options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString()); options.put(LOCK_ENABLED, Boolean.toString(!this.skipLock)); options.put(SKIP_TESTS, Boolean.toString(this.skipTests)); options.put(TEST_ENABLED, Boolean.toString(!this.skipTests)); options.put(EXPERIMENTAL_FEATURES_ENABLED, Boolean.toString(this.experimentalFlag)); options.put(PRESERVE_WHITESPACE, "true"); BuildContext buildContext = new BuildContext(this.sourceRootPath, targetPath, sourcePath, compilerContext); JarResolver jarResolver = JarResolverImpl.getInstance(buildContext, skipCopyLibsFromDist, observabilityIncluded); buildContext.put(BuildContextField.JAR_RESOLVER, jarResolver); buildContext.setOut(outStream); buildContext.setErr(errStream); boolean isSingleFileBuild = buildContext.getSourceType().equals(SINGLE_BAL_FILE); Path outputPath = null == this.output ? Paths.get(System.getProperty("user.dir")) : Paths.get(this.output); TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder() .addTask(new CleanTargetDirTask(), isSingleFileBuild) .addTask(new CreateTargetDirTask()) .addTask(new CompileTask()) .addTask(new CreateBirTask()) .addTask(new CreateLockFileTask(), this.skipLock || isSingleFileBuild) .addTask(new CreateBaloTask(), isSingleFileBuild) .addTask(new CreateJarTask()) .addTask(new CopyResourcesTask(), isSingleFileBuild) .addTask(new CopyObservabilitySymbolsTask(), isSingleFileBuild) .addTask(new RunTestsTask(testReport, coverage, args), this.skipTests || isSingleFileBuild) .addTask(new CreateExecutableTask(), this.compile) .addTask(new CopyExecutableTask(outputPath), !isSingleFileBuild) .addTask(new PrintExecutablePathTask(), this.compile) .addTask(new RunCompilerPluginTask(), this.compile) .addTask(new CleanTargetDirTask(), !isSingleFileBuild) .build(); taskExecutor.executeTasks(buildContext); if (this.exitWhenFinish) { Runtime.getRuntime().exit(0); } }
BuildCommand implements BLauncherCmd { public void execute() { if (this.helpFlag) { String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(BUILD_COMMAND); this.errStream.println(commandUsageInfo); return; } String[] args; if (this.argList == null) { args = new String[0]; } else if (this.buildAll) { args = this.argList.toArray(new String[0]); } else { args = argList.subList(1, argList.size()).toArray(new String[0]); } if (!this.skipTests && this.debugPort != null) { System.setProperty(SYSTEM_PROP_BAL_DEBUG, this.debugPort); } String[] userArgs = LaunchUtils.getUserArgs(args, new HashMap<>()); if (userArgs.length > 0) { CommandUtil.printError(this.errStream, "too many arguments.", buildCmd, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!this.buildAll && (this.argList == null || this.argList.size() == 0)) { CommandUtil.printError(this.errStream, "'build' command requires a module name or a Ballerina file to build/compile. Use '-a' or " + "'--all' to build/compile all the modules of the project.", "ballerina build {<ballerina-file> | <module-name> | -a | --all}", false); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = null != this.sourceRoot ? Paths.get(this.sourceRoot).toAbsolutePath() : this.sourceRootPath; Path sourcePath = null; Path targetPath; if (this.buildAll) { if (null != this.output) { CommandUtil.printError(this.errStream, "'-o' and '--output' are only supported when building a single Ballerina " + "file.", "ballerina build -o <output-file> <ballerina-file> ", true); CommandUtil.exitError(this.exitWhenFinish); return; } if (!ProjectDirs.isProject(this.sourceRootPath)) { Path findRoot = ProjectDirs.findProjectRoot(this.sourceRootPath); if (null == findRoot) { CommandUtil.printError(this.errStream, "you are trying to build/compile a Ballerina project that does not have a " + "Ballerina.toml file.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = findRoot; } targetPath = this.sourceRootPath.resolve(ProjectDirConstants.TARGET_DIR_NAME); } else if (this.argList.get(0).endsWith(BLangConstants.BLANG_SRC_FILE_SUFFIX)) { if (coverage) { coverage = false; this.outStream.println("Code coverage is not yet supported with single bal files. Ignoring the flag " + "and continuing the test run..."); } if (this.compile) { CommandUtil.printError(this.errStream, "'-c' or '--compile' can only be used with modules.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } else { if (Paths.get(this.argList.get(0)).isAbsolute()) { sourcePath = Paths.get(this.argList.get(0)); } else { sourcePath = this.sourceRootPath.resolve(this.argList.get(0)); } this.sourceRootPath = sourcePath.getParent(); if (Files.notExists(sourcePath)) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' Ballerina file does not exist.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!Files.isRegularFile(sourcePath)) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' is not a Ballerina file. Check if it is a symlink or a shortcut.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } try { targetPath = Files.createTempDirectory("ballerina-build-" + System.nanoTime()); } catch (IOException e) { throw LauncherUtils.createLauncherException("Error occurred when creating the executable."); } } } else if (Files.exists( this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME).resolve(this.argList.get(0))) && Files.isDirectory( this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME) .resolve(this.argList.get(0)))) { if (null != this.output) { CommandUtil.printError(this.errStream, "'-o' and '--output' are only supported for building a single Ballerina " + "file.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!RepoUtils.isBallerinaProject(this.sourceRootPath)) { CommandUtil.printError(this.errStream, "you are trying to build/compile a module that is not inside a project.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (Paths.get(argList.get(0)).isAbsolute()) { CommandUtil.printError(this.errStream, "you are trying to build/compile a module giving the absolute path. You " + "only need to give the name of the module.", "ballerina build [-c] <module-name>", true); CommandUtil.exitError(this.exitWhenFinish); return; } String moduleName = argList.get(0); if (moduleName.endsWith("/")) { moduleName = moduleName.substring(0, moduleName.length() - 1); } sourcePath = Paths.get(moduleName); if (Files.notExists(this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME).resolve(sourcePath))) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' module does not exist.", "ballerina build [-c] <module-name>", true); CommandUtil.exitError(this.exitWhenFinish); return; } targetPath = this.sourceRootPath.resolve(ProjectDirConstants.TARGET_DIR_NAME); } else { CommandUtil.printError(this.errStream, "invalid Ballerina source path. It should either be a name of a module in a " + "Ballerina project or a file with a \'" + BLangConstants.BLANG_SRC_FILE_SUFFIX + "\' extension. Use -a or --all " + "to build or compile all modules.", "ballerina build {<ballerina-file> | <module-name> | -a | --all}", true); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = this.sourceRootPath.normalize(); sourcePath = sourcePath == null ? null : sourcePath.normalize(); targetPath = targetPath.normalize(); CompilerContext compilerContext = new CompilerContext(); CompilerOptions options = CompilerOptions.getInstance(compilerContext); options.put(PROJECT_DIR, this.sourceRootPath.toString()); options.put(DUMP_BIR, Boolean.toString(dumpBIR)); options.put(DUMP_BIR_FILE, dumpBIRFile); options.put(OFFLINE, Boolean.toString(this.offline)); options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString()); options.put(LOCK_ENABLED, Boolean.toString(!this.skipLock)); options.put(SKIP_TESTS, Boolean.toString(this.skipTests)); options.put(TEST_ENABLED, Boolean.toString(!this.skipTests)); options.put(EXPERIMENTAL_FEATURES_ENABLED, Boolean.toString(this.experimentalFlag)); options.put(PRESERVE_WHITESPACE, "true"); BuildContext buildContext = new BuildContext(this.sourceRootPath, targetPath, sourcePath, compilerContext); JarResolver jarResolver = JarResolverImpl.getInstance(buildContext, skipCopyLibsFromDist, observabilityIncluded); buildContext.put(BuildContextField.JAR_RESOLVER, jarResolver); buildContext.setOut(outStream); buildContext.setErr(errStream); boolean isSingleFileBuild = buildContext.getSourceType().equals(SINGLE_BAL_FILE); Path outputPath = null == this.output ? Paths.get(System.getProperty("user.dir")) : Paths.get(this.output); TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder() .addTask(new CleanTargetDirTask(), isSingleFileBuild) .addTask(new CreateTargetDirTask()) .addTask(new CompileTask()) .addTask(new CreateBirTask()) .addTask(new CreateLockFileTask(), this.skipLock || isSingleFileBuild) .addTask(new CreateBaloTask(), isSingleFileBuild) .addTask(new CreateJarTask()) .addTask(new CopyResourcesTask(), isSingleFileBuild) .addTask(new CopyObservabilitySymbolsTask(), isSingleFileBuild) .addTask(new RunTestsTask(testReport, coverage, args), this.skipTests || isSingleFileBuild) .addTask(new CreateExecutableTask(), this.compile) .addTask(new CopyExecutableTask(outputPath), !isSingleFileBuild) .addTask(new PrintExecutablePathTask(), this.compile) .addTask(new RunCompilerPluginTask(), this.compile) .addTask(new CleanTargetDirTask(), !isSingleFileBuild) .build(); taskExecutor.executeTasks(buildContext); if (this.exitWhenFinish) { Runtime.getRuntime().exit(0); } } }
BuildCommand implements BLauncherCmd { public void execute() { if (this.helpFlag) { String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(BUILD_COMMAND); this.errStream.println(commandUsageInfo); return; } String[] args; if (this.argList == null) { args = new String[0]; } else if (this.buildAll) { args = this.argList.toArray(new String[0]); } else { args = argList.subList(1, argList.size()).toArray(new String[0]); } if (!this.skipTests && this.debugPort != null) { System.setProperty(SYSTEM_PROP_BAL_DEBUG, this.debugPort); } String[] userArgs = LaunchUtils.getUserArgs(args, new HashMap<>()); if (userArgs.length > 0) { CommandUtil.printError(this.errStream, "too many arguments.", buildCmd, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!this.buildAll && (this.argList == null || this.argList.size() == 0)) { CommandUtil.printError(this.errStream, "'build' command requires a module name or a Ballerina file to build/compile. Use '-a' or " + "'--all' to build/compile all the modules of the project.", "ballerina build {<ballerina-file> | <module-name> | -a | --all}", false); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = null != this.sourceRoot ? Paths.get(this.sourceRoot).toAbsolutePath() : this.sourceRootPath; Path sourcePath = null; Path targetPath; if (this.buildAll) { if (null != this.output) { CommandUtil.printError(this.errStream, "'-o' and '--output' are only supported when building a single Ballerina " + "file.", "ballerina build -o <output-file> <ballerina-file> ", true); CommandUtil.exitError(this.exitWhenFinish); return; } if (!ProjectDirs.isProject(this.sourceRootPath)) { Path findRoot = ProjectDirs.findProjectRoot(this.sourceRootPath); if (null == findRoot) { CommandUtil.printError(this.errStream, "you are trying to build/compile a Ballerina project that does not have a " + "Ballerina.toml file.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = findRoot; } targetPath = this.sourceRootPath.resolve(ProjectDirConstants.TARGET_DIR_NAME); } else if (this.argList.get(0).endsWith(BLangConstants.BLANG_SRC_FILE_SUFFIX)) { if (coverage) { coverage = false; this.outStream.println("Code coverage is not yet supported with single bal files. Ignoring the flag " + "and continuing the test run..."); } if (this.compile) { CommandUtil.printError(this.errStream, "'-c' or '--compile' can only be used with modules.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } else { if (Paths.get(this.argList.get(0)).isAbsolute()) { sourcePath = Paths.get(this.argList.get(0)); } else { sourcePath = this.sourceRootPath.resolve(this.argList.get(0)); } this.sourceRootPath = sourcePath.getParent(); if (Files.notExists(sourcePath)) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' Ballerina file does not exist.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!Files.isRegularFile(sourcePath)) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' is not a Ballerina file. Check if it is a symlink or a shortcut.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } try { targetPath = Files.createTempDirectory("ballerina-build-" + System.nanoTime()); } catch (IOException e) { throw LauncherUtils.createLauncherException("Error occurred when creating the executable."); } } } else if (Files.exists( this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME).resolve(this.argList.get(0))) && Files.isDirectory( this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME) .resolve(this.argList.get(0)))) { if (null != this.output) { CommandUtil.printError(this.errStream, "'-o' and '--output' are only supported for building a single Ballerina " + "file.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!RepoUtils.isBallerinaProject(this.sourceRootPath)) { CommandUtil.printError(this.errStream, "you are trying to build/compile a module that is not inside a project.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (Paths.get(argList.get(0)).isAbsolute()) { CommandUtil.printError(this.errStream, "you are trying to build/compile a module giving the absolute path. You " + "only need to give the name of the module.", "ballerina build [-c] <module-name>", true); CommandUtil.exitError(this.exitWhenFinish); return; } String moduleName = argList.get(0); if (moduleName.endsWith("/")) { moduleName = moduleName.substring(0, moduleName.length() - 1); } sourcePath = Paths.get(moduleName); if (Files.notExists(this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME).resolve(sourcePath))) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' module does not exist.", "ballerina build [-c] <module-name>", true); CommandUtil.exitError(this.exitWhenFinish); return; } targetPath = this.sourceRootPath.resolve(ProjectDirConstants.TARGET_DIR_NAME); } else { CommandUtil.printError(this.errStream, "invalid Ballerina source path. It should either be a name of a module in a " + "Ballerina project or a file with a \'" + BLangConstants.BLANG_SRC_FILE_SUFFIX + "\' extension. Use -a or --all " + "to build or compile all modules.", "ballerina build {<ballerina-file> | <module-name> | -a | --all}", true); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = this.sourceRootPath.normalize(); sourcePath = sourcePath == null ? null : sourcePath.normalize(); targetPath = targetPath.normalize(); CompilerContext compilerContext = new CompilerContext(); CompilerOptions options = CompilerOptions.getInstance(compilerContext); options.put(PROJECT_DIR, this.sourceRootPath.toString()); options.put(DUMP_BIR, Boolean.toString(dumpBIR)); options.put(DUMP_BIR_FILE, dumpBIRFile); options.put(OFFLINE, Boolean.toString(this.offline)); options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString()); options.put(LOCK_ENABLED, Boolean.toString(!this.skipLock)); options.put(SKIP_TESTS, Boolean.toString(this.skipTests)); options.put(TEST_ENABLED, Boolean.toString(!this.skipTests)); options.put(EXPERIMENTAL_FEATURES_ENABLED, Boolean.toString(this.experimentalFlag)); options.put(PRESERVE_WHITESPACE, "true"); BuildContext buildContext = new BuildContext(this.sourceRootPath, targetPath, sourcePath, compilerContext); JarResolver jarResolver = JarResolverImpl.getInstance(buildContext, skipCopyLibsFromDist, observabilityIncluded); buildContext.put(BuildContextField.JAR_RESOLVER, jarResolver); buildContext.setOut(outStream); buildContext.setErr(errStream); boolean isSingleFileBuild = buildContext.getSourceType().equals(SINGLE_BAL_FILE); Path outputPath = null == this.output ? Paths.get(System.getProperty("user.dir")) : Paths.get(this.output); TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder() .addTask(new CleanTargetDirTask(), isSingleFileBuild) .addTask(new CreateTargetDirTask()) .addTask(new CompileTask()) .addTask(new CreateBirTask()) .addTask(new CreateLockFileTask(), this.skipLock || isSingleFileBuild) .addTask(new CreateBaloTask(), isSingleFileBuild) .addTask(new CreateJarTask()) .addTask(new CopyResourcesTask(), isSingleFileBuild) .addTask(new CopyObservabilitySymbolsTask(), isSingleFileBuild) .addTask(new RunTestsTask(testReport, coverage, args), this.skipTests || isSingleFileBuild) .addTask(new CreateExecutableTask(), this.compile) .addTask(new CopyExecutableTask(outputPath), !isSingleFileBuild) .addTask(new PrintExecutablePathTask(), this.compile) .addTask(new RunCompilerPluginTask(), this.compile) .addTask(new CleanTargetDirTask(), !isSingleFileBuild) .build(); taskExecutor.executeTasks(buildContext); if (this.exitWhenFinish) { Runtime.getRuntime().exit(0); } } BuildCommand();  BuildCommand(Path userDir, PrintStream outStream, PrintStream errStream, boolean exitWhenFinish,
                        boolean skipCopyLibsFromDist);  BuildCommand(Path userDir, PrintStream outStream, PrintStream errStream, boolean exitWhenFinish,
                        boolean skipCopyLibsFromDist, Path executableOutputDir); }
BuildCommand implements BLauncherCmd { public void execute() { if (this.helpFlag) { String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(BUILD_COMMAND); this.errStream.println(commandUsageInfo); return; } String[] args; if (this.argList == null) { args = new String[0]; } else if (this.buildAll) { args = this.argList.toArray(new String[0]); } else { args = argList.subList(1, argList.size()).toArray(new String[0]); } if (!this.skipTests && this.debugPort != null) { System.setProperty(SYSTEM_PROP_BAL_DEBUG, this.debugPort); } String[] userArgs = LaunchUtils.getUserArgs(args, new HashMap<>()); if (userArgs.length > 0) { CommandUtil.printError(this.errStream, "too many arguments.", buildCmd, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!this.buildAll && (this.argList == null || this.argList.size() == 0)) { CommandUtil.printError(this.errStream, "'build' command requires a module name or a Ballerina file to build/compile. Use '-a' or " + "'--all' to build/compile all the modules of the project.", "ballerina build {<ballerina-file> | <module-name> | -a | --all}", false); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = null != this.sourceRoot ? Paths.get(this.sourceRoot).toAbsolutePath() : this.sourceRootPath; Path sourcePath = null; Path targetPath; if (this.buildAll) { if (null != this.output) { CommandUtil.printError(this.errStream, "'-o' and '--output' are only supported when building a single Ballerina " + "file.", "ballerina build -o <output-file> <ballerina-file> ", true); CommandUtil.exitError(this.exitWhenFinish); return; } if (!ProjectDirs.isProject(this.sourceRootPath)) { Path findRoot = ProjectDirs.findProjectRoot(this.sourceRootPath); if (null == findRoot) { CommandUtil.printError(this.errStream, "you are trying to build/compile a Ballerina project that does not have a " + "Ballerina.toml file.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = findRoot; } targetPath = this.sourceRootPath.resolve(ProjectDirConstants.TARGET_DIR_NAME); } else if (this.argList.get(0).endsWith(BLangConstants.BLANG_SRC_FILE_SUFFIX)) { if (coverage) { coverage = false; this.outStream.println("Code coverage is not yet supported with single bal files. Ignoring the flag " + "and continuing the test run..."); } if (this.compile) { CommandUtil.printError(this.errStream, "'-c' or '--compile' can only be used with modules.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } else { if (Paths.get(this.argList.get(0)).isAbsolute()) { sourcePath = Paths.get(this.argList.get(0)); } else { sourcePath = this.sourceRootPath.resolve(this.argList.get(0)); } this.sourceRootPath = sourcePath.getParent(); if (Files.notExists(sourcePath)) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' Ballerina file does not exist.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!Files.isRegularFile(sourcePath)) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' is not a Ballerina file. Check if it is a symlink or a shortcut.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } try { targetPath = Files.createTempDirectory("ballerina-build-" + System.nanoTime()); } catch (IOException e) { throw LauncherUtils.createLauncherException("Error occurred when creating the executable."); } } } else if (Files.exists( this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME).resolve(this.argList.get(0))) && Files.isDirectory( this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME) .resolve(this.argList.get(0)))) { if (null != this.output) { CommandUtil.printError(this.errStream, "'-o' and '--output' are only supported for building a single Ballerina " + "file.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!RepoUtils.isBallerinaProject(this.sourceRootPath)) { CommandUtil.printError(this.errStream, "you are trying to build/compile a module that is not inside a project.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (Paths.get(argList.get(0)).isAbsolute()) { CommandUtil.printError(this.errStream, "you are trying to build/compile a module giving the absolute path. You " + "only need to give the name of the module.", "ballerina build [-c] <module-name>", true); CommandUtil.exitError(this.exitWhenFinish); return; } String moduleName = argList.get(0); if (moduleName.endsWith("/")) { moduleName = moduleName.substring(0, moduleName.length() - 1); } sourcePath = Paths.get(moduleName); if (Files.notExists(this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME).resolve(sourcePath))) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' module does not exist.", "ballerina build [-c] <module-name>", true); CommandUtil.exitError(this.exitWhenFinish); return; } targetPath = this.sourceRootPath.resolve(ProjectDirConstants.TARGET_DIR_NAME); } else { CommandUtil.printError(this.errStream, "invalid Ballerina source path. It should either be a name of a module in a " + "Ballerina project or a file with a \'" + BLangConstants.BLANG_SRC_FILE_SUFFIX + "\' extension. Use -a or --all " + "to build or compile all modules.", "ballerina build {<ballerina-file> | <module-name> | -a | --all}", true); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = this.sourceRootPath.normalize(); sourcePath = sourcePath == null ? null : sourcePath.normalize(); targetPath = targetPath.normalize(); CompilerContext compilerContext = new CompilerContext(); CompilerOptions options = CompilerOptions.getInstance(compilerContext); options.put(PROJECT_DIR, this.sourceRootPath.toString()); options.put(DUMP_BIR, Boolean.toString(dumpBIR)); options.put(DUMP_BIR_FILE, dumpBIRFile); options.put(OFFLINE, Boolean.toString(this.offline)); options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString()); options.put(LOCK_ENABLED, Boolean.toString(!this.skipLock)); options.put(SKIP_TESTS, Boolean.toString(this.skipTests)); options.put(TEST_ENABLED, Boolean.toString(!this.skipTests)); options.put(EXPERIMENTAL_FEATURES_ENABLED, Boolean.toString(this.experimentalFlag)); options.put(PRESERVE_WHITESPACE, "true"); BuildContext buildContext = new BuildContext(this.sourceRootPath, targetPath, sourcePath, compilerContext); JarResolver jarResolver = JarResolverImpl.getInstance(buildContext, skipCopyLibsFromDist, observabilityIncluded); buildContext.put(BuildContextField.JAR_RESOLVER, jarResolver); buildContext.setOut(outStream); buildContext.setErr(errStream); boolean isSingleFileBuild = buildContext.getSourceType().equals(SINGLE_BAL_FILE); Path outputPath = null == this.output ? Paths.get(System.getProperty("user.dir")) : Paths.get(this.output); TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder() .addTask(new CleanTargetDirTask(), isSingleFileBuild) .addTask(new CreateTargetDirTask()) .addTask(new CompileTask()) .addTask(new CreateBirTask()) .addTask(new CreateLockFileTask(), this.skipLock || isSingleFileBuild) .addTask(new CreateBaloTask(), isSingleFileBuild) .addTask(new CreateJarTask()) .addTask(new CopyResourcesTask(), isSingleFileBuild) .addTask(new CopyObservabilitySymbolsTask(), isSingleFileBuild) .addTask(new RunTestsTask(testReport, coverage, args), this.skipTests || isSingleFileBuild) .addTask(new CreateExecutableTask(), this.compile) .addTask(new CopyExecutableTask(outputPath), !isSingleFileBuild) .addTask(new PrintExecutablePathTask(), this.compile) .addTask(new RunCompilerPluginTask(), this.compile) .addTask(new CleanTargetDirTask(), !isSingleFileBuild) .build(); taskExecutor.executeTasks(buildContext); if (this.exitWhenFinish) { Runtime.getRuntime().exit(0); } } BuildCommand();  BuildCommand(Path userDir, PrintStream outStream, PrintStream errStream, boolean exitWhenFinish,
                        boolean skipCopyLibsFromDist);  BuildCommand(Path userDir, PrintStream outStream, PrintStream errStream, boolean exitWhenFinish,
                        boolean skipCopyLibsFromDist, Path executableOutputDir); void execute(); @Override String getName(); @Override void printLongDesc(StringBuilder out); @Override void printUsage(StringBuilder out); @Override void setParentCmdParser(CommandLine parentCmdParser); }
BuildCommand implements BLauncherCmd { public void execute() { if (this.helpFlag) { String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(BUILD_COMMAND); this.errStream.println(commandUsageInfo); return; } String[] args; if (this.argList == null) { args = new String[0]; } else if (this.buildAll) { args = this.argList.toArray(new String[0]); } else { args = argList.subList(1, argList.size()).toArray(new String[0]); } if (!this.skipTests && this.debugPort != null) { System.setProperty(SYSTEM_PROP_BAL_DEBUG, this.debugPort); } String[] userArgs = LaunchUtils.getUserArgs(args, new HashMap<>()); if (userArgs.length > 0) { CommandUtil.printError(this.errStream, "too many arguments.", buildCmd, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!this.buildAll && (this.argList == null || this.argList.size() == 0)) { CommandUtil.printError(this.errStream, "'build' command requires a module name or a Ballerina file to build/compile. Use '-a' or " + "'--all' to build/compile all the modules of the project.", "ballerina build {<ballerina-file> | <module-name> | -a | --all}", false); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = null != this.sourceRoot ? Paths.get(this.sourceRoot).toAbsolutePath() : this.sourceRootPath; Path sourcePath = null; Path targetPath; if (this.buildAll) { if (null != this.output) { CommandUtil.printError(this.errStream, "'-o' and '--output' are only supported when building a single Ballerina " + "file.", "ballerina build -o <output-file> <ballerina-file> ", true); CommandUtil.exitError(this.exitWhenFinish); return; } if (!ProjectDirs.isProject(this.sourceRootPath)) { Path findRoot = ProjectDirs.findProjectRoot(this.sourceRootPath); if (null == findRoot) { CommandUtil.printError(this.errStream, "you are trying to build/compile a Ballerina project that does not have a " + "Ballerina.toml file.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = findRoot; } targetPath = this.sourceRootPath.resolve(ProjectDirConstants.TARGET_DIR_NAME); } else if (this.argList.get(0).endsWith(BLangConstants.BLANG_SRC_FILE_SUFFIX)) { if (coverage) { coverage = false; this.outStream.println("Code coverage is not yet supported with single bal files. Ignoring the flag " + "and continuing the test run..."); } if (this.compile) { CommandUtil.printError(this.errStream, "'-c' or '--compile' can only be used with modules.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } else { if (Paths.get(this.argList.get(0)).isAbsolute()) { sourcePath = Paths.get(this.argList.get(0)); } else { sourcePath = this.sourceRootPath.resolve(this.argList.get(0)); } this.sourceRootPath = sourcePath.getParent(); if (Files.notExists(sourcePath)) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' Ballerina file does not exist.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!Files.isRegularFile(sourcePath)) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' is not a Ballerina file. Check if it is a symlink or a shortcut.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } try { targetPath = Files.createTempDirectory("ballerina-build-" + System.nanoTime()); } catch (IOException e) { throw LauncherUtils.createLauncherException("Error occurred when creating the executable."); } } } else if (Files.exists( this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME).resolve(this.argList.get(0))) && Files.isDirectory( this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME) .resolve(this.argList.get(0)))) { if (null != this.output) { CommandUtil.printError(this.errStream, "'-o' and '--output' are only supported for building a single Ballerina " + "file.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (!RepoUtils.isBallerinaProject(this.sourceRootPath)) { CommandUtil.printError(this.errStream, "you are trying to build/compile a module that is not inside a project.", null, false); CommandUtil.exitError(this.exitWhenFinish); return; } if (Paths.get(argList.get(0)).isAbsolute()) { CommandUtil.printError(this.errStream, "you are trying to build/compile a module giving the absolute path. You " + "only need to give the name of the module.", "ballerina build [-c] <module-name>", true); CommandUtil.exitError(this.exitWhenFinish); return; } String moduleName = argList.get(0); if (moduleName.endsWith("/")) { moduleName = moduleName.substring(0, moduleName.length() - 1); } sourcePath = Paths.get(moduleName); if (Files.notExists(this.sourceRootPath.resolve(ProjectDirConstants.SOURCE_DIR_NAME).resolve(sourcePath))) { CommandUtil.printError(this.errStream, "'" + sourcePath + "' module does not exist.", "ballerina build [-c] <module-name>", true); CommandUtil.exitError(this.exitWhenFinish); return; } targetPath = this.sourceRootPath.resolve(ProjectDirConstants.TARGET_DIR_NAME); } else { CommandUtil.printError(this.errStream, "invalid Ballerina source path. It should either be a name of a module in a " + "Ballerina project or a file with a \'" + BLangConstants.BLANG_SRC_FILE_SUFFIX + "\' extension. Use -a or --all " + "to build or compile all modules.", "ballerina build {<ballerina-file> | <module-name> | -a | --all}", true); CommandUtil.exitError(this.exitWhenFinish); return; } this.sourceRootPath = this.sourceRootPath.normalize(); sourcePath = sourcePath == null ? null : sourcePath.normalize(); targetPath = targetPath.normalize(); CompilerContext compilerContext = new CompilerContext(); CompilerOptions options = CompilerOptions.getInstance(compilerContext); options.put(PROJECT_DIR, this.sourceRootPath.toString()); options.put(DUMP_BIR, Boolean.toString(dumpBIR)); options.put(DUMP_BIR_FILE, dumpBIRFile); options.put(OFFLINE, Boolean.toString(this.offline)); options.put(COMPILER_PHASE, CompilerPhase.CODE_GEN.toString()); options.put(LOCK_ENABLED, Boolean.toString(!this.skipLock)); options.put(SKIP_TESTS, Boolean.toString(this.skipTests)); options.put(TEST_ENABLED, Boolean.toString(!this.skipTests)); options.put(EXPERIMENTAL_FEATURES_ENABLED, Boolean.toString(this.experimentalFlag)); options.put(PRESERVE_WHITESPACE, "true"); BuildContext buildContext = new BuildContext(this.sourceRootPath, targetPath, sourcePath, compilerContext); JarResolver jarResolver = JarResolverImpl.getInstance(buildContext, skipCopyLibsFromDist, observabilityIncluded); buildContext.put(BuildContextField.JAR_RESOLVER, jarResolver); buildContext.setOut(outStream); buildContext.setErr(errStream); boolean isSingleFileBuild = buildContext.getSourceType().equals(SINGLE_BAL_FILE); Path outputPath = null == this.output ? Paths.get(System.getProperty("user.dir")) : Paths.get(this.output); TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder() .addTask(new CleanTargetDirTask(), isSingleFileBuild) .addTask(new CreateTargetDirTask()) .addTask(new CompileTask()) .addTask(new CreateBirTask()) .addTask(new CreateLockFileTask(), this.skipLock || isSingleFileBuild) .addTask(new CreateBaloTask(), isSingleFileBuild) .addTask(new CreateJarTask()) .addTask(new CopyResourcesTask(), isSingleFileBuild) .addTask(new CopyObservabilitySymbolsTask(), isSingleFileBuild) .addTask(new RunTestsTask(testReport, coverage, args), this.skipTests || isSingleFileBuild) .addTask(new CreateExecutableTask(), this.compile) .addTask(new CopyExecutableTask(outputPath), !isSingleFileBuild) .addTask(new PrintExecutablePathTask(), this.compile) .addTask(new RunCompilerPluginTask(), this.compile) .addTask(new CleanTargetDirTask(), !isSingleFileBuild) .build(); taskExecutor.executeTasks(buildContext); if (this.exitWhenFinish) { Runtime.getRuntime().exit(0); } } BuildCommand();  BuildCommand(Path userDir, PrintStream outStream, PrintStream errStream, boolean exitWhenFinish,
                        boolean skipCopyLibsFromDist);  BuildCommand(Path userDir, PrintStream outStream, PrintStream errStream, boolean exitWhenFinish,
                        boolean skipCopyLibsFromDist, Path executableOutputDir); void execute(); @Override String getName(); @Override void printLongDesc(StringBuilder out); @Override void printUsage(StringBuilder out); @Override void setParentCmdParser(CommandLine parentCmdParser);  }