Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 08:30:07 +08:00
commit d6f6fcbaad
33 changed files with 4697 additions and 0 deletions

View File

@@ -0,0 +1,101 @@
# Gem Scaffolder Scripts
Executable shell scripts for creating and managing Ruby gems.
## Scripts
### create_gem.sh
Create a new Ruby gem with best practices.
```bash
# Interactive (uses defaults)
./create_gem.sh my_gem
# With RSpec and MIT license
./create_gem.sh my_gem --test=rspec --mit
# With Minitest and Apache license
./create_gem.sh my_gem --test=minitest --apache
# With GitHub Actions CI
./create_gem.sh my_gem --ci=github
# With Code of Conduct
./create_gem.sh my_gem --coc
# All options
./create_gem.sh my_gem --test=rspec --mit --ci=github --coc
```
**Options:**
- `--test=rspec|minitest` - Test framework (default: rspec)
- `--mit|--apache|--gpl` - License (default: mit)
- `--ci=github|circle` - CI provider
- `--coc` - Add Code of Conduct
**Creates:**
```
my_gem/
├── .github/workflows/ci.yml
├── lib/
│ ├── my_gem/version.rb
│ └── my_gem.rb
├── spec/
├── Gemfile
├── Rakefile
└── my_gem.gemspec
```
### add_gem_class.sh
Add a new class to an existing gem with test.
```bash
./add_gem_class.sh my_gem Parser
```
**Creates:**
- `lib/my_gem/parser.rb` - Class file with template
- `spec/my_gem/parser_spec.rb` - RSpec test template
**Features:**
- Follows frozen_string_literal convention
- Proper module namespacing
- Basic class structure with private attr_reader
- Matching spec file with describe blocks
- Reminder to require in main file
## Example Workflow
```bash
# 1. Create new gem
./create_gem.sh data_parser --test=rspec --mit --ci=github
# 2. Navigate to gem
cd data_parser
# 3. Add a Parser class
../add_gem_class.sh data_parser Parser
# 4. Add a Formatter class
../add_gem_class.sh data_parser Formatter
# 5. Install dependencies
bundle install
# 6. Run tests
bundle exec rake spec
```
## Templates
Both scripts use POODR-compliant templates:
- Dependency injection via initialize
- Private attr_readers
- Clear public/private interfaces
- Frozen string literals
- Proper RSpec structure
## Requirements
- bundler gem installed
- git (for gem creation)

View File

@@ -0,0 +1,61 @@
#!/usr/bin/env bash
# Add a new class to an existing gem
set -e
if [ $# -lt 2 ]; then
echo "Usage: $0 <gem_name> <class_name>"
echo "Example: $0 my_gem Parser"
exit 1
fi
GEM_NAME=$1
CLASS_NAME=$2
FILE_NAME=$(echo "$CLASS_NAME" | sed 's/\([A-Z]\)/_\L\1/g;s/^_//')
echo "📝 Adding class $CLASS_NAME to $GEM_NAME gem..."
# Create class file
cat > "lib/${GEM_NAME}/${FILE_NAME}.rb" << RUBY
# frozen_string_literal: true
module $(echo "$GEM_NAME" | sed 's/_\([a-z]\)/\U\1/g;s/^./\U&/')
class $CLASS_NAME
def initialize(input)
@input = input
end
def process
# Implementation here
end
private
attr_reader :input
end
end
RUBY
# Create spec file
mkdir -p "spec/${GEM_NAME}"
cat > "spec/${GEM_NAME}/${FILE_NAME}_spec.rb" << RUBY
# frozen_string_literal: true
RSpec.describe $(echo "$GEM_NAME" | sed 's/_\([a-z]\)/\U\1/g;s/^./\U&/')::$CLASS_NAME do
describe '#process' do
it 'processes input correctly' do
instance = described_class.new("test_input")
result = instance.process
expect(result).to be_nil # Update with actual expectation
end
end
end
RUBY
echo "✅ Created files:"
echo " - lib/${GEM_NAME}/${FILE_NAME}.rb"
echo " - spec/${GEM_NAME}/${FILE_NAME}_spec.rb"
echo ""
echo "Don't forget to require it in lib/${GEM_NAME}.rb:"
echo " require_relative \"${GEM_NAME}/${FILE_NAME}\""

View File

@@ -0,0 +1,84 @@
#!/usr/bin/env bash
# Create a new Ruby gem with best practices
set -e
if [ $# -eq 0 ]; then
echo "Usage: $0 <gem_name> [options]"
echo ""
echo "Options:"
echo " --test=rspec|minitest (default: rspec)"
echo " --mit|--apache|--gpl"
echo " --ci=github|circle"
echo " --coc (add Code of Conduct)"
exit 1
fi
GEM_NAME=$1
shift
TEST_FRAMEWORK="rspec"
LICENSE="mit"
CI=""
COC=""
# Parse options
for arg in "$@"; do
case $arg in
--test=*)
TEST_FRAMEWORK="${arg#*=}"
;;
--mit)
LICENSE="mit"
;;
--apache)
LICENSE="apache"
;;
--gpl)
LICENSE="gpl-3"
;;
--ci=*)
CI="--ci=${arg#*=}"
;;
--coc)
COC="--coc"
;;
esac
done
echo "🔨 Creating gem: $GEM_NAME"
echo " Test framework: $TEST_FRAMEWORK"
echo " License: $LICENSE"
echo ""
# Check if bundler is installed
if ! command -v bundle &> /dev/null; then
echo "Installing bundler..."
gem install bundler
fi
# Create gem
bundle gem "$GEM_NAME" \
--test="$TEST_FRAMEWORK" \
--"$LICENSE" \
$CI \
$COC
cd "$GEM_NAME"
echo ""
echo "✅ Gem created successfully!"
echo ""
echo "Structure:"
tree -L 2 -I 'vendor|tmp' || ls -R
echo ""
echo "Next steps:"
echo " cd $GEM_NAME"
echo " bundle install"
echo " bundle exec rake spec"
echo ""
echo "Edit these files:"
echo " - ${GEM_NAME}.gemspec (add description)"
echo " - lib/${GEM_NAME}.rb (add your code)"
echo " - spec/${GEM_NAME}_spec.rb (add tests)"