Class: JavaKeystore

Inherits:
Object
  • Object
show all
Defined in:
lib/calabash-android/java_keystore.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location, keystore_alias, password) ⇒ JavaKeystore

Returns a new instance of JavaKeystore.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/calabash-android/java_keystore.rb', line 3

def initialize(location, keystore_alias, password)
  raise "No such keystore file '#{location}'" unless File.exists?(File.expand_path(location))
  log "Reading keystore data from keystore file '#{File.expand_path(location)}'"

  keystore_data = system_with_stdout_on_success(Env.keytool_path, '-list', '-v', '-alias', keystore_alias, '-keystore', location, '-storepass', password, '-J"-Dfile.encoding=utf-8"')
  if keystore_data.nil?
    error = "Could not list certificates in keystore. Probably because the password was incorrect."
    @errors = [{:message => error}]
    log error
    raise error
    #TODO: Handle the case where password is correct but the alias is missing.
  end
  @location = location
  @keystore_alias = keystore_alias
  @password = password
  log "Key store data:"
  log keystore_data
  @fingerprint = extract_md5_fingerprint(keystore_data)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



2
3
4
# File 'lib/calabash-android/java_keystore.rb', line 2

def errors
  @errors
end

#fingerprintObject (readonly)

Returns the value of attribute fingerprint.



2
3
4
# File 'lib/calabash-android/java_keystore.rb', line 2

def fingerprint
  @fingerprint
end

#keystore_aliasObject (readonly)

Returns the value of attribute keystore_alias.



2
3
4
# File 'lib/calabash-android/java_keystore.rb', line 2

def keystore_alias
  @keystore_alias
end

#locationObject (readonly)

Returns the value of attribute location.



2
3
4
# File 'lib/calabash-android/java_keystore.rb', line 2

def location
  @location
end

#passwordObject (readonly)

Returns the value of attribute password.



2
3
4
# File 'lib/calabash-android/java_keystore.rb', line 2

def password
  @password
end

Class Method Details

.fail_if_key_missing(map, key) ⇒ Object



87
88
89
# File 'lib/calabash-android/java_keystore.rb', line 87

def self.fail_if_key_missing(map, key)
  raise "Found .calabash_settings but no #{key} defined." unless map[key]
end

.get_keystoresObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/calabash-android/java_keystore.rb', line 62

def self.get_keystores
  if keystore = keystore_from_settings 
    [ keystore ]
  else
    [
      read_keystore_with_default_password_and_alias(File.join(ENV["HOME"], "/.android/debug.keystore")),
      read_keystore_with_default_password_and_alias("debug.keystore"),
      read_keystore_with_default_password_and_alias(File.join(ENV["HOME"], ".local/share/Xamarin/Mono\\ for\\ Android/debug.keystore")),
      read_keystore_with_default_password_and_alias(File.join(ENV["HOME"], "AppData/Local/Xamarin/Mono for Android/debug.keystore")),
    ].compact
  end
end

.keystore_from_settingsObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/calabash-android/java_keystore.rb', line 75

def self.keystore_from_settings
    keystore = JSON.parse(IO.read(".calabash_settings")) if File.exist? ".calabash_settings"
    keystore = JSON.parse(IO.read("calabash_settings")) if File.exist? "calabash_settings"
    return unless keystore
    fail_if_key_missing(keystore, "keystore_location")
    fail_if_key_missing(keystore, "keystore_password")
    fail_if_key_missing(keystore, "keystore_alias")
    keystore["keystore_location"] = File.expand_path(keystore["keystore_location"])
    log("Keystore location specified in #{File.exist?(".calabash_settings") ? ".calabash_settings" : "calabash_settings"}.")
    JavaKeystore.new(keystore["keystore_location"], keystore["keystore_alias"], keystore["keystore_password"])
end

.read_keystore_with_default_password_and_alias(path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/calabash-android/java_keystore.rb', line 44

def self.read_keystore_with_default_password_and_alias(path)
  path = File.expand_path path

  if File.exists? path
    keystore = JavaKeystore.new(path, 'androiddebugkey', 'android')
    if keystore.errors
      log "Trying to "
      nil
    else
      log "Unlocked keystore at #{path} - fingerprint: #{keystore.fingerprint}"
      keystore
    end
  else
    log "Trying to read keystore from: #{path} - no such file"
    nil
  end
end

Instance Method Details

#sign_apk(apk_path, dest_path) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/calabash-android/java_keystore.rb', line 23

def sign_apk(apk_path, dest_path)
  raise "Cannot sign with a miss configured keystore" if errors
  raise "No such file: #{apk_path}" unless File.exists?(apk_path)

  unless system_with_stdout_on_success(Env.jarsigner_path, '-sigalg', 'MD5withRSA', '-digestalg', 'SHA1', '-signedjar', dest_path, '-storepass', password, '-keystore',  location, apk_path, keystore_alias)
    raise "Could not sign app: #{apk_path}"
  end
end

#system_with_stdout_on_success(cmd, *args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/calabash-android/java_keystore.rb', line 32

def system_with_stdout_on_success(cmd, *args)
  a = Escape.shell_command(args)
  cmd = "#{cmd} #{a.gsub("'", '"')}"
  log cmd
  out = `#{cmd}`
  if $?.exitstatus == 0
    out
  else
    nil
  end
end