fastlanefileutilsruby-2.5

Can't test if directory already exists from within Fastlane with Ruby


I can’t test whether a directory exists from within a Fastlane action nor lane. None of the following work for save_path = "./dir_name" nor save_path = "dir_name" in the current directory (from which fastlane is being run):

!Dir.empty?(save_path)
!Dir[save_path].empty?
Dir.exist?(save_path)
File.exist?(save_path)
File.directory?(save_path)

I even tried to expand the relative path:

File.exists? File.expand_path(save_path)

I have referred to the following:

  1. Check if directory is empty in Ruby
  2. How to check if a given directory exists in Ruby
  3. https://blog.bigbinary.com/2017/02/28/dir-emtpy-included-in-ruby-2-4.html
  4. https://ruby-doc.org/core-2.2.0/Dir.html

How does one test to see if a directory exists from within Fastlane? Thank you for reading and for your help!


Solution

  • LOL Well, I discovered my issue. I didn't realize that I had to include the entire path to each of these statements, since I'm using Dir.foreach to iterate over part of the filesystem; the updated syntax is:

    Dir.foreach(save_path) do |dir|
       next if dir == '.' or dir == '..'
       if (File.directory?("#{save_path}#{dir}"))
         begin
           FileUtils.mkdir_p("./#{save_path}#{dir}/images")
         rescue StandardError => e
           UI.message("Failed to make directory ./#{save_path}#{dir}/images: #{e.to_s}")
           return
         end
       end
    end
    

    This is versus File.directory?("#{dir}") in that if statement. I thought that the Ruby Dir and FileUtil libraries would look relative to the current directory for the remainder of the path.