Module: ConfC
- Defined in:
- lib/confc/cli.rb,
lib/confc/copy.rb,
lib/confc/files.rb,
lib/confc/gethome.rb,
lib/confc/version.rb,
lib/confc/conf_clone.rb,
lib/confc/load_config.rb,
lib/confc/to_filenames.rb,
lib/confc/ask_overwrite.rb,
lib/confc/display_verbose.rb,
lib/confc/ask_choose_files.rb,
lib/confc/interrupt_handler.rb,
lib/confc/get_existent_files.rb
Overview
ConfC
Defined Under Namespace
Classes: CLI
Constant Summary collapse
- VERSION =
'0.2.1'.freeze
- INTERRUPT_HANDLER =
proc { puts puts Rainbow("ConfC stopped. #{WTF}").yellow exit(1) }
Class Method Summary collapse
-
.ask_choose_files(existent_files) ⇒ Object
Ask to choose existent files to clone.
-
.ask_overwrite(src, dest) ⇒ Object
Ask to overwrite ‘dest` file with `src` file.
-
.conf_clone(files, path, options = { overwrite: false, verbose: false }) ⇒ Object
Clone given files from given path to current working directory.
-
.copy(src, options = { overwrite: false, verbose: false }) ⇒ Object
Copy given file to current working directory.
-
.display_verbose(src, dest) ⇒ Object
Display verbose information.
-
.files ⇒ Object
Get list of file to be copied.
-
.get_existent_files(path, all_files) ⇒ Object
Get the existent files at given path from all file list.
-
.gethome ⇒ Object
Get user’s home directory.
-
.load_config ⇒ Object
Load ConfC configuration.
-
.should_stop_copying?(src, dest_path, options = { overwrite: false }) ⇒ Boolean
Should stop copying?.
-
.to_filenames(raw_filenames) ⇒ Object
Convert all raw file names to base file names.
Class Method Details
.ask_choose_files(existent_files) ⇒ Object
Ask to choose existent files to clone.
13 14 15 16 17 18 19 |
# File 'lib/confc/ask_choose_files.rb', line 13 def self.ask_choose_files(existent_files) prompt = TTY::Prompt.new(interrupt: ConfC::INTERRUPT_HANDLER) prompt.multi_select("Which files that you want to #{APP_NAME}?") do || .default(*(1..existent_files.length)) .choices existent_files end end |
.ask_overwrite(src, dest) ⇒ Object
Ask to overwrite ‘dest` file with `src` file.
11 12 13 14 15 16 17 18 |
# File 'lib/confc/ask_overwrite.rb', line 11 def self.ask_overwrite(src, dest) colored_src = Rainbow(src).magenta colored_dest = Rainbow(dest).green prompt = TTY::Prompt.new(interrupt: ConfC::INTERRUPT_HANDLER) prompt.yes?( %(Do you want to overwrite "#{colored_src}" with "#{colored_dest}"?) ) end |
.conf_clone(files, path, options = { overwrite: false, verbose: false }) ⇒ Object
Clone given files from given path to current working directory.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/confc/conf_clone.rb', line 8 def self.conf_clone( files, path, = { overwrite: false, verbose: false } ) results = files.map do |file| src = File.(file, path) ConfC.copy(src, ) end results.any? { |result| result } # Return true if any process succeed end |
.copy(src, options = { overwrite: false, verbose: false }) ⇒ Object
Copy given file to current working directory.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/confc/copy.rb', line 25 def self.copy(src, = { overwrite: false, verbose: false }) return false unless File.exist?(src) # Stop if no src file # Stop if dest file exists & prevent overwriting src_filename = File.basename(src) dest_path = File.(src_filename, Dir.pwd) return false if ConfC.(src, dest_path, ) FileUtils.copy(src, Dir.pwd) # Display verbose dest = File.(src_filename, Dir.pwd) ConfC.display_verbose(src, dest) if [:verbose] true end |
.display_verbose(src, dest) ⇒ Object
Display verbose information.
8 9 10 11 12 13 14 15 16 |
# File 'lib/confc/display_verbose.rb', line 8 def self.display_verbose(src, dest) verbose = Rainbow('Copied "').green verbose += Rainbow(src).cyan verbose += Rainbow('" to "').green verbose += Rainbow(dest).cyan verbose += Rainbow('".').green puts verbose end |
.files ⇒ Object
Get list of file to be copied.
8 9 10 11 12 13 14 15 |
# File 'lib/confc/files.rb', line 8 def self.files files_path = File.('../../files.yaml', File.dirname(__FILE__)) if File.exist?(files_path) YAML.load_file(files_path) else [] end end |
.get_existent_files(path, all_files) ⇒ Object
Get the existent files at given path from all file list.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/confc/get_existent_files.rb', line 6 def self.get_existent_files(path, all_files) if all_files.is_a?(Array) all_files.select do |file| file_path = File.(file, path) File.exist?(file_path) end else [] end end |
.gethome ⇒ Object
Get user’s home directory.
6 7 8 |
# File 'lib/confc/gethome.rb', line 6 def self.gethome File. '~' end |
.load_config ⇒ Object
Load ConfC configuration.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/confc/load_config.rb', line 20 def self.load_config loader = Mysticonfig::Loader.new('confc', DEFAULT_CONFIG) config = loader.load if config.empty? DEFAULT_CONFIG.symbolize_keys else # Truncate un-related config config = config.select { |k, _| DEFAULT_CONFIG.keys.include? k } config.symbolize_keys end end |
.should_stop_copying?(src, dest_path, options = { overwrite: false }) ⇒ Boolean
Should stop copying?
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/confc/copy.rb', line 12 def self.( src, dest_path, = { overwrite: false } ) # When dest file exist & no `overwrite`, ask to overwrite. File.exist?(dest_path) && ![:overwrite] && # When answer "don't overwrite", stop!. !ConfC.ask_overwrite(src, dest_path) end |
.to_filenames(raw_filenames) ⇒ Object
Convert all raw file names to base file names.
6 7 8 9 10 11 12 13 14 |
# File 'lib/confc/to_filenames.rb', line 6 def self.to_filenames(raw_filenames) if raw_filenames.is_a?(Array) && !raw_filenames.empty? raw_filenames.map do |raw_filename| File.basename raw_filename end else [] end end |