summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/pathutil-0.16.2/lib/pathutil/helpers.rb
blob: 7398008dba3589b1fb6c49722ad26b8b22110524 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Frozen-string-literal: true
# Copyright: 2015 - 2017 Jordon Bedwell - MIT License
# Encoding: utf-8

class Pathutil
  module Helpers
    extend self

    # --

    def allowed
      return @allowed ||= begin
        {
          :yaml => {
            :classes => [],
            :symbols => []
          }
        }
      end
    end

    # --
    # Wraps around YAML and SafeYAML to provide alternatives to Rubies.
    # @note We default aliases to yes so we can detect if you explicit true.
    # @return Hash
    # --
    def load_yaml(data, safe: true, whitelist_classes: allowed[:yaml][:classes], \
        whitelist_symbols: allowed[:yaml][:symbols], aliases: :yes)

      require "yaml"
      unless safe
        return YAML.load(
          data
        )
      end

      if !YAML.respond_to?(:safe_load)
        setup_safe_yaml whitelist_classes, aliases
        SafeYAML.load(
          data
        )

      else
        YAML.safe_load(
          data,
          whitelist_classes,
          whitelist_symbols,
          aliases
        )
      end
    end

    # --
    # Make a temporary name suitable for temporary files and directories.
    # @return String
    # --
    def make_tmpname(prefix = "", suffix = nil, root = nil)
      prefix = tmpname_prefix(prefix)
      suffix = tmpname_suffix(suffix)

      root ||= Dir::Tmpname.tmpdir
      File.join(root, __make_tmpname(
        prefix, suffix
      ))
    end

    # --
    private
    def __make_tmpname((prefix, suffix), number)
      prefix &&= String.try_convert(prefix) || tmpname_agerr(:prefix, prefix)
      suffix &&= String.try_convert(suffix) || tmpname_agerr(:suffix, suffix)

      time = Time.now.strftime("%Y%m%d")
      path = "#{prefix}#{time}-#{$$}-#{rand(0x100000000).to_s(36)}".dup
      path << "-#{number}" if number
      path << suffix if suffix
      path
    end

    private
    def tmpname_agerr(type, val)
      raise ArgumentError, "unexpected #{type}: #{val.inspect}"
    end

    # --
    private
    def tmpname_suffix(suffix)
      suffix = suffix.join("-") if suffix.is_a?(Array)
      suffix = suffix.gsub(/\A\-/, "") unless !suffix || suffix.empty?
      suffix
    end

    # --
    # Cleanup the temp name prefix, joining if necessary.
    # rubocop:disable Style/ParallelAssignment
    # --
    private
    def tmpname_prefix(prefix)
      ext, prefix = prefix, "" if !prefix.is_a?(Array) && prefix.start_with?(".")
      ext = prefix.pop if prefix.is_a?(Array) && prefix[-1].start_with?(".")
      prefix = prefix.join("-") if prefix.is_a?(Array)

      unless prefix.empty?
        prefix = prefix.gsub(/\-\Z/, "") \
          + "-"
      end

      return [
        prefix, ext || ""
      ]
    end

    # --
    # Wrap around, cleanup, deprecate and use SafeYAML.
    # rubocop:enable Style/ParallelAssignment
    # --
    private
    def setup_safe_yaml(whitelist_classes, aliases)
      warn "WARN: SafeYAML does not support disabling  of aliases." if aliases && aliases != :yes
      warn "WARN: SafeYAML will be removed when Ruby 2.0 goes EOL."
      require "safe_yaml/load"

      SafeYAML.restore_defaults!
      whitelist_classes.map(&SafeYAML.method(
        :whitelist_class!
      ))
    end
  end
end