diff --git a/lib/completely/commands/base.rb b/lib/completely/commands/base.rb index a17fd45..69f1139 100644 --- a/lib/completely/commands/base.rb +++ b/lib/completely/commands/base.rb @@ -5,15 +5,19 @@ module Commands class Base < MisterBin::Command class << self - def config_path_usage - param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]" + def param_config_path + param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable" end - def function_usage + def option_function option "-f --function NAME", "Modify the name of the function in the generated script" end - def debug_usage + def environment_config_path + environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]" + end + + def environment_debug environment "COMPLETELY_DEBUG", "It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a completion is requested" end end @@ -29,7 +33,15 @@ def completions end def config_path - args['CONFIG_PATH'] || 'completely.yaml' + @config_path ||= args['CONFIG_PATH'] || ENV['COMPLETELY_CONFIG_PATH'] || 'completely.yaml' + end + + def script_path + @script_path ||= args['SCRIPT_PATH'] || ENV['COMPLETELY_SCRIPT_PATH'] || "#{config_basename}.bash" + end + + def config_basename + File.basename config_path, File.extname(config_path) end def syntax_warning diff --git a/lib/completely/commands/generate.rb b/lib/completely/commands/generate.rb index 5bb49ac..088f9cd 100644 --- a/lib/completely/commands/generate.rb +++ b/lib/completely/commands/generate.rb @@ -5,21 +5,24 @@ module Commands class Generate < Base help "Generate the bash completion script to a file" - usage "completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]" + usage "completely generate [CONFIG_PATH SCRIPT_PATH --function NAME --wrap NAME]" usage "completely generate (-h|--help)" - function_usage + option_function option "-w --wrap NAME", "Wrap the completion script inside a function that echos the script. This is useful if you wish to embed it directly in your script" - config_path_usage - param "OUTPUT_PATH", "Path to the output bash script [default: completely.bash]" - debug_usage + param_config_path + param "SCRIPT_PATH", "Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension\nCan also be set by an environment variable" + + environment_config_path + environment "COMPLETELY_SCRIPT_PATH", "Path to the output bash script" + environment_debug def run wrap = args['--wrap'] output = wrap ? wrapper_function(wrap) : script - File.write output_path, output - say "Saved !txtpur!#{output_path}" + File.write script_path, output + say "Saved !txtpur!#{script_path}" syntax_warning unless completions.valid? end @@ -29,10 +32,6 @@ def wrapper_function(wrapper_name) completions.wrapper_function wrapper_name end - def output_path - @output_path ||= args['OUTPUT_PATH'] || "completely.bash" - end - end end end diff --git a/lib/completely/commands/init.rb b/lib/completely/commands/init.rb index 277dde5..4116ab2 100644 --- a/lib/completely/commands/init.rb +++ b/lib/completely/commands/init.rb @@ -8,7 +8,8 @@ class Init < Base usage "completely init [CONFIG_PATH]" usage "completely init (-h|--help)" - config_path_usage + param_config_path + environment_config_path def run if File.exist? config_path diff --git a/lib/completely/commands/preview.rb b/lib/completely/commands/preview.rb index 127f453..f4b0123 100644 --- a/lib/completely/commands/preview.rb +++ b/lib/completely/commands/preview.rb @@ -8,9 +8,10 @@ class Preview < Base usage "completely preview [CONFIG_PATH --function NAME]" usage "completely preview (-h|--help)" - function_usage - config_path_usage - debug_usage + option_function + param_config_path + environment_config_path + environment_debug def run puts script diff --git a/lib/completely/commands/test.rb b/lib/completely/commands/test.rb index 98c8fbc..26ed8f6 100644 --- a/lib/completely/commands/test.rb +++ b/lib/completely/commands/test.rb @@ -18,10 +18,10 @@ class Test < Base param "COMPLINE", "The command to test completions for. This will be handled as if a TAB was pressed immediately at the end of it, so the last word is considered the active cursor. If you wish to complete for the next word instead, end your command with a space." - environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]" + environment_config_path environment "COMPLETELY_SCRIPT_PATH", "Path to a completions script. When set, this script will be tested instead of the completely configuration file" environment "COMPLETELY_SCRIPT_FUNCTION", "The main completion function to call when using a custom script. If not set, the basename of the script path will be used, prefixed by an underscore" - debug_usage + environment_debug example %q[completely test "mygit pu"] example %q[completely test "mygit pull "] diff --git a/lib/completely/templates/template.erb b/lib/completely/templates/template.erb index 4e0accf..f8c6ec2 100644 --- a/lib/completely/templates/template.erb +++ b/lib/completely/templates/template.erb @@ -7,6 +7,7 @@ <%= function_name %>() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" @@ -20,7 +21,10 @@ case "$compline" in % patterns.each do |pattern| % next if pattern.empty? - <%= pattern.case_string %>) COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur")) ;; + <%= pattern.case_string %>) + COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur")) + ;; + % end esac } && diff --git a/spec/approvals/cli/generate/custom-path-env b/spec/approvals/cli/generate/custom-path-env new file mode 100644 index 0000000..050d17b --- /dev/null +++ b/spec/approvals/cli/generate/custom-path-env @@ -0,0 +1 @@ +Saved hello.bash diff --git a/spec/approvals/cli/generate/custom-path-env2 b/spec/approvals/cli/generate/custom-path-env2 new file mode 100644 index 0000000..2acdbda --- /dev/null +++ b/spec/approvals/cli/generate/custom-path-env2 @@ -0,0 +1 @@ +Saved spec/tmp/tada.bash diff --git a/spec/approvals/cli/generate/help b/spec/approvals/cli/generate/help index 472b766..ccd8ac2 100644 --- a/spec/approvals/cli/generate/help +++ b/spec/approvals/cli/generate/help @@ -1,7 +1,7 @@ Generate the bash completion script to a file Usage: - completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME] + completely generate [CONFIG_PATH SCRIPT_PATH --function NAME --wrap NAME] completely generate (-h|--help) Options: @@ -18,11 +18,20 @@ Options: Parameters: CONFIG_PATH Path to the YAML configuration file [default: completely.yaml] + Can also be set by an environment variable - OUTPUT_PATH - Path to the output bash script [default: completely.bash] + SCRIPT_PATH + Path to the output bash script. When not provided, the name of the input + file will be used with a .bash extension + Can also be set by an environment variable Environment Variables: + COMPLETELY_CONFIG_PATH + Path to a completely configuration file [default: completely.yaml] + + COMPLETELY_SCRIPT_PATH + Path to the output bash script + COMPLETELY_DEBUG It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a diff --git a/spec/approvals/cli/generated-script b/spec/approvals/cli/generated-script index 9949d20..5b880e0 100644 --- a/spec/approvals/cli/generated-script +++ b/spec/approvals/cli/generated-script @@ -7,14 +7,27 @@ _mygit_completions() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in - 'status'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;; - 'commit'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;; - 'init'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;; - *) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;; + 'status'*) + COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) + ;; + + 'commit'*) + COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) + ;; + + 'init'*) + COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) + ;; + + *) + COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) + ;; + esac } && complete -F _mygit_completions mygit diff --git a/spec/approvals/cli/generated-script-alt b/spec/approvals/cli/generated-script-alt index 8f83859..1a4866b 100644 --- a/spec/approvals/cli/generated-script-alt +++ b/spec/approvals/cli/generated-script-alt @@ -7,14 +7,27 @@ _mycomps() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in - 'status'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;; - 'commit'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;; - 'init'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;; - *) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;; + 'status'*) + COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) + ;; + + 'commit'*) + COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) + ;; + + 'init'*) + COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) + ;; + + *) + COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) + ;; + esac } && complete -F _mycomps mygit diff --git a/spec/approvals/cli/generated-wrapped-script b/spec/approvals/cli/generated-wrapped-script index 87db078..ffd1c2a 100644 --- a/spec/approvals/cli/generated-wrapped-script +++ b/spec/approvals/cli/generated-wrapped-script @@ -8,14 +8,27 @@ give_comps() { echo $'_mygit_completions() {' echo $' local cur compline' echo $' _init_completion -s || return' + echo $'' echo $' cur=${COMP_WORDS[COMP_CWORD]}' echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"' echo $'' echo $' case "$compline" in' - echo $' \'status\'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;;' - echo $' \'commit\'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;;' - echo $' \'init\'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;;' - echo $' *) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;' + echo $' \'status\'*)' + echo $' COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur"))' + echo $' ;;' + echo $'' + echo $' \'commit\'*)' + echo $' COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur"))' + echo $' ;;' + echo $'' + echo $' \'init\'*)' + echo $' COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur"))' + echo $' ;;' + echo $'' + echo $' *)' + echo $' COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur"))' + echo $' ;;' + echo $'' echo $' esac' echo $'} &&' echo $'complete -F _mygit_completions mygit' diff --git a/spec/approvals/cli/init/custom-path-env b/spec/approvals/cli/init/custom-path-env new file mode 100644 index 0000000..a0bec5d --- /dev/null +++ b/spec/approvals/cli/init/custom-path-env @@ -0,0 +1 @@ +Saved spec/tmp/hello.yml diff --git a/spec/approvals/cli/init/help b/spec/approvals/cli/init/help index 06c0db8..5665f0a 100644 --- a/spec/approvals/cli/init/help +++ b/spec/approvals/cli/init/help @@ -11,3 +11,8 @@ Options: Parameters: CONFIG_PATH Path to the YAML configuration file [default: completely.yaml] + Can also be set by an environment variable + +Environment Variables: + COMPLETELY_CONFIG_PATH + Path to a completely configuration file [default: completely.yaml] diff --git a/spec/approvals/cli/preview/help b/spec/approvals/cli/preview/help index 0f226ea..b7f3024 100644 --- a/spec/approvals/cli/preview/help +++ b/spec/approvals/cli/preview/help @@ -14,8 +14,12 @@ Options: Parameters: CONFIG_PATH Path to the YAML configuration file [default: completely.yaml] + Can also be set by an environment variable Environment Variables: + COMPLETELY_CONFIG_PATH + Path to a completely configuration file [default: completely.yaml] + COMPLETELY_DEBUG It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a diff --git a/spec/approvals/completions/function b/spec/approvals/completions/function index 8880eff..9bad5f3 100644 --- a/spec/approvals/completions/function +++ b/spec/approvals/completions/function @@ -8,13 +8,23 @@ send_completions() { echo $'_completely_completions() {' echo $' local cur compline' echo $' _init_completion -s || return' + echo $'' echo $' cur=${COMP_WORDS[COMP_CWORD]}' echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"' echo $'' echo $' case "$compline" in' - echo $' \'generate\'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;;' - echo $' \'init\'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;;' - echo $' *) COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) ;;' + echo $' \'generate\'*)' + echo $' COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur"))' + echo $' ;;' + echo $'' + echo $' \'init\'*)' + echo $' COMPREPLY=($(compgen -W "--help" -- "$cur"))' + echo $' ;;' + echo $'' + echo $' *)' + echo $' COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur"))' + echo $' ;;' + echo $'' echo $' esac' echo $'} &&' echo $'complete -F _completely_completions completely' diff --git a/spec/approvals/completions/script b/spec/approvals/completions/script index 841c744..ec156c2 100644 --- a/spec/approvals/completions/script +++ b/spec/approvals/completions/script @@ -7,13 +7,23 @@ _completely_completions() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in - 'generate'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;; - 'init'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;; - *) COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) ;; + 'generate'*) + COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) + ;; + + 'init'*) + COMPREPLY=($(compgen -W "--help" -- "$cur")) + ;; + + *) + COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) + ;; + esac } && complete -F _completely_completions completely diff --git a/spec/approvals/completions/script-only-spaces b/spec/approvals/completions/script-only-spaces index ecdf919..a7b1dfb 100644 --- a/spec/approvals/completions/script-only-spaces +++ b/spec/approvals/completions/script-only-spaces @@ -7,12 +7,19 @@ _completely_completions() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" case "$compline" in - 'generate'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;; - 'init'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;; + 'generate'*) + COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) + ;; + + 'init'*) + COMPREPLY=($(compgen -W "--help" -- "$cur")) + ;; + esac } && complete -F _completely_completions completely diff --git a/spec/approvals/completions/script-with-debug b/spec/approvals/completions/script-with-debug index e5741a9..045ab32 100644 --- a/spec/approvals/completions/script-with-debug +++ b/spec/approvals/completions/script-with-debug @@ -7,6 +7,7 @@ _completely_completions() { local cur compline _init_completion -s || return + cur=${COMP_WORDS[COMP_CWORD]} compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}" diff --git a/spec/completely/commands/generate_spec.rb b/spec/completely/commands/generate_spec.rb index ef7ceab..dfa4bc3 100644 --- a/spec/completely/commands/generate_spec.rb +++ b/spec/completely/commands/generate_spec.rb @@ -25,7 +25,44 @@ end end - context "with CONFIG_PATH OUTPUT_PATH" do + context "with COMPLETELY_CONFIG_PATH env var" do + before do + reset_tmp_dir + system "cp lib/completely/templates/sample.yaml spec/tmp/hello.yml" + system "rm -f completely.yaml" + ENV['COMPLETELY_CONFIG_PATH'] = 'spec/tmp/hello.yml' + end + + after do + ENV['COMPLETELY_CONFIG_PATH'] = nil + system "rm -f hello.bash" + end + + it "generates the bash script to hello.bash" do + expect { subject.run %w[generate] }.to output_approval('cli/generate/custom-path-env') + expect(File.read "hello.bash").to match_approval('cli/generated-script') + end + end + + context "with COMPLETELY_SCRIPT_PATH env var" do + let(:outfile) { 'spec/tmp/tada.bash' } + + before do + reset_tmp_dir + ENV['COMPLETELY_SCRIPT_PATH'] = outfile + end + + after do + ENV['COMPLETELY_SCRIPT_PATH'] = nil + end + + it "generates the bash script to the requested path" do + expect { subject.run %w[generate] }.to output_approval('cli/generate/custom-path-env2') + expect(File.read outfile).to match_approval('cli/generated-script') + end + end + + context "with CONFIG_PATH SCRIPT_PATH" do before { reset_tmp_dir } it "generates the bash script to the specified path" do diff --git a/spec/completely/commands/init_spec.rb b/spec/completely/commands/init_spec.rb index b3b24d6..983082c 100644 --- a/spec/completely/commands/init_spec.rb +++ b/spec/completely/commands/init_spec.rb @@ -29,6 +29,21 @@ end end + context "with COMPLETELY_CONFIG_PATH env var" do + before do + reset_tmp_dir + ENV['COMPLETELY_CONFIG_PATH'] = 'spec/tmp/hello.yml' + end + + after { ENV['COMPLETELY_CONFIG_PATH'] = nil } + + it "creates a new sample file with the requested name" do + expect { subject.run %w[init] } + .to output_approval('cli/init/custom-path-env') + expect(File.read 'spec/tmp/hello.yml').to eq sample + end + end + context "when the config file already exists" do before { system "cp lib/completely/templates/sample.yaml completely.yaml" } after { system "rm -f completely.yaml" } diff --git a/spec/completely/commands/preview_spec.rb b/spec/completely/commands/preview_spec.rb index 771cd21..60bc8f5 100644 --- a/spec/completely/commands/preview_spec.rb +++ b/spec/completely/commands/preview_spec.rb @@ -25,6 +25,22 @@ end end + context "with COMPLETELY_CONFIG_PATH env var" do + before do + reset_tmp_dir + system "cp lib/completely/templates/sample.yaml spec/tmp/hello.yml" + system "rm -f completely.yaml" + ENV['COMPLETELY_CONFIG_PATH'] = 'spec/tmp/hello.yml' + end + + after { ENV['COMPLETELY_CONFIG_PATH'] = nil } + + it "outputs the generated script to STDOUT" do + expect { subject.run %w[preview] } + .to output_approval('cli/generated-script') + end + end + context "with an invalid configuration" do it "outputs a warning to STDERR" do expect { subject.run %w[preview spec/fixtures/broken.yaml] } diff --git a/spec/completely/completions_spec.rb b/spec/completely/completions_spec.rb index 0f9673f..0044cb6 100644 --- a/spec/completely/completions_spec.rb +++ b/spec/completely/completions_spec.rb @@ -41,7 +41,7 @@ end end - context "when COMPLETELY_DEBUG is set", :focus do + context "when COMPLETELY_DEBUG is set" do before { ENV['COMPLETELY_DEBUG'] = '1' } after { ENV['COMPLETELY_DEBUG'] = nil } @@ -59,7 +59,7 @@ end end - describe '#tester', :focus do + describe '#tester' do it "returns a Tester object" do expect(subject.tester).to be_a Tester end