-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtextsCreator.rb
executable file
·81 lines (65 loc) · 1.84 KB
/
textsCreator.rb
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
32
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env ruby
require "rexml/document"
require "xcodeproj"
require "fileutils"
include REXML
def error(message="Unknown error")
puts "ERROR: #{message}"
exit 1
end
pathToTrema = ARGV[1]
if ARGV.count < 2
error("amount of arguments is not enough")
end
if ARGV[2].nil?
structName = "Texts"
dirPath = "src/main/resources/trema/"
textsPath = "src/main/resources/trema/" + structName + ".swift"
else
textsPath = ARGV[2]
structName = textsPath.split("/").last.split(".").first
dirPath = ""
textsPath.split("/").each do |subString|
if !(subString == structName + ".swift")
dirPath += subString
dirPath += "/"
end
end
end
file = File.open(pathToTrema).read
doc = Document.new file
root = doc.root
shouldAdd = false
if File.file?(textsPath)
outfile = File.open(textsPath,"w")
else
outfile = File.new(textsPath,"w")
shouldAdd = true
end
outfile.puts "import GirdersSwift\n\n"
outfile.puts "struct " + structName + " { \n"
doc.elements.each("trema/text") do |e|
e.elements.each("context") do |c|
if c.has_text?()
outfile.puts " // " + c.text()
end
end
camelizedName = e.attributes["key"].split('_').inject { |m, p| m + p.capitalize }
outfile.puts " static let " + camelizedName + " = " + "translate(\"" + e.attributes["key"] + "\")"
end
outfile.puts "\n}"
outfile.close
if shouldAdd
product_name = ARGV[0]
project_file = product_name + ".xcodeproj"
project = Xcodeproj::Project.open(project_file)
# Add a file to the project in the main group
file_name = structName + ".swift"
xcFile = project[dirPath].new_file(file_name)
## Add the file to the main target
project.targets.each do |target|
target.add_file_references([xcFile])
end
## Save the project file
project.save()
end