summaryrefslogtreecommitdiff
path: root/tooling/lib/tooling/helpers/file_handler.rb
blob: 88248e31df2de054dc95b0dffa58b51b8142f54f (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
# frozen_string_literal: true

require 'fileutils'

module Tooling
  module Helpers
    module FileHandler
      def read_array_from_file(file)
        FileUtils.touch file

        File.read(file).split(' ')
      end

      def write_array_to_file(file, content_array, append: true)
        FileUtils.touch file

        # We sort the array to make it easier to read the output file
        content_array.sort!

        output_content =
          if append
            [File.read(file), *content_array].join(' ').lstrip
          else
            content_array.join(' ')
          end

        File.write(file, output_content)
      end
    end
  end
end