Class: Terrestrial::Cli::Editor::Swift

Inherits:
BaseEditor
  • Object
show all
Defined in:
lib/terrestrial/cli/editor/swift.rb

Class Method Summary collapse

Methods inherited from BaseEditor

edit_file

Class Method Details

.a_string_not_followed_by_translated(string) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/terrestrial/cli/editor/swift.rb', line 75

def self.a_string_not_followed_by_translated(string)
  # Does not match:
  #
  #   @"foo".translated
  #   or
  #   @"foo" translatedWithContext
  #
  #   (?!(\.|\s)translated) means don't match either
  #   period or single whitepspace character,
  #   followed by translated


  /"#{Regexp.quote(string)}"(?!\.translated)/
end

.add_import(file) ⇒ Object



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
# File 'lib/terrestrial/cli/editor/swift.rb', line 33

def self.add_import(file)
  # Adds import Terrestrial as the last import
  # statement at the top of the file.
  #
  # It goes through the file from top to bottom looking for the first import
  # statement. After it finds it, it will look for the first line without
  # an import statement. When it finds it, it will write the import line,
  # and all following lines are just copied over.

  found_first_import = false
  imported = false

  edit_file(file) do |line, line_number, file|
    # Detect first import statement
    if !found_first_import && line.start_with?("import ")
      found_first_import = true
      file.puts line
    # Terrestrial had already been imported
    elsif line.start_with?("import Terrestrial")
      imported = true
      file.puts line
    # Not imported, had found first import, and doesn't start with "import"
    #   -> import
    elsif !imported && found_first_import && !line.start_with?("import ")
      file.puts "import Terrestrial"
      file.puts ""
      imported = true
    # Copy over as normal
    else
      file.puts line
    end
  end
end

.build_string_with_variables(entry) ⇒ Object



29
30
31
# File 'lib/terrestrial/cli/editor/swift.rb', line 29

def self.build_string_with_variables(entry)
  "NSString(format: \"#{entry.identifier}\".translated, #{swift_variables(entry.string).join(", ")})"
end

.do_edit_string(line, entry) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/terrestrial/cli/editor/swift.rb', line 17

def self.do_edit_string(line, entry)
  if has_swift_variables? entry.string
    edit_with_variables(line, entry)
  else
    line.gsub(a_string_not_followed_by_translated(entry.string), "\"#{entry.identifier}\".translated")
  end
end

.edit_with_variables(line, entry) ⇒ Object



25
26
27
# File 'lib/terrestrial/cli/editor/swift.rb', line 25

def self.edit_with_variables(line, entry)
  line.gsub(a_string_not_followed_by_translated(entry.string), build_string_with_variables(entry))
end

.find_and_edit_line(new_string) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/terrestrial/cli/editor/swift.rb', line 6

def self.find_and_edit_line(new_string)
  edit_file(new_string.file) do |line, line_number, file|
    if line_number == new_string.line_number
      file.puts do_edit_string(line, new_string)
    else
      file.puts line
    end
    line_number += 1
  end
end

.has_swift_variables?(string) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/terrestrial/cli/editor/swift.rb', line 67

def self.has_swift_variables?(string)
  swift_variables(string).any?
end

.swift_variables(string) ⇒ Object



71
72
73
# File 'lib/terrestrial/cli/editor/swift.rb', line 71

def self.swift_variables(string)
  string.scan(Parser::Swift::VARIABLE_REGEX).map(&:first)
end