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

feat: env-doctor script #10078

Merged
merged 2 commits into from
Sep 17, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"docs:markdown-for-agoric-documentation-repo": "run-s docs:markdown-build 'docs:update-functions-path md'",
"docs:markdown-build": "typedoc --plugin typedoc-plugin-markdown --tsconfig tsconfig.build.json",
"docs:update-functions-path": "node ./scripts/update-typedoc-functions-path.cjs",
"doctor": "./scripts/env-doctor.sh",
"lerna": "lerna",
"link-cli": "yarn run create-agoric-cli",
"create-agoric-cli": "node ./scripts/create-agoric-cli.cjs",
Expand Down
128 changes: 128 additions & 0 deletions scripts/env-doctor.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low priority:

ChatGPT suggested:

command -v brew >/dev/null 2>&1 || { echo "brew is required but not installed. Exiting."; exit 1; }
command -v yarn >/dev/null 2>&1 || { echo "yarn is required but not installed. Exiting."; exit 1; }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChatGPT also suggests:

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
  # bail with error
fi


set -e

# Check if running on macOS
if [[ "$(uname)" != "Darwin" ]]; then
echo "Error: This script only works on macOS."
echo "Your current operating system is: $(uname)"
exit 1
fi
Comment on lines +5 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this MacOS only? A decent chunk of us are on Linux

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because some remedies use Homebrew. Also Linux uses are less likely to need it. ;-) But feel free to improve the script.


# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to run a diagnostic and its remedy
run_diagnostic() {
local description="$1"
local diagnostic="$2"
local remedy_description="$3"
local remedy="$4"

echo -e "${YELLOW}Diagnostic:${NC} $description"
if eval "$diagnostic"; then
echo -e "${GREEN}✓ Passed${NC}"
else
echo -e "${RED}✗ Failed${NC}"
echo -e "${YELLOW}Remedy:${NC} $remedy_description"
if [[ $AUTO_YES != "true" ]]; then
read -p "Do you want to apply this remedy? (Yes/No/All) " answer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should explicitly initialize AUTO_YES with false at the start of the script?

case ${answer:0:1} in
y | Y) ;;
a | A) AUTO_YES=true ;;
*) return ;;
esac
fi
if ! eval "$remedy"; then
echo -e "${RED}Remedy failed. Please seek more help.${NC}"
exit 1
fi
echo "Remedy applied."
fi
echo
}

# Diagnostics and remedies
run_diagnostic \
"Check if fnm is installed" \
"command -v fnm >/dev/null 2>&1" \
"Install fnm" \
"brew install fnm"

run_diagnostic \
"Check if Node.js is installed" \
"command -v node >/dev/null 2>&1" \
"Install Node.js using fnm" \
"fnm install --lts && fnm use lts-latest"

run_diagnostic \
"Check if Node.js version matches .node-version" \
"[ \"$(node --version)\" = \"v$(cat .node-version)\" ]" \
"Install the correct Node.js version using fnm" \
"fnm install $(cat .node-version) && fnm use $(cat .node-version)"

run_diagnostic \
"Check if Yarn version matches package.json" \
"[ \"$(yarn --version)\" = \"$(node -p "require('./package.json').packageManager.split('@')[1]")\" ]" \
"Install correct Yarn version" \
"corepack enable && yarn set version $(node -p "require('./package.json').packageManager.split('@')[1]")"

run_diagnostic \
"Check if Git is installed" \
"command -v git >/dev/null 2>&1" \
"Install Git" \
"brew install git"

run_diagnostic \
"Check if VSCode is installed" \
"command -v code >/dev/null 2>&1" \
"Install Visual Studio Code" \
"brew install --cask visual-studio-code"

run_diagnostic \
"Check if Docker is installed" \
"command -v docker >/dev/null 2>&1" \
"Install Docker" \
"brew install --cask docker"

run_diagnostic \
"Build repo" \
"yarn build" \
"Clean, reinstall dependencies, and rebuild" \
"git clean -fdx "**/node_modules" "**/bundles" && yarn install && yarn build"
Copy link
Member

@mhofman mhofman Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know glob patterns were supported by git cli, but that makes sense!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

learned this from @kriskowal

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently globs are not working for me. Where can I find info on how it's supposed to work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I found that globs may not be enabled by default. Also I think that glob pattern doesn't actually work. What worked for me (more aggressive version with more things cleaned):

-- ':(glob)**/node_modules/**' ':(glob)**/bundles/**' ':(glob)**/dist/**' ':(glob)**/build/**'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I tend to do bin/agd build these days instead as that also builds the golang stuff for those of use who care. However it has its own "download deps" logic which may not be desirable here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also try using exclusions instead of inclusions. But I'll leave this as an exercise for whoever runs into the problem next. This is meant to be a crowdsourced solution set

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inclusions is probably safer. It's just that I don't understand how this line even works given the quoting, and that glob patterns are not enabled by default.


# Function to run a recommendation
run_recommendation() {
local description="$1"
local recommendation="$2"

echo -e "${BLUE}Recommendation:${NC} $description"
if [[ $AUTO_YES != "true" ]]; then
read -p "Do you want to apply this recommendation? (Yes/No/All) " answer
case ${answer:0:1} in
y | Y) ;;
a | A) AUTO_YES=true ;;
*) return ;;
esac
fi
eval "$recommendation"
echo "Recommendation applied."
echo
}

echo -e "${GREEN}Environment check complete.${NC}"

# Recommendations
run_recommendation \
"Configure VS Code with recommended settings" \
"./scripts/configure-vscode.sh"

run_recommendation \
"Run yarn doctor in a3p-integration" \
"cd a3p-integration && yarn doctor && cd .."

echo -e "${GREEN}All checks and recommendations complete.${NC}"
Loading