Example #1
0
        public override bool Execute()
        {
            Log.LogMessage("Started ProcessGoogleServicesJson...");

            Log.LogMessage("Android Package Name: {0}", AndroidPackageName);

            // Paths to write resource files to
            var xmlPath    = Path.Combine(MonoAndroidResDirIntermediate, "xml", RESFILE_XML);
            var valuesPath = Path.Combine(MonoAndroidResDirIntermediate, "values", RESFILE_VALUES);

            var wroteXmlPath    = false;
            var wroteValuesPath = false;

            if (GoogleServicesJsons == null || !GoogleServicesJsons.Any())
            {
                Log.LogMessage("No GoogleServicesJson Build Action items specified, skipping task.");
                DeleteFiles(valuesPath, xmlPath);
                return(true);
            }

            if (GoogleServicesJsons.Count() > 1)
            {
                Log.LogMessage("More than one GoogleServicesJson file specified for build configuration, using first one found.");
            }

            var gsItem = GoogleServicesJsons.First();

            var gsPath = CleanPath(gsItem.ItemSpec);

            GoogleServices googleServices;

            try {
                using (var sr = File.OpenRead(gsPath)) {
                    googleServices = GoogleServicesJsonProcessor.ProcessJson(AndroidPackageName, sr);
                }
                if (googleServices == null)
                {
                    throw new NullReferenceException();
                }
            } catch (Exception ex) {
                Log.LogError("Failed to Read or Deserialize GoogleServicesJson file: {0}{1}{2}", gsPath, Environment.NewLine, ex);
                DeleteFiles(valuesPath, xmlPath);
                return(false);
            }

            if (string.IsNullOrEmpty(AndroidPackageName))
            {
                Log.LogError("Android Package Name not specified for project");
                return(false);
            }

            var resolvedClientInfo = googleServices.GetClient(AndroidPackageName);

            if (resolvedClientInfo == null)
            {
                Log.LogWarning("Failed to find client_info in google-services.json matching package name: {0}", AndroidPackageName);
            }

            var valuesItems = new Dictionary <string, string> {
                { "ga_trackingId", googleServices.GetGATrackingId(AndroidPackageName) },
                { "gcm_defaultSenderId", googleServices.GetDefaultGcmSenderId() },
                { "google_app_id", googleServices.GetGoogleAppId(AndroidPackageName) },
                { "test_banner_ad_unit_id", googleServices.GetTestBannerAdUnitId(AndroidPackageName) },
                { "test_interstitial_ad_unit_id", googleServices.GetTestInterstitialAdUnitId(AndroidPackageName) },
                { "default_web_client_id", googleServices.GetDefaultWebClientId(AndroidPackageName) },
                { "firebase_database_url", googleServices.GetFirebaseDatabaseUrl() },
                { "google_api_key", googleServices.GetGoogleApiKey(AndroidPackageName) },
                { "google_crash_reporting_api_key", googleServices.GetCrashReportingApiKey(AndroidPackageName) },
                { "google_storage_bucket", googleServices.GetStorageBucket(AndroidPackageName) },
            };

            // We only want to create the file if not all of these values are missing
            if (valuesItems.Any(kvp => !string.IsNullOrEmpty(kvp.Value)))
            {
                Log.LogMessage("Writing Resource File: {0}", valuesPath);
                WriteResourceDoc(valuesPath, valuesItems);
                wroteValuesPath = true;
                Log.LogMessage("Wrote Resource File: {0}", valuesPath);
            }
            else
            {
                if (File.Exists(valuesPath))
                {
                    Log.LogMessage("Deleting Resource File: {0}", valuesPath);
                    try {
                        File.Delete(valuesPath);
                    } catch (Exception ex) {
                        Log.LogWarning("Failed to delete Resource File: {0}{1}{2}", valuesPath, Environment.NewLine, ex);
                    }
                }
            }

            var xmlItems = new Dictionary <string, string> {
                { "ga_trackingId", googleServices.GetGATrackingId(AndroidPackageName) }
            };

            // We only want to create the file if not all of these values are missing
            if (xmlItems.Any(kvp => !string.IsNullOrEmpty(kvp.Value)))
            {
                Log.LogMessage("Writing Resource File: {0}", xmlPath);
                WriteResourceDoc(xmlPath, xmlItems);
                wroteXmlPath = true;
                Log.LogMessage("Wrote Resource File: {0}", xmlPath);
            }
            else
            {
                // If no
                if (File.Exists(xmlPath))
                {
                    Log.LogMessage("Deleting Resource File: {0}", xmlPath);
                    try {
                        File.Delete(xmlPath);
                    } catch (Exception ex) {
                        Log.LogWarning("Failed to delete Resource File: {0}{1}{2}", xmlPath, Environment.NewLine, ex);
                    }
                }
            }

            var outputFiles = new List <ITaskItem> ();

            if (wroteXmlPath)
            {
                outputFiles.Add(new TaskItem(xmlPath));
            }
            if (wroteValuesPath)
            {
                outputFiles.Add(new TaskItem(valuesPath));
            }

            if (outputFiles.Any())
            {
                GoogleServicesGeneratedResources = outputFiles.ToArray();
            }

            Log.LogMessage("Writing stamp file...");

            var stampText = string.Empty;

            if (wroteXmlPath)
            {
                stampText += xmlPath + Environment.NewLine;
            }
            if (wroteValuesPath)
            {
                stampText += valuesPath + Environment.NewLine;
            }

            File.WriteAllText(Path.Combine(IntermediateOutputPath ?? MonoAndroidResDirIntermediate, "ProcessGoogleServicesJson.stamp"), stampText.Trim());

            Log.LogMessage("Finished ProcessGoogleServicesJson...");
            return(true);
        }
Example #2
0
        public override bool Execute()
        {
            Log.LogMessage("Started ProcessGoogleServicesJson...");

            var resPath       = ResPath.GetMetadata("FullPath");
            var stampPath     = StampPath.GetMetadata("FullPath");
            var resValuesPath = ResStringsPath.GetMetadata("FullPath");
            var resXmlPath    = ResXmlPath.GetMetadata("FullPath");

            Log.LogMessage("Android Package Name: {0}", AndroidPackageName);
            Log.LogMessage("Resource Path: {0}", resPath);
            Log.LogMessage("Resource Strings Path: {0}", resValuesPath);
            Log.LogMessage("Resource Xml Path: {0}", resXmlPath);
            Log.LogMessage("Stamp Path: {0}", stampPath);

            // Return out the absolute path of our resources dir
            ResPathAbs = new ITaskItem[] { new TaskItem(resPath) };

            if (GoogleServicesJsons == null || !GoogleServicesJsons.Any())
            {
                Log.LogMessage("No GoogleServicesJson Build Action items specified, skipping task.");
                DeleteFiles(resValuesPath, resXmlPath);
                return(true);
            }

            if (GoogleServicesJsons.Count() > 1)
            {
                Log.LogMessage("More than one GoogleServicesJson file specified for build configuration, using first one found.");
            }

            var gsItem = GoogleServicesJsons.First();

            var gsPath = CleanPath(gsItem.ItemSpec);

            GoogleServices googleServices;

            try {
                using (var sr = File.OpenRead(gsPath))
                    googleServices = GoogleServicesJsonProcessor.ProcessJson(AndroidPackageName, sr);

                if (googleServices == null)
                {
                    throw new NullReferenceException();
                }
            } catch (Exception ex) {
                Log.LogError("Failed to Read or Deserialize GoogleServicesJson file: {0}{1}{2}", gsPath, Environment.NewLine, ex);
                DeleteFiles(resValuesPath, resXmlPath);
                return(false);
            }

            if (string.IsNullOrEmpty(AndroidPackageName))
            {
                Log.LogError("Android Package Name not specified for project");
                return(false);
            }

            var resolvedClientInfo = googleServices.GetClient(AndroidPackageName);

            if (resolvedClientInfo == null)
            {
                Log.LogWarning("Failed to find client_info in google-services.json matching package name: {0}", AndroidPackageName);
            }

            var valuesItems = new Dictionary <string, string> {
                { "ga_trackingId", googleServices.GetGATrackingId(AndroidPackageName) },
                { "gcm_defaultSenderId", googleServices.GetDefaultGcmSenderId() },
                { "google_app_id", googleServices.GetGoogleAppId(AndroidPackageName) },
                { "test_banner_ad_unit_id", googleServices.GetTestBannerAdUnitId(AndroidPackageName) },
                { "test_interstitial_ad_unit_id", googleServices.GetTestInterstitialAdUnitId(AndroidPackageName) },
                { "default_web_client_id", googleServices.GetDefaultWebClientId(AndroidPackageName) },
                { "firebase_database_url", googleServices.GetFirebaseDatabaseUrl() },
                { "google_api_key", googleServices.GetGoogleApiKey(AndroidPackageName) },
                { "google_crash_reporting_api_key", googleServices.GetCrashReportingApiKey(AndroidPackageName) },
                { "google_storage_bucket", googleServices.GetStorageBucket(AndroidPackageName) },
                { "project_id", googleServices.ProjectInfo.ProjectId }
            };

            // We only want to create the file if not all of these values are missing
            if (valuesItems.Any(kvp => !string.IsNullOrEmpty(kvp.Value)))
            {
                Log.LogMessage("Writing Resource File: {0}", resValuesPath);
                WriteResourceDoc(resValuesPath, valuesItems);
                Log.LogMessage("Wrote Resource File: {0}", resValuesPath);
            }
            else
            {
                if (File.Exists(resValuesPath))
                {
                    Log.LogMessage("Deleting Resource File: {0}", resValuesPath);
                    try {
                        File.Delete(resValuesPath);
                    } catch (Exception ex) {
                        Log.LogWarning("Failed to delete Resource File: {0}{1}{2}", resValuesPath, Environment.NewLine, ex);
                    }
                }
            }

            var xmlItems = new Dictionary <string, string> {
                { "ga_trackingId", googleServices.GetGATrackingId(AndroidPackageName) }
            };

            // We only want to create the file if not all of these values are missing
            if (xmlItems.Any(kvp => !string.IsNullOrEmpty(kvp.Value)))
            {
                Log.LogMessage("Writing Resource File: {0}", resXmlPath);
                WriteResourceDoc(resXmlPath, xmlItems);
                Log.LogMessage("Wrote Resource File: {0}", resXmlPath);
            }
            else
            {
                // If no
                if (File.Exists(resXmlPath))
                {
                    Log.LogMessage("Deleting Resource File: {0}", resXmlPath);
                    try {
                        File.Delete(resXmlPath);
                    } catch (Exception ex) {
                        Log.LogWarning("Failed to delete Resource File: {0}{1}{2}", resXmlPath, Environment.NewLine, ex);
                    }
                }
            }

            var stampTxt = string.Empty;

            if (File.Exists(resXmlPath))
            {
                stampTxt += resXmlPath + Environment.NewLine;
            }
            if (File.Exists(resValuesPath))
            {
                stampTxt += resValuesPath + Environment.NewLine;
            }

            File.WriteAllText(stampPath, stampTxt);

            Log.LogMessage("Finished ProcessGoogleServicesJson...");
            return(true);
        }