@@ -9,7 +9,7 @@ class Add < Base
99 usage "bashly add colors [--force]"
1010 usage "bashly add yaml [--force]"
1111 usage "bashly add validations [--force]"
12- usage "bashly add comp FORMAT [OUTPUT]"
12+ usage "bashly add comp FORMAT [OUTPUT --force ]"
1313 usage "bashly add (-h|--help)"
1414
1515 option "-f --force" , "Overwrite existing files"
@@ -32,129 +32,81 @@ class Add < Base
3232 environment "BASHLY_SOURCE_DIR" , "The path containing the bashly configuration and source files [default: src]"
3333
3434 def strings_command
35- safe_copy asset ( "templates/strings.yml" ) , " #{ Settings . source_dir } /bashly-strings.yml"
35+ add_lib Library :: Strings . new
3636 end
3737
3838 def lib_command
39- safe_copy_file "sample_function.sh"
39+ add_lib Library :: Sample . new
4040 end
4141
4242 def config_command
43- safe_copy_file "config.sh"
43+ add_lib Library :: Config . new
4444 end
4545
4646 def colors_command
47- safe_copy_file "colors.sh"
47+ add_lib Library :: Colors . new
4848 end
4949
5050 def yaml_command
51- safe_copy_file "yaml.sh"
51+ add_lib Library :: YAML . new
5252 end
5353
5454 def validations_command
55- safe_copy_dir "validations"
55+ add_lib Library :: Validations . new
5656 end
5757
5858 def comp_command
5959 format = args [ 'FORMAT' ]
6060 output = args [ 'OUTPUT' ]
61-
61+
6262 case format
63+ when "script"
64+ path = output || "#{ Settings . target_dir } /completions.bash"
65+ add_lib Library ::CompletionsScript . new ( path )
66+
6367 when "function"
64- save_comp_function output
68+ function = output || "send_completions"
69+ path = "#{ Settings . source_dir } /lib/#{ function } .sh"
70+ add_lib Library ::CompletionsFunction . new ( path , function : function )
71+
6572 when "yaml"
66- save_comp_yaml output
67- when "script"
68- save_comp_script output
73+ path = output || " #{ Settings . target_dir } /completions.yml"
74+ add_lib Library :: CompletionsYAML . new ( path )
75+
6976 else
7077 raise Error , "Unrecognized format: #{ format } "
78+
7179 end
7280
7381 end
7482
7583 private
7684
77- def safe_copy_dir ( dir )
78- Dir [ asset ( "templates/lib/#{ dir } /*.sh" ) ] . sort . each do |file |
79- safe_copy_file "#{ dir } /#{ File . basename file } "
85+ def add_lib ( handler )
86+ files_created = 0
87+ handler . files . each do |file |
88+ created = safe_write file [ :path ] , file [ :content ]
89+ files_created += 1 if created
8090 end
91+ message = handler . post_install_message
92+ say "\n #{ message } " if message and files_created > 0
8193 end
8294
83- def safe_copy_file ( file )
84- safe_copy asset ( "templates/lib/#{ file } " ) , "#{ Settings . source_dir } /lib/#{ file } "
85- end
86-
87- def safe_copy ( source , target )
95+ def safe_write ( path , content )
8896 if !Dir . exist? Settings . source_dir
8997 raise InitError , "Directory !txtgrn!#{ Settings . source_dir } !txtrst! does not exist\n Run !txtpur!bashly init!txtrst! first"
9098 end
9199
92- if File . exist? target and !args [ '--force' ]
93- say "skipped !txtgrn!#{ target } !txtrst! (exists)"
100+ if File . exist? path and !args [ '--force' ]
101+ say "skipped !txtgrn!#{ path } !txtrst! (exists)"
102+ false
103+
94104 else
95- deep_copy source , target
96- say "created !txtgrn!#{ target } "
97- end
98- end
99-
100- def deep_copy ( source , target )
101- target_dir = File . dirname target
102- FileUtils . mkdir_p target_dir unless Dir . exist? target_dir
103- FileUtils . cp source , target
104- end
105-
106- def config
107- @config ||= Config . new "#{ Settings . source_dir } /bashly.yml"
108- end
109-
110- def command
111- @command ||= Script ::Command . new config
112- end
113-
114- def completions
115- @completions ||= command . completion_data
116- end
117-
118- def completions_script
119- @completions_script ||= command . completion_script
120- end
121-
122- def completions_function
123- @completions_function ||= command . completion_function
124- end
125-
126- def save_comp_yaml ( filename = nil )
127- filename ||= "#{ Settings . target_dir } /completions.yml"
128- File . write filename , completions . to_yaml
129- say "created !txtgrn!#{ filename } "
130- say ""
131- say "This file can be converted to a completions script using the !txtgrn!completely!txtrst! gem."
132- end
133-
134- def save_comp_script ( filename = nil )
135- filename ||= "#{ Settings . target_dir } /completions.bash"
136- File . write filename , completions_script
137- say "created !txtgrn!#{ filename } "
138- say ""
139- say "In order to enable completions, run:"
140- say ""
141- say " !txtpur!$ source #{ filename } "
142- end
143-
144- def save_comp_function ( name = nil )
145- name ||= "send_completions"
146- target_dir = "#{ Settings . source_dir } /lib"
147- filename = "#{ target_dir } /#{ name } .sh"
105+ File . deep_write path , content
106+ say "created !txtgrn!#{ path } "
107+ true
148108
149- FileUtils . mkdir_p target_dir unless Dir . exist? target_dir
150- File . write filename , completions_function
151-
152- say "created !txtgrn!#{ filename } "
153- say ""
154- say "In order to use it in your script, create a command or a flag (for example: !txtgrn!#{ command . name } completions!txtrst! or !txtgrn!#{ command . name } --completions!txtrst!) that calls the !txtgrn!#{ name } !txtrst! function."
155- say "Your users can then run something like this to enable completions:"
156- say ""
157- say " !txtpur!$ eval \" $(#{ command . name } completions)\" "
109+ end
158110 end
159111
160112 end
0 commit comments