Module: Pray::ManifestMethods

Included in:
Pray
Defined in:
lib/pray/manifest.rb,
lib/pray/manifest_parser.rb,
lib/pray/manifest_formatter.rb,
lib/pray/manifest_constraint.rb,
lib/pray/manifest_parser_blocks.rb,
lib/pray/manifest_parser_helpers.rb

Defined Under Namespace

Modules: ParserBlocks, ParserHelpers Classes: BlockParser

Constant Summary collapse

PACKAGE_KEYWORDS =
%w[pray use include agent package].freeze

Class Method Summary collapse

Class Method Details

.format_package_declaration(package) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pray/manifest_formatter.rb', line 9

def format_package_declaration(package)
  parts = ["pray \"#{package.name}\""]
  parts << "\"#{package.constraint}\"" unless package.constraint == "*"
  parts << "path: \"#{package.path}\"" if package.path
  parts << "source: \"#{package.source}\"" if package.source
  parts << "git: \"#{package.git}\"" if package.git
  parts << "tag: \"#{package.tag}\"" if package.tag
  parts << "rev: \"#{package.rev}\"" if package.rev
  parts << "tarball: \"#{package.tarball}\"" if package.tarball
  parts << "oci: \"#{package.oci}\"" if package.oci
  parts << "file: \"#{package.file}\"" if package.file
  unless package.exports.empty?
    parts << if package.exports.length == 1
      "export: \"#{package.exports.first}\""
    else
      "exports: [#{format_string_keyword_list(package.exports)}]"
    end
  end
  parts << "targets: [#{format_string_keyword_list(package.targets)}]" unless package.targets.empty?
  parts << "features: [#{format_string_keyword_list(package.features)}]" unless package.features.empty?
  parts << "optional: true" if package.optional
  parts.join(", ")
end

.format_string_keyword_list(values) ⇒ Object



56
57
58
# File 'lib/pray/manifest_formatter.rb', line 56

def format_string_keyword_list(values)
  values.map { |value| "\"#{value}\"" }.join(", ")
end

.parse_manifest(text) ⇒ Object



119
120
121
122
# File 'lib/pray/manifest.rb', line 119

def parse_manifest(text)
  lines = Literal.prepare_parser_lines(text)
  BlockParser.new(lines).parse_root
end

.parse_quoted(input) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/pray/manifest_constraint.rb', line 56

def parse_quoted(input)
  quote = input[0]
  return unless quote == "\"" || quote == "'"

  rest = input[1..]
  ending = rest.index(quote)
  return unless ending

  [rest[0, ending], rest[(ending + 1)..]]
end

.read_manifest_text(manifest_path) ⇒ Object



113
114
115
116
117
# File 'lib/pray/manifest.rb', line 113

def read_manifest_text(manifest_path)
  File.read(manifest_path)
rescue Errno::ENOENT
  raise Error.manifest("missing #{manifest_path}; run pray init to create one")
end

.replace_package_declaration(text, package) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pray/manifest_formatter.rb', line 33

def replace_package_declaration(text, package)
  name = package.name
  prefixes = [
    "pray \"#{name}\"", "pray '#{name}'",
    "use \"#{name}\"", "include \"#{name}\"",
    "agent \"#{name}\"", "agent '#{name}'",
    "package \"#{name}\"", "package '#{name}'"
  ]
  lines = text.lines.map(&:chomp)
  replaced = 0
  lines.each_index do |index|
    trimmed = lines[index].lstrip
    next unless prefixes.any? { |prefix| trimmed.start_with?(prefix) }

    lines[index] = rewrite_constraint_on_line(lines[index], package.constraint)
    replaced += 1
  end
  raise Error.manifest("package #{name} not found in manifest") if replaced.zero?
  output = lines.join("\n")
  output += "\n" if text.end_with?("\n") && !output.end_with?("\n")
  output
end

.rewrite_constraint_on_line(line, constraint) ⇒ Object



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
# File 'lib/pray/manifest_constraint.rb', line 9

def rewrite_constraint_on_line(line, constraint)
  indent = line[/\A\s*/]
  trimmed = line.lstrip
  after_keyword = skip_package_keyword(trimmed)
  raise Error.manifest("package declaration is missing a keyword") unless after_keyword

  after_keyword = after_keyword.lstrip
  parsed_name = parse_quoted(after_keyword)
  raise Error.manifest("package declaration is missing a quoted name") unless parsed_name

  name, after_name = parsed_name
  quoted_constraint = "\"#{constraint}\""
  keyword_and_name = trimmed[0, trimmed.length - after_name.length]
  remainder = after_name.lstrip
  return "#{indent}#{keyword_and_name}, #{quoted_constraint}" if remainder.empty?
  unless remainder.start_with?(",")
    raise Error.manifest("package #{name} declaration is missing a comma after the name")
  end

  after_comma = remainder[1..].lstrip
  if after_comma.start_with?("\"", "'")
    parsed_constraint = parse_quoted(after_comma)
    unless parsed_constraint
      raise Error.manifest("package #{name} declaration has an unclosed constraint")
    end

    return "#{indent}#{keyword_and_name}, #{quoted_constraint}#{parsed_constraint[1]}"
  end

  "#{indent}#{keyword_and_name}, #{quoted_constraint}, #{after_comma}"
end

.skip_package_keyword(input) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pray/manifest_constraint.rb', line 41

def skip_package_keyword(input)
  PACKAGE_KEYWORDS.each do |keyword|
    next unless input.start_with?(keyword)

    rest = input[keyword.length..]
    next_character = rest[0]
    whitespace_or_quote = next_character.nil? ||
      next_character.match?(/\s/) ||
      next_character == "\"" ||
      next_character == "'"
    return rest if whitespace_or_quote
  end
  nil
end