Module: Flavours

Defined in:
lib/flavours.rb,
lib/flavours/json.rb,
lib/flavours/assets.rb,
lib/flavours/colors.rb,
lib/flavours/gradle.rb,
lib/flavours/images.rb,
lib/flavours/xmlres.rb,
lib/flavours/version.rb

Constant Summary collapse

VERSION =
"0.0.10"

Class Method Summary collapse

Class Method Details

.assets_file_path(directory, m, flavour_name) ⇒ Object



8
9
10
# File 'lib/flavours/assets.rb', line 8

def self.assets_file_path directory, m, flavour_name
  return "#{directory}/#{m}/src/#{flavour_name.gsub(/[^0-9a-z ]/i, '')}/res"
end

.base_json_file_stringObject



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
# File 'lib/flavours/json.rb', line 51

def self.base_json_file_string
  return '{
  "flavours": [
      {
          "flavourName": "YourFlavor",
          "packageName": "somePackageName",
          "buildConfig": {
              "API_KEY": "someApiKey"
          },
          "iconUrl": "https://someurl.com/image.png",
          "colorsXML": {
              "primaryColor": "#E51919"
          },
          "stringsXML": {
              "someString": "someValue"
          }
      },
      {
          "flavourName": "YourFlavor2",
          "packageName": "somePackageName2",
          "buildConfig": {
              "API_KEY": "someApiKey"
          },
          "iconUrl": "https://someurl.com/image.png",
          "colorsXML": {
              "primaryColor": "#E51919"
          },
          "stringsXML": {
              "someString": "someValue"
          }
      }
  ]
}'
end

.blue(text) ⇒ Object



16
17
18
# File 'lib/flavours/colors.rb', line 16

def self.blue(text)
  colorize(text, 36)
end

.build_config_string_for_flavour(flavour) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flavours/gradle.rb', line 44

def self.build_config_string_for_flavour flavour
  if flavour['buildConfig']
    @buildconfig = ''
    flavour['buildConfig'].each_pair do |k, v|
      @buildconfig += "            buildConfigField \"String\" , \"#{k}\" ,  \"\\\"#{v}\\\"\"\n"
    end
    return @buildconfig
  end

  return ''
end

.check_for_newer_versionObject



4
5
6
7
8
# File 'lib/flavours/version.rb', line 4

def self.check_for_newer_version
  unless Gem.latest_version_for('flavours').to_s == VERSION
    Flavours::purple "\n  A newer version of flavours is available. Run '[sudo] gem update flavours'."
  end
end

.colorize(text, color_code) ⇒ Object

Main Colorize Functions



3
4
5
# File 'lib/flavours/colors.rb', line 3

def self.colorize(text, color_code)
  puts "\e[#{color_code}m#{text}\e[0m"
end

.create(flavours, directory, m) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/flavours/gradle.rb', line 9

def self.create flavours, directory, m
  @flavour_string = ''
  flavours.each do |f|
    @flavour_string += gradle_string_for_flavour f
    Flavours::green "  #{f['flavourName'].gsub(/[^0-9a-z ]/i, '')}" unless $nolog
    Flavours::create_images directory, m, f
    Flavours::create_xml_resources directory, m, f
    puts
  end

  set_and_save_flavours_text @flavour_string, directory, m
end

.create_images(directory, m, flavour_hash) ⇒ Object

Images



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/flavours/images.rb', line 12

def self.create_images directory, m, flavour_hash
  # Bad Params
  if !directory || !flavour_hash || !m
    return false
  end

  # Resize Icons
  resize_icons directory, m, flavour_hash

  # Move Drawables
  move_drawables directory, m, flavour_hash if flavour_hash['drawables']
end

.create_xml_for_hash(directory, m, xml_hash, res_folder_name, res_file_name, res_key_name, flavour_name) ⇒ Object

Main Method



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flavours/xmlres.rb', line 25

def self.create_xml_for_hash directory, m, xml_hash, res_folder_name, res_file_name, res_key_name, flavour_name
  # Create Directory
  Flavours::make_asset_directory directory, m, flavour_name, res_folder_name
  # Set Up
  res_path = Flavours::file_path_with_folder_name(directory, res_folder_name, m, flavour_name) + "/#{res_file_name}.xml"
  xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n    <resources>\n"
  # Create Strings
  if xml_hash.kind_of?(Array)
    xml_hash.each do |hash|
      xmlString += res_string res_key_name, hash['name'], hash['value'], hash['type']
    end
  else
    xml_hash.each_pair do |k, v|
      xmlString += res_string res_key_name, k, v, nil
    end
  end
  # Finish Her Off
  xmlString += '    </resources>'
  File.open(res_path, 'w') do |f|
    f.write(xmlString)
  end
end

.create_xml_resources(directory, m, flavour_hash) ⇒ Object

Create XML



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/flavours/xmlres.rb', line 8

def self.create_xml_resources directory, m, flavour_hash
  flavour_name = flavour_hash['flavourName']
  flavour_hash.each_pair do |k, v|
    if k == 'colorsXML'
      create_xml_for_hash directory, m, v, 'values', 'colors', 'color', flavour_name
      puts "  - Created colors.xml for #{flavour_name}"
    elsif k == 'stringsXML'
      create_xml_for_hash directory, m, v, 'values', 'strings', 'string', flavour_name
      puts "  - Created strings.xml for #{flavour_name}"
    elsif k == 'settingsXML'
      create_xml_for_hash directory, m, v, 'values', 'settings', 'item', flavour_name
      puts "  - Created settings.xml for #{flavour_name}"
    end
  end
end

.file_path_with_folder_name(directory, folder, m, flavour_name) ⇒ Object



12
13
14
# File 'lib/flavours/assets.rb', line 12

def self.file_path_with_folder_name directory, folder, m, flavour_name
  return "#{assets_file_path directory, m, flavour_name}/#{folder}"
end

.gradle_directory(directory, m) ⇒ Object



22
23
24
# File 'lib/flavours/gradle.rb', line 22

def self.gradle_directory directory, m
  return directory + '/' + m + '/build.gradle'
end

.gradle_string_for_flavour(flavour) ⇒ Object



37
38
39
40
41
# File 'lib/flavours/gradle.rb', line 37

def self.gradle_string_for_flavour flavour
  package = flavour['packageName'] ? "            packageName \"#{flavour['packageName']}\"\n" : ''
  buildConfig = flavour['buildConfig'] ? build_config_string_for_flavour(flavour) : ''
  return "        #{flavour['flavourName'].gsub(/[^0-9a-z ]/i, '')} {\n#{package}#{buildConfig}        }\n"
end

.gradle_text(directory, m) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/flavours/gradle.rb', line 27

def self.gradle_text directory, m
  path = gradle_directory directory, m
  if File.exists?(path)
    return File.open(path).read
  end

  return nil
end

.green(text) ⇒ Object



12
13
14
# File 'lib/flavours/colors.rb', line 12

def self.green(text)
  colorize(text, 32)
end

.json_object_from_directory(directory) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/flavours/json.rb', line 8

def self.json_object_from_directory directory
  return nil unless directory

  # Check for longbow.json
  @json_path = directory + '/flavours.json'
  unless File.exists?(@json_path)
    Flavours::red "\n  Couldn't find flavours.json at #{@json_path}\n"
    puts "  Run this command to install the correct files:\n  longbow install\n"
    return nil
  end

  # Create hash from longbow.json
  json_contents = File.open(@json_path).read
  return json_object_from_string json_contents
end

.json_object_from_string(contents) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/flavours/json.rb', line 33

def self.json_object_from_string contents
  begin
    !!JSON.parse(contents)
  rescue
    return nil
  end

  return JSON.parse(contents)
end

.json_object_from_url(url) ⇒ Object



25
26
27
28
29
30
# File 'lib/flavours/json.rb', line 25

def self.json_object_from_url url
  return nil unless url
  contents = ''
  open(url) {|io| contents = io.read}
  return json_object_from_string contents
end

.lint_json_object(obj) ⇒ Object



44
45
46
47
48
# File 'lib/flavours/json.rb', line 44

def self.lint_json_object obj
  return false unless obj
  return false unless obj['flavours']
  return true
end

.make_asset_directory(directory, m, flavour_name, folder_name) ⇒ Object



4
5
6
# File 'lib/flavours/assets.rb', line 4

def self.make_asset_directory directory, m, flavour_name, folder_name
  FileUtils::mkdir_p file_path_with_folder_name directory, folder_name, m, flavour_name
end

.move_drawable(directory, m, flavour_hash, i) ⇒ Object

Move Drawable



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/flavours/images.rb', line 103

def self.move_drawable directory, m, flavour_hash, i
  # Set Up
  f_name = flavour_hash['flavourName']
  # Make Path
  Flavours::make_asset_directory directory, m, f_name, 'drawable'
  # Find Image
  image = Image.read("#{directory}/#{i['path']}").first
  if image
    image.write "#{Flavours::file_path_with_folder_name(directory, 'drawable', m, f_name)}/#{i['name']}"
  end
end

.move_drawables(directory, m, flavour_hash) ⇒ Object

Move Drawables



92
93
94
95
96
97
98
99
# File 'lib/flavours/images.rb', line 92

def self.move_drawables directory, m, flavour_hash
  imgs = flavour_hash['drawables']
  if imgs && imgs.kind_of?(Array)
    imgs.each do |i|
      move_drawable directory, m, flavour_hash, i
    end
  end
end

.path_for_downloaded_image_from_url(directory, filename, url, folder) ⇒ Object

Download Image from URL



79
80
81
82
83
84
85
86
87
88
# File 'lib/flavours/images.rb', line 79

def self.path_for_downloaded_image_from_url directory, filename, url, folder
  img_path = directory + '/resources/'+ folder + '/'
  img_file_name = filename + '.png'
  FileUtils::mkdir_p img_path
  File.open(img_path + img_file_name, 'wb') do |f|
    f.write open(url).read
  end

  return img_path + img_file_name
end

.purple(text) ⇒ Object



20
21
22
# File 'lib/flavours/colors.rb', line 20

def self.purple(text)
  colorize(text, 35)
end

.red(text) ⇒ Object

Specific Colors



8
9
10
# File 'lib/flavours/colors.rb', line 8

def self.red(text)
  colorize(text, 31)
end

.res_string(res, name, value, type) ⇒ Object

Resource String



49
50
51
52
# File 'lib/flavours/xmlres.rb', line 49

def self.res_string res, name, value, type
  type_string = type ? " type=\"#{type}\"" : ''
  return "        <#{res} name=\"#{name}\"#{type_string}>#{value.gsub("'", %q(\\\'))}</#{res}>\n"
end

.resize_icons(directory, m, flavour_hash) ⇒ Object

Create & Resize Icons



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
# File 'lib/flavours/images.rb', line 27

def self.resize_icons directory, m, flavour_hash
  # Set Up
  name = flavour_hash['flavourName']

  # Get Image Information
  img_path = ''
  if flavour_hash['iconUrl']
    img_path = self.path_for_downloaded_image_from_url directory, name, flavour_hash['iconUrl'], 'icons'
  elsif flavour_hash['iconPath']
    img_path = "#{directory}/#{flavour_hash['iconPath']}"
  else
    return
  end

  # Create Play Store Version (512x512)
  play_store_path = "#{directory}/resources/icons"
  img = Image.read(img_path).first
  resize_image_to_directory play_store_path, img, '512x512', "#{name}-512"

  # Make Assets Directory
  drawables = ['drawable-xxxhdpi','drawable-xxhdpi','drawable-xhdpi','drawable-hdpi','drawable-mdpi']
  drawables.each do |d|
    Flavours::make_asset_directory directory, m, name, d
  end

  # Resize
  image_sizes = ['192x192', '144x144', '96x96', '72x72', '48x48']
  image_sizes.each_index do |i|
    size = image_sizes[i]
    drawable = drawables[i]
    img_dir = file_path_with_folder_name directory, drawable, m, name
    image = Image.read(img_path).first
    next unless image
    resize_image_to_directory img_dir, image, size, 'ic_launcher'
  end

  puts '  - Created Icon images for ' + name unless $nolog
  return true
end

.resize_image_to_directory(directory, image, size, tag) ⇒ Object

Resize Image to Directory



69
70
71
72
73
74
75
# File 'lib/flavours/images.rb', line 69

def self.resize_image_to_directory directory, image, size, tag
  sizes = size.split('x')
  new_w = Integer(sizes[0])
  new_h = Integer(sizes[1])
  image.resize_to_fill! new_w, new_h
  image.write  directory + '/' + tag + '.png'
end

.set_and_save_flavours_text(flavours_text, directory, m) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/flavours/gradle.rb', line 57

def self.set_and_save_flavours_text flavours_text, directory, m
  @gradle = gradle_text directory, m
  matches = @gradle.match /productFlavors \{(?>[^()]|(\g<0>))*\}\n\n/
  new_flavour_text = "    productFlavors {\n#{flavours_text}    }\n\n"
  if matches
    @gradle = @gradle.sub(matches[0], new_flavour_text)
  else
    @gradle = @gradle.sub('android {',"android {\n#{new_flavour_text}")
  end

  File.open(gradle_directory(directory, m), 'w') do |f|
    f.write(@gradle)
  end
end