Skip to content

Commit 1a2a0c4

Browse files
committed
Merge pull request #4 from ash-shell/br.refactor
Refactor
2 parents 98a7352 + e7e499f commit 1a2a0c4

File tree

4 files changed

+81
-62
lines changed

4 files changed

+81
-62
lines changed

HELP.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Usage:
33
apm:init
44
Initializes the current directory with an ash_modules file.
55

6-
apm:install [$git_url] [--global]
6+
apm:install [$git_url [--global]]
77
Installs the modules defined in the ash_modules file.
88

99
If `$git_url` is passed, only that ash module will be

callable.sh

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/bin/bash
22

3-
Apm_modules_file_name="Ashmodules"
4-
Apm_modules_clone_directory=".ash_modules_tmp"
5-
Apm_modules_file_path="$Ash__call_directory/$Apm_modules_file_name"
6-
Apm_local_modules_directory_path="$Ash__call_directory/$Ash__modules_foldername"
7-
Apm_local_modules_clone_path="$Ash__call_directory/$Apm_modules_clone_directory"
3+
Apm_MODULES_FILE_NAME="Ashmodules"
4+
Apm_MODULES_CLONE_DIRECTORY=".ash_modules_tmp"
5+
Apm_MODULES_FILE_PATH="$Ash__call_directory/$Apm_MODULES_FILE_NAME"
6+
Apm_LOCAL_MODULES_DIRECTORY_PATH="$Ash__call_directory/$Ash__modules_foldername"
7+
Apm_LOCAL_MODULES_CLONE_PATH="$Ash__call_directory/$Apm_MODULES_CLONE_DIRECTORY"
88

99
##################################################
1010
# This function is an alias for `ash self:help`.
@@ -27,8 +27,8 @@ Apm__callable_help() {
2727
##################################################
2828
Apm__callable_init() {
2929
# Hasn't been created
30-
if [[ ! -f "$Apm_modules_file_path" ]]; then
31-
touch "$Apm_modules_file_path"
30+
if [[ ! -f "$Apm_MODULES_FILE_PATH" ]]; then
31+
touch "$Apm_MODULES_FILE_PATH"
3232
Logger__success "Directory successfully initialized"
3333

3434
# Has already been created
@@ -50,9 +50,9 @@ Apm__callable_init() {
5050
##################################################
5151
Apm__callable_install() {
5252
# Creating modules directory
53-
if [[ "$2" != "--global" && ! -d "$Apm_local_modules_directory_path" ]]; then
54-
mkdir "$Apm_local_modules_directory_path"
55-
touch "$Apm_local_modules_directory_path/$Ash_module_aliases_file"
53+
if [[ "$2" != "--global" && ! -d "$Apm_LOCAL_MODULES_DIRECTORY_PATH" ]]; then
54+
mkdir "$Apm_LOCAL_MODULES_DIRECTORY_PATH"
55+
touch "$Apm_LOCAL_MODULES_DIRECTORY_PATH/$Ash_module_aliases_file"
5656
fi
5757

5858
# If user is passing in URL
@@ -81,9 +81,9 @@ Apm__callable_modules() {
8181
# This function will update a global module or
8282
# Ash itself.
8383
#
84-
# @param $1: The global module's `name` as defined
85-
# in it's ash_config.yaml file. To update Ash
86-
# itself, simply just pass `ash` here.
84+
# @param $1: The alias or package of a global
85+
# module. To update Ash itself, simply just pass
86+
# `ash` here.
8787
##################################################
8888
Apm__callable_update(){
8989
local module_name="$1"
@@ -94,52 +94,11 @@ Apm__callable_update(){
9494
return
9595
fi
9696

97-
# Checking if we're updating ash
97+
# Update
9898
if [[ "$module_name" = 'ash' ]]; then
99-
cd $Ash__source_directory
100-
101-
# Updating
102-
git pull origin master
103-
git submodule update
104-
105-
# Checking for success
106-
if [ $? -eq 0 ]; then
107-
Logger__success "Ash was updated"
108-
else
109-
Logger__error "Something went wrong, Ash was not updated"
110-
Logger__error "You will have to manually update at $Ash__source_directory"
111-
fi
112-
99+
Apm_update_ash
113100
return
114-
fi
115-
116-
# Expanding alias
117-
local alias_file="$Ash__source_directory/$Ash_global_modules_directory/$Ash_module_aliases_file"
118-
local has_key=$(YamlParse__has_key "$alias_file" "$module_name")
119-
if [[ "$has_key" == $Ash__true ]]; then
120-
eval $(YamlParse__parse "$alias_file" "Apm_update_")
121-
local variable="Apm_update_$module_name"
122-
module_name=${!variable}
123-
fi
124-
125-
# Checking if we're passing a valid global module
126-
local directory="$Ash__source_directory/$Ash_global_modules_directory/$module_name"
127-
local directory_config="$directory/$Ash_config_filename"
128-
if [[ -f "$directory_config" ]]; then
129-
Logger__log "Updating $module_name"
130-
131-
# Updating
132-
cd "$directory"
133-
git pull origin master
134-
135-
# Checking for success
136-
if [ $? -eq 0 ]; then
137-
Logger__success "$module_name was updated"
138-
else
139-
Logger__error "Something went wrong, $module_name was not updated"
140-
Logger__error "You will have to manually update at $directory"
141-
fi
142101
else
143-
Logger__error "Module \"$module_name\" does not exist"
102+
Apm_update_module "$module_name"
144103
fi
145104
}

lib/install.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Apm_install_modules_file() {
1111
if [[ "$line" != "" ]]; then
1212
Apm_install_url "$line"
1313
fi
14-
done < "$Apm_modules_file_path"
14+
done < "$Apm_MODULES_FILE_PATH"
1515
}
1616

1717
##################################################
@@ -24,12 +24,12 @@ Apm_install_url() {
2424
# If global flag is passed
2525
if [[ -n "$2" && "$2" == "--global" ]]; then
2626
Apm_install_path="$Ash__source_directory/$Ash_global_modules_directory"
27-
Apm_clone_path="$Ash__source_directory/$Apm_modules_clone_directory"
27+
Apm_clone_path="$Ash__source_directory/$Apm_MODULES_CLONE_DIRECTORY"
2828

2929
# If no global flag is passed
3030
else
31-
Apm_install_path="$Apm_local_modules_directory_path"
32-
Apm_clone_path="$Apm_local_modules_clone_path"
31+
Apm_install_path="$Apm_LOCAL_MODULES_DIRECTORY_PATH"
32+
Apm_clone_path="$Apm_LOCAL_MODULES_CLONE_PATH"
3333
fi
3434

3535
# Creating temporary clone path

lib/update.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
##################################################
4+
# This function updates Ash itself
5+
##################################################
6+
Apm_update_ash() {
7+
cd $Ash__source_directory
8+
9+
# Updating
10+
git pull origin master
11+
git submodule update
12+
13+
# Checking for success
14+
if [ $? -eq 0 ]; then
15+
Logger__success "Ash was updated"
16+
else
17+
Logger__error "Something went wrong, Ash was not updated"
18+
Logger__error "You will have to manually update at $Ash__source_directory"
19+
fi
20+
}
21+
22+
##################################################
23+
# This function updates a global module by either
24+
# its package or alias.
25+
#
26+
# @param $1: The alias or package of a module
27+
##################################################
28+
Apm_update_module() {
29+
local module_name="$1"
30+
31+
# Expanding alias
32+
local alias_file="$Ash__source_directory/$Ash_global_modules_directory/$Ash_module_aliases_file"
33+
local has_key=$(YamlParse__has_key "$alias_file" "$module_name")
34+
if [[ "$has_key" == $Ash__true ]]; then
35+
eval $(YamlParse__parse "$alias_file" "Apm_update_")
36+
local variable="Apm_update_$module_name"
37+
module_name=${!variable}
38+
fi
39+
40+
# Checking if we're passing a valid global module
41+
local directory="$Ash__source_directory/$Ash_global_modules_directory/$module_name"
42+
local directory_config="$directory/$Ash_config_filename"
43+
if [[ -f "$directory_config" ]]; then
44+
Logger__log "Updating $module_name"
45+
46+
# Updating
47+
cd "$directory"
48+
git pull origin master
49+
50+
# Checking for success
51+
if [ $? -eq 0 ]; then
52+
Logger__success "$module_name was updated"
53+
else
54+
Logger__error "Something went wrong, $module_name was not updated"
55+
Logger__error "You will have to manually update at $directory"
56+
fi
57+
else
58+
Logger__error "Module \"$module_name\" does not exist"
59+
fi
60+
}

0 commit comments

Comments
 (0)