Skip to content

Commit

Permalink
DEV: Fix methods removed in Ruby 3.2 (discourse#15459)
Browse files Browse the repository at this point in the history
* File.exists? is deprecated and removed in Ruby 3.2 in favor of
File.exist?
* Dir.exists? is deprecated and removed in Ruby 3.2 in favor of
Dir.exist?
  • Loading branch information
peterzhu2118 authored Jan 5, 2022
1 parent 692ba18 commit c5fd8c4
Show file tree
Hide file tree
Showing 75 changed files with 140 additions and 140 deletions.
2 changes: 1 addition & 1 deletion app/controllers/user_avatars_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def show_in_site(hostname)
elsif upload && optimized = get_optimized_image(upload, size)
if optimized.local?
optimized_path = Discourse.store.path_for(optimized)
image = optimized_path if File.exists?(optimized_path)
image = optimized_path if File.exist?(optimized_path)
else
return proxy_avatar(Discourse.store.cdn_url(optimized.url), upload.created_at)
end
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/regular/export_csv_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def execute(args)
dirname = "#{UserExport.base_directory}/#{filename}"

# ensure directory exists
FileUtils.mkdir_p(dirname) unless Dir.exists?(dirname)
FileUtils.mkdir_p(dirname) unless Dir.exist?(dirname)

# Generate a compressed CSV file
begin
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/regular/export_user_archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def execute(args)
dirname = "#{UserExport.base_directory}/#{filename}"

# ensure directory exists
FileUtils.mkdir_p(dirname) unless Dir.exists?(dirname)
FileUtils.mkdir_p(dirname) unless Dir.exist?(dirname)

# Generate a compressed CSV file
zip_filename = nil
Expand Down
2 changes: 1 addition & 1 deletion app/models/global_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def resolve(current, default)
class FileProvider < BaseProvider
attr_reader :data
def self.from(file)
if File.exists?(file)
if File.exist?(file)
parse(file)
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/handle_chunk_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.merge_chunks(chunk, params)

def check_chunk
# check whether the chunk has already been uploaded
has_chunk_been_uploaded = File.exists?(@chunk) && File.size(@chunk) == @params[:current_chunk_size]
has_chunk_been_uploaded = File.exist?(@chunk) && File.size(@chunk) == @params[:current_chunk_size]
# 200 = exists, 404 = not uploaded yet
status = has_chunk_been_uploaded ? 200 : 404
end
Expand All @@ -30,7 +30,7 @@ def upload_chunk
# path to chunk file
dir = File.dirname(@chunk)
# ensure directory exists
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
# save chunk to the directory
File.open(@chunk, "wb") { |f| f.write(@params[:file].tempfile.read) }
end
Expand Down
2 changes: 1 addition & 1 deletion bin/unicorn
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def ensure_cache_clean!
super_sha = Digest::SHA1.hexdigest(core_git_sha + plugins_combined_git_sha)
hash_file = "#{RAILS_ROOT}/tmp/plugin-hash"

old_hash = File.exists?(hash_file) ? File.read(hash_file) : nil
old_hash = File.exist?(hash_file) ? File.read(hash_file) : nil

if old_hash && old_hash != super_sha
puts "WARNING: It looks like your discourse plugins or core version have recently changed."
Expand Down
2 changes: 1 addition & 1 deletion config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)

require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])

if (ENV['DISABLE_BOOTSNAP'] != '1')
begin
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/100-watch_for_restart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

Thread.new do
file = "#{Rails.root}/tmp/restart"
old_time = File.ctime(file).to_i if File.exists? file
old_time = File.ctime(file).to_i if File.exist? file
wait_seconds = 4

if Rails.env.development? && $PROGRAM_NAME =~ /puma/
Expand All @@ -25,7 +25,7 @@
begin
listener = Listen.to("#{Rails.root}/tmp", only: /restart/) do

time = File.ctime(file).to_i if File.exists? file
time = File.ctime(file).to_i if File.exist? file

if old_time != time
Rails.logger.info "attempting to reload #{$$} #{$PROGRAM_NAME} in #{wait_seconds} seconds"
Expand Down
2 changes: 1 addition & 1 deletion config/unicorn.conf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
if supervisor > 0
Thread.new do
while true
unless File.exists?("/proc/#{supervisor}")
unless File.exist?("/proc/#{supervisor}")
puts "Kill self supervisor is gone"
Process.kill "TERM", Process.pid
end
Expand Down
4 changes: 2 additions & 2 deletions lib/autospec/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class Autospec::Formatter < RSpec::Core::Formatters::BaseTextFormatter

def initialize(output)
super
FileUtils.mkdir_p("tmp") unless Dir.exists?("tmp")
File.delete(RSPEC_RESULT) if File.exists?(RSPEC_RESULT)
FileUtils.mkdir_p("tmp") unless Dir.exist?("tmp")
File.delete(RSPEC_RESULT) if File.exist?(RSPEC_RESULT)
@fail_file = File.open(RSPEC_RESULT, "w")
end

Expand Down
2 changes: 1 addition & 1 deletion lib/autospec/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def process_change(files)
if spec == file && line
with_line = spec + ":" << line.to_s
end
if File.exists?(spec) || Dir.exists?(spec)
if File.exist?(spec) || Dir.exist?(spec)
if with_line != spec
specs << [file, spec, runner]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/autospec/qunit_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def kill_process(pid)

def try_to_find_module_name(file)
file, _ = file.split(/:\d+$/)
return unless File.exists?(file)
return unless File.exist?(file)
File.open(file, "r").each_line do |line|
if m = /module\(['"]([^'"]+)/i.match(line)
return m[1]
Expand Down
2 changes: 1 addition & 1 deletion lib/autospec/reload_css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.run_on_change(paths)
paths.map! do |p|
hash = nil
fullpath = "#{Rails.root}/#{p}"
hash = Digest::MD5.hexdigest(File.read(fullpath)) if File.exists?(fullpath)
hash = Digest::MD5.hexdigest(File.read(fullpath)) if File.exist?(fullpath)
p = p.sub(/\.sass\.erb/, "")
p = p.sub(/\.sass/, "")
p = p.sub(/\.scss/, "")
Expand Down
2 changes: 1 addition & 1 deletion lib/backup_restore/backup_file_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def extract_db_dump
if @is_archive
# for compatibility with backups from Discourse v1.5 and below
old_dump_path = File.join(@tmp_directory, OLD_DUMP_FILENAME)
File.exists?(old_dump_path) ? old_dump_path : File.join(@tmp_directory, BackupRestore::DUMP_FILE)
File.exist?(old_dump_path) ? old_dump_path : File.join(@tmp_directory, BackupRestore::DUMP_FILE)
else
File.join(@tmp_directory, @filename)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/backup_restore/local_backup_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.base_directory(db: nil, root_directory: nil)
root_directory ||= File.join(Rails.root, "public", "backups")

base_directory = File.join(root_directory, current_db)
FileUtils.mkdir_p(base_directory) unless Dir.exists?(base_directory)
FileUtils.mkdir_p(base_directory) unless Dir.exist?(base_directory)
base_directory
end

Expand All @@ -25,13 +25,13 @@ def remote?

def file(filename, include_download_source: false)
path = path_from_filename(filename)
create_file_from_path(path, include_download_source) if File.exists?(path)
create_file_from_path(path, include_download_source) if File.exist?(path)
end

def delete_file(filename)
path = path_from_filename(filename)

if File.exists?(path)
if File.exist?(path)
File.delete(path)
reset_cache
end
Expand Down
2 changes: 1 addition & 1 deletion lib/backup_restore/meta_data_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def validate
def extract_metadata
metadata_path = File.join(@tmp_directory, METADATA_FILE) if @tmp_directory.present?

if metadata_path.present? && File.exists?(metadata_path)
if metadata_path.present? && File.exist?(metadata_path)
metadata = load_metadata_file(metadata_path)
elsif @filename =~ /-#{BackupRestore::VERSION_PREFIX}(\d{14})/
metadata = { version: Regexp.last_match[1].to_i }
Expand Down
2 changes: 1 addition & 1 deletion lib/demon/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def run
end

def already_running?
if File.exists? pid_file
if File.exist? pid_file
pid = File.read(pid_file).to_i
if Demon::Base.alive?(pid)
return pid
Expand Down
2 changes: 1 addition & 1 deletion lib/discourse_dev/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize
@file_path = File.join(Rails.root, "config", "dev.yml")
default_config = YAML.load_file(default_file_path)

if File.exists?(file_path)
if File.exist?(file_path)
user_config = YAML.load_file(file_path)
else
puts "I did no detect a custom `config/dev.yml` file, creating one for you where you can amend defaults."
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/discourse_markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def image_cache_dir
def load_image(image)
cache_path = ::File.join(image_cache_dir, image[:filename])

if !::File.exists?(cache_path)
if !::File.exist?(cache_path)
FileUtils.mkdir_p(image_cache_dir)
temp_file = ::FileHelper.download(
image[:url],
Expand Down
2 changes: 1 addition & 1 deletion lib/file_store/base_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def get_cache_path_for(filename)

def get_from_cache(filename)
path = get_cache_path_for(filename)
File.open(path) if File.exists?(path)
File.open(path) if File.exist?(path)
end

def cache_file(file, filename)
Expand Down
10 changes: 5 additions & 5 deletions lib/file_store/local_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def store_file(file, path)
def remove_file(url, _)
return unless is_relative?(url)
source = "#{public_dir}#{url}"
return unless File.exists?(source)
return unless File.exist?(source)
destination = "#{public_dir}#{url.sub("/uploads/", "/uploads/tombstone/")}"
dir = Pathname.new(destination).dirname
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
FileUtils.remove(destination) if File.exists?(destination)
FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
FileUtils.remove(destination) if File.exist?(destination)
FileUtils.move(source, destination, force: true)
FileUtils.touch(destination)
end
Expand Down Expand Up @@ -62,7 +62,7 @@ def path_for(upload)
end

def purge_tombstone(grace_period)
if Dir.exists?(Discourse.store.tombstone_dir)
if Dir.exist?(Discourse.store.tombstone_dir)
Discourse::Utils.execute_command(
'find', tombstone_dir, '-mtime', "+#{grace_period}", '-type', 'f', '-delete'
)
Expand All @@ -75,7 +75,7 @@ def get_path_for(type, upload_id, sha, extension)

def copy_file(file, path)
dir = Pathname.new(path).dirname
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
# move the file to the right location
# not using mv, cause permissions are no good on move
File.open(path, "wb") { |f| f.write(file.read) }
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/locale_file_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def locale_files

def reference_file(path)
path = path.gsub(/\.\w{2,}\.yml$/, ".#{REFERENCE_LOCALE}.yml")
path if File.exists?(path)
path if File.exist?(path)
end

def traverse_hash(hash, parent_keys, &block)
Expand Down
4 changes: 2 additions & 2 deletions lib/letter_avatar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def generate(username, size, opts = nil)
size = FULLSIZE if size > FULLSIZE
filename = cached_path(identity, size)

return filename if cache && File.exists?(filename)
return filename if cache && File.exist?(filename)

fullsize = fullsize_path(identity)
generate_fullsize(identity) if !cache || !File.exists?(fullsize)
generate_fullsize(identity) if !cache || !File.exist?(fullsize)

# Optimizing here is dubious, it can save up to 2x for large images (eg 359px)
# BUT... we are talking 2400 bytes down to 1200 bytes, both fit in one packet
Expand Down
8 changes: 4 additions & 4 deletions lib/plugin/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def add_directory_column(column_name, query:, icon: nil)
end

def delete_extra_automatic_assets(good_paths)
return unless Dir.exists? auto_generated_path
return unless Dir.exist? auto_generated_path

filenames = good_paths.map { |f| File.basename(f) }
# nuke old files
Expand Down Expand Up @@ -701,7 +701,7 @@ def activate!
end

public_data = File.dirname(path) + "/public"
if Dir.exists?(public_data)
if Dir.exist?(public_data)
target = Rails.root.to_s + "/public/plugins/"

Discourse::Utils.execute_command('mkdir', '-p', target)
Expand Down Expand Up @@ -839,7 +839,7 @@ def css_asset_exists?(target = nil)
end

def js_asset_exists?
File.exists?(js_file_path)
File.exist?(js_file_path)
end

# Receives an array with two elements:
Expand Down Expand Up @@ -1085,7 +1085,7 @@ def validate_directory_column_name(column_name)
end

def write_asset(path, contents)
unless File.exists?(path)
unless File.exist?(path)
ensure_directory(path)
File.open(path, "w") { |f| f.write(contents) }
end
Expand Down
4 changes: 2 additions & 2 deletions lib/plugin_gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ def self.load(path, name, version, opts = nil)
spec_file += "-#{opts[:platform]}" if opts[:platform]
spec_file += ".gemspec"

unless File.exists? spec_file
unless File.exist? spec_file
command = "gem install #{name} -v #{version} -i #{gems_path} --no-document --ignore-dependencies --no-user-install"
command += " --source #{opts[:source]}" if opts[:source]
puts command
puts `#{command}`
end

if File.exists? spec_file
if File.exist? spec_file
Gem.path << gems_path
Gem::Specification.load(spec_file).activate

Expand Down
8 changes: 4 additions & 4 deletions lib/s3_inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def for_each_inventory_row
end

def download_inventory_file_to_tmp_directory(file)
return if File.exists?(file[:filename])
return if File.exist?(file[:filename])

log "Downloading inventory file '#{file[:key]}' to tmp directory..."
failure_message = "Failed to inventory file '#{file[:key]}' to tmp directory."
Expand Down Expand Up @@ -219,8 +219,8 @@ def prepare_for_all_sites
def cleanup!
return if @preloaded_inventory_file
files.each do |file|
File.delete(file[:filename]) if File.exists?(file[:filename])
File.delete(file[:filename][0...-3]) if File.exists?(file[:filename][0...-3])
File.delete(file[:filename]) if File.exist?(file[:filename])
File.delete(file[:filename][0...-3]) if File.exist?(file[:filename][0...-3])
end
end

Expand Down Expand Up @@ -253,7 +253,7 @@ def files

def download_and_decompress_files
files.each do |file|
next if File.exists?(file[:filename][0...-3])
next if File.exist?(file[:filename][0...-3])

download_inventory_file_to_tmp_directory(file)
decompress_inventory_file(file)
Expand Down
8 changes: 4 additions & 4 deletions lib/stylesheet/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def self.precompile_theme_css

def self.last_file_updated
if Rails.env.production?
@last_file_updated ||= if File.exists?(MANIFEST_FULL_PATH)
@last_file_updated ||= if File.exist?(MANIFEST_FULL_PATH)
File.readlines(MANIFEST_FULL_PATH, 'r')[0]
else
mtime = max_file_mtime
Expand Down Expand Up @@ -224,7 +224,7 @@ def stylesheet_details(target = :desktop, media = 'all')
builder = Builder.new(target: target, theme: theme, manager: self)

next if builder.theme&.component && !scss_checker.has_scss(theme_id)
builder.compile unless File.exists?(builder.stylesheet_fullpath)
builder.compile unless File.exist?(builder.stylesheet_fullpath)
href = builder.stylesheet_path(current_hostname)

data[:new_href] = href
Expand All @@ -242,7 +242,7 @@ def stylesheet_details(target = :desktop, media = 'all')
end
else
builder = Builder.new(target: target, manager: self)
builder.compile unless File.exists?(builder.stylesheet_fullpath)
builder.compile unless File.exist?(builder.stylesheet_fullpath)
href = builder.stylesheet_path(current_hostname)

data = { target: target, new_href: href }
Expand Down Expand Up @@ -285,7 +285,7 @@ def color_scheme_stylesheet_details(color_scheme_id = nil, media)
manager: self
)

builder.compile unless File.exists?(builder.stylesheet_fullpath)
builder.compile unless File.exist?(builder.stylesheet_fullpath)

href = builder.stylesheet_path(current_hostname)
stylesheet[:new_href] = href
Expand Down
2 changes: 1 addition & 1 deletion lib/stylesheet/manager/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(target: :desktop, theme: nil, color_scheme: nil, manager:)

def compile(opts = {})
if !opts[:force]
if File.exists?(stylesheet_fullpath)
if File.exist?(stylesheet_fullpath)
unless StylesheetCache.where(target: qualified_target, digest: digest).exists?
begin
source_map = begin
Expand Down
Loading

0 comments on commit c5fd8c4

Please sign in to comment.