Skip to content

Commit 2efca29

Browse files
committed
implement 'bashly add comp'
1 parent be45d2d commit 2efca29

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

bashly.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
1818
s.required_ruby_version = ">= 2.7.0"
1919

2020
s.add_runtime_dependency 'colsole', '~> 0.6'
21-
s.add_runtime_dependency 'completely', '~> 0.1'
21+
s.add_runtime_dependency 'completely', '~> 0.1', '>= 0.1.2'
2222
s.add_runtime_dependency 'mister_bin', '~> 0.7'
2323
s.add_runtime_dependency 'requires', '~> 0.1'
2424
end

lib/bashly/commands/add.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ class Add < Base
88
usage "bashly add config [--force]"
99
usage "bashly add colors [--force]"
1010
usage "bashly add yaml [--force]"
11+
usage "bashly add comp FORMAT [OUTPUT]"
1112
usage "bashly add (-h|--help)"
1213

1314
option "-f --force", "Overwrite existing files"
1415

16+
param "FORMAT", "Output format, can be one of:\n function : generate a function file to be included in your script.\n script : generate a standalone bash completions script\n yaml : generate a yaml compatible with 'completely'"
17+
param "OUTPUT", "For the 'comp function' command: Name of the generated function.\nFor the 'comp script' or 'comp yaml' commands: path to output file.\nIn all cases, this is optional and will have sensible defaults."
18+
1519
command "strings", "Copy an additional configuration file to your project, allowing you to customize all the tips and error strings."
1620
command "lib", "Create the additional lib directory for additional user scripts. All *.sh scripts in this folder will be included in the final bash script."
1721
command "config", "Add standard functions for handling INI files to the lib directory."
1822
command "colors", "Add standard functions for printing colorful and formatted text to the lib directory."
1923
command "yaml", "Add standard functions for reading YAML files to the lib directory."
24+
command "comp", "Generate a bash completions script or function."
2025

2126
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
2227

@@ -40,6 +45,23 @@ def yaml_command
4045
safe_copy_lib "yaml.sh"
4146
end
4247

48+
def comp_command
49+
format = args['FORMAT']
50+
output = args['OUTPUT']
51+
52+
case format
53+
when "function"
54+
save_comp_function output
55+
when "yaml"
56+
save_comp_yaml output
57+
when "script"
58+
save_comp_script output
59+
else
60+
raise Error, "Unrecognized format: #{format}"
61+
end
62+
63+
end
64+
4365
private
4466

4567
def safe_copy_lib(libfile)
@@ -64,6 +86,50 @@ def deep_copy(source, target)
6486
FileUtils.mkdir_p target_dir unless Dir.exist? target_dir
6587
FileUtils.cp source, target
6688
end
89+
90+
def config
91+
@config ||= Config.new "#{Settings.source_dir}/bashly.yml"
92+
end
93+
94+
def command
95+
@command ||= Models::Command.new config
96+
end
97+
98+
def completions
99+
@completions ||= command.completion_data
100+
end
101+
102+
def completions_script
103+
@completions_script ||= command.completion_script
104+
end
105+
106+
def completions_function
107+
@completions_function ||= command.completion_function
108+
end
109+
110+
def save_comp_yaml(filename = nil)
111+
filename ||= "completions.yaml"
112+
File.write filename, completions.to_yaml
113+
say "created !txtgrn!#{filename}"
114+
end
115+
116+
def save_comp_script(filename = nil)
117+
filename ||= "completions.bash"
118+
File.write filename, completions_script
119+
say "created !txtgrn!#{filename}"
120+
end
121+
122+
def save_comp_function(name = nil)
123+
name ||= "send_completions"
124+
target_dir = "#{Settings.source_dir}/lib"
125+
filename = "#{target_dir}/#{name}.sh"
126+
127+
FileUtils.mkdir_p target_dir unless Dir.exist? target_dir
128+
File.write filename, completions_function
129+
130+
say "created !txtgrn!#{filename}"
131+
end
132+
67133
end
68134
end
69135
end

lib/bashly/concerns/completions.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'completely'
2+
13
module Bashly
24
# This is a `Command` concern responsible for providing bash completion data
35
module Completions
@@ -11,8 +13,20 @@ def completion_data(with_version: true)
1113
result
1214
end
1315

16+
def completion_script
17+
completion_generator.script
18+
end
19+
20+
def completion_function(name = nil)
21+
completion_generator.wrapper_function(name)
22+
end
23+
1424
private
1525

26+
def completion_generator
27+
Completely::Completions.new(completion_data)
28+
end
29+
1630
def completion_flag_names
1731
flags.map(&:name) + flags.map(&:short)
1832
end

spec/approvals/cli/add/help

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Usage:
66
bashly add config [--force]
77
bashly add colors [--force]
88
bashly add yaml [--force]
9+
bashly add comp FORMAT [OUTPUT]
910
bashly add (-h|--help)
1011

1112
Commands:
@@ -27,13 +28,28 @@ Commands:
2728
yaml
2829
Add standard functions for reading YAML files to the lib directory.
2930

31+
comp
32+
Generate a bash completions script or function.
33+
3034
Options:
3135
-f --force
3236
Overwrite existing files
3337

3438
-h --help
3539
Show this help
3640

41+
Parameters:
42+
FORMAT
43+
Output format, can be one of:
44+
function : generate a function file to be included in your script.
45+
script : generate a standalone bash completions script
46+
yaml : generate a yaml compatible with 'completely'
47+
48+
OUTPUT
49+
For the 'comp function' command: Name of the generated function.
50+
For the 'comp script' or 'comp yaml' commands: path to output file.
51+
In all cases, this is optional and will have sensible defaults.
52+
3753
Environment Variables:
3854
BASHLY_SOURCE_DIR
3955
The path containing the bashly configuration and source files [default: src]

spec/approvals/cli/generate/usage

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ Usage:
44
bashly add config [--force]
55
bashly add colors [--force]
66
bashly add yaml [--force]
7+
bashly add comp FORMAT [OUTPUT]
78
bashly add (-h|--help)

0 commit comments

Comments
 (0)