diff --git a/gradle.properties b/gradle.properties index 060688c6..321c5ec1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group=org.ballerinalang -version=1.3.2-SNAPSHOT +version=1.3.2-build1 ballerinaJreVersion=0.8.0 diff --git a/resources/bin/bal b/resources/bin/bal index 47afea83..2a333666 100755 --- a/resources/bin/bal +++ b/resources/bin/bal @@ -30,11 +30,29 @@ elif test -d "$CURRENT_PATH/../dependencies/jdk8u202-b08-jre"; then JAVA_COMMAND="$CURRENT_PATH/../dependencies/jdk8u202-b08-jre/bin/java" fi +if [ "$1" == "completion" ] +then + if [ "$2" == "bash" ] + then + printf "#!/usr/bin/env bash\n\n" + cat $CURRENT_PATH/../scripts/bal_completion.bash + elif [ "$2" == "zsh" ] + printf "#!/usr/bin/env bash\n\n" + printf "autoload -U +X bashcompinit && bashcompinit\n" + printf "autoload -U +X compinit && compinit\n\n" + cat $CURRENT_PATH/../scripts/bal_completion.bash + fi + if [ $? -ne '0' ]; then + echo "Failed to generate the completion script" + EXIT_CODE=$? + fi + exit 0 +fi + RUN_COMMAND=false RUN_BALLERINA=true -if [ "$1" == "dist" ] || [ "$2" == "dist" ] || [ "$1" == "update" ]|| [ "$2" == "update" ] || \ -[ "$1" == "completion" ] || [ "$2" == "completion" ] +if [ "$1" == "dist" ] || [ "$2" == "dist" ] || [ "$1" == "update" ]|| [ "$2" == "update" ] then RUN_COMMAND=true RUN_BALLERINA=false diff --git a/resources/scripts/bal_completion.bash b/resources/scripts/bal_completion.bash new file mode 100644 index 00000000..7be4e39f --- /dev/null +++ b/resources/scripts/bal_completion.bash @@ -0,0 +1,49 @@ +# Get the file path based on OS type +if [[ "$OSTYPE" == "linux-gnu"* ]]; then + dists_path=/usr/lib/ballerina/distributions +elif [[ "$OSTYPE" == "darwin"* ]]; then + dists_path=/Library/Ballerina/distributions +fi + +_bal_completions() +{ + balCommands="-v --version add build clean dist doc format help new pull push run test update version" + dist="list pull remove update use" + + # Get current ballerina version + if test -f "$HOME/.ballerina/ballerina-version"; then + bal_version=$(cat $HOME/.ballerina/ballerina-version) + else + bal_version=$(cat $dists_path/ballerina-version) + fi + + file_path=$dists_path/$bal_version/resources/command-completion/command-completion.csv + + if test -f "$file_path"; then + # Read the optional flags from command-completion file + while IFS=, read -r cmdname flags + do + if [[ "${COMP_WORDS[1]}" = "$cmdname" ]]; then + flags_arr="$flags" + elif [[ "$cmdname" == "bal" ]]; then + balCommands="$flags" + fi + done < <(tail -n +2 $file_path) + fi + + if [[ "$COMP_CWORD" == "1" ]]; then + COMPREPLY=($(compgen -W "$balCommands -h --help" -- "${COMP_WORDS[$COMP_CWORD]}")) + return + else + if [[ ("${COMP_WORDS[1]}" = "dist") ]]; then + COMPREPLY=($(compgen -W "$dist -h --help" "${COMP_WORDS[$COMP_CWORD]}")) + return + elif [[ "${COMP_WORDS[$COMP_CWORD]}" =~ ^\-.* ]] && [ -f "$file_path" ]; then + # If last word has - we will suggest flags. + COMPREPLY=($(compgen -W "${flags_arr[@]} -h --help" -- "${COMP_WORDS[$COMP_CWORD]}")) + return + fi + fi +} + +complete -o bashdefault -o default -F _bal_completions bal diff --git a/resources/scripts/install b/resources/scripts/install index a56ada82..f1c96fa1 100755 --- a/resources/scripts/install +++ b/resources/scripts/install @@ -43,6 +43,12 @@ if [ $? -ne '0' ]; then exit $? fi +\cp $CURRENT_PATH/ballerina-command-@version@/scripts/bal_completion.bash $CURRENT_PATH/../scripts + +if [ $? -ne '0' ]; then + echo "error occurred while copying completion script file." +fi + echo "Updating environment variables" if [ -f "/usr/lib/ballerina/bin/ballerina" ] then diff --git a/src/main/java/org/ballerinalang/command/BallerinaCliCommands.java b/src/main/java/org/ballerinalang/command/BallerinaCliCommands.java index f6c3dc0a..676a2050 100644 --- a/src/main/java/org/ballerinalang/command/BallerinaCliCommands.java +++ b/src/main/java/org/ballerinalang/command/BallerinaCliCommands.java @@ -33,7 +33,4 @@ public class BallerinaCliCommands { public static final String USE = "use"; public static final String REMOVE = "remove"; public static final String VERSION = "version"; - public static final String COMPLETION = "completion"; - public static final String BASH = "bash"; - public static final String ZSH = "zsh"; } diff --git a/src/main/java/org/ballerinalang/command/Main.java b/src/main/java/org/ballerinalang/command/Main.java index 8d631600..1b039fe8 100644 --- a/src/main/java/org/ballerinalang/command/Main.java +++ b/src/main/java/org/ballerinalang/command/Main.java @@ -17,9 +17,7 @@ package org.ballerinalang.command; import org.ballerinalang.command.cmd.BCommand; -import org.ballerinalang.command.cmd.BashCommand; import org.ballerinalang.command.cmd.BuildCommand; -import org.ballerinalang.command.cmd.CompletionCommand; import org.ballerinalang.command.cmd.DefaultCommand; import org.ballerinalang.command.cmd.DistributionCommand; import org.ballerinalang.command.cmd.HelpCommand; @@ -30,7 +28,6 @@ import org.ballerinalang.command.cmd.UpdateToolCommand; import org.ballerinalang.command.cmd.UseCommand; import org.ballerinalang.command.cmd.VersionCommand; -import org.ballerinalang.command.cmd.ZshCommand; import org.ballerinalang.command.exceptions.CommandException; import org.ballerinalang.command.util.ErrorUtil; import picocli.CommandLine; @@ -74,18 +71,6 @@ private static Optional getInvokedCmd(String... args) { CommandLine distCmdParser = new CommandLine(distCmd); distCmd.setParentCmdParser(distCmdParser); - CompletionCommand completionCmd = new CompletionCommand(outStream); - CommandLine completionCmdParser = new CommandLine(completionCmd); - completionCmd.setParentCmdParser(completionCmdParser); - - BashCommand bashCmd = new BashCommand(outStream); - completionCmdParser.addSubcommand(BallerinaCliCommands.BASH, bashCmd); - bashCmd.setParentCmdParser(completionCmdParser); - - ZshCommand zshCmd = new ZshCommand(outStream); - completionCmdParser.addSubcommand(BallerinaCliCommands.ZSH, zshCmd); - zshCmd.setParentCmdParser(completionCmdParser); - ListCommand listCmd = new ListCommand(outStream); distCmdParser.addSubcommand(BallerinaCliCommands.LIST, listCmd); listCmd.setParentCmdParser(distCmdParser); @@ -113,11 +98,7 @@ private static Optional getInvokedCmd(String... args) { distCmdParser.setCommandName("dist"); distCmdParser.setPosixClusteredShortOptionsAllowed(false); - completionCmdParser.setCommandName("completion"); - completionCmdParser.setPosixClusteredShortOptionsAllowed(false); - cmdParser.addSubcommand(BallerinaCliCommands.DIST, distCmdParser); - cmdParser.addSubcommand(BallerinaCliCommands.COMPLETION, completionCmdParser); UpdateToolCommand updateToolCommand = new UpdateToolCommand(outStream); cmdParser.addSubcommand(BallerinaCliCommands.UPDATE, updateToolCommand); diff --git a/src/main/java/org/ballerinalang/command/cmd/BashCommand.java b/src/main/java/org/ballerinalang/command/cmd/BashCommand.java deleted file mode 100644 index 952ea41a..00000000 --- a/src/main/java/org/ballerinalang/command/cmd/BashCommand.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.ballerinalang.command.cmd; - -import org.ballerinalang.command.BallerinaCliCommands; -import org.ballerinalang.command.util.ErrorUtil; -import org.ballerinalang.command.util.ToolUtil; -import picocli.CommandLine; - -import java.io.PrintStream; -import java.util.List; - -/** - * This class represents the "Bash" command and it holds arguments and flags specified by the user. - * - * @since 2.0.0 - */ -@CommandLine.Command(name = "bash", description = "Ballerina bash commands") -public class BashCommand extends Command implements BCommand { - @CommandLine.Parameters(description = "Command name") - private List listCommands; - - @CommandLine.Option(names = {"--help", "-h", "?"}, hidden = true) - private boolean helpFlag; - - private CommandLine parentCmdParser; - - public BashCommand(PrintStream printStream) { - super(printStream); - } - - public void execute() { - if (helpFlag) { - printUsageInfo("completion-" + BallerinaCliCommands.BASH); - return; - } - - if (listCommands == null) { - getPrintStream().println(ToolUtil.getCompletionScript()); - return; - } - - if (listCommands.size() > 0) { - throw ErrorUtil.createDistSubCommandUsageExceptionWithHelp("too many arguments", BallerinaCliCommands.BASH); - } - } - - @Override - public String getName() { - return BallerinaCliCommands.BASH; - } - - @Override - public void printLongDesc(StringBuilder out) { - - } - - @Override - public void printUsage(StringBuilder out) { - } - - @Override - public void setParentCmdParser(CommandLine parentCmdParser) { - this.parentCmdParser = parentCmdParser; - } -} diff --git a/src/main/java/org/ballerinalang/command/cmd/CompletionCommand.java b/src/main/java/org/ballerinalang/command/cmd/CompletionCommand.java deleted file mode 100644 index 4acb7739..00000000 --- a/src/main/java/org/ballerinalang/command/cmd/CompletionCommand.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.ballerinalang.command.cmd; - -import org.ballerinalang.command.BallerinaCliCommands; -import org.ballerinalang.command.util.ErrorUtil; -import picocli.CommandLine; - -import java.io.PrintStream; -import java.util.List; - -/** - * This class represents the "Completion" command and it holds arguments and flags specified by the user. - * - * @since 2.0.0 - */ -@CommandLine.Command(name = "completion", description = "Ballerina completion commands") -public class CompletionCommand extends Command implements BCommand { - @CommandLine.Parameters(description = "Command name") - private List completionCommands; - - @CommandLine.Option(names = { "--help", "-h", "?" }, hidden = true, description = "for more information") - private boolean helpFlag; - - public CompletionCommand(PrintStream printStream) { - super(printStream); - } - - private CommandLine parentCmdParser; - - @Override - public void execute() { - if (helpFlag || completionCommands == null) { - printUsageInfo(BallerinaCliCommands.COMPLETION); - return; - } - - if (completionCommands.size() > 1) { - throw ErrorUtil.createUsageExceptionWithHelp("too many arguments", BallerinaCliCommands.COMPLETION); - } - - throw ErrorUtil.createUsageExceptionWithHelp("unknown command '" + completionCommands.get(0) + "'", - BallerinaCliCommands.COMPLETION); - } - - @Override - public String getName() { - return BallerinaCliCommands.COMPLETION; - } - - @Override - public void printLongDesc(StringBuilder out) { - - } - - @Override - public void printUsage(StringBuilder out) { - } - - @Override - public void setParentCmdParser(CommandLine parentCmdParser) { - this.parentCmdParser = parentCmdParser; - } -} diff --git a/src/main/java/org/ballerinalang/command/cmd/ZshCommand.java b/src/main/java/org/ballerinalang/command/cmd/ZshCommand.java deleted file mode 100644 index c9ad9f16..00000000 --- a/src/main/java/org/ballerinalang/command/cmd/ZshCommand.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.ballerinalang.command.cmd; - -import org.ballerinalang.command.BallerinaCliCommands; -import org.ballerinalang.command.util.ErrorUtil; -import org.ballerinalang.command.util.ToolUtil; -import picocli.CommandLine; - -import java.io.PrintStream; -import java.util.List; - -/** - * This class represents the "Zsh" command and it holds arguments and flags specified by the user. - * - * @since 2.0.0 - */ -@CommandLine.Command(name = "zsh", description = "Ballerina Z Shell commands") -public class ZshCommand extends Command implements BCommand { - @CommandLine.Parameters(description = "Command name") - private List listCommands; - - @CommandLine.Option(names = {"--help", "-h", "?"}, hidden = true) - private boolean helpFlag; - - private CommandLine parentCmdParser; - - public ZshCommand(PrintStream printStream) { - super(printStream); - } - - public void execute() { - if (helpFlag) { - printUsageInfo("completion-" + BallerinaCliCommands.ZSH); - return; - } - - if (listCommands == null) { - getPrintStream().println("autoload -U +X bashcompinit && bashcompinit"); - getPrintStream().println("autoload -U +X compinit && compinit\n"); - getPrintStream().println(ToolUtil.getCompletionScript()); - return; - } - - if (listCommands.size() > 0) { - throw ErrorUtil.createDistSubCommandUsageExceptionWithHelp("too many arguments", BallerinaCliCommands.ZSH); - } - } - - @Override - public String getName() { - return BallerinaCliCommands.ZSH; - } - - @Override - public void printLongDesc(StringBuilder out) { - - } - - @Override - public void printUsage(StringBuilder out) { - } - - @Override - public void setParentCmdParser(CommandLine parentCmdParser) { - this.parentCmdParser = parentCmdParser; - } -} diff --git a/src/main/java/org/ballerinalang/command/util/ToolUtil.java b/src/main/java/org/ballerinalang/command/util/ToolUtil.java index 94d0d5f9..3c4aa8a2 100644 --- a/src/main/java/org/ballerinalang/command/util/ToolUtil.java +++ b/src/main/java/org/ballerinalang/command/util/ToolUtil.java @@ -37,8 +37,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -46,10 +44,6 @@ import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; /** * Ballerina tool utilities. @@ -829,18 +823,4 @@ private static void addExecutablePermissionToDirectory(String filePath) { + "'"); } } - - /** - * Retrieve completion script. - * - * @return completion script - */ - public static String getCompletionScript() { - String fileName = "completion-script/bal_completion.bash"; - try { - return ToolUtil.readFileAsString(fileName); - } catch (IOException e) { - throw ErrorUtil.createCommandException("failed to read the completion script: " + e.getMessage()); - } - } } diff --git a/src/main/resources/cli-help/ballerina-completion-bash.help b/src/main/resources/cli-help/ballerina-completion-bash.help deleted file mode 100644 index e6e22620..00000000 --- a/src/main/resources/cli-help/ballerina-completion-bash.help +++ /dev/null @@ -1,8 +0,0 @@ -NAME - bal-completion-bash - Generate the bash completion script - -SYNOPSIS - bal completion bash - -DESCRIPTION - Bash enables you to generate the bash completion script diff --git a/src/main/resources/cli-help/ballerina-completion-zsh.help b/src/main/resources/cli-help/ballerina-completion-zsh.help deleted file mode 100644 index 98131412..00000000 --- a/src/main/resources/cli-help/ballerina-completion-zsh.help +++ /dev/null @@ -1,8 +0,0 @@ -NAME - bal-completion-zsh - Generate the zsh completion script - -SYNOPSIS - bal completion zsh - -DESCRIPTION - Zsh enables you to generate the zsh completion script diff --git a/src/main/resources/cli-help/ballerina-completion.help b/src/main/resources/cli-help/ballerina-completion.help deleted file mode 100644 index a0072b0d..00000000 --- a/src/main/resources/cli-help/ballerina-completion.help +++ /dev/null @@ -1,20 +0,0 @@ -NAME - bal-completion - Manage Ballerina CLI completion - -SYNOPSIS - bal completion - -DESCRIPTION - Completion enables you to manage Ballerina CLI completion. - -OPTIONS - -h, --help - Print usage details of a command. - -BALLERINA COMMANDS - Here is a list of available subcommands: - - bash Generate the bash completion script. - zsh Generate the zsh completion script. - -Use 'bal help completion ' for more information on a specific command. diff --git a/src/main/resources/completion-script/bal_completion.bash b/src/main/resources/completion-script/bal_completion.bash deleted file mode 100644 index c3c29ab0..00000000 --- a/src/main/resources/completion-script/bal_completion.bash +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash - -_bal_completions() -{ - flagVar=" -- " - for ((i = 1; i < ${#COMP_WORDS[@]}; i++)) - do - flagVar="$flagVar ${COMP_WORDS[$i]}" - done - balCommands=$(bal compgen $flagVar 2>/dev/null) - - if [ $? == '0' ]; then - COMPREPLY=($(compgen -W "$balCommands")) - return - fi - COMPREPLY=($(compgen -o plusdirs -f -X '!*.bal' -- "${COMP_WORDS[COMP_CWORD]}")) -} - -complete -o nospace -F _bal_completions bal diff --git a/src/test/java/org/ballerinalang/command/CommandTest.java b/src/test/java/org/ballerinalang/command/CommandTest.java index 9364dcd6..d79cece5 100644 --- a/src/test/java/org/ballerinalang/command/CommandTest.java +++ b/src/test/java/org/ballerinalang/command/CommandTest.java @@ -16,13 +16,10 @@ package org.ballerinalang.command; -import org.ballerinalang.command.cmd.BashCommand; import org.ballerinalang.command.cmd.BuildCommand; -import org.ballerinalang.command.cmd.CompletionCommand; import org.ballerinalang.command.cmd.HelpCommand; import org.ballerinalang.command.cmd.ListCommand; import org.ballerinalang.command.cmd.VersionCommand; -import org.ballerinalang.command.cmd.ZshCommand; import org.ballerinalang.command.util.OSUtils; import org.junit.Before; import org.testng.Assert; @@ -83,25 +80,4 @@ public void helpCommandTest() { Assert.assertTrue(outContent.toString().contains("dist Manage Ballerina distributions")); Assert.assertTrue(outContent.toString().contains("update Update the Ballerina tool")); } - - @Test - public void completionCommandTest() { - CompletionCommand completionCommand = new CompletionCommand(testStream); - completionCommand.execute(); - Assert.assertTrue(outContent.toString().contains("bal-completion")); - } - - @Test - public void bashCommandTest() { - BashCommand bashCommand = new BashCommand(testStream); - bashCommand.execute(); - Assert.assertTrue(outContent.toString().contains("#!/bin/bash")); - } - - @Test - public void zshCommandTest() { - ZshCommand zshCommand = new ZshCommand(testStream); - zshCommand.execute(); - Assert.assertTrue(outContent.toString().contains("bashcompinit && bashcompinit")); - } }