Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
* Upgrade version number to 0.0.1
* Remove lib prefix from key tracking data
* Add error key to tracking data
* Add param names to network method param
* Divide logic into methods
  • Loading branch information
Jeasmine committed Nov 29, 2021
1 parent 4f63d4d commit 64e4bb8
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 133 deletions.
89 changes: 50 additions & 39 deletions lib/osnetwork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,66 @@
require_relative 'osproject_android'

class NetworkHandler
@instance = new
@instance = new

private_class_method :new
private_class_method :new

def self.instance
@instance
end
URL = 'https://api.onesignal.com/api/v1/track'

def track_uri
URI.parse('https://api.onesignal.com/api/v1/track')
end
def self.instance
@instance
end

def getHttp_net(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
return http
end
def get_http_net()
uri = URI.parse(URL)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http
end

def send_track_from_message(app_id, platform, lang, success_mesage, append_message)
actions_taken = success_mesage.gsub(" * ", "").gsub("\n",";")
actions_taken += append_message
send_track_command_actions(app_id, platform, lang, OSProject.default_command, actions_taken)
end
def send_track_error(app_id:, platform:, lang:, error_message:)
send_track_command_actions(app_id: app_id, platform: platform, lang: lang, command: OSProject.default_command, error_message: error_message)
end

def send_track_actions(app_id, platform, lang, actions_taken)
send_track_command_actions(app_id, platform, lang, OSProject.default_command, actions_taken)
end
def send_track_actions(app_id:, platform:, lang:, actions_taken:)
send_track_command_actions(app_id: app_id, platform: platform, lang: lang, command: OSProject.default_command, actions_taken: actions_taken)
end

def send_track_command_actions(app_id, platform, lang, command, actions_taken)
uri = track_uri
http = getHttp_net()
# Send command used by the user for tracking
# There are cases where --help, --version commands might be used, and there is no other data in addition to the command name
def send_track_command(command)
http = get_http_net()

request = Net::HTTP::Post.new(uri.request_uri)

request['app_id'] = app_id
request['OS-Usage-Data'] = 'lib-name=' + OSProject.tool_name + ',lib-version=' + OSProject.version + ',lib-os=' + OSProject.os + ',lib-type=' + platform + ',lib-lang=' + lang + ',lib-command=' + command + ',lib-actions=' + actions_taken
request = Net::HTTP::Post.new(URL)

response = http.request(request)
end
request['app_id'] = ""
request['OS-Usage-Data'] = get_usage_data(nil, nil, command, nil)

def send_track_command(command)
uri = track_uri
http = getHttp_net(uri)

request = Net::HTTP::Post.new(uri.request_uri)
response = http.request(request)
end

request['app_id'] = ""
request['OS-Usage-Data'] = 'lib-name=' + OSProject.tool_name + ',lib-version=' + OSProject.version + ',lib-os=' + OSProject.os + ',lib-command=' + command
private

response = http.request(request)
end
def send_track_command_actions(app_id:, platform:nil, lang:nil, command:nil, actions_taken:nil, error_message:nil)
http = get_http_net()

request = Net::HTTP::Post.new(URL)

request['app_id'] = app_id
request['OS-Usage-Data'] = get_usage_data(platform: platform, lang: lang, command: command, actions_taken: actions_taken, error_message: error_message)

response = http.request(request)
end

def get_usage_data(platform:nil, lang:nil, command:nil, actions_taken:nil, error_message:nil)
data = "kind=sdk, name=#{OSProject::TOOL_NAME}, version=#{OSProject::VERSION}, target-os=#{OSProject.os}"

data += ", type=#{platform}" if platform
data += ", lang=#{lang}" if lang
data += ", command=#{command}" if command
data += ", actions=#{actions_taken}" if actions_taken
data += ", error=#{error_message}" if error_message

return data
end
end
11 changes: 3 additions & 8 deletions lib/osproject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class OSProject
attr_accessor :fcm_id
attr_accessor :apns_id

VERSION = '0.0.1'
TOOL_NAME = 'onesignal-cli'

def initialize(type, dir, lang, os_app_id)
@type = type
@dir = dir
Expand All @@ -31,17 +34,9 @@ def has_sdk?
raise Exception.new "Not Implemented"
end

def self.version
'0.0.0'
end

def self.os
'mac'
end

def self.tool_name
'onesignal-cli'
end

def self.default_command
'install-sdk'
Expand Down
197 changes: 111 additions & 86 deletions lib/osproject_android.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def initialize(app_class_location, os_app_id)
directory_split = app_class_location.split('/', -1)
lang_array = directory_split[-1].split(".")

if lang_array.length() == 1
if lang_array.length == 1
puts 'Missing Application class file extension (.kt or .java)'
error_track_message = "error=user missed Application class file extension;"
NetworkHandler.instance.send_track_actions(os_app_id, 'android', "nil", error_track_message)
error_track_message = "User missed Application class file extension;"
NetworkHandler.instance.send_track_error(app_id: os_app_id, platform: 'android', lang: nil, error_message: error_track_message)
exit(1)
end

Expand All @@ -23,8 +23,8 @@ def initialize(app_class_location, os_app_id)

unless lang == "java" || lang == "kt"
puts 'Invalid language (java or kotlin)'
error_track_message = "error=user entered invalid language: " + lang + ";"
NetworkHandler.instance.send_track_actions(os_app_id, 'android', lang, error_track_message)
error_track_message = "User entered invalid language: " + lang + ";"
NetworkHandler.instance.send_track_error(app_id: os_app_id, platform: 'android', lang: lang, error_message: error_track_message)
exit(1)
end

Expand All @@ -37,34 +37,12 @@ def add_sdk!
raise
end

network_handler = NetworkHandler.instance

project_dir = dir
app_dir = app_class_location.split('/')[0]
build_gradle_dir = project_dir + '/build.gradle'
build_gradle_app_dir = project_dir + '/' + app_dir + '/build.gradle'
success_mesage = ""

begin
content = File.read(build_gradle_dir)
rescue
puts "File not found: " + build_gradle_dir
puts "Call CLI tool from base project directory"
error_track_message = "error=user called CLI from invalid directory file: " + build_gradle_dir + " not found;"
network_handler.send_track_from_message(os_app_id, 'android', lang, success_mesage, error_track_message)
return
end

begin
content = File.read(build_gradle_app_dir)
rescue
puts "Directory not found: " + project_dir + '/' + app_dir
puts "Provide --entrypoint param as Application file path directory. If no Appplication class available, OneSignal will create it at the directory provided."
puts "Example: app/src/main/java/com/onesignal/testapplication/OneSignalApplication.java"
error_track_message = "error=user entered invalid Application file path: " + project_dir + '/' + app_dir + " directory not found;"
network_handler.send_track_from_message(os_app_id, 'android', lang, success_mesage, error_track_message)
return
end
check_build_gradle_path(project_dir, app_dir, build_gradle_dir, build_gradle_app_dir)

application_class_created = false
application_class_exists = true
Expand All @@ -86,8 +64,9 @@ def add_sdk!

unless user_response == "y" || user_response == "yes" || user_response == "n" || user_response == "no"
puts 'Invalid response (Y/N)'
error_track_message = "error=user entered invalid response for Application class creation command used: " + user_response + ";"
NetworkHandler.instance.send_track_from_message(os_app_id, 'android', lang, success_mesage, error_track_message)

error_track_message = "User entered invalid response for Application class creation. Command used: #{user_response};"
NetworkHandler.instance.send_track_error(app_id: os_app_id, platform: 'android', lang: lang, error_message: error_track_message)
exit(1)
end

Expand All @@ -99,13 +78,62 @@ def add_sdk!
end
end

success_mesage = add_deps_build_gradle(build_gradle_dir)
success_mesage += add_deps_app_build_gradle(build_gradle_app_dir)

if application_class_exists
success_mesage += add_application_init_code(project_dir, app_class_location, lang)
end

show_finish_message(project_dir, application_class_created, app_class_location, application_name, success_mesage)
end

def check_insert_lines(directory, regex, addition, success_mesage = "")
if !File.readlines(directory).any?{ |l| l[addition] }
result = _insert_lines(directory, regex, addition)
if (result.nil?)
return ""
end
return success_mesage
end
return ""
end

def check_insert_block(directory, regex, addition, addition_block, success_mesage = "")
if !File.readlines(directory).any?{ |l| l[addition] }
_insert_lines(directory, regex, addition_block)
return success_mesage
end
return ""
end

def show_finish_message(project_dir, application_class_created, app_class_location, application_name, success_mesage)
actions_taken = success_mesage.gsub(" * ", "").gsub("\n",";")

if application_class_created
success_mesage += " * Created " + application_name + " Application class at " + project_dir + '/' + app_class_location + "\n"
success_mesage += " * Added Application class to AndroidManifest file\n"

actions_taken += "Created " + application_name + " Application;Added Application class to AndroidManifest file;"
end

success_mesage = "*** OneSignal integration completed successfully! ***\n\n" + success_mesage

if success_mesage == "*** OneSignal integration completed successfully! ***\n\n"
NetworkHandler.instance.send_track_actions(app_id: os_app_id, platform: 'android', lang: lang, actions_taken: "no actions, sdk already integrated;")
puts "*** OneSignal already integrated, no changes needed ***\n\n"
else
NetworkHandler.instance.send_track_actions(app_id: os_app_id, platform: 'android', lang: lang, actions_taken: actions_taken)
puts success_mesage
end
end

# add deps to /build.gradle
def add_deps_build_gradle(build_gradle_dir)
gradle_plugin_mesage = " * Added repository provider gradlePluginPortal() to project build.gradle\n"
os_gradle_plugin_mesage = " * Added dependency \"gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.9, 0.99.99]\" to project build.gradle \n"
app_os_gradle_plugin_mesage = " * Added plugin 'com.onesignal.androidsdk.onesignal-gradle-plugin' to app build.gradle\n"
app_os_dependency_message = " * Added dependency 'com.onesignal:OneSignal:[4.0.0, 4.99.99]' to app build.gradle \n"
os_gradle_plugin_mesage = " * Added dependency \"gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.9, 0.99.99]\" to project build.gradle \n"

# TODO: this gradle tack is a very brittle approach.
# add deps to /build.gradle
success_mesage = ""
success_mesage += check_insert_lines(build_gradle_dir,
Regexp.quote("mavenCentral()"),
"gradlePluginPortal()",
Expand All @@ -126,74 +154,71 @@ def add_sdk!
"classpath \"gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.9, 0.99.99]\"",
os_gradle_plugin_mesage)

# add deps to /app/build.gradle
success_mesage += check_insert_lines(build_gradle_app_dir,
"implementation \"androidx.appcompat:appcompat:[^']*\"",
"implementation \"com.onesignal:OneSignal:[4.0.0, 4.99.99]\"",
app_os_dependency_message)
success_mesage
end

success_mesage += check_insert_lines(build_gradle_app_dir,
"implementation 'androidx.appcompat:appcompat:[^']*'",
"implementation 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'",
app_os_dependency_message)
# add deps to /app/build.gradle
def add_deps_app_build_gradle(build_gradle_app_dir)
app_os_dependency_message = " * Added dependency 'com.onesignal:OneSignal:[4.0.0, 4.99.99]' to app build.gradle \n"
app_os_gradle_plugin_mesage = " * Added plugin 'com.onesignal.androidsdk.onesignal-gradle-plugin' to app build.gradle\n"

success_mesage = ""
success_mesage += check_insert_lines(build_gradle_app_dir,
"implementation \"com.google.android.material:material:[^']*\"",
"implementation \"com.onesignal:OneSignal:[4.0.0, 4.99.99]\"",
app_os_dependency_message)
"implementation \"androidx.appcompat:appcompat:[^']*\"",
"implementation \"com.onesignal:OneSignal:[4.0.0, 4.99.99]\"",
app_os_dependency_message)

success_mesage += check_insert_lines(build_gradle_app_dir,
"implementation 'com.google.android.material:material:[^']*'",
"implementation 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'",
app_os_dependency_message)
"implementation 'androidx.appcompat:appcompat:[^']*'",
"implementation 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'",
app_os_dependency_message)

success_mesage += check_insert_lines(build_gradle_app_dir,
"apply plugin: 'com.android.application'",
"apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'",
app_os_gradle_plugin_mesage)
"implementation \"com.google.android.material:material:[^']*\"",
"implementation \"com.onesignal:OneSignal:[4.0.0, 4.99.99]\"",
app_os_dependency_message)

success_mesage += check_insert_lines(build_gradle_app_dir,
"id 'com.android.application'",
"id 'com.onesignal.androidsdk.onesignal-gradle-plugin'",
app_os_gradle_plugin_mesage)
"implementation 'com.google.android.material:material:[^']*'",
"implementation 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'",
app_os_dependency_message)

if application_class_exists
success_mesage += add_application_init_code(project_dir, app_class_location, lang)
end
success_mesage += check_insert_lines(build_gradle_app_dir,
"apply plugin: 'com.android.application'",
"apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'",
app_os_gradle_plugin_mesage)

if application_class_created
success_mesage += " * Created " + application_name + " Application class at " + project_dir + '/' + app_class_location + "\n"
success_mesage += " * Added Application class to AndroidManifest file\n"
end
success_mesage += check_insert_lines(build_gradle_app_dir,
"id 'com.android.application'",
"id 'com.onesignal.androidsdk.onesignal-gradle-plugin'",
app_os_gradle_plugin_mesage)

actions_taken = success_mesage.gsub(" * ", "").gsub("\n",";")
success_mesage = "*** OneSignal integration completed successfully! ***\n\n" + success_mesage
if success_mesage == "*** OneSignal integration completed successfully! ***\n\n"
network_handler.send_track_actions(os_app_id, 'android', lang, "no actions, sdk already integrated;")
puts "*** OneSignal already integrated, no changes needed ***\n\n"
else
network_handler.send_track_actions(os_app_id, 'android', lang, actions_taken)
puts success_mesage
end
success_mesage
end

def check_insert_lines(directory, regex, addition, success_mesage = "")
if !File.readlines(directory).any?{ |l| l[addition] }
result = _insert_lines(directory, regex, addition)
if (result.nil?)
return ""
end
return success_mesage
def check_build_gradle_path(project_dir, app_dir, build_gradle_dir, build_gradle_app_dir)
begin
content = File.read(build_gradle_dir)
rescue
puts "File not found: " + build_gradle_dir
puts "Call CLI tool from base project directory"

error_track_message = "User called CLI from invalid directory file. " + build_gradle_dir + " not found;"
NetworkHandler.instance.send_track_error(app_id: os_app_id, platform: 'android', lang: lang, error_message: error_track_message)
exit(1)
end
return ""
end

def check_insert_block(directory, regex, addition, addition_block, success_mesage = "")
if !File.readlines(directory).any?{ |l| l[addition] }
_insert_lines(directory, regex, addition_block)
return success_mesage
begin
content = File.read(build_gradle_app_dir)
rescue
puts "Directory not found: " + project_dir + '/' + app_dir
puts "Provide --entrypoint param as Application file path directory. If no Appplication class available, OneSignal will create it at the directory provided."
puts "Example: app/src/main/java/com/onesignal/testapplication/OneSignalApplication.java"

error_track_message = "User entered invalid Application file path. " + project_dir + '/' + app_dir + " directory not found;"
NetworkHandler.instance.send_track_error(app_id: os_app_id, platform: 'android', lang: lang, error_message: error_track_message)
exit(1)
end
return ""
end

def create_application_file(project_dir, app_dir, package_directory, app_class_location, application_name, lang)
Expand Down

0 comments on commit 64e4bb8

Please sign in to comment.