Class: Synvert::Core::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/synvert/core/utils.rb

Class Method Summary collapse

Class Method Details

.eval_snippet(snippet_name) ⇒ Object



11
12
13
# File 'lib/synvert/core/utils.rb', line 11

def eval_snippet(snippet_name)
  eval(load_snippet(snippet_name), binding, "(eval #{snippet_name})")
end

.glob(file_patterns) ⇒ Array<String>

Glob file paths.

Parameters:

  • file_patterns (Array<String>)

    file patterns

Returns:

  • (Array<String>)

    file paths



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/synvert/core/utils.rb', line 37

def glob(file_patterns)
  Dir.chdir(Configuration.root_path) do
    all_files = file_patterns.flat_map { |pattern| Dir.glob(pattern) }
    ignored_files = []

    if Configuration.respect_gitignore
      Open3.popen3('git check-ignore --stdin') do |stdin, stdout, _stderr, _wait_thr|
        stdin.puts(all_files.join("\n"))
        stdin.close

        ignored_files = stdout.read.split("\n")
      end
    end

    filtered_files = filter_only_paths(all_files - ignored_files)

    filtered_files -= get_explicitly_skipped_files
  end
end

.load_snippet(snippet_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/synvert/core/utils.rb', line 15

def load_snippet(snippet_name)
  if is_valid_url?(snippet_name)
    uri = URI.parse(format_url(snippet_name))
    return uri.open.read if remote_snippet_exists?(uri)

    raise Errors::SnippetNotFound.new("#{snippet_name} not found")
  elsif is_valid_file?(snippet_name)
    return File.read(snippet_name, encoding: 'UTF-8')
  else
    snippet_path = snippet_expand_path(snippet_name)
    return File.read(snippet_path, encoding: 'UTF-8') if File.exist?(snippet_path)

    snippet_uri = URI.parse(format_url(remote_snippet_url(snippet_name)))
    return snippet_uri.open.read if remote_snippet_exists?(snippet_uri)

    raise Errors::SnippetNotFound.new("#{snippet_name} not found")
  end
end