Skip to content

Commit

Permalink
Update 3.0.0 (#71)
Browse files Browse the repository at this point in the history
* chore(SDK-3613) - Update AndroidPostImport

- On android-wrapper reimport, remove old version of .androidlib before converting new one

* Use .asset for CleverTap Settings (#69)

* Fix create unitypackage (#70)

* Run postprocessor only if not a package export

* Rename meta files
Rename meta files otherwise unity will create an empty folder when package is created

* Mark the android-wrapper folder as Android Platform only

* Update unitypackage

---------
Co-authored-by: PJClevertap <[email protected]>
  • Loading branch information
nzagorchev authored Jan 19, 2024
1 parent 9823101 commit 6a4a30e
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 63 deletions.
16 changes: 12 additions & 4 deletions CleverTap/Editor/AndroidPostImport.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
using System.Linq;
using UnityEditor;

namespace CleverTapSDK.Private
using System;
using System.Linq;
using UnityEditor;

namespace CleverTapSDK.Private
{
class AndroidPostImport : AssetPostprocessor
{
private static readonly string ctExportCommandLineArg = "-ct-export";

static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
{
string[] cmdArgs = Environment.GetCommandLineArgs();
if (cmdArgs.Contains(ctExportCommandLineArg))
return;

if (AssetDatabase.IsValidFolder("Assets/CleverTap/Plugins/Android/clevertap-android-wrapper") && importedAssets?.Length > 0 && importedAssets.Contains("Assets/CleverTap/Plugins/Android/clevertap-android-wrapper"))
{
AssetDatabase.DeleteAsset("Assets/CleverTap/Plugins/Android/clevertap-android-wrapper.androidlib");
AssetDatabase.MoveAsset("Assets/CleverTap/Plugins/Android/clevertap-android-wrapper", "Assets/CleverTap/Plugins/Android/clevertap-android-wrapper.androidlib");
}
}
Expand Down
85 changes: 49 additions & 36 deletions CleverTap/Editor/CleverTapPostBuildProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEditor;
using UnityEngine;

namespace CleverTapSDK.Private
{
Expand Down Expand Up @@ -53,48 +54,60 @@ private enum Position { Begin, End };
public static void OnPostProcessBuild(BuildTarget target, string path)
{
if (target == BuildTarget.iOS)
{
string projPath = PBXProject.GetPBXProjectPath(path);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
{
IOSPostProcess(path);
}
}

private static void IOSPostProcess(string path)
{
string projPath = PBXProject.GetPBXProjectPath(path);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));

#if UNITY_2019_3_OR_NEWER
var projectTarget = proj.GetUnityFrameworkTargetGuid();
var projectTarget = proj.GetUnityFrameworkTargetGuid();
#else
var projectTarget = proj.TargetGuidByName(PBXProject.GetUnityTargetName());
#endif

// Add linker flags
proj.AddBuildProperty(projectTarget, "OTHER_LDFLAGS", "-ObjC");

File.WriteAllText(projPath, proj.WriteToString());

// Update plist
string plistPath = path + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));

// Get root
PlistElementDict rootDict = plist.root;

IOSConfigSettings iOSConfig = IOSConfigSettings.Load();
// write CleverTapAccountID and CleverTapToken
rootDict.SetString("CleverTapAccountID", iOSConfig.CleverTapAccountId);
rootDict.SetString("CleverTapToken", iOSConfig.CleverTapAccountToken);
rootDict.SetString("CleverTapRegion", iOSConfig.CleverTapAccountRegion);

// Write Disable IDFV Flag
rootDict.SetBoolean("CleverTapDisableIDFV", iOSConfig.CleverTapDisableIDFV);

// Write to file
File.WriteAllText(plistPath, plist.WriteToString());

// Update AppController
InsertCodeIntoControllerClass(path, iOSConfig.CleverTapEnablePersonalization);
}
}

private static void InsertCodeIntoControllerClass(string projectPath, bool personalizationEnabled)
// Add linker flags
proj.AddBuildProperty(projectTarget, "OTHER_LDFLAGS", "-ObjC");

File.WriteAllText(projPath, proj.WriteToString());

// Update plist
string plistPath = path + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));

// Get root
PlistElementDict rootDict = plist.root;

CleverTapSettings settings = AssetDatabase.LoadAssetAtPath<CleverTapSettings>(CleverTapSettings.settingsPath);
if (settings != null)
{
rootDict.SetString("CleverTapAccountID", settings.CleverTapAccountId);
rootDict.SetString("CleverTapToken", settings.CleverTapAccountToken);
rootDict.SetString("CleverTapRegion", settings.CleverTapAccountRegion);
rootDict.SetBoolean("CleverTapDisableIDFV", settings.CleverTapDisableIDFV);

// Write to file
File.WriteAllText(plistPath, plist.WriteToString());
}
else
{
Debug.Log($"CleverTapSettings have not been set.\n" +
$"Please update them from {CleverTapSettingsWindow.ITEM_NAME} or " +
$"set them manually in the project Info.plist.");
}

// Update AppController
bool personalizationEnabled = settings == null || settings.CleverTapEnablePersonalization;
InsertCodeIntoControllerClass(path, personalizationEnabled);
}

private static void InsertCodeIntoControllerClass(string projectPath, bool personalizationEnabled)
{
string filepath = projectPath + PATH_CONTROLLER;
string[] methodSignatures = { SIGNATURE_PUSH_TOKEN, SIGNATURE_URL, SIGNATURE_NOTIF_LOCAL, SIGNATURE_NOTIF_REMOTE, SIGNATURE_NOTIF_REMOTE_BG };
Expand Down
23 changes: 23 additions & 0 deletions CleverTap/Editor/CleverTapSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using UnityEngine;

[System.Serializable]
public class CleverTapSettings : ScriptableObject
{
public string CleverTapAccountId;
public string CleverTapAccountToken;
public string CleverTapAccountRegion;
public bool CleverTapEnablePersonalization { get; set; } = true;
public bool CleverTapDisableIDFV;

public override string ToString()
{
return $"CleverTapSettings:\n" +
$"CleverTapAccountId: {CleverTapAccountId}\n" +
$"CleverTapAccountToken: {CleverTapAccountToken}\n" +
$"CleverTapAccountRegion: {CleverTapAccountRegion}\n" +
$"CleverTapEnablePersonalization: {CleverTapEnablePersonalization}\n" +
$"CleverTapDisableIDFV: {CleverTapDisableIDFV}";
}

internal static readonly string settingsPath = "Assets/CleverTapSettings.asset";
}
11 changes: 11 additions & 0 deletions CleverTap/Editor/CleverTapSettings.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 52 additions & 20 deletions CleverTap/Editor/CleverTapSettingsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,80 @@

namespace CleverTapSDK.Private
{
// Custom editor window class
public class CleverTapSettingsWindow : EditorWindow
{
private const string NAME = "CleverTap Settings";
internal const string ITEM_NAME = "Assets/CleverTap Settings";
private static readonly string windowName = "CleverTap Settings";

private IOSConfigSettings config;
private CleverTapSettings settings;

// Add menu item to open the window
[MenuItem("Assets/" + NAME)]
[MenuItem(ITEM_NAME)]
public static void ShowWindow()
{
GetWindow<CleverTapSettingsWindow>(NAME);
GetWindow<CleverTapSettingsWindow>(windowName);
}

private void OnEnable()
{
// Load settings from EditorPrefs
config = IOSConfigSettings.Load();
settings = LoadCleverTapSettings();
}

private void OnGUI()
{
// iOS Settings
GUILayout.Label("iOS Settings", EditorStyles.boldLabel);
if (settings == null)
{
GUILayout.Label("Error loading settings", EditorStyles.boldLabel);
return;
}

// Input text fields
config.CleverTapAccountId = EditorGUILayout.TextField("CleverTapAccountId", config.CleverTapAccountId);
config.CleverTapAccountToken = EditorGUILayout.TextField("CleverTapAccountToken", config.CleverTapAccountToken);
config.CleverTapAccountRegion = EditorGUILayout.TextField("CleverTapAccountRegion", config.CleverTapAccountRegion);
GUILayout.Label(windowName, EditorStyles.boldLabel);

// Checkboxes
config.CleverTapEnablePersonalization = EditorGUILayout.Toggle("CleverTapEnablePersonalization", config.CleverTapEnablePersonalization);
config.CleverTapDisableIDFV = EditorGUILayout.Toggle("CleverTapDisableIDFV", config.CleverTapDisableIDFV);
settings.CleverTapAccountId = EditorGUILayout.TextField("CleverTapAccountId", settings.CleverTapAccountId);
settings.CleverTapAccountToken = EditorGUILayout.TextField("CleverTapAccountToken", settings.CleverTapAccountToken);
settings.CleverTapAccountRegion = EditorGUILayout.TextField("CleverTapAccountRegion", settings.CleverTapAccountRegion);

settings.CleverTapEnablePersonalization = EditorGUILayout.Toggle("CleverTapEnablePersonalization", settings.CleverTapEnablePersonalization);
settings.CleverTapDisableIDFV = EditorGUILayout.Toggle("CleverTapDisableIDFV", settings.CleverTapDisableIDFV);

// Save button
if (GUILayout.Button("Save Settings"))
{
// Save settings to EditorPrefs
config.Save();
SaveCleverTapSettings();
Debug.Log($"{windowName} saved!");
}
}

Debug.Log($"{NAME} saved!");
private static CleverTapSettings LoadCleverTapSettings()
{
try
{
// Load settings from .asset file
CleverTapSettings settings = AssetDatabase.LoadAssetAtPath<CleverTapSettings>(CleverTapSettings.settingsPath);

if (settings == null)
{
Debug.Log("Asset not found. Creating asset.");
// Create a new instance if it doesn't exist
settings = CreateInstance<CleverTapSettings>();
AssetDatabase.CreateAsset(settings, CleverTapSettings.settingsPath);
AssetDatabase.SaveAssets();
// Refresh the database to make sure the new asset is recognized
AssetDatabase.Refresh();
}
return settings;
}
catch (System.Exception ex)
{
Debug.LogException(ex);
return null;
}
}

private void SaveCleverTapSettings()
{
// Save settings to .asset file
EditorUtility.SetDirty(settings);
AssetDatabase.SaveAssetIfDirty(settings);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified CleverTapSDK.unitypackage
Binary file not shown.
3 changes: 3 additions & 0 deletions Scripts/create_unity_package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ echo $ORIGINAL_ANDROID_LIB_FOLDER_PATH
# Rename the folder
if [ -d "$ORIGINAL_ANDROID_LIB_FOLDER_PATH" ]; then
mv "$ORIGINAL_ANDROID_LIB_FOLDER_PATH" "$RENAMED_ANDROID_LIB_FOLDER_PATH"
mv "$ORIGINAL_ANDROID_LIB_FOLDER_PATH.meta" "$RENAMED_ANDROID_LIB_FOLDER_PATH.meta"
else
echo "Original folder not found!"
exit 1
Expand Down Expand Up @@ -78,6 +79,7 @@ fi
echo "📦 Creating CleverTapSDK.unitypackage, this may take a minute."
$UNITY_BIN -gvh_disable \
-nographics \
-ct-export \
-projectPath $PROJECT \
-force-free -quit -batchmode -logFile exportlog.txt \
-importPackage $PROJECT/external-dependency-manager-latest.unitypackage \
Expand All @@ -90,3 +92,4 @@ rm $SYMBOLIC_LINK_PATH

# Revert the folder name back to original after the build
mv "$RENAMED_ANDROID_LIB_FOLDER_PATH" "$ORIGINAL_ANDROID_LIB_FOLDER_PATH"
mv "$RENAMED_ANDROID_LIB_FOLDER_PATH.meta" "$ORIGINAL_ANDROID_LIB_FOLDER_PATH.meta"

0 comments on commit 6a4a30e

Please sign in to comment.