bashly 0.6.9 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/lib/bashly/commands/add.rb +36 -84
  3. data/lib/bashly/commands/generate.rb +47 -7
  4. data/lib/bashly/commands/init.rb +1 -1
  5. data/lib/bashly/commands/preview.rb +2 -2
  6. data/lib/bashly/concerns/asset_helper.rb +4 -0
  7. data/lib/bashly/extensions/file.rb +13 -0
  8. data/lib/bashly/extensions/string.rb +1 -1
  9. data/lib/bashly/library/base.rb +57 -0
  10. data/lib/bashly/library/colors.rb +9 -0
  11. data/lib/bashly/library/completions.rb +28 -0
  12. data/lib/bashly/library/completions_function.rb +26 -0
  13. data/lib/bashly/library/completions_script.rb +17 -0
  14. data/lib/bashly/library/completions_yaml.rb +15 -0
  15. data/lib/bashly/library/config.rb +9 -0
  16. data/lib/bashly/library/sample.rb +9 -0
  17. data/lib/bashly/library/strings.rb +12 -0
  18. data/lib/bashly/library/validations.rb +9 -0
  19. data/lib/bashly/library/yaml.rb +9 -0
  20. data/lib/bashly/{models → script}/argument.rb +1 -1
  21. data/lib/bashly/{models → script}/base.rb +1 -1
  22. data/lib/bashly/{models → script}/command.rb +1 -1
  23. data/lib/bashly/{models → script}/environment_variable.rb +1 -1
  24. data/lib/bashly/{models → script}/flag.rb +1 -1
  25. data/lib/bashly/{models/script.rb → script/wrapper.rb} +2 -2
  26. data/lib/bashly/templates/lib/colors.sh +12 -15
  27. data/lib/bashly/templates/lib/config.sh +34 -35
  28. data/lib/bashly/templates/lib/sample_function.sh +10 -10
  29. data/lib/bashly/templates/lib/validations/validate_dir_exists.sh +1 -0
  30. data/lib/bashly/templates/lib/validations/validate_file_exists.sh +1 -0
  31. data/lib/bashly/templates/lib/validations/validate_integer.sh +1 -0
  32. data/lib/bashly/templates/lib/validations/validate_not_empty.sh +1 -0
  33. data/lib/bashly/templates/lib/yaml.sh +12 -15
  34. data/lib/bashly/version.rb +1 -1
  35. data/lib/bashly/views/command/parse_requirements.erb +1 -1
  36. data/lib/bashly/views/command/required_args_filter.erb +1 -6
  37. data/lib/bashly/views/command/required_flags_filter.erb +1 -4
  38. data/lib/bashly/views/{script → wrapper}/bash3_bouncer.erb +0 -0
  39. data/lib/bashly/views/{script → wrapper}/header.erb +0 -0
  40. data/lib/bashly/views/{script → wrapper}/wrapper.erb +0 -0
  41. data/lib/bashly.rb +2 -1
  42. metadata +23 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c76c3000789b54fa2d9be8f7a61d93556e6a8665f4ea7769588797a25fa78f49
4
- data.tar.gz: a5a0eae36b00cf879082836211f2aa21b38b280993e2dccfc907f3e6fcb99104
3
+ metadata.gz: 13bbac52a39c2f6afb768d043911861a31f8e6abc8ff1472340a13c2834d5944
4
+ data.tar.gz: 54c4fea2c664dcd0c6aace3360004d715d87b5d4ed98ffd7d3ccdc6c0a120406
5
5
  SHA512:
6
- metadata.gz: ae77cd9b8fa000d9e12f2948fb5c41bd02e34bd6bdcdd91f3a3a627778f66c9472e956e92fb65b502fa78fe2e77ba6c4dfb12c1f35abed36f1012ce502b7cab9
7
- data.tar.gz: 5e14659441abf6b11ca53a5d0d656e5097519acc7a2e842f3cdfb6374fd52a0403c6ddeacf4f193da1ab18687de909b074e4d581ce4700c9fc28e2b589ef961d
6
+ metadata.gz: f1c273771d9048c074fb9551ba4521e9cb38c98a819f53647186822155898f767b7dd95093642e01464788378833d4dd9e408efecf304a9feb1ba09ebf2edef3
7
+ data.tar.gz: efbdad6c22cfe1b7fbb46533db42f4c14b828259efeb5ede1099e20980ad505773af36897e27f8c6c7b25538a523427e63c1a5dac7ff958d753ae3810caf5eb9
@@ -9,7 +9,7 @@ module Bashly
9
9
  usage "bashly add colors [--force]"
10
10
  usage "bashly add yaml [--force]"
11
11
  usage "bashly add validations [--force]"
12
- usage "bashly add comp FORMAT [OUTPUT]"
12
+ usage "bashly add comp FORMAT [OUTPUT --force]"
13
13
  usage "bashly add (-h|--help)"
14
14
 
15
15
  option "-f --force", "Overwrite existing files"
@@ -32,129 +32,81 @@ module Bashly
32
32
  environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
33
33
 
34
34
  def strings_command
35
- safe_copy asset("templates/strings.yml"), "#{Settings.source_dir}/bashly-strings.yml"
35
+ add_lib Library::Strings.new
36
36
  end
37
37
 
38
38
  def lib_command
39
- safe_copy_file "sample_function.sh"
39
+ add_lib Library::Sample.new
40
40
  end
41
41
 
42
42
  def config_command
43
- safe_copy_file "config.sh"
43
+ add_lib Library::Config.new
44
44
  end
45
45
 
46
46
  def colors_command
47
- safe_copy_file "colors.sh"
47
+ add_lib Library::Colors.new
48
48
  end
49
49
 
50
50
  def yaml_command
51
- safe_copy_file "yaml.sh"
51
+ add_lib Library::YAML.new
52
52
  end
53
53
 
54
54
  def validations_command
55
- safe_copy_dir "validations"
55
+ add_lib Library::Validations.new
56
56
  end
57
57
 
58
58
  def comp_command
59
59
  format = args['FORMAT']
60
60
  output = args['OUTPUT']
61
-
61
+
62
62
  case format
63
+ when "script"
64
+ path = output || "#{Settings.target_dir}/completions.bash"
65
+ add_lib Library::CompletionsScript.new(path)
66
+
63
67
  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
+
65
72
  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
+
69
76
  else
70
77
  raise Error, "Unrecognized format: #{format}"
78
+
71
79
  end
72
80
 
73
81
  end
74
82
 
75
83
  private
76
84
 
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
80
90
  end
91
+ message = handler.post_install_message
92
+ say "\n#{message}" if message and files_created > 0
81
93
  end
82
94
 
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)
88
96
  if !Dir.exist? Settings.source_dir
89
97
  raise InitError, "Directory !txtgrn!#{Settings.source_dir}!txtrst! does not exist\nRun !txtpur!bashly init!txtrst! first"
90
98
  end
91
99
 
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 "!txtblu!skipped!txtrst! #{path} (exists)"
102
+ false
103
+
94
104
  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 ||= Models::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 "!txtgrn!created!txtrst! #{path}"
107
+ true
148
108
 
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
158
110
  end
159
111
 
160
112
  end
@@ -3,12 +3,13 @@ module Bashly
3
3
  class Generate < Base
4
4
  help "Generate the bash script and required files"
5
5
 
6
- usage "bashly generate [--force --quiet --wrap FUNCTION]"
6
+ usage "bashly generate [--force --quiet --upgrade --wrap FUNCTION]"
7
7
  usage "bashly generate (-h|--help)"
8
8
 
9
9
  option "-f --force", "Overwrite existing files"
10
- option "-w --wrap FUNCTION", "Wrap the entire script in a function so it can also be sourced"
11
10
  option "-q --quiet", "Disable on-screen progress report"
11
+ option "-u --upgrade", "Upgrade all added library functions"
12
+ option "-w --wrap FUNCTION", "Wrap the entire script in a function so it can also be sourced"
12
13
 
13
14
  environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
14
15
  environment "BASHLY_TARGET_DIR", "The path to use for creating the bash script [default: .]"
@@ -18,6 +19,7 @@ module Bashly
18
19
 
19
20
  def run
20
21
  create_user_files
22
+ upgrade_libs if args['--upgrade']
21
23
  create_master_script
22
24
  quiet_say "run !txtpur!#{master_script_path} --help!txtrst! to test your bash script"
23
25
  end
@@ -28,6 +30,44 @@ module Bashly
28
30
  say message unless args['--quiet']
29
31
  end
30
32
 
33
+ def upgrade_libs
34
+ generated_files.each do |file|
35
+ content = File.read file
36
+
37
+ if content =~ /\[@bashly-upgrade (.+)\]/
38
+ lib = $1
39
+
40
+ case lib
41
+ when "colors"
42
+ upgrade file, Library::Colors.new
43
+ when "config"
44
+ upgrade file, Library::Config.new
45
+ when "yaml"
46
+ upgrade file, Library::YAML.new
47
+ when "validations"
48
+ upgrade file, Library::Validations.new
49
+ when /completions (.+)/
50
+ upgrade file, Library::CompletionsFunction.new(file, function: $1)
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ def generated_files
57
+ Dir["#{Settings.source_dir}/**/*.*"].sort
58
+ end
59
+
60
+ def upgrade(existing_file, handler)
61
+ file = handler.files.select { |f| f[:path] == existing_file }.first
62
+
63
+ if file
64
+ File.deep_write file[:path], file[:content]
65
+ quiet_say "!txtcyn!updated!txtrst! #{file[:path]}"
66
+ else
67
+ quiet_say "!txtred!warning!txtrst! not upgrading !txtcyn!#{existing_file}!txtrst!, path mismatch"
68
+ end
69
+ end
70
+
31
71
  def create_user_files
32
72
  quiet_say "creating user files in !txtgrn!#{Settings.source_dir}"
33
73
 
@@ -55,21 +95,21 @@ module Bashly
55
95
 
56
96
  def create_file(file, content)
57
97
  if File.exist? file and !args['--force']
58
- quiet_say "skipped !txtgrn!#{file}!txtrst! (exists)"
98
+ quiet_say "!txtblu!skipped!txtrst! #{file} (exists)"
59
99
  else
60
100
  File.write file, content
61
- quiet_say "created !txtgrn!#{file}"
101
+ quiet_say "!txtgrn!created!txtrst! #{file}"
62
102
  end
63
103
  end
64
104
 
65
105
  def create_master_script
66
106
  File.write master_script_path, script.code
67
107
  FileUtils.chmod "+x", master_script_path
68
- quiet_say "created !txtgrn!#{master_script_path}"
108
+ quiet_say "!txtgrn!created!txtrst! #{master_script_path}"
69
109
  end
70
110
 
71
111
  def script
72
- @script ||= Models::Script.new(command, args['--wrap'])
112
+ @script ||= Script::Wrapper.new(command, args['--wrap'])
73
113
  end
74
114
 
75
115
  def master_script_path
@@ -81,7 +121,7 @@ module Bashly
81
121
  end
82
122
 
83
123
  def command
84
- @command ||= Models::Command.new config
124
+ @command ||= Script::Command.new config
85
125
  end
86
126
 
87
127
  end
@@ -17,7 +17,7 @@ module Bashly
17
17
  end
18
18
  Dir.mkdir target_dir unless Dir.exist? target_dir
19
19
  File.write "#{target_dir}/bashly.yml", yaml_content
20
- say "created !txtgrn!#{target_dir}/bashly.yml"
20
+ say "!txtgrn!created!txtrst! #{target_dir}/bashly.yml"
21
21
  say "run !txtpur!bashly generate!txtrst! to create the bash script"
22
22
  end
23
23
 
@@ -10,8 +10,8 @@ module Bashly
10
10
 
11
11
  def run
12
12
  config = Config.new "#{Settings.source_dir}/bashly.yml"
13
- command = Models::Command.new(config)
14
- script = Models::Script.new command
13
+ command = Script::Command.new(config)
14
+ script = Script::Wrapper.new command
15
15
  puts script.code
16
16
  end
17
17
  end
@@ -3,5 +3,9 @@ module Bashly
3
3
  def asset(path)
4
4
  File.expand_path "../#{path}", __dir__
5
5
  end
6
+
7
+ def asset_content(path)
8
+ File.read asset(path)
9
+ end
6
10
  end
7
11
  end
@@ -0,0 +1,13 @@
1
+ require "fileutils"
2
+
3
+ class File
4
+ def self.deep_write(file, content)
5
+ dir = File.dirname file
6
+ FileUtils.mkdir_p dir unless Dir.exist? dir
7
+ File.write file, content
8
+ end
9
+
10
+ def self.append(path, content)
11
+ File.open(path, "a") { |f| f << content }
12
+ end
13
+ end
@@ -24,7 +24,7 @@ class String
24
24
  end
25
25
 
26
26
  def lint
27
- gsub(/\s+\n/m, "\n\n")
27
+ gsub(/\s+\n/m, "\n\n").lines.reject { |l| l =~ /^\s*##/ }.join ""
28
28
  end
29
29
 
30
30
  end
@@ -0,0 +1,57 @@
1
+ module Bashly
2
+ module Library
3
+ class Base
4
+ include AssetHelper
5
+
6
+ attr_reader :target_path, :options
7
+
8
+ def initialize(target_path = nil, options = nil)
9
+ @target_path = target_path || Settings.source_dir
10
+ @options = options || {}
11
+ end
12
+
13
+ def files
14
+ case content
15
+ when String then content_from_string content
16
+ when Hash then [content]
17
+ else content
18
+ end
19
+ end
20
+
21
+ def post_install_message
22
+ nil
23
+ end
24
+
25
+ def content
26
+ raise NotImplementedError, "Please implement either #content"
27
+ end
28
+
29
+ private
30
+
31
+ def content_from_string(string)
32
+ if File.directory? asset("templates/lib/#{string}")
33
+ content_for_dir string
34
+ else
35
+ [content_for_file(string)]
36
+ end
37
+ end
38
+
39
+ def content_for_file(file)
40
+ {
41
+ path: "#{target_path}/lib/#{file}",
42
+ content: asset_content("templates/lib/#{file}")
43
+ }
44
+ end
45
+
46
+ def content_for_dir(dir)
47
+ Dir[asset("templates/lib/#{dir}/*.sh")].sort.map do |file|
48
+ {
49
+ path: "#{target_path}/lib/#{dir}/#{File.basename file}",
50
+ content: File.read(file)
51
+ }
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,9 @@
1
+ module Bashly
2
+ module Library
3
+ class Colors < Base
4
+ def content
5
+ "colors.sh"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ module Bashly
2
+ module Library
3
+ class Completions < Base
4
+ def content
5
+ { path: target_path, content: file_content }
6
+ end
7
+
8
+ def file_content
9
+ raise NotImplementedError, "Please implement #file_content"
10
+ end
11
+
12
+ protected
13
+
14
+ def completions
15
+ @completions ||= command.completion_data
16
+ end
17
+
18
+ def config
19
+ @config ||= Bashly::Config.new "#{Settings.source_dir}/bashly.yml"
20
+ end
21
+
22
+ def command
23
+ @command ||= Script::Command.new config
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ module Bashly
2
+ module Library
3
+ class CompletionsFunction < Completions
4
+ def file_content
5
+ [
6
+ "# [@bashly-upgrade completions #{function_name}]",
7
+ command.completion_function(function_name)
8
+ ].join "\n"
9
+ end
10
+
11
+ def post_install_message
12
+ <<~EOF
13
+ In order to enable completions 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!#{function_name}!txtrst! function.
14
+
15
+ Your users can then run something like this to enable completions:
16
+
17
+ !txtpur!$ eval \"$(#{command.name} completions)\"
18
+ EOF
19
+ end
20
+
21
+ def function_name
22
+ options[:function]
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ module Bashly
2
+ module Library
3
+ class CompletionsScript < Completions
4
+ def file_content
5
+ command.completion_script
6
+ end
7
+
8
+ def post_install_message
9
+ <<~EOF
10
+ In order to enable completions, run:
11
+
12
+ !txtpur!$ source #{target_path}
13
+ EOF
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module Bashly
2
+ module Library
3
+ class CompletionsYAML < Completions
4
+ def file_content
5
+ completions.to_yaml
6
+ end
7
+
8
+ def post_install_message
9
+ <<~EOF
10
+ This file can be converted to a completions script using the !txtgrn!completely!txtrst! gem.
11
+ EOF
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module Bashly
2
+ module Library
3
+ class Config < Base
4
+ def content
5
+ "config.sh"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Bashly
2
+ module Library
3
+ class Sample < Base
4
+ def content
5
+ "sample_function.sh"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module Bashly
2
+ module Library
3
+ class Strings < Base
4
+ def content
5
+ {
6
+ path: "#{target_path}/bashly-strings.yml",
7
+ content: asset_content("templates/strings.yml")
8
+ }
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module Bashly
2
+ module Library
3
+ class Validations < Base
4
+ def content
5
+ "validations"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Bashly
2
+ module Library
3
+ class YAML < Base
4
+ def content
5
+ "yaml.sh"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Argument < Base
4
4
  def usage_string
5
5
  required ? name.upcase : "[#{name.upcase}]"
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Base
4
4
  include Renderable
5
5
 
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Command < Base
4
4
  include Completions
5
5
 
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class EnvironmentVariable < Base
4
4
  def usage_string(extended: false)
5
5
  result = [name.upcase]
@@ -1,5 +1,5 @@
1
1
  module Bashly
2
- module Models
2
+ module Script
3
3
  class Flag < Base
4
4
  def aliases
5
5
  if long and short
@@ -1,6 +1,6 @@
1
1
  module Bashly
2
- module Models
3
- class Script
2
+ module Script
3
+ class Wrapper
4
4
  include Renderable
5
5
 
6
6
  attr_reader :command, :function_name
@@ -1,18 +1,15 @@
1
- # ---
2
- # Color functions
3
- # This file is a part of Bashly standard library
4
- #
5
- # Usage:
6
- # Use any of the functions below to color or format a portion of a string.
7
- #
8
- # echo "before $(red this is red) after"
9
- # echo "before $(green_bold this is green_bold) after"
10
- #
11
- # Color output will be disabled if `NO_COLOR` environment variable is set
12
- # in compliance with https://no-color.org/
13
- #
14
- # ---
15
-
1
+ ## Color functions [@bashly-upgrade colors]
2
+ ## This file is a part of Bashly standard library
3
+ ##
4
+ ## Usage:
5
+ ## Use any of the functions below to color or format a portion of a string.
6
+ ##
7
+ ## echo "before $(red this is red) after"
8
+ ## echo "before $(green_bold this is green_bold) after"
9
+ ##
10
+ ## Color output will be disabled if `NO_COLOR` environment variable is set
11
+ ## in compliance with https://no-color.org/
12
+ ##
16
13
  print_in_color() {
17
14
  local color="$1"
18
15
  shift
@@ -1,24 +1,23 @@
1
- # ---
2
- # Config functions
3
- # This file is a part of Bashly standard library
4
- #
5
- # Usage:
6
- # - In your script, set the CONFIG_FILE variable. For rxample:
7
- # CONFIG_FILE=settings.ini.
8
- # If it is unset, it will default to 'config.ini'.
9
- # - Use any of the functions below to access the config file.
10
- # ---
11
-
12
- # Create a new config file.
13
- # There is normally no need to use this function, it is used by other
14
- # functions as needed.
1
+ ## Config functions [@bashly-upgrade config]
2
+ ## This file is a part of Bashly standard library
3
+ ##
4
+ ## Usage:
5
+ ## - In your script, set the CONFIG_FILE variable. For rxample:
6
+ ## CONFIG_FILE=settings.ini.
7
+ ## If it is unset, it will default to 'config.ini'.
8
+ ## - Use any of the functions below to access the config file.
9
+ ##
10
+ ## Create a new config file.
11
+ ## There is normally no need to use this function, it is used by other
12
+ ## functions as needed.
13
+ ##
15
14
  config_init() {
16
15
  CONFIG_FILE=${CONFIG_FILE:=config.ini}
17
16
  [[ -f "$CONFIG_FILE" ]] || touch "$CONFIG_FILE"
18
17
  }
19
18
 
20
- # Get a value from the config.
21
- # Usage: result=$(config_get hello)
19
+ ## Get a value from the config.
20
+ ## Usage: result=$(config_get hello)
22
21
  config_get() {
23
22
  local key=$1
24
23
  local regex="^$key *= *(.+)$"
@@ -36,8 +35,8 @@ config_get() {
36
35
  echo "$value"
37
36
  }
38
37
 
39
- # Add or update a key=value pair in the config.
40
- # Usage: config_set key value
38
+ ## Add or update a key=value pair in the config.
39
+ ## Usage: config_set key value
41
40
  config_set() {
42
41
  local key=$1
43
42
  shift
@@ -68,8 +67,8 @@ config_set() {
68
67
  printf "%b\n" "$output" > "$CONFIG_FILE"
69
68
  }
70
69
 
71
- # Delete a key from the config.
72
- # Usage: config_del key
70
+ ## Delete a key from the config.
71
+ ## Usage: config_del key
73
72
  config_del() {
74
73
  local key=$1
75
74
 
@@ -87,19 +86,19 @@ config_del() {
87
86
  printf "%b\n" "$output" > "$CONFIG_FILE"
88
87
  }
89
88
 
90
- # Show the config file
89
+ ## Show the config file
91
90
  config_show() {
92
91
  config_init
93
92
  cat "$CONFIG_FILE"
94
93
  }
95
94
 
96
- # Return an array of the keys in the config file.
97
- # Usage:
98
- #
99
- # for k in $(config_keys); do
100
- # echo "- $k = $(config_get "$k")";
101
- # done
102
- #
95
+ ## Return an array of the keys in the config file.
96
+ ## Usage:
97
+ ##
98
+ ## for k in $(config_keys); do
99
+ ## echo "- $k = $(config_get "$k")";
100
+ ## done
101
+ ##
103
102
  config_keys() {
104
103
  local regex="^([a-zA-Z0-9_\-\/\.]+) *="
105
104
 
@@ -117,13 +116,13 @@ config_keys() {
117
116
  echo "${keys[@]}"
118
117
  }
119
118
 
120
- # Returns true if the specified key exists in the config file.
121
- # Usage:
122
- #
123
- # if config_has_key "key" ; then
124
- # echo "key exists"
125
- # fi
126
- #
119
+ ## Returns true if the specified key exists in the config file.
120
+ ## Usage:
121
+ ##
122
+ ## if config_has_key "key" ; then
123
+ ## echo "key exists"
124
+ ## fi
125
+ ##
127
126
  config_has_key() {
128
127
  [[ $(config_get "$1") ]]
129
128
  }
@@ -1,13 +1,13 @@
1
- # Add any function here that is needed in more than one parts of your
2
- # application, or that you otherwise wish to extract from the main function
3
- # scripts.
4
- #
5
- # Note that code here should be wrapped inside bash functions, and it is
6
- # recommended to have a separate file for each function.
7
- #
8
- # Subdirectories will also be scanned for *.sh, so you have no reason not
9
- # to organize your code neatly.
10
- #
1
+ ## Add any function here that is needed in more than one parts of your
2
+ ## application, or that you otherwise wish to extract from the main function
3
+ ## scripts.
4
+ ##
5
+ ## Note that code here should be wrapped inside bash functions, and it is
6
+ ## recommended to have a separate file for each function.
7
+ ##
8
+ ## Subdirectories will also be scanned for *.sh, so you have no reason not
9
+ ## to organize your code neatly.
10
+ ##
11
11
  sample_function() {
12
12
  echo "it works"
13
13
  }
@@ -1,3 +1,4 @@
1
+ ## [@bashly-upgrade validations]
1
2
  validate_dir_exists() {
2
3
  [[ -d "$1" ]] || echo "must be an existing directory"
3
4
  }
@@ -1,3 +1,4 @@
1
+ ## [@bashly-upgrade validations]
1
2
  validate_file_exists() {
2
3
  [[ -f "$1" ]] || echo "must be an existing file"
3
4
  }
@@ -1,3 +1,4 @@
1
+ ## [@bashly-upgrade validations]
1
2
  validate_integer() {
2
3
  [[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer"
3
4
  }
@@ -1,3 +1,4 @@
1
+ ## [@bashly-upgrade validations]
1
2
  validate_not_empty() {
2
3
  [[ -z "$1" ]] && echo "must not be empty"
3
4
  }
@@ -1,18 +1,15 @@
1
- # ---
2
- # YAML parser
3
- # This file is a part of Bashly standard library
4
- # Does not support arrays, only hashes
5
- #
6
- # Source: https://stackoverflow.com/a/21189044/413924
7
- #
8
- # Usage:
9
- #
10
- # yaml_load "settings.yml" # print variables
11
- # yaml_load "settings.yml" "config_" # use prefix
12
- # eval $(yaml_load "settings.yml") # create variables in scope
13
- #
14
- # ---
15
-
1
+ ## YAML parser [@bashly-upgrade yaml]
2
+ ## This file is a part of Bashly standard library
3
+ ## Does not support arrays, only hashes
4
+ ##
5
+ ## Source: https://stackoverflow.com/a/21189044/413924
6
+ ##
7
+ ## Usage:
8
+ ##
9
+ ## yaml_load "settings.yml" # print variables
10
+ ## yaml_load "settings.yml" "config_" # use prefix
11
+ ## eval $(yaml_load "settings.yml") # create variables in scope
12
+ ##
16
13
  yaml_load() {
17
14
  local prefix=$2
18
15
  local s='[[:space:]]*' w='[a-zA-Z0-9_]*'
@@ -1,3 +1,3 @@
1
1
  module Bashly
2
- VERSION = "0.6.9"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -8,9 +8,9 @@ parse_requirements() {
8
8
  <%= render(:environment_variables_filter).indent 2 %>
9
9
  <%= render(:dependencies_filter).indent 2 %>
10
10
  <%= render(:command_filter).indent 2 %>
11
+ <%= render(:parse_requirements_while).indent 2 %>
11
12
  <%= render(:required_args_filter).indent 2 %>
12
13
  <%= render(:required_flags_filter).indent 2 %>
13
- <%= render(:parse_requirements_while).indent 2 %>
14
14
  <%= render(:catch_all_filter).indent 2 %>
15
15
  <%= render(:default_assignments).indent 2 %>
16
16
  <%= render(:whitelist_filter).indent 2 %>
@@ -1,12 +1,7 @@
1
1
  # :command.required_args_filter
2
2
  % required_args.each do |arg|
3
- if [[ -n ${1+x} && $1 != -* ]]; then
4
- <%= arg.render(:validations).indent 2 %>
5
- args[<%= arg.name %>]=$1
6
- shift
7
- else
3
+ if [[ -z ${args[<%= arg.name %>]+x} ]]; then
8
4
  printf "<%= strings[:missing_required_argument] % { arg: arg.name.upcase, usage: usage_string } %>\n"
9
5
  exit 1
10
6
  fi
11
-
12
7
  % end
@@ -1,9 +1,6 @@
1
1
  # :command.required_flags_filter
2
- % if required_flags.any?
3
- argstring="$*"
4
- % end
5
2
  % required_flags.each do |flag|
6
- if [[ <%= flag.aliases.map { |a| %Q["$argstring" != *#{a}*] }.join " && " %> ]]; then
3
+ if [[ -z ${args[<%= flag.long %>]+x} ]]; then
7
4
  printf "<%= strings[:missing_required_flag] % { usage: flag.usage_string } %>\n"
8
5
  exit 1
9
6
  fi
File without changes
File without changes
data/lib/bashly.rb CHANGED
@@ -9,6 +9,7 @@ requires 'bashly/concerns'
9
9
 
10
10
  requires 'bashly/settings'
11
11
  requires 'bashly/exceptions'
12
- requires 'bashly/models/base'
12
+ requires 'bashly/script/base'
13
13
  requires 'bashly/commands/base'
14
+ requires 'bashly/library/base'
14
15
  requires 'bashly'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bashly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.9
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-26 00:00:00.000000000 Z
11
+ date: 2021-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colsole
@@ -88,14 +88,26 @@ files:
88
88
  - lib/bashly/config.rb
89
89
  - lib/bashly/exceptions.rb
90
90
  - lib/bashly/extensions/array.rb
91
+ - lib/bashly/extensions/file.rb
91
92
  - lib/bashly/extensions/string.rb
93
+ - lib/bashly/library/base.rb
94
+ - lib/bashly/library/colors.rb
95
+ - lib/bashly/library/completions.rb
96
+ - lib/bashly/library/completions_function.rb
97
+ - lib/bashly/library/completions_script.rb
98
+ - lib/bashly/library/completions_yaml.rb
99
+ - lib/bashly/library/config.rb
100
+ - lib/bashly/library/sample.rb
101
+ - lib/bashly/library/strings.rb
102
+ - lib/bashly/library/validations.rb
103
+ - lib/bashly/library/yaml.rb
92
104
  - lib/bashly/message_strings.rb
93
- - lib/bashly/models/argument.rb
94
- - lib/bashly/models/base.rb
95
- - lib/bashly/models/command.rb
96
- - lib/bashly/models/environment_variable.rb
97
- - lib/bashly/models/flag.rb
98
- - lib/bashly/models/script.rb
105
+ - lib/bashly/script/argument.rb
106
+ - lib/bashly/script/base.rb
107
+ - lib/bashly/script/command.rb
108
+ - lib/bashly/script/environment_variable.rb
109
+ - lib/bashly/script/flag.rb
110
+ - lib/bashly/script/wrapper.rb
99
111
  - lib/bashly/settings.rb
100
112
  - lib/bashly/templates/bashly.yml
101
113
  - lib/bashly/templates/lib/colors.sh
@@ -149,9 +161,9 @@ files:
149
161
  - lib/bashly/views/flag/case.erb
150
162
  - lib/bashly/views/flag/usage.erb
151
163
  - lib/bashly/views/flag/validations.erb
152
- - lib/bashly/views/script/bash3_bouncer.erb
153
- - lib/bashly/views/script/header.erb
154
- - lib/bashly/views/script/wrapper.erb
164
+ - lib/bashly/views/wrapper/bash3_bouncer.erb
165
+ - lib/bashly/views/wrapper/header.erb
166
+ - lib/bashly/views/wrapper/wrapper.erb
155
167
  homepage: https://github.com/dannyben/bashly
156
168
  licenses:
157
169
  - MIT