Class: Getlocal::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/getlocal/cli.rb

Instance Method Summary collapse

Instance Method Details

#fetch(project) ⇒ Object



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
# File 'lib/getlocal/cli.rb', line 23

def fetch(project)

  # Check if we are in the right place
  if Dir.glob('*.lproj').empty?
    puts "Wrong directory please select the directory that contains the .lproj folders"
    return
  end

  username = options[:user]

  # Check if we need to ask for a password
  if options[:password]
    password = options[:password]
  else
    print "Password:"
    password = STDIN.noecho(&:gets).chomp
    puts ""
  end

  p = PowerBar.new

  auth = {:username => username, :password => password}

  # Find all the languages we support
  supportedLanguages = []
  Dir.glob("*.lproj") do |filePath|
    f = File.basename(filePath, ".*")
    supportedLanguages << f unless f == "Base"
  end

  puts "Fetching localisations for" if options[:verbose]
  puts "" if options[:verbose]

  total = Dir.glob("Base.lproj/*.strings").count * supportedLanguages.count
  current = 0

  # Loop through the string files we have localy
  Dir.glob("Base.lproj/*.strings") do |stringFilePath|

    fileName = File.basename(stringFilePath)
    puts "-- #{fileName} --" if options[:verbose]
    puts "" if options[:verbose]

    # Request the translations for each supported language
    supportedLanguages.each do |lang|

      current = current.next
      p.show(:msg => 'Fetching Translation', :done => current, :total => total) unless options[:verbose]

      tempfile = Tempfile.new("file")

      puts "Fetching #{lang} for #{fileName}." if options[:verbose]
      begin
        response = HTTParty.get("https://api.getlocalization.com/#{project}/api/translations/file/#{fileName}/#{lang}/", :basic_auth => auth, :timeout => options[:timeout])
      rescue
        puts "Oh no, somthing fucked up."
        return
      else
        if response.code == 200
          puts "File downloaded" if options[:verbose]
          tempfile.binmode # This might not be necessary depending on the zip file
          tempfile.write(response.body)


          destFolder = lang + ".lproj"
          destFile = fileName

          destPath = destFolder + '/' + destFile

          puts "moveing translations to #{destPath}" if options[:verbose]

          if Dir.exists?(destFolder)
            File.delete(destPath) if File.exist?(destPath)
            FileUtils.mv(tempfile.path, destPath)
          else
            puts destFolder + " folder not found. Couldn't import " + destFile if options[:verbose]
          end
        elsif response.code == 401
          puts "The username or password are invailed"
          return
        else
          puts "Bad response. Close but no cigar."
          puts "Sorry couldn't get #{lang} translations this time."
        end
      ensure
        tempfile.close
      end
      puts "" if options[:verbose]
    end
    puts "" if options[:verbose]
    puts "" if options[:verbose]
  end

end

#update(project) ⇒ Object



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
# File 'lib/getlocal/cli.rb', line 121

def update(project)
  username = options[:user]

  # Check if we need to ask for a password
  if options[:password]
    password = options[:password]
  else
    print "Password:"
    password = STDIN.noecho(&:gets).chomp
    puts ""
  end

  if !options[:verbose] then
    Thread.new do
      #set up spinner
      glyphs = ['|', '/', '-', "\\"]
      while true
        glyphs.each do |g|
          print "\r#{g}"
          sleep 0.15
        end
      end
    end
  end

  auth = {:username => username, :password => password}

  puts "Requesting the list of master files" if options[:verbose]
  response = HTTParty.get("https://api.getlocalization.com/#{project}/api/list-master/json/", :basic_auth => auth)

  if response.code == 200 then
    parsedResponse = JSON.parse(response.body)
    if parsedResponse['success'] == "1"
      puts "Recived list" if options[:verbose]
      currentMasterFiles = parsedResponse['master_files']
    else
      puts "couldn't fetch list of master files"
      return
    end
  else
    puts "couldn't fetch list of master files"
    return
  end

  Dir.glob("Base.lproj/*.strings") do |stringFilePath|

    alreadyExists = currentMasterFiles.include?(stringFilePath.gsub("Base.lproj/", ""))

    body = {"file" => File.new(stringFilePath)}

    if alreadyExists
      # Update master
      puts "Updateing " + stringFilePath if options[:verbose]
      response = HTTMultiParty.post("https://api.getlocalization.com/#{project}/api/update-master/", :basic_auth => auth, :query => body)
    else
      #Upload new master
      puts "Creating " + stringFilePath if options[:verbose]
      response = HTTMultiParty.post("https://api.getlocalization.com/#{project}/api/create-master/ios/en/", :basic_auth => auth, :query => body)
    end

    puts "Upload complete with responce code #{response.code}" if options[:verbose]
    puts "" if options[:verbose]
  end

end