Created
September 15, 2023 02:33
-
-
Save mensly/7a3b79750a4045092cb055b3e2df0a4d to your computer and use it in GitHub Desktop.
Simplified Deeplink Testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
deeplink.py | |
Script to make it easier to test deeplinks from the command line without remembering arcane | |
incantations to conjure the spirits of the computer. | |
Add to your path somewhere, and run in your project directory passing the url of the deeplink. | |
If the platform name 'ios' or 'android' is in the working directory name, it doesn't need to be | |
passed as an argument, though this can be done as well. The Android package name can also be | |
explicitly included. | |
""" | |
import argparse | |
import os | |
def deeplink_ios(url): | |
command = f"xcrun simctl openurl booted '{url}'" | |
print(f"Running command: {command}") | |
os.system(command) | |
def deeplink_android(url, package): | |
command = f"adb shell am start -W -a android.intent.action.VIEW -d \"{url}\"" | |
if package: | |
command += f" {package}" | |
print(f"Running command: {command}") | |
os.system(command) | |
def get_default_platform(): | |
directory = os.getcwd() | |
try: | |
android = directory.rindex('android') | |
except ValueError: | |
android = -1 | |
try: | |
ios = directory.rindex('ios') | |
except ValueError: | |
ios = -1 | |
if android > ios: | |
return 'android' | |
elif ios > android: | |
return 'ios' | |
else: | |
raise Exception("platform is required if path name does not contain 'ios' or 'android'") | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--platform", help="ios or android, defaults based on working directory path name") | |
parser.add_argument("--package", help="optional explicit android package") | |
parser.add_argument("url", help="url for deeplink") | |
args = parser.parse_args() | |
platform = args.platform or get_default_platform() | |
if platform == 'ios': | |
deeplink_ios(args.url) | |
elif platform == 'android': | |
deeplink_android(args.url, args.package) | |
else: | |
raise Exception(f"unsupported platform: {platform}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment