Skip to content

Commit 7256fba

Browse files
authored
Merge pull request #144 from DannyBen/add/lib-auto-upgrade
Auto upgrade libraries
2 parents bb05f71 + 6986942 commit 7256fba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+538
-19
lines changed

examples/colors/src/lib/colors.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ---
2-
# Color functions
2+
# Color functions [@bashly-upgrade colors]
33
# This file is a part of Bashly standard library
44
#
55
# Usage:

examples/completions/src/lib/send_completions.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# [@bashly-upgrade completions send_completions]
12
send_completions() {
23
echo $'#!/usr/bin/env bash'
34
echo $''
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# [@bashly-upgrade validations]
12
validate_dir_exists() {
23
[[ -d "$1" ]] || echo "must be an existing directory"
34
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# [@bashly-upgrade validations]
12
validate_file_exists() {
23
[[ -f "$1" ]] || echo "must be an existing file"
34
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# [@bashly-upgrade validations]
12
validate_integer() {
23
[[ "$1" =~ ^[0-9]+$ ]] || echo "must be an integer"
34
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# [@bashly-upgrade validations]
12
validate_not_empty() {
23
[[ -z "$1" ]] && echo "must not be empty"
34
}

lib/bashly/commands/add.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ def safe_write(path, content)
9898
end
9999

100100
if File.exist? path and !args['--force']
101-
say "skipped !txtgrn!#{path}!txtrst! (exists)"
101+
say "!txtblu!skipped!txtrst! #{path} (exists)"
102102
false
103103

104104
else
105105
File.deep_write path, content
106-
say "created !txtgrn!#{path}"
106+
say "!txtgrn!created!txtrst! #{path}"
107107
true
108108

109109
end

lib/bashly/commands/generate.rb

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ module Commands
33
class Generate < Base
44
help "Generate the bash script and required files"
55

6-
usage "bashly generate [--force --quiet --wrap FUNCTION]"
6+
usage "bashly generate [--force --quiet --upgrade --wrap FUNCTION]"
77
usage "bashly generate (-h|--help)"
88

99
option "-f --force", "Overwrite existing files"
10-
option "-w --wrap FUNCTION", "Wrap the entire script in a function so it can also be sourced"
1110
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"
1213

1314
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
1415
environment "BASHLY_TARGET_DIR", "The path to use for creating the bash script [default: .]"
@@ -18,6 +19,7 @@ class Generate < Base
1819

1920
def run
2021
create_user_files
22+
upgrade_libs if args['--upgrade']
2123
create_master_script
2224
quiet_say "run !txtpur!#{master_script_path} --help!txtrst! to test your bash script"
2325
end
@@ -28,6 +30,44 @@ def quiet_say(message)
2830
say message unless args['--quiet']
2931
end
3032

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+
3171
def create_user_files
3272
quiet_say "creating user files in !txtgrn!#{Settings.source_dir}"
3373

@@ -55,17 +95,17 @@ def create_all_command_files
5595

5696
def create_file(file, content)
5797
if File.exist? file and !args['--force']
58-
quiet_say "skipped !txtgrn!#{file}!txtrst! (exists)"
98+
quiet_say "!txtblu!skipped!txtrst! #{file} (exists)"
5999
else
60100
File.write file, content
61-
quiet_say "created !txtgrn!#{file}"
101+
quiet_say "!txtgrn!created!txtrst! #{file}"
62102
end
63103
end
64104

65105
def create_master_script
66106
File.write master_script_path, script.code
67107
FileUtils.chmod "+x", master_script_path
68-
quiet_say "created !txtgrn!#{master_script_path}"
108+
quiet_say "!txtgrn!created!txtrst! #{master_script_path}"
69109
end
70110

71111
def script

lib/bashly/commands/init.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def run
1717
end
1818
Dir.mkdir target_dir unless Dir.exist? target_dir
1919
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"
2121
say "run !txtpur!bashly generate!txtrst! to create the bash script"
2222
end
2323

lib/bashly/extensions/file.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ def self.deep_write(file, content)
66
FileUtils.mkdir_p dir unless Dir.exist? dir
77
File.write file, content
88
end
9+
10+
def self.append(path, content)
11+
File.open(path, "a") { |f| f << content }
12+
end
913
end

0 commit comments

Comments
 (0)