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

rubocop: safe corrections #358

Merged
merged 2 commits into from
Feb 6, 2025
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
430 changes: 5 additions & 425 deletions .rubocop_todo.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in prmd.gemspec
gemspec
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'bundler/gem_tasks'
require 'rake/testtask'
require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new do |t|
t.pattern = 'test/**/*_test.rb'
t.pattern = "test/**/*_test.rb"
end

task default: :test
6 changes: 3 additions & 3 deletions bin/prmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby
require 'optparse'
require_relative '../lib/prmd'
require_relative '../lib/prmd/cli'
require "optparse"
require_relative "../lib/prmd"
require_relative "../lib/prmd/cli"

Prmd::CLI.run(ARGV.dup, bin: File.basename(__FILE__))
18 changes: 9 additions & 9 deletions lib/prmd.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require_relative 'prmd/version'
require_relative 'prmd/load_schema_file'
require_relative 'prmd/commands'
require_relative 'prmd/schema'
require_relative 'prmd/link'
require_relative 'prmd/utils'
require_relative 'prmd/template'
require_relative 'prmd/url_generator'
require_relative 'prmd/hash_helpers'
require_relative "prmd/version"
require_relative "prmd/load_schema_file"
require_relative "prmd/commands"
require_relative "prmd/schema"
require_relative "prmd/link"
require_relative "prmd/utils"
require_relative "prmd/template"
require_relative "prmd/url_generator"
require_relative "prmd/hash_helpers"
34 changes: 16 additions & 18 deletions lib/prmd/cli.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require_relative 'core_ext/optparse'
require_relative 'cli/combine'
require_relative 'cli/doc'
require_relative 'cli/generate'
require_relative 'cli/render'
require_relative 'cli/stub'
require_relative 'cli/verify'
require_relative "core_ext/optparse"
require_relative "cli/combine"
require_relative "cli/doc"
require_relative "cli/generate"
require_relative "cli/render"
require_relative "cli/stub"
require_relative "cli/verify"

# :nodoc:
module Prmd
Expand All @@ -15,11 +15,11 @@ module CLI
def self.make_command_parsers(props = {})
{
combine: CLI::Combine.make_parser(props),
doc: CLI::Doc.make_parser(props),
init: CLI::Generate.make_parser(props),
render: CLI::Render.make_parser(props),
stub: CLI::Stub.make_parser(props),
verify: CLI::Verify.make_parser(props)
doc: CLI::Doc.make_parser(props),
init: CLI::Generate.make_parser(props),
render: CLI::Render.make_parser(props),
stub: CLI::Stub.make_parser(props),
verify: CLI::Verify.make_parser(props),
}
end

Expand All @@ -35,29 +35,27 @@ def self.commands
# @param [Hash<Symbol, Object>] options
# @param [Hash<Symbol, Object>] props
def self.make_parser(options, props = {})
binname = props.fetch(:bin, 'prmd')
binname = props.fetch(:bin, "prmd")

# This is only used to attain the help commands
commands = make_command_parsers(props)
help_text = commands.values.map do |command|
" #{command.banner}"
end.join("\n")

global = OptionParser.new do |opts|
OptionParser.new do |opts|
opts.banner = "Usage: #{binname} [options] [command [options]]"
opts.separator "\nAvailable options:"
opts.on('--version', 'Return version') do
opts.on("--version", "Return version") do
puts "prmd #{Prmd::VERSION}"
exit(0)
end
opts.on('--noop', 'Commands will not execute') do |v|
opts.on("--noop", "Commands will not execute") do |v|
options[:noop] = v
end
opts.separator "\nAvailable commands:"
opts.separator help_text
end

global
end

# Parse top level CLI options from argv
Expand Down
20 changes: 8 additions & 12 deletions lib/prmd/cli/base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'json'
require_relative '../core_ext/optparse'
require_relative '../load_schema_file'
require "json"
require_relative "../core_ext/optparse"
require_relative "../load_schema_file"

module Prmd
module CLI
Expand All @@ -23,7 +23,6 @@ module Base
# @return [OptionParser] newly created parser
# @abstract
def make_parser(options = {})
#
end

# Runs the provided parser with the provided argv.
Expand Down Expand Up @@ -82,9 +81,7 @@ def parse_options(argv, options = {})
def write_result(data, options = {})
output_file = options[:output_file]
if output_file
File.open(output_file, 'w') do |f|
f.write(data)
end
File.write(output_file, data)
else
$stdout.puts data
end
Expand All @@ -96,11 +93,11 @@ def write_result(data, options = {})
# @return [Array[Symbol, String]] source, data
def try_read(filename = nil)
if filename && !filename.empty?
return :file, Prmd.load_schema_file(filename)
[:file, Prmd.load_schema_file(filename)]
elsif !$stdin.tty?
return :io, JSON.load($stdin.read)
[:io, JSON.load($stdin.read)]
else
abort 'Nothing to read'
abort "Nothing to read"
end
end

Expand All @@ -111,7 +108,6 @@ def try_read(filename = nil)
# @return [void]
# @abstract
def execute(options = {})
#
end

# Method called when the command is ran with the :noop option enabled.
Expand All @@ -120,7 +116,7 @@ def execute(options = {})
# @param [Hash<Symbol, Object>] options
# @return [void]
def noop_execute(options = {})
$stderr.puts options
warn options
end

# Run this command given a argv and optional options Hash.
Expand Down
12 changes: 6 additions & 6 deletions lib/prmd/cli/combine.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require_relative 'base'
require_relative '../commands/combine'
require_relative "base"
require_relative "../commands/combine"

module Prmd
module CLI
Expand All @@ -12,17 +12,17 @@ module Combine
# @param (see Prmd::CLI::Base#make_parser)
# @return (see Prmd::CLI::Base#make_parser)
def self.make_parser(options = {})
binname = options.fetch(:bin, 'prmd')
binname = options.fetch(:bin, "prmd")

OptionParser.new do |opts|
opts.banner = "#{binname} combine [options] <file or directory>"
opts.on('-m', '--meta FILENAME', String, 'Set defaults for schemata') do |m|
opts.on("-m", "--meta FILENAME", String, "Set defaults for schemata") do |m|
yield :meta, m
end
opts.on('-o', '--output-file FILENAME', String, 'File to write result to') do |n|
opts.on("-o", "--output-file FILENAME", String, "File to write result to") do |n|
yield :output_file, n
end
opts.on('-t', '--type-as-string', 'Allow type as string') do |t|
opts.on("-t", "--type-as-string", "Allow type as string") do |t|
options[:type_as_string] = t
end
end
Expand Down
18 changes: 9 additions & 9 deletions lib/prmd/cli/doc.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative 'base'
require_relative '../commands/render'
require_relative '../hash_helpers'
require_relative "base"
require_relative "../commands/render"
require_relative "../hash_helpers"

module Prmd
module CLI
Expand All @@ -13,22 +13,22 @@ module Doc
# @param (see Prmd::CLI::Base#make_parser)
# @return (see Prmd::CLI::Base#make_parser)
def self.make_parser(options = {})
binname = options.fetch(:bin, 'prmd')
binname = options.fetch(:bin, "prmd")

OptionParser.new do |opts|
opts.banner = "#{binname} doc [options] <combined schema>"
opts.on('-s', '--settings FILENAME', String, 'Config file to use') do |s|
opts.on("-s", "--settings FILENAME", String, "Config file to use") do |s|
settings = Prmd.load_schema_file(s) || {}
options = HashHelpers.deep_symbolize_keys(settings)
yield :settings, options
end
opts.on('-c', '--content-type application/json', String, 'Content-Type header') do |c|
opts.on("-c", "--content-type application/json", String, "Content-Type header") do |c|
yield :content_type, c
end
opts.on('-o', '--output-file FILENAME', String, 'File to write result to') do |n|
opts.on("-o", "--output-file FILENAME", String, "File to write result to") do |n|
yield :output_file, n
end
opts.on('-p', '--prepend header,overview', Array, 'Prepend files to output') do |p|
opts.on("-p", "--prepend header,overview", Array, "Prepend files to output") do |p|
yield :prepend, p
end
end
Expand Down Expand Up @@ -58,7 +58,7 @@ def self.set_option(options, key, value)
# @return (see Prmd::CLI::Base#execute)
def self.execute(options = {})
filename = options.fetch(:argv).first
template = File.expand_path('templates', File.dirname(__FILE__))
template = File.expand_path("templates", File.dirname(__FILE__))
_, data = try_read(filename)
schema = Prmd::Schema.new(data)
opts = options.merge(template: template)
Expand Down
14 changes: 7 additions & 7 deletions lib/prmd/cli/generate.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative 'base'
require_relative '../commands/init'
require_relative '../utils'
require_relative "base"
require_relative "../commands/init"
require_relative "../utils"

module Prmd
module CLI
Expand All @@ -15,17 +15,17 @@ module Generate
# @param (see Prmd::CLI::Base#make_parser)
# @return (see Prmd::CLI::Base#make_parser)
def self.make_parser(options = {})
binname = options.fetch(:bin, 'prmd')
binname = options.fetch(:bin, "prmd")

OptionParser.new do |opts|
opts.banner = "#{binname} init [options] <resource name>"
opts.on('-t', '--template templates', String, 'Use alternate template') do |t|
opts.on("-t", "--template templates", String, "Use alternate template") do |t|
yield :template, t
end
opts.on('-y', '--yaml', 'Generate YAML') do |y|
opts.on("-y", "--yaml", "Generate YAML") do |y|
yield :yaml, y
end
opts.on('-o', '--output-file FILENAME', String, 'File to write result to') do |n|
opts.on("-o", "--output-file FILENAME", String, "File to write result to") do |n|
yield :output_file, n
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/prmd/cli/render.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require_relative 'base'
require_relative '../commands/render'
require_relative "base"
require_relative "../commands/render"

module Prmd
module CLI
Expand All @@ -12,20 +12,20 @@ module Render
# @param (see Prmd::CLI::Base#make_parser)
# @return (see Prmd::CLI::Base#make_parser)
def self.make_parser(options = {})
binname = options.fetch(:bin, 'prmd')
binname = options.fetch(:bin, "prmd")

OptionParser.new do |opts|
opts.banner = "#{binname} render [options] <combined schema>"
opts.on('-c', '--content-type application/json', String, 'Content-Type header') do |c|
opts.on("-c", "--content-type application/json", String, "Content-Type header") do |c|
yield :content_type, c
end
opts.on('-o', '--output-file FILENAME', String, 'File to write result to') do |n|
opts.on("-o", "--output-file FILENAME", String, "File to write result to") do |n|
yield :output_file, n
end
opts.on('-p', '--prepend header,overview', Array, 'Prepend files to output') do |p|
opts.on("-p", "--prepend header,overview", Array, "Prepend files to output") do |p|
yield :prepend, p
end
opts.on('-t', '--template templates', String, 'Use alternate template') do |t|
opts.on("-t", "--template templates", String, "Use alternate template") do |t|
yield :template, t
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/prmd/cli/stub.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative 'base'
require_relative "base"

module Prmd
module CLI
Expand All @@ -11,7 +11,7 @@ module Stub
# @param (see Prmd::CLI::Base#make_parser)
# @return (see Prmd::CLI::Base#make_parser)
def self.make_parser(options = {})
binname = options.fetch(:bin, 'prmd')
binname = options.fetch(:bin, "prmd")

OptionParser.new do |opts|
opts.banner = "#{binname} stub [options] <combined schema>"
Expand Down
14 changes: 7 additions & 7 deletions lib/prmd/cli/verify.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require_relative 'base'
require_relative '../commands/verify'
require_relative "base"
require_relative "../commands/verify"

module Prmd
module CLI
Expand All @@ -12,17 +12,17 @@ module Verify
# @param (see Prmd::CLI::Base#make_parser)
# @return (see Prmd::CLI::Base#make_parser)
def self.make_parser(options = {})
binname = options.fetch(:bin, 'prmd')
binname = options.fetch(:bin, "prmd")

OptionParser.new do |opts|
opts.banner = "#{binname} verify [options] <combined schema>"
opts.on('-y', '--yaml', 'Generate YAML') do |y|
opts.on("-y", "--yaml", "Generate YAML") do |y|
yield :yaml, y
end
opts.on('-o', '--output-file FILENAME', String, 'File to write result to') do |n|
opts.on("-o", "--output-file FILENAME", String, "File to write result to") do |n|
yield :output_file, n
end
opts.on('-s', '--custom-schema FILENAME', String, 'Path to custom schema') do |n|
opts.on("-s", "--custom-schema FILENAME", String, "Path to custom schema") do |n|
yield :custom_schema, n
end
end
Expand All @@ -42,7 +42,7 @@ def self.execute(options = {})
errors = Prmd.verify(data, custom_schema: custom_schema)
unless errors.empty?
errors.map! { |error| "#{filename}: #{error}" } if filename
errors.each { |error| $stderr.puts(error) }
errors.each { |error| warn(error) }
exit(1)
end
result = options[:yaml] ? data.to_yaml : JSON.pretty_generate(data)
Expand Down
8 changes: 4 additions & 4 deletions lib/prmd/commands.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative 'commands/combine'
require_relative 'commands/init'
require_relative 'commands/render'
require_relative 'commands/verify'
require_relative "commands/combine"
require_relative "commands/init"
require_relative "commands/render"
require_relative "commands/verify"
Loading