Skip to content

Commit

Permalink
lint & fix sh ; remove all package content except for update scripts …
Browse files Browse the repository at this point in the history
…before moving updated package content except for update scripts ; edit gitignore ; keep package, lock, and webpack config
  • Loading branch information
froger-me committed Jan 10, 2024
1 parent 4b31f0b commit 8e2fb9d
Show file tree
Hide file tree
Showing 9 changed files with 2,493 additions and 68 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
**/node_modules/
**/package-lock.json
**/package.json
**/webpack.config.js
**/__pycache__/
**/.eslintrc
**/.installed
2 changes: 1 addition & 1 deletion integration/dummy-generic/dummy-generic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fi
### INSTALLING THE PACKAGE ###

if [ "$1" == "install" ] && [ "$(bash "$(dirname "$0")/wppus-api.sh" is_installed)" == "false" ] && [ "$2" != "" ]; then
bash "$(dirname "$0")/wppus-api.sh" install $2
bash "$(dirname "$0")/wppus-api.sh" install "$2"
echo "Installed"

exit 0
Expand Down
2,367 changes: 2,367 additions & 0 deletions integration/dummy-generic/node-src/package-lock.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions integration/dummy-generic/node-src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"dependencies": {
"adm-zip": "^0.5.10",
"node-machine-id": "^1.1.12"
},
"devDependencies": {
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
}
}
10 changes: 10 additions & 0 deletions integration/dummy-generic/node-src/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path');

module.exports = {
entry: './exports.js',
output: {
filename: '../node-dist/exports.js',
path: __dirname,
},
target: 'node'
};
33 changes: 22 additions & 11 deletions integration/dummy-generic/wppus-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,31 +250,42 @@ async function main() {
// set the permissions of the new file to the permissions of the old file
fs.chmodSync('/tmp/' + package_name + '/' + script_name, octal_mode);

// move the updated main script to the current directory ;
// the updated main script is in charge of overriding the update
// scripts by moving files around after update
let files = fs.readdirSync('/tmp/' + package_name);
// delete all files in the current directory, except for update scripts
let files = fs.readdirSync(__dirname);

for (let i = 0; i < files.length; i++) {
let file = files[i];

if (file !== script_name) {
let destinationPath = path.join(__dirname, file);
// check if the file does not start with `wppus`, or is .json
if (!file.startsWith("wppus") || file.endsWith(".json")) {
let deletePath = path.join(__dirname, file);

if (fs.existsSync(destinationPath)) {
if (fs.existsSync(deletePath)) {

if (parseInt(process.version.slice(1).split('.')[0]) >= 14) {
fs.rmSync(destinationPath, { recursive: true, force: true });
fs.rmSync(deletePath, { recursive: true, force: true });
} else {

if (fs.lstatSync(destinationPath).isDirectory()) {
fs.rmdirSync(destinationPath, { recursive: true });
if (fs.lstatSync(deletePath).isDirectory()) {
fs.rmdirSync(deletePath, { recursive: true });
} else {
fs.unlinkSync(destinationPath);
fs.unlinkSync(deletePath);
}
}
}
}
}

// move the updated package files to the current directory ;
// the updated package is in charge of overriding the update script
// with new ones after update (may be contained in a subdirectory)
files = fs.readdirSync('/tmp/' + package_name);

for (let i = 0; i < files.length; i++) {
let file = files[i];

// check if the file does not start with `wppus`, or is .json
if (!file.startsWith("wppus") || file.endsWith(".json")) {
fs.renameSync('/tmp/' + package_name + '/' + file, path.join(__dirname, file));
}
}
Expand Down
20 changes: 15 additions & 5 deletions integration/dummy-generic/wppus-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,24 @@ public static function update() {
# set the permissions of the new file to the permissions of the old file
chmod( $octal_mode, '/tmp/' . self::$package_name . '/' . self::$package_name . '.php' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod

# move the updated main script to the current directory ;
# the updated main script is in charge of overriding the update
# scripts by moving files around after update
$t_info = PATHINFO_FILENAME;
$t_ext = PATHINFO_EXTENSION;

# delete all files in the current directory, except for update scripts
foreach ( glob( __DIR__ . '/*' ) as $file ) {

# check if the file does not start with `wppus`, or is .json
if ( 'wppus' !== substr( basename( $file ), 0, 5 ) && 'json' !== pathinfo( $file, $t_ext ) ) {
unlink( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
}
}

# move the updated package files to the current directory ;
# the updated package is in charge of overriding the update script
# with new ones after update (may be contained in a subdirectory)
foreach ( glob( '/tmp/' . self::$package_name . '/*' ) as $file ) {

if ( pathinfo( $file, $t_info ) !== pathinfo( self::$package_script, $t_info ) ) {
# check if the file does not start with `wppus`, or is .json
if ( 'wppus' !== substr( basename( $file ), 0, 5 ) && 'json' !== pathinfo( $file, $t_ext ) ) {
rename( $file, dirname( self::$package_script ) . '/' . basename( $file ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename
}
}
Expand Down
16 changes: 12 additions & 4 deletions integration/dummy-generic/wppus-api.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,20 @@ def update():
# set the permissions of the new file to the permissions of the old file
os.chmod(os.path.join(tempfile.gettempdir(), package_name, script_name), int(octal_mode, 8))

# move the updated main script to the current directory ;
# the updated main script is in charge of overriding the update
# scripts by moving files around after update
# delete all files in the current directory, except for update scripts
for file in os.listdir(os.path.dirname(__file__)):

# check if the file does not start with `wppus`, or is .json
if not file.startswith("wppus") or file.endswith(".json"):
os.remove(os.path.join(os.path.dirname(__file__), file))

# move the updated package files to the current directory ;
# the updated package is in charge of overriding the update script
# with new ones after update (may be contained in a subdirectory)
for file in os.listdir(os.path.join(tempfile.gettempdir(), package_name)):

if file != script_name:
# check if the file does not start with `wppus`, or is .json
if not file.startswith("wppus") or file.endswith(".json"):
src = os.path.join(tempfile.gettempdir(), package_name, file)
dest = os.path.join(os.path.dirname(__file__), file)

Expand Down
100 changes: 56 additions & 44 deletions integration/dummy-generic/wppus-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,57 @@
# WP Packages Update Server is installed in wppus.json

# define the url of the server
url=$(jq -r '.server' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")
url=$(jq -r '.server' "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json")
# define the package name
package_name="$(basename "$(cd "$(dirname "$0")"; pwd -P)")"
package_name="$(basename "$(cd "$(dirname "$0")" || exit; pwd -P)")"
# define the package script
package_script="$(cd "$(dirname "$0")"; pwd -P)/$(basename "$(cd "$(dirname "$0")"; pwd -P)").sh"
# define the current script name
script_name="$(basename $0)"
package_script="$(cd "$(dirname "$0")" || exit; pwd -P)/$(basename "$(cd "$(dirname "$0")" || exit; pwd -P)").sh"
# define the update zip archive name
zip_name="$package_name.zip"
# define the current version of the package from the wppus.json file
version=$(jq -r '.packageData.Version' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")
version=$(jq -r '.packageData.Version' "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json")
# define license_key from the wppus.json file
license_key=$(jq -r '.licenseKey' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")
license_key=$(jq -r '.licenseKey' "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json")
# define license_signature from the wppus.json file
license_signature=$(jq -r '.licenseSignature' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")
license_signature=$(jq -r '.licenseSignature' "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json")

# define the domain
if [[ "$(uname)" == "Darwin" ]]; then
# macOS
domain=$(echo "$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}')" | tr -d '\n')
domain=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}' | tr -d '\n')
elif [[ "$(uname)" == "Linux" ]]; then
# Ubuntu
domain=$(echo "$(cat /var/lib/dbus/machine-id)" | tr -d '\n')
domain=$(tr -d '\n' < /var/lib/dbus/machine-id)
fi

### INSTALLING THE PACKAGE ###

function install() {
# add the license key to wppus.json
jq --indent 4 '.licenseKey = "'"$1"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
jq --indent 4 '.licenseKey = "'"$1"'"' "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json"
# add a file '.installed' in current directory
touch "$(cd "$(dirname "$0")"; pwd -P)/.installed"
touch "$(cd "$(dirname "$0")" || exit; pwd -P)/.installed"
}

### UNINSTALLING THE PACKAGE ###

function uninstall() {
local license=$license_key
license_signature=""

# remove the license key from wppus.json
jq --indent 4 '.licenseKey = ""' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
jq --indent 4 '.licenseKey = ""' "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json"
# remove the license signature from wppus.json
jq --indent 4 '.licenseSignature = ""' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
jq --indent 4 '.licenseSignature = ""' "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json"
# remove the file '.installed' from current directory
rm "$(cd "$(dirname "$0")"; pwd -P)/.installed"
rm "$(cd "$(dirname "$0")" || exit; pwd -P)/.installed"
}

### CHECKING IF THE PACKAGE IS INSTALLED ###

function is_installed() {

# check if the file '.installed exists in current directory
if [ -f "$(cd "$(dirname "$0")"; pwd -P)/.installed" ]; then
if [ -f "$(cd "$(dirname "$0")" || exit; pwd -P)/.installed" ]; then

# return true
echo "true"
Expand All @@ -79,11 +76,13 @@ function is_installed() {
### SENDING AN API REQUEST ###

function send_api_request() {
# build the request url
local full_url
local response
local IFS='&'
local full_url=$(printf "%s/%s/?%s" "$url" "$1" "${*:2}")
# build the request url
full_url=$(printf "%s/%s/?%s" "$url" "$1" "${*:2}")
# make the request
local response=$(curl -sL "$full_url")
response=$(curl -sL "$full_url")

# return the response
echo "$response"
Expand Down Expand Up @@ -119,6 +118,7 @@ function urldecode() {
### CHECKING FOR UPDATES ###

function check_for_updates() {
local response
# build the request url
local endpoint="wppus-update-api"
local args=(
Expand All @@ -130,7 +130,7 @@ function check_for_updates() {
"update_type=Generic"
)
# make the request
local response=$(send_api_request "$endpoint" "${args[@]}")
response=$(send_api_request "$endpoint" "${args[@]}")

# return the response
echo "$response"
Expand All @@ -139,6 +139,8 @@ function check_for_updates() {
### ACTIVATING A LICENSE ###

function activate_license() {
local response
local signature
# build the request url
local endpoint="wppus-license-api"
local args=(
Expand All @@ -148,12 +150,12 @@ function activate_license() {
"package_slug=$(urlencode "$package_name")"
)
# make the request
local response=$(send_api_request "$endpoint" "${args[@]}")
response=$(send_api_request "$endpoint" "${args[@]}")
# get the signature from the response
local signature=$(urldecode $(echo -n "$response" | jq -r '.license_signature'))
signature="$(urldecode "$(echo -n "$response" | jq -r '.license_signature')")"

# add the license signature to wppus.json
jq --indent 4 '.licenseSignature = "'"$signature"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
jq --indent 4 '.licenseSignature = "'"$signature"'"' "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json"

license_signature=$signature
}
Expand All @@ -173,24 +175,25 @@ function deactivate_license() {
# # make the request
send_api_request "$endpoint" "${args[@]}" > /dev/null
# remove the license signature from wppus.json
jq --indent 4 'del(.licenseSignature)' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
jq --indent 4 'del(.licenseSignature)' "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json"

license_signature=""
}

### DOWNLOADING THE PACKAGE ###

function download_update() {
local url
# get the download url from the response in $1
local url=$(urldecode $(echo -n "$1" | jq -r '.download_url'))
url="$(urldecode "$(echo -n "$1" | jq -r '.download_url')")"
# set the path to the downloaded file
local output_file="/tmp/$zip_name"

# make the request
curl -sS -L -o $output_file "$url"
curl -sS -L -o "$output_file" "$url"

# return the path to the downloaded file
echo $output_file
echo "$output_file"
}

### MAIN SCRIPT ###
Expand All @@ -206,23 +209,23 @@ fi
# check if the script was called with the argument "install"
if [ "$1" == "install" ]; then
# install the package
echo $(install "$2")
install "$2"

exit 0
fi

# check if the script was called with the argument "uninstall"
if [ "$1" == "uninstall" ]; then
# uninstall the package
echo $(uninstall)
uninstall

exit 0
fi

# check if the script was called with the argument "is_installed"
if [ "$1" == "is_installed" ]; then
# check if the package is installed
echo $(is_installed)
is_installed

exit 0
fi
Expand All @@ -241,7 +244,7 @@ if [ "$1" == "update" ]; then
output_file=$(download_update "$response")

# extract the zip in /tmp/$(package_name)
unzip -q -o $output_file -d /tmp
unzip -q -o "$output_file" -d /tmp

# get the permissions of the old file
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
Expand All @@ -251,26 +254,35 @@ if [ "$1" == "update" ]; then
fi

# set the permissions of the new file to the permissions of the old file
chmod $OCTAL_MODE /tmp/$package_name/$package_name.sh

# move all the files and directories except the wppus-api (all languages) to the
# current directory ; the updated main script is in charge of
# overriding the update scripts by moving files around after update
for file in /tmp/$package_name/*; do
if [[ "${file%.*}" != "${script_name%.*}" ]]; then
mv /tmp/$package_name/$file $(dirname "$0")
chmod "$OCTAL_MODE" /tmp/"$package_name"/"$package_name".sh

# delete all files in the current directory, except for update scripts
for file in "$(cd "$(dirname "$0")" || exit; pwd -P)"/*; do
# check if the file does not start with `wppus`, or is .json
if [[ ! "$file" == wppus* ]] || [[ "$file" == *.json ]]; then
rm -rf "$file"
fi
done

# move the updated package files to the current directory ;
# the updated package is in charge of overriding the update script
# with new ones after update (may be contained in a subdirectory)
for file in "/tmp/$package_name"/*; do
# check if the file does not start with `wppus`, or is .json
if [[ ! "$file" == wppus* ]] || [[ "$file" == *.json ]]; then
mv "/tmp/$package_name/$file" "$(dirname "$0")"
fi
done

# add the license key to wppus.json
jq --indent 4 '.licenseKey = "'"$license_key"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
jq --indent 4 '.licenseKey = "'"$license_key"'"' "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json"
# add the license signature to wppus.json
jq --indent 4 '.licenseSignature = "'"$signature"'"' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")"; pwd -P)/wppus.json"
jq --indent 4 '.licenseSignature = "'"$signature"'"' "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json" > tmp.json && mv tmp.json "$(cd "$(dirname "$0")" || exit; pwd -P)/wppus.json"

# remove the directory
rm -rf /tmp/$package_name
rm -rf /tmp/"$package_name"
# remove the zip
rm $output_file
rm "$output_file"

exit 0
fi
Expand Down

0 comments on commit 8e2fb9d

Please sign in to comment.