Sha256: 3e1949741a962909a7f9f1c0ab249bdbb4572953e40d73cd99121b25b92eec2e

Contents?: true

Size: 1.48 KB

Versions: 20

Compression:

Stored size: 1.48 KB

Contents

module Roger
  # The cleaner safely cleans up paths
  class Release::Cleaner
    def initialize(pattern)
      @pattern = [pattern].flatten
    end

    # We switch to the build path and append the globbed files for safety, so even if you manage
    # to sneak in a pattern like "/**/*" it won't do you any good as it will be reappended
    # to the path
    def call(release, _options = {})
      Dir.chdir(release.build_path.to_s) do
        @pattern.each do |pattern|
          Dir.glob(pattern).each do |file|
            clean_path(release, file)
          end
        end
      end
    end

    def clean_path(release, file)
      path = File.join(release.build_path.to_s, file)
      if inside_build_path?(release.build_path, path)
        release.log(self, "Cleaning up \"#{path}\" in build")
        rm_rf(path)
        true
      else
        release.log(self, "FAILED cleaning up \"#{path}\" in build")
        false
      end
    end

    protected

    def inside_build_path?(build_path, path)
      begin
        build_path = Pathname.new(build_path).realpath.to_s
        path = Pathname.new(path)
        if path.absolute?
          path = path.realpath.to_s
        else
          path = Pathname.new(File.join(build_path.to_s, path)).realpath.to_s
        end
      rescue Errno::ENOENT
        # Real path does not exist
        return false
      end

      if path[build_path]
        return true
      else
        fail "Cleaning pattern is not inside build directory"
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
roger-1.7.0 lib/roger/release/cleaner.rb
roger-1.6.4 lib/roger/release/cleaner.rb
roger-1.6.3 lib/roger/release/cleaner.rb
roger-1.6.2 lib/roger/release/cleaner.rb
roger-1.6.1 lib/roger/release/cleaner.rb
roger-1.6.0 lib/roger/release/cleaner.rb
roger-1.5.0 lib/roger/release/cleaner.rb
roger-1.4.6 lib/roger/release/cleaner.rb
roger-1.4.5 lib/roger/release/cleaner.rb
roger-1.4.4 lib/roger/release/cleaner.rb
roger-1.4.3 lib/roger/release/cleaner.rb
roger-1.4.2 lib/roger/release/cleaner.rb
roger-1.3.5 lib/roger/release/cleaner.rb
roger-1.4.1 lib/roger/release/cleaner.rb
roger-1.4.0 lib/roger/release/cleaner.rb
roger-1.3.4 lib/roger/release/cleaner.rb
roger-1.3.3 lib/roger/release/cleaner.rb
roger-1.3.2 lib/roger/release/cleaner.rb
roger-1.2.2 lib/roger/release/cleaner.rb
roger-1.2.1 lib/roger/release/cleaner.rb