Skip to content

Commit

Permalink
Merge pull request #145 from suganyasuven/test-cli
Browse files Browse the repository at this point in the history
Intercept the completion script inside bal shell script
  • Loading branch information
Suganya authored Apr 8, 2021
2 parents b687caa + fb62af9 commit 4149419
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 364 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=org.ballerinalang
version=1.3.2-SNAPSHOT
version=1.3.2-build1
ballerinaJreVersion=0.8.0
22 changes: 20 additions & 2 deletions resources/bin/bal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions resources/scripts/bal_completion.bash
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions resources/scripts/install
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
19 changes: 0 additions & 19 deletions src/main/java/org/ballerinalang/command/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -74,18 +71,6 @@ private static Optional<BCommand> 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);
Expand Down Expand Up @@ -113,11 +98,7 @@ private static Optional<BCommand> 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);
Expand Down
80 changes: 0 additions & 80 deletions src/main/java/org/ballerinalang/command/cmd/BashCommand.java

This file was deleted.

78 changes: 0 additions & 78 deletions src/main/java/org/ballerinalang/command/cmd/CompletionCommand.java

This file was deleted.

82 changes: 0 additions & 82 deletions src/main/java/org/ballerinalang/command/cmd/ZshCommand.java

This file was deleted.

Loading

0 comments on commit 4149419

Please sign in to comment.