diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..6f28522 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,20 @@ +version: 2 +updates: + - package-ecosystem: bundler + directory: "/" + schedule: + interval: monthly + time: "06:00" + timezone: America/Los_Angeles + open-pull-requests-limit: 10 + labels: + - "๐Ÿ  Housekeeping" + - package-ecosystem: github-actions + directory: "/" + schedule: + interval: monthly + time: "06:00" + timezone: America/Los_Angeles + open-pull-requests-limit: 10 + labels: + - "๐Ÿ  Housekeeping" diff --git a/.github/release-drafter.yml b/.github/release-drafter.yml new file mode 100644 index 0000000..07fafcd --- /dev/null +++ b/.github/release-drafter.yml @@ -0,0 +1,25 @@ +name-template: "$RESOLVED_VERSION" +tag-template: "v$RESOLVED_VERSION" +categories: + - title: "โš ๏ธ Breaking Changes" + label: "โš ๏ธ Breaking" + - title: "โœจ New Features" + label: "โœจ Feature" + - title: "๐Ÿ› Bug Fixes" + label: "๐Ÿ› Bug Fix" + - title: "๐Ÿ“š Documentation" + label: "๐Ÿ“š Docs" + - title: "๐Ÿ  Housekeeping" + label: "๐Ÿ  Housekeeping" +version-resolver: + minor: + labels: + - "โš ๏ธ Breaking" + - "โœจ Feature" + default: patch +change-template: "- $TITLE (#$NUMBER) @$AUTHOR" +no-changes-template: "- No changes" +template: | + $CHANGES + + **Full Changelog:** https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...v$RESOLVED_VERSION diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..6d0c9aa --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,30 @@ +name: CI +on: + pull_request: + push: + branches: + - main +jobs: + rubocop: + name: "Rubocop" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: "ruby" + bundler-cache: true + - run: bundle exec rubocop + test: + name: "Test / Ruby ${{ matrix.ruby }}" + runs-on: ubuntu-latest + strategy: + matrix: + ruby: ["3.0", "3.1", "3.2", "head"] + steps: + - uses: actions/checkout@v4 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - run: bundle exec rake test diff --git a/.github/workflows/release-drafter.yml.dist b/.github/workflows/release-drafter.yml.dist new file mode 100644 index 0000000..896edaf --- /dev/null +++ b/.github/workflows/release-drafter.yml.dist @@ -0,0 +1,14 @@ +name: Release Drafter + +on: + push: + branches: + - main + +jobs: + update_release_draft: + runs-on: ubuntu-latest + steps: + - uses: release-drafter/release-drafter@v5 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e0f123 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/site/ +/spec/reports/ +/tmp/ +/Gemfile.lock diff --git a/.kodiak.toml b/.kodiak.toml new file mode 100644 index 0000000..58e87f6 --- /dev/null +++ b/.kodiak.toml @@ -0,0 +1,13 @@ +# .kodiak.toml +# Minimal config. version is the only required field. +version = 1 + +[merge.automerge_dependencies] +# auto merge all PRs opened by "dependabot" that are "minor" or "patch" version upgrades. "major" version upgrades will be ignored. +versions = ["minor", "patch"] +usernames = ["dependabot"] + +# if using `update.always`, add dependabot to `update.ignore_usernames` to allow +# dependabot to update and close stale dependency upgrades. +[update] +ignored_usernames = ["dependabot"] diff --git a/.overcommit.yml b/.overcommit.yml new file mode 100644 index 0000000..1a8645e --- /dev/null +++ b/.overcommit.yml @@ -0,0 +1,32 @@ +# Overcommit hooks run automatically on certain git operations, like "git commit". +# For a complete list of options that you can use to customize hooks, see: +# https://github.com/sds/overcommit + +gemfile: false +verify_signatures: false + +PreCommit: + BundleCheck: + enabled: true + + FixMe: + enabled: true + keywords: ["FIXME"] + exclude: + - .overcommit.yml + + LocalPathsInGemfile: + enabled: true + + RuboCop: + enabled: true + required_executable: bundle + command: ["bundle", "exec", "rubocop"] + on_warn: fail + + YamlSyntax: + enabled: true + +PostCheckout: + ALL: + quiet: true diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..6aec58b --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +/CODE_OF_CONDUCT.md diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..60d3d28 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,110 @@ +require: + - rubocop-minitest + - rubocop-packaging + - rubocop-performance + - rubocop-rake + +AllCops: + DisplayCopNames: true + DisplayStyleGuide: true + NewCops: enable + TargetRubyVersion: 3.0 + Exclude: + - "tmp/**/*" + - "vendor/**/*" + +Layout/FirstArrayElementIndentation: + EnforcedStyle: consistent + +Layout/FirstArrayElementLineBreak: + Enabled: true + +Layout/FirstHashElementLineBreak: + Enabled: true + +Layout/FirstMethodArgumentLineBreak: + Enabled: true + +Layout/HashAlignment: + EnforcedColonStyle: + - table + - key + EnforcedHashRocketStyle: + - table + - key + +Layout/MultilineArrayLineBreaks: + Enabled: true + +Layout/MultilineHashKeyLineBreaks: + Enabled: true + +Layout/MultilineMethodArgumentLineBreaks: + Enabled: true + +Layout/MultilineMethodCallIndentation: + EnforcedStyle: indented + +Layout/SpaceAroundEqualsInParameterDefault: + EnforcedStyle: no_space + +Metrics/AbcSize: + Max: 20 + Exclude: + - "test/**/*" + +Metrics/BlockLength: + Exclude: + - "*.gemspec" + - "Rakefile" + +Metrics/ClassLength: + Exclude: + - "test/**/*" + +Metrics/MethodLength: + Max: 18 + Exclude: + - "test/**/*" + +Metrics/ParameterLists: + Max: 6 + +Minitest/EmptyLineBeforeAssertionMethods: + Enabled: false + +Naming/MemoizedInstanceVariableName: + Enabled: false + +Naming/VariableNumber: + Enabled: false + +Rake/Desc: + Enabled: false + +Style/BarePercentLiterals: + EnforcedStyle: percent_q + +Style/ClassAndModuleChildren: + Enabled: false + +Style/Documentation: + Enabled: false + +Style/DoubleNegation: + Enabled: false + +Style/EmptyMethod: + Enabled: false + +Style/FrozenStringLiteralComment: + Enabled: false + +Style/NumericPredicate: + Enabled: false + +Style/StringLiterals: + EnforcedStyle: double_quotes + +Style/TrivialAccessors: + AllowPredicates: true diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5ada4d1 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1 @@ +Release notes for this project are kept here: https://github.com/mattbrictson/gem/releases diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..ec4c262 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,84 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at owner@example.com. All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of actions. + +**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, +available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..dc4450c --- /dev/null +++ b/Gemfile @@ -0,0 +1,11 @@ +source "https://rubygems.org" +gemspec + +gem "minitest", "~> 5.11" +gem "minitest-reporters", "~> 1.3" +gem "rake", "~> 13.0" +gem "rubocop", "1.56.4" +gem "rubocop-minitest", "0.32.2" +gem "rubocop-packaging", "0.5.2" +gem "rubocop-performance", "1.19.1" +gem "rubocop-rake", "0.6.0" diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..e396b62 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2023 Example Owner + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..71ce29b --- /dev/null +++ b/README.md @@ -0,0 +1,66 @@ +# gem template + +This is a GitHub template for creating Ruby gems. Press [**Use this template**](https://github.com/mattbrictson/gem/generate) to generate a project from this template. In the generated project, run this script to rename the gem to meet your needs: + +``` +ruby rename_template.rb +``` + +Note that to get the full benefits of the script, you will need the [gh](https://github.com/cli/cli) command installed. + +This template is based on `bundle gem` with some notable improvements: + +- GitHub Actions configuration +- Minitest, with minitest-reporters for nicely formatted test output +- Rubocop with a good set of configuration +- CLI scaffolding, built on top of Thor (optional; for more background, read [Fixing Thorโ€™s CLI Quirks](https://mattbrictson.com/blog/fixing-thor-cli-behavior)) +- [release-drafter](https://github.com/apps/release-drafter) GitHub Action for automating release notes +- A `rake bump` task to keep your Ruby and Bundler dependencies up to date +- A nice README with badges ready to go (see below) + +--- + + + +# example + +[![Gem Version](https://img.shields.io/gem/v/replace_with_gem_name)](https://rubygems.org/gems/replace_with_gem_name) +[![Gem Downloads](https://img.shields.io/gem/dt/replace_with_gem_name)](https://www.ruby-toolbox.com/projects/replace_with_gem_name) +[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mattbrictson/gem/ci.yml)](https://github.com/mattbrictson/gem/actions/workflows/ci.yml) +[![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/mattbrictson/gem)](https://codeclimate.com/github/mattbrictson/gem) + +TODO: Description of this gem goes here. + +--- + +- [Quick start](#quick-start) +- [Support](#support) +- [License](#license) +- [Code of conduct](#code-of-conduct) +- [Contribution guide](#contribution-guide) + +## Quick start + +``` +gem install example +``` + +```ruby +require "example" +``` + +## Support + +If you want to report a bug, or have ideas, feedback or questions about the gem, [let me know via GitHub issues](https://github.com/mattbrictson/gem/issues/new) and I will do my best to provide a helpful answer. Happy hacking! + +## License + +The gem is available as open source under the terms of the [MIT License](LICENSE.txt). + +## Code of conduct + +Everyone interacting in this projectโ€™s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md). + +## Contribution guide + +Pull requests are welcome! diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..4c38ec6 --- /dev/null +++ b/Rakefile @@ -0,0 +1,108 @@ +require "bundler/gem_tasks" +require "rake/testtask" +require "rubocop/rake_task" + +Rake::TestTask.new(:test) do |t| + t.libs << "test" + t.libs << "lib" + t.test_files = FileList["test/**/*_test.rb"] +end + +RuboCop::RakeTask.new + +task default: %i[test rubocop] + +# == "rake release" enhancements ============================================== + +Rake::Task["release"].enhance do + puts "Don't forget to publish the release on GitHub!" + system "open https://github.com/mattbrictson/gem/releases" +end + +task :disable_overcommit do + ENV["OVERCOMMIT_DISABLE"] = "1" +end + +Rake::Task[:build].enhance [:disable_overcommit] + +task :verify_gemspec_files do + git_files = `git ls-files -z`.split("\x0") + gemspec_files = Gem::Specification.load("example.gemspec").files.sort + ignored_by_git = gemspec_files - git_files + next if ignored_by_git.empty? + + raise <<~ERROR + + The `spec.files` specified in example.gemspec include the following files + that are being ignored by git. Did you forget to add them to the repo? If + not, you may need to delete these files or modify the gemspec to ensure + that they are not included in the gem by mistake: + + #{ignored_by_git.join("\n").gsub(/^/, ' ')} + + ERROR +end + +Rake::Task[:build].enhance [:verify_gemspec_files] + +# == "rake bump" tasks ======================================================== + +task bump: %w[bump:bundler bump:ruby bump:year] + +namespace :bump do + task :bundler do + version = Gem.latest_version_for("bundler").to_s + replace_in_file "Gemfile.lock", /^BUNDLED WITH\n\s+(\d\S+)$/ => version + end + + task :ruby do + replace_in_file "example.gemspec", /ruby_version = .*">= (.*)"/ => RubyVersions.lowest + replace_in_file ".rubocop.yml", /TargetRubyVersion: (.*)/ => RubyVersions.lowest + replace_in_file ".github/workflows/ci.yml", /ruby: (\[.+\])/ => RubyVersions.all.inspect + end + + task :year do + replace_in_file "LICENSE.txt", /\(c\) (\d+)/ => Date.today.year.to_s + end +end + +require "date" +require "open-uri" +require "yaml" + +def replace_in_file(path, replacements) + contents = File.read(path) + orig_contents = contents.dup + replacements.each do |regexp, text| + raise "Can't find #{regexp} in #{path}" unless regexp.match?(contents) + + contents.gsub!(regexp) do |match| + match[regexp, 1] = text + match + end + end + File.write(path, contents) if contents != orig_contents +end + +module RubyVersions + class << self + def lowest + all.first + end + + def all + patches = versions.values_at(:stable, :security_maintenance).compact.flatten + sorted_minor_versions = patches.map { |p| p[/\d+\.\d+/] }.sort_by(&:to_f) + [*sorted_minor_versions, "head"] + end + + private + + def versions + @_versions ||= begin + yaml = URI.open("https://raw.githubusercontent.com/ruby/www.ruby-lang.org/HEAD/_data/downloads.yml") + YAML.safe_load(yaml, symbolize_names: true) + end + end + end +end diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..7299789 --- /dev/null +++ b/bin/console @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +require "bundler/setup" +require "example" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# (If you use this, don't forget to add pry to your Gemfile!) +# require "pry" +# Pry.start + +require "irb" +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..76e4080 --- /dev/null +++ b/bin/setup @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +which overcommit > /dev/null 2>&1 && overcommit --install +bundle install + +# Do any other automated setup that you need to do here diff --git a/example.gemspec b/example.gemspec new file mode 100644 index 0000000..742b20a --- /dev/null +++ b/example.gemspec @@ -0,0 +1,30 @@ +require_relative "lib/example/version" + +Gem::Specification.new do |spec| + spec.name = "example" + spec.version = Example::VERSION + spec.authors = ["Example Owner"] + spec.email = ["owner@example.com"] + + spec.summary = "" + spec.homepage = "https://github.com/mattbrictson/gem" + spec.license = "MIT" + spec.required_ruby_version = ">= 3.0" + + spec.metadata = { + "bug_tracker_uri" => "https://github.com/mattbrictson/gem/issues", + "changelog_uri" => "https://github.com/mattbrictson/gem/releases", + "source_code_uri" => "https://github.com/mattbrictson/gem", + "homepage_uri" => spec.homepage, + "rubygems_mfa_required" => "true" + } + + # Specify which files should be added to the gem when it is released. + spec.files = Dir.glob(%w[LICENSE.txt README.md {exe,lib}/**/*]).reject { |f| File.directory?(f) } + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + # Runtime dependencies + spec.add_dependency "thor", "~> 1.2" +end diff --git a/exe/example b/exe/example new file mode 100755 index 0000000..0f6248f --- /dev/null +++ b/exe/example @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby + +require "example" +Example::CLI.start(ARGV) diff --git a/lib/example.rb b/lib/example.rb new file mode 100644 index 0000000..65de3ca --- /dev/null +++ b/lib/example.rb @@ -0,0 +1,5 @@ +module Example + autoload :CLI, "example/cli" + autoload :VERSION, "example/version" + autoload :ThorExt, "example/thor_ext" +end diff --git a/lib/example/cli.rb b/lib/example/cli.rb new file mode 100644 index 0000000..399f45e --- /dev/null +++ b/lib/example/cli.rb @@ -0,0 +1,14 @@ +require "thor" + +module Example + class CLI < Thor + extend ThorExt::Start + + map %w[-v --version] => "version" + + desc "version", "Display example version", hide: true + def version + say "example/#{VERSION} #{RUBY_DESCRIPTION}" + end + end +end diff --git a/lib/example/thor_ext.rb b/lib/example/thor_ext.rb new file mode 100644 index 0000000..9d045e9 --- /dev/null +++ b/lib/example/thor_ext.rb @@ -0,0 +1,69 @@ +module Example + module ThorExt + # Configures Thor to behave more like a typical CLI, with better help and error handling. + # + # - Passing -h or --help to a command will show help for that command. + # - Unrecognized options will be treated as errors (instead of being silently ignored). + # - Error messages will be printed in red to stderr, without stack trace. + # - Full stack traces can be enabled by setting the VERBOSE environment variable. + # - Errors will cause Thor to exit with a non-zero status. + # + # To take advantage of this behavior, your CLI should subclass Thor and extend this module. + # + # class CLI < Thor + # extend ThorExt::Start + # end + # + # Start your CLI with: + # + # CLI.start + # + # In tests, prevent Kernel.exit from being called when an error occurs, like this: + # + # CLI.start(args, exit_on_failure: false) + # + module Start + def self.extended(base) + super + base.check_unknown_options! + end + + def start(given_args=ARGV, config={}) + config[:shell] ||= Thor::Base.shell.new + handle_help_switches(given_args) do |args| + dispatch(nil, args, nil, config) + end + rescue StandardError => e + handle_exception_on_start(e, config) + end + + private + + def handle_help_switches(given_args) + yield(given_args.dup) + rescue Thor::UnknownArgumentError => e + retry_with_args = [] + + if given_args.first == "help" + retry_with_args = ["help"] if given_args.length > 1 + elsif (e.unknown & %w[-h --help]).any? + retry_with_args = ["help", (given_args - e.unknown).first] + end + raise unless retry_with_args.any? + + yield(retry_with_args) + end + + def handle_exception_on_start(error, config) + return if error.is_a?(Errno::EPIPE) + raise if ENV["VERBOSE"] || !config.fetch(:exit_on_failure, true) + + message = error.message.to_s + message.prepend("[#{error.class}] ") if message.empty? || !error.is_a?(Thor::Error) + + config[:shell]&.say_error(message, :red) + exit(false) + end + end + end +end diff --git a/lib/example/version.rb b/lib/example/version.rb new file mode 100644 index 0000000..9d0fa24 --- /dev/null +++ b/lib/example/version.rb @@ -0,0 +1,3 @@ +module Example + VERSION = "0.1.0".freeze +end diff --git a/rename_template.rb b/rename_template.rb new file mode 100755 index 0000000..b22cb90 --- /dev/null +++ b/rename_template.rb @@ -0,0 +1,253 @@ +#!/usr/bin/env ruby + +require "bundler/inline" +require "fileutils" +require "io/console" +require "open3" +require "shellwords" + +def main # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + assert_git_repo! + git_meta = read_git_data + + gem_name = ask("Gem name?", default: git_meta[:origin_repo_name]) + gem_summary = ask("Gem summary (< 60 chars)?", default: "") + author_email = ask("Author email?", default: git_meta[:user_email]) + author_name = ask("Author name?", default: git_meta[:user_name]) + github_repo = ask("GitHub repository?", default: git_meta[:origin_repo_path]) + exe = ask_yes_or_no("Include an executable (CLI) in this gem?", default: "N") + + created_labels = \ + if gh_present? + puts + puts "I would like to use the `gh` executable to create labels in your repo." + puts "These labels will be used to generate nice-looking release notes." + puts + if ask_yes_or_no("Create GitHub labels using `gh`?", default: "Y") + create_labels(github_repo) + true + end + end + + replace_in_file(".github/dependabot.yml", /\s+labels:\n\s+-.*$/ => "") unless created_labels + + git "mv", ".github/workflows/release-drafter.yml.dist", ".github/workflows/release-drafter.yml" + + FileUtils.mkdir_p "lib/#{as_path(gem_name)}" + FileUtils.mkdir_p "test/#{as_path(gem_name)}" + + ensure_executable "bin/console" + ensure_executable "bin/setup" + + if exe + replace_in_file "exe/example", + "example" => as_path(gem_name), + "Example" => as_module(gem_name) + + git "mv", "exe/example", "exe/#{gem_name}" + ensure_executable "exe/#{gem_name}" + + replace_in_file "lib/example/cli.rb", + "example" => as_path(gem_name), + "Example" => as_module(gem_name) + + git "mv", "lib/example/cli.rb", "lib/#{as_path(gem_name)}/cli.rb" + reindent_module "lib/#{as_path(gem_name)}/cli.rb" + + replace_in_file "lib/example/thor_ext.rb", "Example" => as_module(gem_name) + git "mv", "lib/example/thor_ext.rb", "lib/#{as_path(gem_name)}/thor_ext.rb" + reindent_module "lib/#{as_path(gem_name)}/thor_ext.rb" + else + git "rm", "exe/example", "lib/example/cli.rb", "lib/example/thor_ext.rb" + replace_in_file "example.gemspec", 'spec.add_dependency "thor"' => '# spec.add_dependency "thor"' + remove_line "lib/example.rb", /autoload :ThorExt/ + remove_line "lib/example.rb", /autoload :CLI/ + end + + replace_in_file "LICENSE.txt", + "Example Owner" => author_name + + replace_in_file "Rakefile", + "example.gemspec" => "#{gem_name}.gemspec", + "mattbrictson/gem" => github_repo + + replace_in_file "README.md", + "mattbrictson/gem" => github_repo, + 'require "example"' => %Q(require "#{as_path(gem_name)}"), + "example" => gem_name, + "replace_with_gem_name" => gem_name, + /\A.*\n+/m => "" + + replace_in_file "CHANGELOG.md", + "mattbrictson/gem" => github_repo + + replace_in_file "CODE_OF_CONDUCT.md", + "owner@example.com" => author_email + + replace_in_file "bin/console", + 'require "example"' => %Q(require "#{as_path(gem_name)}") + + replace_in_file "example.gemspec", + "mattbrictson/gem" => github_repo, + '"Example Owner"' => author_name.inspect, + '"owner@example.com"' => author_email.inspect, + '"example"' => gem_name.inspect, + "example/version" => "#{as_path(gem_name)}/version", + "Example::VERSION" => "#{as_module(gem_name)}::VERSION", + /summary\s*=\s*("")/ => gem_summary.inspect + + git "mv", "example.gemspec", "#{gem_name}.gemspec" + + replace_in_file "lib/example.rb", + "example" => as_path(gem_name), + "Example" => as_module(gem_name) + + git "mv", "lib/example.rb", "lib/#{as_path(gem_name)}.rb" + reindent_module "lib/#{as_path(gem_name)}.rb" + + replace_in_file "lib/example/version.rb", + "Example" => as_module(gem_name) + + git "mv", "lib/example/version.rb", "lib/#{as_path(gem_name)}/version.rb" + reindent_module "lib/#{as_path(gem_name)}/version.rb" + + replace_in_file "test/example_test.rb", + "Example" => as_module(gem_name) + + git "mv", "test/example_test.rb", "test/#{as_path(gem_name)}_test.rb" + + replace_in_file "test/test_helper.rb", + 'require "example"' => %Q(require "#{as_path(gem_name)}") + + git "rm", "rename_template.rb" + Dir.unlink("lib/example") if Dir.empty?("lib/example") + + puts <<~MESSAGE + + All set! + + The project has been renamed from "example" to "#{gem_name}". + Review the changes and then run: + + git commit && git push + + MESSAGE +end + +def assert_git_repo! + return if File.file?(".git/config") + + warn("This doesn't appear to be a git repo. Can't continue. :(") + exit(1) +end + +def git(*args) + sh! "git", *args +end + +def ensure_executable(path) + return if File.executable?(path) + + FileUtils.chmod 0o755, path + git "add", path +end + +def sh!(*args) + puts ">>>> #{args.join(' ')}" + stdout, status = Open3.capture2(*args) + raise("Failed to execute: #{args.join(' ')}") unless status.success? + + stdout +end + +def remove_line(file, pattern) + text = File.read(file) + text = text.lines.filter.grep_v(pattern).join + File.write(file, text) + git "add", file +end + +def ask(question, default: nil, echo: true) + prompt = "#{question} " + prompt << "[#{default}] " unless default.nil? + print prompt + answer = if echo + $stdin.gets.chomp + else + $stdin.noecho(&:gets).tap { $stdout.print "\n" }.chomp + end + answer.to_s.strip.empty? ? default : answer +end + +def ask_yes_or_no(question, default: "N") + default = default == "Y" ? "Y/n" : "y/N" + answer = ask(question, default: default) + + answer != "y/N" && answer.match?(/^y/i) +end + +def read_git_data + return {} unless git("remote", "-v").match?(/^origin/) + + origin_url = git("remote", "get-url", "origin").chomp + origin_repo_path = origin_url[%r{[:/]([^/]+/[^/]+?)(?:\.git)?$}, 1] + + { + origin_repo_name: origin_repo_path&.split("/")&.last, + origin_repo_path: origin_repo_path, + user_email: git("config", "user.email").chomp, + user_name: git("config", "user.name").chomp + } +end + +def replace_in_file(path, replacements) + contents = File.read(path) + replacements.each do |regexp, text| + contents.gsub!(regexp) do |match| + next text if Regexp.last_match(1).nil? + + match[regexp, 1] = text + match + end + end + + File.write(path, contents) + git "add", path +end + +def as_path(gem_name) + gem_name.tr("-", "/") +end + +def as_module(gem_name) + parts = gem_name.split("-") + parts.map do |part| + part.gsub(/^[a-z]|_[a-z]/) { |str| str[-1].upcase } + end.join("::") +end + +def reindent_module(path) + contents = File.read(path) + namespace_mod = contents[/(?:module|class) (\S+)/, 1] + return unless namespace_mod.include?("::") + + contents.sub!(namespace_mod, namespace_mod.split("::").last) + namespace_mod.split("::")[0...-1].reverse_each do |mod| + contents = "module #{mod}\n#{contents.gsub(/^/, ' ')}end\n" + end + + File.write(path, contents) + git "add", path +end + +def gh_present? + system "gh label clone -h > /dev/null 2>&1" +rescue StandardError + false +end + +def create_labels(github_repo) + system "gh label clone mattbrictson/gem --repo #{github_repo.shellescape}" +end + +main if $PROGRAM_NAME == __FILE__ diff --git a/test/example_test.rb b/test/example_test.rb new file mode 100644 index 0000000..6d61ec2 --- /dev/null +++ b/test/example_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ExampleTest < Minitest::Test + def test_that_it_has_a_version_number + refute_nil ::Example::VERSION + end +end diff --git a/test/support/reporters.rb b/test/support/reporters.rb new file mode 100644 index 0000000..186482c --- /dev/null +++ b/test/support/reporters.rb @@ -0,0 +1,7 @@ +require "minitest/reporters" + +if ENV["CI"] + Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new) +else + Minitest::Reporters.use!(Minitest::Reporters::DefaultReporter.new) +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..e0c01e9 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,5 @@ +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) +require "example" + +require "minitest/autorun" +Dir[File.expand_path("support/**/*.rb", __dir__)].each { |rb| require(rb) }