-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
588 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/env bash | ||
set -Eeu -o pipefail | ||
# Title: commands.d/* | ||
# Description: this script is a valet command | ||
# Author: github.com/jcaillon | ||
|
||
# import the main script (should always be skipped if the command is run from valet, this is mainly for shellcheck) | ||
if [[ -z "${GLOBAL_CORE_INCLUDED:-}" ]]; then | ||
# shellcheck source=../libraries.d/core | ||
source "$(dirname -- "$(command -v valet)")/libraries.d/core" | ||
fi | ||
# --- END OF COMMAND COMMON PART | ||
|
||
# shellcheck source=../libraries.d/lib-string | ||
source string | ||
# shellcheck source=../libraries.d/lib-io | ||
source io | ||
# shellcheck source=../libraries.d/lib-interactive | ||
source interactive | ||
|
||
#=============================================================== | ||
# >>> command: self add-command | ||
#=============================================================== | ||
|
||
##<<VALET_COMMAND | ||
# command: self add-command | ||
# function: selfAddCommand | ||
# author: github.com/jcaillon | ||
# shortDescription: Add a new command to the current extension. | ||
# description: |- | ||
# Call this function in an extension directory to add a new command to the extension. | ||
# | ||
# This will create a file from a command template in the ⌜commands.d⌝ directory. | ||
# arguments: | ||
# - name: commandName | ||
# description: |- | ||
# The name of the command to create. | ||
# examples: | ||
# - name: self add-command my-command | ||
# description: |- | ||
# Create a new command named ⌜my-command⌝ in the current extension under the ⌜commands.d⌝ directory. | ||
##VALET_COMMAND | ||
function selfAddCommand() { | ||
local commandName | ||
core::parseArguments "$@" && eval "${RETURNED_VALUE}" | ||
core::checkParseResults "${help:-}" "${parsingErrors:-}" | ||
|
||
local templateFlavor="default" | ||
|
||
# the command name should only contains letters, digits, spaces and hyphens | ||
if [[ ! ${commandName} =~ ^[-[:alnum:]" "]+$ ]]; then | ||
core::fail "The command name should only contain letters, digits, spaces and hyphens." | ||
return 1 | ||
fi | ||
|
||
# check if we are working for an extension | ||
core::getUserDirectory | ||
if [[ ${PWD} != "${RETURNED_VALUE}"* && ! -d "commands.d" ]]; then | ||
log::warning "The current directory is not under the valet user directory ⌜${RETURNED_VALUE}⌝." | ||
if ! interactive::promptYesNo "It does not look like the current directory ⌜${PWD}⌝ is a valet extension, do you want to proceed anyway?" true; then | ||
log::info "Aborting the creation of the command." | ||
log::info "You should first create an extension with ⌜valet self extend⌝ and then cd into the created directory." | ||
return 0 | ||
fi | ||
fi | ||
|
||
local fileName="${commandName// /-}" | ||
string::kebabCaseToCamelCase "${fileName}" | ||
local functionName="${RETURNED_VALUE}" | ||
local newCommandFilePath="${PWD}/commands.d/${fileName}.sh" | ||
local commandTemplateFile="${GLOBAL_VALET_HOME}/extras/template-command-${templateFlavor}.sh" | ||
|
||
if [[ -f ${newCommandFilePath} ]]; then | ||
log::warning "The command file ⌜${newCommandFilePath}⌝ already exists." | ||
if ! interactive::promptYesNo "Do you want to override the existing command file?" true; then | ||
log::info "Aborting the creation of the command." | ||
return 0 | ||
fi | ||
rm -f "${newCommandFilePath}" | ||
fi | ||
|
||
# create the commands directory if it does not exist | ||
io::createDirectoryIfNeeded "${PWD}/commands.d" | ||
|
||
io::readFile "${commandTemplateFile}" | ||
local templateContent="${RETURNED_VALUE//_COMMAND_NAME_/"${commandName}"}" | ||
templateContent="${templateContent//_FUNCTION_NAME_/"${functionName}"}" | ||
|
||
printf "%s" "${templateContent}" >"${newCommandFilePath}" | ||
|
||
log::success "The command ⌜${commandName}⌝ has been created with the file ⌜${newCommandFilePath}⌝." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env bash | ||
set -Eeu -o pipefail | ||
# Title: commands.d/* | ||
# Description: this script is a valet command | ||
# Author: github.com/jcaillon | ||
|
||
# import the main script (should always be skipped if the command is run from valet, this is mainly for shellcheck) | ||
if [[ -z "${GLOBAL_CORE_INCLUDED:-}" ]]; then | ||
# shellcheck source=../libraries.d/core | ||
source "$(dirname -- "$(command -v valet)")/libraries.d/core" | ||
fi | ||
# --- END OF COMMAND COMMON PART | ||
|
||
# shellcheck source=../libraries.d/lib-string | ||
source string | ||
# shellcheck source=../libraries.d/lib-io | ||
source io | ||
# shellcheck source=../libraries.d/lib-interactive | ||
source interactive | ||
|
||
#=============================================================== | ||
# >>> command: self add-library | ||
#=============================================================== | ||
|
||
##<<VALET_COMMAND | ||
# command: self add-library | ||
# function: selfAddLibrary | ||
# author: github.com/jcaillon | ||
# shortDescription: Add a new library to the current extension. | ||
# description: |- | ||
# Call this function in an extension directory to add a new library to the extension. | ||
# | ||
# This will create a file from a library template in the ⌜libraries.d⌝ directory. | ||
# arguments: | ||
# - name: libraryName | ||
# description: |- | ||
# The name of the library to create. | ||
# examples: | ||
# - name: self add-library my-library | ||
# description: |- | ||
# Create a new library named ⌜my-library⌝ in the current extension under the ⌜libraries.d⌝ directory. | ||
##VALET_COMMAND | ||
function selfAddLibrary() { | ||
local libraryName | ||
core::parseArguments "$@" && eval "${RETURNED_VALUE}" | ||
core::checkParseResults "${help:-}" "${parsingErrors:-}" | ||
|
||
local templateFlavor="default" | ||
|
||
# the library name should only contains letters, digits, underscores and hyphens | ||
if [[ ! ${libraryName} =~ ^[-[:alnum:]_]+$ ]]; then | ||
core::fail "The library name should only contain letters, digits, spaces and hyphens." | ||
return 1 | ||
fi | ||
|
||
# check if we are working for an extension | ||
core::getUserDirectory | ||
if [[ ${PWD} != "${RETURNED_VALUE}"* && ! -d "libraries.d" ]]; then | ||
log::warning "The current directory is not under the valet user directory ⌜${RETURNED_VALUE}⌝." | ||
if ! interactive::promptYesNo "It does not look like the current directory ⌜${PWD}⌝ is a valet extension, do you want to proceed anyway?" true; then | ||
log::info "Aborting the creation of the library." | ||
log::info "You should first create an extension with ⌜valet self extend⌝ and then cd into the created directory." | ||
return 0 | ||
fi | ||
fi | ||
|
||
local newCommandFilePath="${PWD}/libraries.d/lib-${libraryName}" | ||
local commandTemplateFile="${GLOBAL_VALET_HOME}/extras/template-library-${templateFlavor}.sh" | ||
|
||
if [[ -f ${newCommandFilePath} ]]; then | ||
log::warning "The library file ⌜${newCommandFilePath}⌝ already exists." | ||
if ! interactive::promptYesNo "Do you want to override the existing library file?" true; then | ||
log::info "Aborting the creation of the library." | ||
return 0 | ||
fi | ||
rm -f "${newCommandFilePath}" | ||
fi | ||
|
||
# create the commands directory if it does not exist | ||
io::createDirectoryIfNeeded "${PWD}/libraries.d" | ||
|
||
io::readFile "${commandTemplateFile}" | ||
local templateContent="${RETURNED_VALUE//_LIBRARY_NAME_/"${libraryName}"}" | ||
|
||
printf "%s" "${templateContent}" >"${newCommandFilePath}" | ||
|
||
log::success "The library ⌜${libraryName}⌝ has been created with the file ⌜${newCommandFilePath}⌝." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env bash | ||
|
||
#=============================================================== | ||
# >>> command: _COMMAND_NAME_ | ||
#=============================================================== | ||
|
||
: "--- | ||
command: _COMMAND_NAME_ | ||
function: _FUNCTION_NAME_ | ||
shortDescription: My new command one line description. | ||
description: |- | ||
My long description. | ||
sudo: false | ||
hideInMenu: false | ||
arguments: | ||
- name: firstArg | ||
description: |- | ||
First argument. | ||
- name: more... | ||
description: |- | ||
Will be an an array of strings. | ||
options: | ||
- name: -o, --option1 | ||
description: |- | ||
First option. | ||
noEnvironmentVariable: true | ||
- name: -2, --this-is-option2 <level> | ||
description: |- | ||
An option with a value. | ||
noEnvironmentVariable: false | ||
examples: | ||
- name: _COMMAND_NAME_ -o -2 value1 arg1 more1 more2 | ||
description: |- | ||
Call _COMMAND_NAME_ with option1, option2 and some arguments. | ||
---" | ||
function _FUNCTION_NAME_() { | ||
local -a more | ||
local firstArg option1 thisIsOption2 | ||
core::parseArguments "$@" && eval "${RETURNED_VALUE}" | ||
core::checkParseResults "${help:-}" "${parsingErrors:-}" | ||
|
||
log::info "First argument: ${firstArg:-}." | ||
log::info "Option 1: ${option1:-}." | ||
log::info "Option 2: ${thisIsOption2:-}." | ||
log::info "More: ${more[*]}." | ||
|
||
# example use of a library function | ||
# Importing the string library (note that we could also do that at the beginning of the script) | ||
# shellcheck disable=SC1091 | ||
source string | ||
string::extractBetween "<b>My bold text</b>" "<b>" "</b>" | ||
local extractedText="${RETURNED_VALUE}" | ||
log::info "Extracted text is: ⌜${extractedText:-}⌝" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env bash | ||
|
||
#=============================================================== | ||
# >>> library: _LIBRARY_NAME_ | ||
#=============================================================== | ||
|
||
# ## _LIBRARY_NAME_::myFunction | ||
# | ||
# Description of the function goes there. | ||
# | ||
# - $1: **first argument** _as string_: | ||
# description of the first argument | ||
# - $2: force _as bool_: | ||
# (optional) description of the second argument | ||
# (defaults to false) | ||
# | ||
# Returns: | ||
# | ||
# - $?: The exit code of the executable. | ||
# - `RETURNED_VALUE`: The value to return | ||
# | ||
# ```bash | ||
# _LIBRARY_NAME_::myFunction hi true | ||
# echo "${RETURNED_VALUE}" | ||
# ``` | ||
# | ||
# > A note about the function. | ||
function _LIBRARY_NAME_::myFunction() { | ||
:; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.