Skip to content

Commit

Permalink
Workflows and Helpers (#21)
Browse files Browse the repository at this point in the history
MarcioFPaludo authored Oct 24, 2024
1 parent dff2cd5 commit 240a30e
Showing 12 changed files with 181 additions and 57 deletions.
23 changes: 9 additions & 14 deletions .github/workflows/bump-version.yml
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
@@ -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

36 changes: 0 additions & 36 deletions .github/workflows/on-pull-request-close.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/publish-gem.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Create a release for the gem
name: Publish Gem in ruby Gems

on:
workflow_dispatch:
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/mp_utils.rb
Original file line number Diff line number Diff line change
@@ -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'
12 changes: 12 additions & 0 deletions lib/utils/ansi.rb
Original file line number Diff line number Diff line change
@@ -69,6 +69,18 @@ class ANSI
# @return [Array<Integer>] 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>, Symbol, String] One or more ANSI codes represented as symbols, integers, or strings.
1 change: 1 addition & 0 deletions lib/utils/constants.rb
Original file line number Diff line number Diff line change
@@ -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{<color:(#{COLOR_TOKEN_VALUE})>((?:(?!<color:#{COLOR_TOKEN_VALUE}>|</color>).)*)</color>}m.freeze
40 changes: 40 additions & 0 deletions lib/utils/string.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 12 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions spec/utils/ansi_spec.rb
Original file line number Diff line number Diff line change
@@ -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)
51 changes: 51 additions & 0 deletions spec/utils/string_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 240a30e

Please sign in to comment.