Class: PuppetPotGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_pot_generator.rb,
lib/puppet_pot_generator/version.rb

Overview

version of this gem

Constant Summary collapse

TRANSLATE_FUNCTION =
'translate'.freeze
PATH_TO_MANIFESTS =
'./manifests/**/*.pp'.freeze
PATH_TO_LOCALES =
'./locales'.freeze
PUPPET_POT_FILE =
File.join(PATH_TO_LOCALES, 'puppet.pot')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePuppetPotGenerator

Returns a new instance of PuppetPotGenerator.



23
24
25
# File 'lib/puppet_pot_generator.rb', line 23

def initialize
  @potgenerator_visitor ||= Puppet::Pops::Visitor.new(nil, 'potgenerator', 0, 0)
end

Instance Attribute Details

#metricsObject (readonly)

Returns the value of attribute metrics.



5
6
7
# File 'lib/puppet_pot_generator.rb', line 5

def metrics
  @metrics
end

Class Method Details

.generate_puppet_pot_fileObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/puppet_pot_generator.rb', line 11

def self.generate_puppet_pot_file
  raise "PuppetPotGenerator: #{PATH_TO_LOCALES} folder does not exist" unless File.directory?(PATH_TO_LOCALES)
  raise 'PuppetPotGenerator: requires version 5 of greater of puppet' unless Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0
  parser = Puppet::Pops::Parser::EvaluatingParser.new
  ppg = PuppetPotGenerator.new
  open(PUPPET_POT_FILE, 'w') { |file| file << ppg.header }
  Dir[PATH_TO_MANIFESTS].each do |file|
    program = parser.parse_file(file)
    ppg.compute(program)
  end
end

Instance Method Details

#compute(target) ⇒ Object



27
28
29
30
# File 'lib/puppet_pot_generator.rb', line 27

def compute(target)
  @path = []
  target._pcore_all_contents(@path) { |element| potgenerator(element) }
end

#headerObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/puppet_pot_generator.rb', line 32

def header
  <<-HEADER
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\\n"
"Report-Msgid-Bugs-To: \\n"
"POT-Creation-Date: #{Time.now}\\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
"Language-Team: LANGUAGE <[email protected]>\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"X-Generator: Translate Toolkit 2.0.0\\n"

HEADER
end

#potgenerator(o) ⇒ Object



68
69
70
# File 'lib/puppet_pot_generator.rb', line 68

def potgenerator(o)
  @potgenerator_visitor.visit_this_0(self, o)
end

#potgenerator_CallExpression(o) ⇒ Object

rubocop:disable Style/MethodName



74
75
76
# File 'lib/puppet_pot_generator.rb', line 74

def potgenerator_CallExpression(o) # rubocop:disable Style/MethodName
  report_on_translate_function(o)
end

#potgenerator_Object(o) ⇒ Object

rubocop:disable Style/MethodName



72
# File 'lib/puppet_pot_generator.rb', line 72

def potgenerator_Object(o); end

#report_on_translate_function(statement) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet_pot_generator.rb', line 51

def report_on_translate_function(statement)
  return unless statement.functor_expr.class == Puppet::Pops::Model::QualifiedName && statement.functor_expr.value == TRANSLATE_FUNCTION
  line_number = statement.arguments[0].locator.line_for_offset(statement.arguments[0].offset)
  path_to_file = statement.arguments[0].file
  string_to_be_translated = if defined?(statement.arguments[0].segments)
                              statement.arguments[0].segments[0].value
                            else
                              statement.arguments[0].value
                            end
  pot_entry =  '#. ' + path_to_file + ':' + line_number.to_s + "\n"
  pot_entry << 'msgid "' + string_to_be_translated + "\"\n"
  pot_entry << "msgstr \"\"\n"
  open(PUPPET_POT_FILE, 'a') do |file|
    file << pot_entry
  end
end