-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwine2xls.rb
More file actions
executable file
·197 lines (168 loc) · 6.51 KB
/
Copy pathtwine2xls.rb
File metadata and controls
executable file
·197 lines (168 loc) · 6.51 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env ruby
#
# Questo script serve per automatizzare il processo di aggiornamento delle strighe di traduzione
# Tra file twine ed excel
#
#
# © Tiknil
#
############################################################################################################################################
## FUNCTIONS
############################################################################################################################################
$version = '0.0.1'
$year = '2018'
$section_background_color = 'a3fff5'
#definizione lingue gestite
$languages = {
"it" => {:desc => "Italiano (it)", :index => 1},
"en" => {:desc => "Inglese (en)", :index => 2}
}
############################################################################################################################################
## MAIN PROGRAM
############################################################################################################################################
if ARGV.length == 0 || ARGV[0] == 'help'
puts "Twine2xls\nby Tiknil © #{$year} - Version " + $version + "\n\n"
end
begin
# Controllo dipendenze
require 'thor'
require 'fileutils' # per varie utilità sul file system
require 'terminal-table' # per visualizzare tabelle ASCII nel terminale
require 'colorize' # per colorare gli output nel terminale
require 'tempfile' # per creare dei file temporanei in memoria
require 'ruby-progressbar' # per le progress bar nel terminale
require 'tty-spinner' # per gli spinner indefiniti nel terminale
require 'rubygems' # dipendenza di rubyXL
require 'rubyXL' # per leggere e scrivere i file xlsx
require 'rubyXL/convenience_methods'
rescue LoadError
# Installazione dipendenze
puts "Installazione dipendenze mancanti in corso..."
# Non posso usare Utils.require_authentication in questo punto
if Process.uid != 0
abort "Il comando dev'essere eseguito con privilegi di amministratore (sudo)"
end
result = system 'gem install thor & gem install fileutils & gem install terminal-table & gem install colorize & gem install tempfile & gem install ruby-progressbar & gem install tty-spinner'
if result == true
puts "Esegui nuovamente il comando richiesto, le dipendenze sono state installate correttamente"
else
puts "C'è stato un errore durante l'installazione di alcune dipendenze"
end
abort
end
class Twine2xls < Thor
desc "export", "Esporta il file twine in xslx"
option :input, :required => true, :type => :string, :desc => 'Il file twine da esportare', :aliases => '-i'
option :output, :default => 'output.xlsx', :type => :string, :desc => 'Il percorso del file Excel di output', :aliases => '-o'
def export
#puts "args: #{options[:input]}, #{options[:output]}"
if !File.exist? options[:input]
abort "Il file #{options[:input]} non esiste"
end
if File.exist? options[:output]
puts "Il file #{options[:output]} esiste già, sovrascriverlo? (Y/n)"
confirm = STDIN.gets.chomp
if confirm != "Y"
abort
end
end
# Predispongo l'oggetto Excel
workbook = RubyXL::Workbook.new
worksheet = workbook[0]
worksheet.sheet_name = 'Traduzioni'
#Prima colonna, CHIAVE
#Headers
worksheet.add_cell 0,0,"Keys"
$languages.each do |key, value|
worksheet.add_cell 0, value[:index] , value[:desc]
worksheet.change_column_width(value[:index], 100)
end
row_index = 0
#worksheet.change_column_fill(0, 'e7fd55') #Sfondo colonna chiavi
worksheet.change_row_bold(0, true)
worksheet.change_row_border(0, :bottom, 'medium')
spinner = TTY::Spinner.new(":spinner Parsing del file twine in corso... \r", :clear => true, :hide_cursor => true)
spinner.auto_spin
File.open(options[:input], 'r') do |file|
file.each_line do |line|
trimmed_line = line.strip
if trimmed_line.start_with? "[["
#Section
section = trimmed_line.gsub('[','').gsub(']','')
row_index += 1
worksheet.add_cell row_index, 0, section
worksheet.change_row_fill(row_index, $section_background_color)
worksheet.sheet_data[row_index][0].change_font_bold(true)
worksheet.merge_cells(row_index, 0, row_index, 5)
elsif trimmed_line.start_with? "["
#Key
key = trimmed_line.gsub('[','').gsub(']','')
row_index += 1
worksheet.add_cell row_index, 0, key
else
#Localizations
elements = trimmed_line.split " = "
found = false
if elements.length > 0
$languages.each do |key, value|
if elements[0].strip == key
found = true
worksheet.add_cell row_index, value[:index], elements[1]
end
end
end
if !found
#row_index += 1
end
end
end
end
# Scrivo il file Excel nel percorso di output
workbook.write(options[:output])
File.chmod(0777, options[:output])
spinner.success
end
desc "import", "Importa il file xlsx nel file twine"
option :input, :required => true, :type => :string, :desc => 'Il file xlsx da importare in twine', :aliases => '-i'
option :output, :default => 'output.txt', :type => :string, :desc => 'Il percorso del file twine di output', :aliases => '-o'
def import
puts "args: #{options[:input]}, #{options[:output]}"
if !File.exist? options[:input]
abort "Il file #{options[:input]} non esiste"
end
if File.exist? options[:output]
puts "Il file #{options[:output]} esiste già, sovrascriverlo? (Y/n)"
confirm = STDIN.gets.chomp
if confirm != "Y"
abort
end
end
workbook = RubyXL::Parser.parse(options[:input])
worksheet = workbook.worksheets[0]
File.open(options[:output], 'w') do |out|
worksheet.each { |row|
if row.index_in_collection > 0
if row.size == 0
out << "\n"
elsif row.size == 1
out << "\n[[#{row[0].value}]]\n"
else
if row[0] == nil || row[0].value == nil || row[0].value.to_s.empty?
out << "\n"
else
out << "\t[#{row[0].value}]\n"
$languages.each do |key, value|
if row[value[:index]] != nil
out << "\t\t#{key} = #{row[value[:index]].value}\n"
end
end
end
end
end
}
File.chmod(0777, options[:output])
end
end
end
#Command line entry point
Twine2xls.start(ARGV)