Skip to content

Commit

Permalink
Opt into Ruby frozen string literals throughout the codebase (#446)
Browse files Browse the repository at this point in the history
This PR adds the `# frozen_string_literal: true` comment to all of
tomo's Ruby files.

This prepares tomo for a future version of Ruby (4?), where string
literals will likely become frozen by default.
  • Loading branch information
mattbrictson authored May 27, 2024
1 parent 18ad2b2 commit 1c055f5
Show file tree
Hide file tree
Showing 162 changed files with 346 additions and 29 deletions.
3 changes: 0 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ Style/FetchEnvVar:
Style/FormatStringToken:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false

Style/NumericLiterals:
Enabled: false

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"
gemspec

Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rake/testtask"
require "rubocop/rake_task"
Expand Down
1 change: 1 addition & 0 deletions exe/tomo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "tomo"
Tomo::CLI.new.call(ARGV)
4 changes: 3 additions & 1 deletion lib/tomo.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
autoload :CLI, "tomo/cli"
autoload :Colors, "tomo/colors"
Expand All @@ -21,7 +23,7 @@ module Tomo
autoload :TaskLibrary, "tomo/task_library"
autoload :VERSION, "tomo/version"

DEFAULT_CONFIG_PATH = ".tomo/config.rb".freeze
DEFAULT_CONFIG_PATH = ".tomo/config.rb"

class << self
attr_accessor :logger
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
autoload :Command, "tomo/cli/command"
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/command.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
class Command
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/common_options.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
module CommonOptions
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/completions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
class Completions
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/deploy_options.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
module DeployOptions
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
class Error < ::Tomo::Error
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/interrupted_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
class InterruptedError < Error
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/options.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
class Options
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"

module Tomo
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/project_options.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
module ProjectOptions
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/rules.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
class Rules
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/rules/argument.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Tomo::CLI::Rules
class Argument
def initialize(label, values_proc:, multiple: false, required: false)
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/rules/switch.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Tomo::CLI::Rules
class Switch
def initialize(key, *switches, callback_proc:, required: false, &convert_proc)
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/rules/value_switch.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Tomo::CLI::Rules
class ValueSwitch < Switch
include Tomo::Colors
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/rules_evaluator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
class RulesEvaluator
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/state.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
class State
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/cli/usage.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class CLI
class Usage
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/colors.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
module Colors
ANSI_CODES = {
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/commands.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
module Commands
autoload :CompletionScript, "tomo/commands/completion_script"
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/commands/completion_script.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
module Commands
class CompletionScript
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/commands/default.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
module Commands
class Default < CLI::Command
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/commands/deploy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
module Commands
class Deploy < CLI::Command
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/commands/help.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
module Commands
class Help
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/commands/init.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "erb"
require "fileutils"

Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/commands/run.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
module Commands
class Run < CLI::Command
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/commands/setup.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
module Commands
class Setup < CLI::Command
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/commands/tasks.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
module Commands
class Tasks < CLI::Command
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/commands/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
module Commands
class Version < CLI::Command
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
autoload :DSL, "tomo/configuration/dsl"
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
module DSL
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/dsl/batch_block.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
module DSL
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/dsl/config_file.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
module DSL
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/dsl/environment_block.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
module DSL
Expand Down
4 changes: 3 additions & 1 deletion lib/tomo/configuration/dsl/error_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
module DSL
Expand Down Expand Up @@ -55,7 +57,7 @@ def highlighted_lines # rubocop:disable Metrics/AbcSize
last = [dsl_lines.length, error_line_no + 1].min
width = last.to_s.length

(first..last).each_with_object("") do |line_no, result|
(first..last).each_with_object(+"") do |line_no, result|
line = dsl_lines[line_no - 1]
line_no_prefix = line_no.to_s.rjust(width)

Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/dsl/hosts_and_settings.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
module DSL
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/dsl/tasks_block.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
module DSL
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/environment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
class Environment
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/glob.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
class Glob
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/plugin_file_not_found_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
class PluginFileNotFoundError < Error
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/plugins_registry.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
class PluginsRegistry
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/plugins_registry/file_resolver.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
class PluginsRegistry::FileResolver
Expand Down
4 changes: 3 additions & 1 deletion lib/tomo/configuration/plugins_registry/gem_resolver.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

module Tomo
class Configuration
class PluginsRegistry::GemResolver
PLUGIN_PREFIX = "tomo/plugin".freeze
PLUGIN_PREFIX = "tomo/plugin"
private_constant :PLUGIN_PREFIX

def self.resolve(name)
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/project_not_found_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
class ProjectNotFoundError < Tomo::Error
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/role_based_task_filter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
class RoleBasedTaskFilter
Expand Down
6 changes: 4 additions & 2 deletions lib/tomo/configuration/unknown_environment_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
class UnknownEnvironmentError < Tomo::Error
Expand All @@ -23,10 +25,10 @@ def wrong_envs
ERROR

if suggestions.any?
error << suggestions.to_console
error + suggestions.to_console
else
envs = known_environments.map { |env| blue(" #{env}") }
error << <<~ENVS
error + <<~ENVS
The following environments are available:
Expand Down
6 changes: 4 additions & 2 deletions lib/tomo/configuration/unknown_plugin_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
class UnknownPluginError < Tomo::Error
Expand All @@ -9,9 +11,9 @@ def to_console
ERROR

sugg = Error::Suggestions.new(dictionary: known_plugins, word: name)
error << sugg.to_console if sugg.any?
error += sugg.to_console if sugg.any?

error << gem_suggestion
error + gem_suggestion
end

private
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/configuration/unspecified_environment_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Tomo
class Configuration
class UnspecifiedEnvironmentError < Tomo::Error
Expand Down
2 changes: 2 additions & 0 deletions lib/tomo/console.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require "io/console"

Expand Down
4 changes: 3 additions & 1 deletion lib/tomo/console/key_reader.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "forwardable"
require "io/console"
require "time"
Expand Down Expand Up @@ -35,7 +37,7 @@ def seconds_since_last_press
end

def read_chars_nonblock
chars = ""
chars = +""
loop do
next_char = raw { read_nonblock(1) }
break if next_char.nil?
Expand Down
Loading

0 comments on commit 1c055f5

Please sign in to comment.