Skip to content

Instantly share code, notes, and snippets.

@Sylvance
Last active January 7, 2023 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sylvance/5b6a51351052d2b973e07dda0863ef95 to your computer and use it in GitHub Desktop.
Save Sylvance/5b6a51351052d2b973e07dda0863ef95 to your computer and use it in GitHub Desktop.
Bash script to create a ruby gem
#!/usr/bin/env bash
### Usage
# Ensure your shell has access to your github account and rubygems account.
# Install github cli. In mac do;
# ```sh
# brew install gh
# gh auth login
# ```
# After downloading the script below run it with;
# ```sh
# chmod +x gem_create.sh
# ./gem_create.sh
# ```
s_gemspec_summary="TODO: Write a short summary, because RubyGems requires one."
s_gemspec_description="TODO: Write a longer description or delete this line."
s_gemspec_homepage="TODO: Put your gem's website or public repo URL here."
s_gemspec_allowed_push_host="TODO: Set to your gem server 'https://example.com'"
s_gemspec_source_code_uri="TODO: Put your gem's public repo URL here."
s_gemspec_changelog_uri="TODO: Put your gem's CHANGELOG.md URL here."
# Ask for user input
read -p "Enter gem's name: " gem_name
read -p "Enter github username: " r_github_username
read -p "Enter gem summary: " r_gemspec_summary
read -p "Enter gem description: " r_gemspec_description
r_gemspec_homepage="https://github.com/$r_github_username/$gem_name/blob/main/README.md"
r_gemspec_allowed_push_host="https://rubygems.org"
r_gemspec_source_code_uri="https://github.com/$r_github_username/$gem_name"
r_gemspec_changelog_uri="https://github.com/$r_github_username/$gem_name/blob/main/CHANGELOG.md"
gemspec_filename="$gem_name.gemspec"
git_uri="git@github.com:${r_github_username}/${gem_name}.git"
spec_filename="spec/${gem_name}_spec.rb"
# Create gem
echo "[$gem_name] Creating the gem."
bundle gem $gem_name
# Change into the gem directory
echo "[$gem_name] Changing into the gem directory."
cd $gem_name
# Replace stuff in the $gem_name.gemspec with usable stuff
echo "[$gem_name] Replacing gemspec summary."
if [[ $s_gemspec_summary != "" && $r_gemspec_summary != "" ]]; then
sed -i'' -e "s/$s_gemspec_summary/$r_gemspec_summary/" $gemspec_filename
fi
echo "[$gem_name] Replacing gemspec description."
if [[ $s_gemspec_description != "" && $r_gemspec_description != "" ]]; then
sed -i'' -e "s/$s_gemspec_description/$r_gemspec_description/" $gemspec_filename
fi
echo "[$gem_name] Replacing gemspec homepage."
if [[ $s_gemspec_homepage != "" && $r_gemspec_homepage != "" ]]; then
sed -i'' -e "s|${s_gemspec_homepage}|${r_gemspec_homepage}|" $gemspec_filename
fi
echo "[$gem_name] Replacing gemspec allowed push host."
if [[ $s_gemspec_allowed_push_host != "" && $r_gemspec_allowed_push_host != "" ]]; then
sed -i'' -e "s|${s_gemspec_allowed_push_host}|${r_gemspec_allowed_push_host}|" $gemspec_filename
fi
echo "[$gem_name] Replacing gemspec source code uri."
if [[ $s_gemspec_source_code_uri != "" && $r_gemspec_source_code_uri != "" ]]; then
sed -i'' -e "s|${s_gemspec_source_code_uri}|${r_gemspec_source_code_uri}|" $gemspec_filename
fi
echo "[$gem_name] Replacing gemspec changelog uri."
if [[ $s_gemspec_changelog_uri != "" && $r_gemspec_changelog_uri != "" ]]; then
sed -i'' -e "s|${s_gemspec_changelog_uri}|${r_gemspec_changelog_uri}|" $gemspec_filename
fi
rm -rf "${gemspec_filename}-e"
# Remove unused spec in spec/$1_spec.rb by deleting line 7-10
echo "[$gem_name] Removing unused spec."
ed -s ${spec_filename} <<EOF
7,10d
w
q
EOF
echo "[$gem_name] Installing bundler gems."
bundle install
echo "[$gem_name] Adding ruby platform gems."
bundle lock --add-platform ruby
echo "[$gem_name] Adding linux platform gems."
bundle lock --add-platform x86_64-linux
echo "[$gem_name] Adding mac platform gems."
bundle lock --add-platform arm64-darwin-20
# Run tests
echo "[$gem_name] Running the first test."
bundle exec rspec spec
echo "[$gem_name] Creating remote github repo."
gh repo create $gem_name --public
echo "[$gem_name] Pushing gem repo to github."
git add .
git commit -m "[$gem_name] Bundle initial gem files"
git remote add origin $git_uri
git branch -M main
git push -u origin main
echo "[$gem_name] Building gem and pushing to rubygems org."
bundle exec rake release
echo "[$gem_name] Check for any errors above."
echo "[$gem_name] Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment