Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardrb compliant and move CI to GitHub Actions #1

Merged
merged 7 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[*]
indent_size = 2
indent_style = space
trim_trailing_whitespace = true
insert_final_newline = true
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: test

on:
workflow_dispatch:
schedule:
- cron: '3 0 2 * *'
push:

jobs:
test:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '2.7'
- '3.0'
- '3.1'
- '3.2'
- '3.3'
steps:
- uses: actions/checkout@v4
- name: Set up RUby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: lint
run: bundle exec rake standard
- name: test
run: bundle exec rake spec
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require:
- standard

inherit_gem:
standard: config/base.yml
3 changes: 3 additions & 0 deletions .standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# For available configuration options, see:
# https://github.com/testdouble/standard
ruby_version: 2.7
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## [0.3.5] 2024-08-16

- standardrb compliant

## [0.1.0] 2021-04-16

- initial release
13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,16 @@ source "https://rubygems.org"

# Specify your gem's dependencies in graph.gemspec
gemspec

gem "bundler", "~> 2.0"
gem "rake", "~> 13.0"

gem "standard", "~> 1.37"
gem "rubocop", "~> 1.62"
gem "yard"

group :test do
gem "minitest", "~> 5.0"
gem "minitest-reporters", "~> 1"
gem "minitest-power_assert"
end
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ Rake::TestTask.new(:spec) do |t|
t.test_files = FileList["spec/**/*_spec.rb"]
end

require "standard/rake"

YARD::Rake::YardocTask.new do |t|
t.files = ["lib/**/*.rb"]
t.options = ["--any", "--extra", "--opts"]
end

task :default => :spec
task default: :spec
4 changes: 2 additions & 2 deletions exe/simple-hd-graph
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#! /usr/bin/env ruby

$LOAD_PATH.unshift File.absolute_path(File.join(__dir__, '../lib'))
$LOAD_PATH.unshift File.absolute_path(File.join(__dir__, "../lib"))

require 'simple-hd-graph'
require "simple-hd-graph"

Version = SimpleHdGraph::VERSION
cmd = SimpleHdGraph::Command.new
Expand Down
4 changes: 2 additions & 2 deletions lib/simple-hd-graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class Error < StandardError; end
# Your code goes here...
end

Dir.glob(__dir__ + '/simple-hd-graph/**/*.rb').each { |f|
require f.sub(/\.rb\z/, '')
Dir.glob(__dir__ + "/simple-hd-graph/**/*.rb").each { |f|
require f.sub(/\.rb\z/, "")
}
15 changes: 7 additions & 8 deletions lib/simple-hd-graph/command.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require 'optparse'
require "optparse"

module SimpleHdGraph
class Error < StandardError; end
class FileNotExist < Error; end
class DirectoryNotExist < Error; end
class DirectoryNotExist < Error; end # rubocop:disable Layout/EmptyLineBetweenDefs

class Command
#
Expand Down Expand Up @@ -39,7 +38,7 @@ def parse(argv)
# @return [String]
#
def stream
if (@dir)
if @dir
reader.read_dir(@dir)
else
reader.read_file(@file)
Expand All @@ -58,25 +57,25 @@ def start
# :reek:NestedIterators, :reek:DuplicateMethodCall
def opts
OptionParser.new do |opt|
opt.on('-d DIR', '--dir', 'dirname') { |value|
opt.on("-d DIR", "--dir", "dirname") { |value|
if File.exist?(value) && File.directory?(value)
@dir = value
else
raise DirectoryNotExist, value
end
}
opt.on('-f FILE', '--file', 'filename') { |value|
opt.on("-f FILE", "--file", "filename") { |value|
if File.exist?(value) && File.file?(value)
@file = value
else
raise FileNotExist, value
end
}
opt.on('-r RENDERER', '--renderer', 'renderer') { |value|
opt.on("-r RENDERER", "--renderer", "renderer") { |value|
begin
@renderer = SimpleHdGraph::Renderer.method(value)
rescue NameError
STDERR.puts "[Warining] renderer `#{value}` not found. falling back to :plantuml"
warn "[Warining] renderer `#{value}` not found. falling back to :plantuml"
end
}
end
Expand Down
9 changes: 4 additions & 5 deletions lib/simple-hd-graph/context.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'simple-hd-graph/node'
require "simple-hd-graph/node"

module SimpleHdGraph
class ContextNode < Node
Expand Down Expand Up @@ -39,15 +39,14 @@ def set_depends(depends)
@depends = depends
end

# :reek:NestedIterators, :reek:TooManyStatements
def refill_relation
@resource ||= []
@resource ||= []
@relations ||= []
@resources.each { |resource|
dependencies = resource.has
if dependencies
if dependencies.respond_to? :each
dependencies.each { |dependency|
@relations << { resource.id => @resource_dict[dependency] }
@relations << {resource.id => @resource_dict[dependency]}
}
end
}
Expand Down
43 changes: 21 additions & 22 deletions lib/simple-hd-graph/node.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'dry/inflector'
require "dry/inflector"

module SimpleHdGraph
class Node
class RequiredFieldNotFilled < StandardError; end
class RequiredFieldNotFilled < Error; end

class << self
#
Expand All @@ -13,7 +13,7 @@ def required(*names)
end
end

CAMELIZE_SEPARATOR = ' ,.、。'
CAMELIZE_SEPARATOR = /[ ,.、。]/

def initialize
@inflector = Dry::Inflector.new
Expand All @@ -22,27 +22,26 @@ def initialize
#
# @param struct [Hash]
#
# :reek:TooManyStatements
def load(struct)
klass = self.class

required_fields = if klass.instance_variables.grep(/@required_fields/).size > 0
klass.instance_variable_get('@required_fields')
else
nil
end

if required_fields.is_a? Array
filled = required_fields.all? {|field|
if struct.has_key? field
true
else
raise RequiredFieldNotFilled, field
end
}
else
filled = true
end
required_fields =
if klass.instance_variables.grep(/@required_fields/).size > 0
klass.instance_variable_get(:@required_fields)
end

filled =
if required_fields.is_a? Array
required_fields.all? { |field|
if struct.has_key? field
true
else
raise RequiredFieldNotFilled, field
end
}
else
true
end

@content = struct if filled
end
Expand All @@ -52,7 +51,7 @@ def load(struct)
# @return [String]
#
def camelize(str)
@inflector.camelize(@inflector.underscore(str.gsub(/[#{CAMELIZE_SEPARATOR}]/, '_')))
@inflector.camelize(@inflector.underscore(str.gsub(CAMELIZE_SEPARATOR, "_")))
end
end
end
24 changes: 11 additions & 13 deletions lib/simple-hd-graph/parser.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
require 'yaml'
require "yaml"

module SimpleHdGraph
#
# parse for YAML string
#
# :reek:InstanceVaariableAssumption
class Parser
KEYWORD_ID ||= 'id'.freeze
KEYWORD_RESOURCES ||= 'resources'.freeze
KEYWORD_DEPENDS ||= 'depends'.freeze
KEYWORD_ID = "id".freeze
KEYWORD_RESOURCES = "resources".freeze
KEYWORD_DEPENDS = "depends".freeze

#
# @param document [String]
#
# :reek:TooManyStatements
def parse(document)
contexts = []

YAML.load_stream(document) do |node|
next unless node

context = nil
context = nil
resources = nil
depends = nil
depends = nil

node.each_pair { |key, value|
case key
when KEYWORD_ID
context = ContextNode.new
context.load({ id: value })
context.load({id: value})
when KEYWORD_DEPENDS
depends = value
when KEYWORD_RESOURCES
Expand Down Expand Up @@ -62,7 +60,7 @@ def pack_depends_into_context(depends, context)
def pack_resources_into_context(resources, context)
resources.each { |key, resource|
rn = ResourceNode.new
rn.load_with_context({ id: context.id }, { key => resource })
rn.load_with_context({id: context.id}, {key => resource})
context << rn
}
end
Expand All @@ -71,7 +69,7 @@ def pack_resources_into_context(resources, context)
# @param contexts [Array]
#
def refill_relation(contexts)
contexts.each {|context|
contexts.each { |context|
context.refill_relation
}
end
Expand All @@ -83,9 +81,9 @@ def refill_depends(contexts)
contexts.map { |context|
if context.depends
regularized = context.depends.map { |dependee|
d = contexts.select { |c|
d = contexts.find { |c|
c.alias == dependee
}.first
}

{
context.id => d ? d.id : dependee
Expand Down
3 changes: 1 addition & 2 deletions lib/simple-hd-graph/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class Reader
# @param file [String]
# @return [String]
#
# :reek:UtilityFunction
def read_file(file)
File.read(file)
end
Expand All @@ -14,7 +13,7 @@ def read_file(file)
# @return [String]
#
def read_dir(dir)
Dir.glob("#{dir}/**/*.{yml,yaml}").map {|file|
Dir.glob("#{dir}/**/*.{yml,yaml}").map { |file|
read_file(file)
}.join("---\n")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/simple-hd-graph/renderer/plantuml.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Dir.glob(File.join(__dir__, '**/*.rb')).each { |f| require f }
Dir.glob(File.join(__dir__, "plantuml/*.rb")).sort.each { |f| require f }

module SimpleHdGraph
module Renderer
Expand Down
Loading