Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a script that inserts strict type declarations #7716

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions add-strict-types.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# Verify an argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <directory>"
echo "Error: Directory argument missing."
exit 1
fi

# Directory containing PHP files, passed as an argument
DIRECTORY="$1"

# Loop over each PHP file in the specified directory
find "$DIRECTORY" -type f -name "*.php" | while read -r file; do
[ -n "$DEBUG" ] && echo "Processing file: $file"
# Check if the file contains the strict_types declaration
if ! grep -qE 'declare\s*\(\s*strict_types\s*=\s*1\s*\)\s*;' "$file"; then
[ -n "$DEBUG" ] && echo "Declaration not found in: $file"
# If not, prepend the strict_types declaration
{
echo '<?php declare( strict_types = 1 ); ?>'
cat "$file"
} > "$file.tmp" && mv "$file.tmp" "$file"
else
[ -n "$DEBUG" ] && echo "Declaration found in: $file"
fi
done
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"deploy:theme": "node ./theme-utils.mjs deploy-theme",
"deploy:zip": "node ./theme-utils.mjs build-com-zip",
"deploy:land": "node ./theme-utils.mjs land-diff",
"deploy:add-strict-typing": "node ./theme-utils.mjs add-strict-typing",
"pull:all": "node ./theme-utils.mjs pull-all-themes",
"core:pull": "node ./theme-utils.mjs pull-core-themes",
"core:push": "node ./theme-utils.mjs push-core-themes",
Expand Down
25 changes: 22 additions & 3 deletions theme-utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ const commands = {
additionalArgs: '<array of theme slugs>',
run: (args) => deployThemes(args?.[1].split(/[ ,]+/))
},
"add-strict-typing": {
helpText: 'Adds strict typing to any changed themes.',
run: () => addStrictTypesToChangedThemes()
},
"build-com-zip": {
helpText: 'Build the production zip file for the specified theme.',
additionalArgs: '<theme-slug>',
Expand Down Expand Up @@ -205,6 +209,20 @@ async function deployPreview() {
console.log(`\n\nCommit log of changes to be deployed:\n\n${logs}\n\n`);
}

async function addStrictTypesToChangedThemes() {
let hash = await getLastDeployedHash();
const changedThemes = await getChangedThemes(hash);

for (let theme of changedThemes) {
await executeCommand(`
bash -c "./add-strict-types.sh ${theme}"
`, true)
.catch((err) => {
console.log(`Error adding strict types to ${theme}: ${err}`);
});
}
}

/*
Execute the first phase of a deployment.
* Gets the last deployed hash from the sandbox
Expand Down Expand Up @@ -240,8 +258,9 @@ async function pushButtonDeploy() {
try {
await cleanSandbox();

let hash = await getLastDeployedHash();
let thingsWentBump = await versionBumpThemes();
const hash = await getLastDeployedHash();
await addStrictTypesToChangedThemes();
const thingsWentBump = await versionBumpThemes();

if (thingsWentBump) {
prompt = await inquirer.prompt([{
Expand All @@ -257,7 +276,7 @@ async function pushButtonDeploy() {
}
}

let changedThemes = await getChangedThemes(hash);
const changedThemes = await getChangedThemes(hash);

if (!changedThemes.length) {
console.log(`\n\nEverything is upto date. Nothing new to deploy.\n\n`);
Expand Down
Loading