diff --git a/.github/workflows/bump-version.yml b/.github/workflows/bump-version.yml index 359a8b6..e1cbdbb 100644 --- a/.github/workflows/bump-version.yml +++ b/.github/workflows/bump-version.yml @@ -25,7 +25,7 @@ permissions: jobs: bump_version: - name: Build + Publish + name: Generate Bump Version runs-on: ubuntu-latest steps: - name: Check out the repository @@ -44,16 +44,11 @@ jobs: run: | rake version:${{ inputs.bump_version }} - - name: Commit and push changes - run: | - git config --global user.name 'github-actions' - git config --global user.email 'github-actions@github.com' - git add lib/version.rb - git commit -m "Bump version to ${{ steps.bump_version.outputs.new_version }}" - git push - - - name: Create new tag - run: | - new_version=${{ steps.bump_version.outputs.new_version }} - git tag "v$new_version" - git push origin "v$new_version" + - name: Commit changes and create tag + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: Bump version to ${{ steps.bump_version.outputs.new_version }} + tagging_message: 'v${{steps.bump_version.outputs.new_version}}' + commit_user_name: Bump Version Workflow + file_pattern: 'lib/version.rb' + branch: main diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml new file mode 100644 index 0000000..888e808 --- /dev/null +++ b/.github/workflows/create-release.yml @@ -0,0 +1,41 @@ +name: Create MPUtils Release + +on: + workflow_dispatch: + inputs: + bump_version: + description: 'Define the version number to bump' + required: true + default: patch + type: choice + options: + - major + - minor + - patch + +permissions: + contents: write + packages: write + +jobs: + tests: + secrets: inherit + uses: ./.github/workflows/ruby-tests.yml + bump_version: + secrets: inherit + uses: ./.github/workflows/bump-version.yml + with: + bump_version: ${{ inputs.bump_version }} + publish_gem: + needs: bump_version + secrets: inherit + uses: ./.github/workflows/publish-gem.yml + documentation: + needs: publish_gem + secrets: inherit + uses: ./.github/workflows/create-doc-page.yml +# git_release: +# needs: publish_gem +# secrets: inherit +# uses: ./.github/workflows/create-git-release.yml + \ No newline at end of file diff --git a/.github/workflows/on-pull-request-close.yml b/.github/workflows/on-pull-request-close.yml deleted file mode 100644 index ce028b0..0000000 --- a/.github/workflows/on-pull-request-close.yml +++ /dev/null @@ -1,36 +0,0 @@ -name: Run On Pull Request Closed - -on: - pull_request: - types: - - closed - -permissions: - contents: write - packages: write - -jobs: - bump_major: - if: > - github.event.pull_request.merged == true && - contains(join(github.event.pull_request.labels.*.name, ','), 'version:major') - secrets: inherit - uses: ./.github/workflows/bump-version.yml - with: - bump_version: major - bump_minor: - if: > - github.event.pull_request.merged == true && - contains(join(github.event.pull_request.labels.*.name, ','), 'version:minor') - secrets: inherit - uses: ./.github/workflows/bump-version.yml - with: - bump_version: minor - bump_patch: - if: > - github.event.pull_request.merged == true && - contains(join(github.event.pull_request.labels.*.name, ','), 'version:patch') - secrets: inherit - uses: ./.github/workflows/bump-version.yml - with: - bump_version: patch \ No newline at end of file diff --git a/.github/workflows/publish-gem.yml b/.github/workflows/publish-gem.yml index 455065f..3d14ae1 100644 --- a/.github/workflows/publish-gem.yml +++ b/.github/workflows/publish-gem.yml @@ -1,4 +1,4 @@ -name: Create a release for the gem +name: Publish Gem in ruby Gems on: workflow_dispatch: diff --git a/Rakefile b/Rakefile index 304fa25..0f5f964 100644 --- a/Rakefile +++ b/Rakefile @@ -36,7 +36,7 @@ namespace :version do content.gsub!(/VERSION.+'#{MPUtils::VERSION}'/, "VERSION = '#{version}'") File.open(path, 'w') { |file| file << content } - system("echo \"::set-output name=new_version::#{version}\"") + system("echo \"{new_version}={#{version}}\" >> $GITHUB_OUTPUT") end end end diff --git a/lib/mp_utils.rb b/lib/mp_utils.rb index df5802a..22ac166 100644 --- a/lib/mp_utils.rb +++ b/lib/mp_utils.rb @@ -3,6 +3,7 @@ require_relative 'utils/key' require_relative 'utils/ansi' require_relative 'utils/array' +require_relative 'utils/string' require_relative 'utils/message' require_relative 'utils/question' require_relative 'utils/version_manager' diff --git a/lib/utils/ansi.rb b/lib/utils/ansi.rb index c28d089..353385b 100644 --- a/lib/utils/ansi.rb +++ b/lib/utils/ansi.rb @@ -69,6 +69,18 @@ class ANSI # @return [Array] the ANSI codes stored in the instance. attr_reader :codes + # Removes ANSI escape codes from a given string. + # + # This class method is designed to take a string input and remove any ANSI + # escape codes, which are often used for text formatting in terminal outputs. + # + # @param value [String] the string from which ANSI codes should be removed. + # If the input is not a string, it will be converted to a string using `to_s`. + # @return [String] a new string with ANSI escape codes removed. + def self.remove_from_string(value) + value.to_s.gsub(CONSTANTS::ANSI::TOKEN_REGEX, '') + end + # Initializes a new instance of the ANSI class. # # @param code [Array, Symbol, String] One or more ANSI codes represented as symbols, integers, or strings. diff --git a/lib/utils/constants.rb b/lib/utils/constants.rb index a194c2f..f2337c2 100644 --- a/lib/utils/constants.rb +++ b/lib/utils/constants.rb @@ -6,6 +6,7 @@ module CONSTANTS # @!visibility private module ANSI + TOKEN_REGEX = /\e\[(\d|;)+m/.freeze COLOR_DIGITS_REGEX = /^(\d+);(\d+);(\d+)|(\d+)$/.freeze COLOR_TOKEN_VALUE = '(?:[\w|\d]+|\d+\;\d+\;\d+)(?::[\w|\d]+|:\d+;\d+;\d+)?' COLORS = %r{((?:(?!|).)*)}m.freeze diff --git a/lib/utils/string.rb b/lib/utils/string.rb new file mode 100644 index 0000000..8f61180 --- /dev/null +++ b/lib/utils/string.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require_relative 'question' +require_relative 'message' +require_relative 'ansi' +require_relative 'key' + +# This class provides additional methods to the standard Ruby String class, +# allowing for the removal of ANSI codes, and conversion to Key, Question, +# and Message objects. +class String + # Removes ANSI escape codes from the string. + # + # @return [String] a new string with ANSI codes removed. + def remove_ansi + ANSI.remove_from_string(self) + end + + # Converts the string to a Key object. + # + # @return [Key] a new Key object initialized with the string. + def to_key + Key.new(self) + end + + # Converts the string to a Question object. + # + # @return [Question] a new Question object initialized with the string. + def to_question + Question.new(self) + end + + # Converts the string to a Message object. + # + # @param replaces [Hash, nil] optional replacements to be applied in the message. + # @return [Message] a new Message object initialized with the string and optional replacements. + def to_message(replaces: nil) + Message.new(self, replaces: replaces) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a4f7e32..1a78894 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,13 +2,24 @@ require 'simplecov' -SimpleCov.minimum_coverage 90 SimpleCov.start do + enable_coverage_for_eval + enable_coverage :branch + primary_coverage :branch + + minimum_coverage branch: 90, line: 90 + maximum_coverage_drop branch: 5, line: 5 + minimum_coverage_by_file branch: 85, line: 85 + add_filter 'spec' add_filter 'version.rb' add_filter 'mp_utils.rb' track_files 'lib/**/*.rb' + + SimpleCov.at_exit do + SimpleCov.result.format! + end end # This file was generated by the `rspec --init` command. Conventionally, all @@ -27,10 +38,6 @@ # # See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| - config.after(:suite) do - SimpleCov.result.format! - end - config.expect_with :rspec do |expectations| expectations.include_chain_clauses_in_custom_matcher_descriptions = true end diff --git a/spec/utils/ansi_spec.rb b/spec/utils/ansi_spec.rb index 0580317..ed1d5fc 100644 --- a/spec/utils/ansi_spec.rb +++ b/spec/utils/ansi_spec.rb @@ -3,6 +3,18 @@ require_relative '../../lib/utils/ansi' RSpec.describe ANSI do + describe '.remove_from_string' do + it 'removes ANSI escape codes from a string' do + string_with_ansi = "\e[31mHello\e[0m World" + expect(ANSI.remove_from_string(string_with_ansi)).to eq('Hello World') + end + + it 'returns the same string if there are no ANSI codes' do + plain_string = 'Hello World' + expect(ANSI.remove_from_string(plain_string)).to eq('Hello World') + end + end + describe '#initialize' do it 'initializes with a symbol' do ansi = ANSI.new(:bold) diff --git a/spec/utils/string_spec.rb b/spec/utils/string_spec.rb new file mode 100644 index 0000000..2ae4646 --- /dev/null +++ b/spec/utils/string_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require_relative '../../lib/utils/string' + +RSpec.describe String do + describe '#remove_ansi' do + it 'removes ANSI escape codes from the string' do + string_with_ansi = "\e[31mHello\e[0m World" + expect(string_with_ansi.remove_ansi).to eq('Hello World') + end + + it 'returns the same string if there are no ANSI codes' do + plain_string = 'Hello World' + expect(plain_string.remove_ansi).to eq('Hello World') + end + end + + describe '#to_key' do + it 'converts the string to a Key object' do + string = 'my_key' + key = string.to_key + expect(key).to be_a(Key) + expect(key.value).to eq('my_key') + end + end + + describe '#to_question' do + it 'converts the string to a Question object' do + string = 'Is this a question?' + question = string.to_question + expect(question).to be_a(Question) + end + end + + describe '#to_message' do + it 'converts the string to a Message object without replacements' do + string = 'This is a message' + message = string.to_message + expect(message).to be_a(Message) + expect(message.message).to eq('This is a message') + end + + it 'converts the string to a Message object with replacements' do + string = 'Hello, <||name||>s' + replacements = { name: 'Alice' } + message = string.to_message(replaces: replacements) + expect(message).to be_a(Message) + expect(message.message).to eq(string) + end + end +end