Skip to content

Commit

Permalink
allow Google Services task to read an existing file or create one
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Jul 1, 2021
1 parent 9c702c8 commit cc1970c
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 10 deletions.
17 changes: 17 additions & 0 deletions docs/google/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Google Firebase is an incredibly popular solution for app analytics and cross platform push notifications. Unfortunately we see a lot of developers following poor programming practices including the `google-services.json` and `GoogleService-Info.plist` in source control. The Mobile.BuildTools is all about empowering you to follow best practices while developing your mobile apps.

To start be sure to add these files to your `.gitignore` that way the file isn't accidentally checked into source control while you use it for local development. The Mobile.BuildTools is able to bring these resources in at build time using your favorite CI Build service. To get started you'll want to come up with a variable name and set this in your `buildtools.json` like the following:

```json
{
"google": {
"servicesJson": "GoogleServicesJson",
"infoPlist": "GoogleServicesInfoPlist"
}
}
```

If the variable exists locally or on your CI Server, the Mobile.BuildTools will automatically look it up and determine if it is a file path. If it is it will add the appropriate include for iOS/Android. Otherwise it will take the contents of the variable and add the file to the Intermediate Output Directory (the obj folder) during the build, and it will again add the appropriate platform include.

!!! note
Due to some build agents, such as all Windows agents on Azure DevOps, performing a ToUpper on all environment variable names, the Mobile.BuildTools will do a Case insensitive lookup to match the variable name.
28 changes: 28 additions & 0 deletions docs/schemas/v2/buildtools.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@
}
}
},
"GoogleServices": {
"type": [
"object",
"null"
],
"description": "GoogleServices location. This can be a file location or the raw contents.",
"properties": {
"servicesJson": {
"type": [
"string",
"null"
],
"default": "Environment Variable name",
"description": "Environment Variable name containing the file contents or file path to the google-services.json"
},
"infoPlist": {
"type": [
"string",
"null"
],
"default": "Environment Variable name",
"description": "Environment Variable name containing the file contents or file path to the GoogleService-Info.plist"
}
}
},
"ImageResize": {
"type": [
"object",
Expand Down Expand Up @@ -385,6 +410,9 @@
"css": {
"$ref": "#/definitions/XamarinCss"
},
"google": {
"$ref": "#/definitions/GoogleServices"
},
"images": {
"$ref": "#/definitions/ImageResize"
},
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ nav:
- App Manifests:
- Tokenized Manifests: manifests/index.md
- Build Versioning: manifests/versioning.md
- Google Firebase: google/index.md
- Image Assets:
- Getting Started: images/index.md
- Configuring Images: images/configuring-images.md
Expand Down
48 changes: 38 additions & 10 deletions src/Mobile.BuildTools/Tasks/GoogleTask.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Mobile.BuildTools.Build;
Expand All @@ -17,24 +18,23 @@ public class GoogleTask : BuildToolsTaskBase

internal override void ExecuteInternal(IBuildConfiguration config)
{
if (config.Platform == Platform.Android && !string.IsNullOrEmpty(config.Configuration.Google?.ServicesJson))
if (config.Platform == Platform.Android)
{
GetGoogleServicesJson(config);
}
else if (config.Platform == Platform.iOS && !string.IsNullOrEmpty(config.Configuration.Google?.InfoPlist))
else if (config.Platform == Platform.iOS)
{
GetGoogleInfoPlist(config);
}
}

private void GetGoogleServicesJson(IBuildConfiguration buildConfig)
{
var json = Environment.GetEnvironmentVariable(buildConfig.Configuration.Google.ServicesJson);
if (string.IsNullOrEmpty(json))
var path = GetResourcePath(buildConfig.Configuration.Google?.ServicesJson, buildConfig.IntermediateOutputPath, "google-services.json");

if (string.IsNullOrEmpty(path))
return;

var path = Path.Combine(buildConfig.IntermediateOutputPath, "aritchie-sucks", "google-services.json");
File.WriteAllText(path, json);
// Output to GoogleServicesJson Include="path"
var item = new TaskItem(path);
item.SetMetadata("LogicalName", "google-services.json");
Expand All @@ -43,16 +43,44 @@ private void GetGoogleServicesJson(IBuildConfiguration buildConfig)

private void GetGoogleInfoPlist(IBuildConfiguration buildConfig)
{
var json = Environment.GetEnvironmentVariable(buildConfig.Configuration.Google.InfoPlist);
if (string.IsNullOrEmpty(json))
var path = GetResourcePath(buildConfig.Configuration.Google?.InfoPlist, buildConfig.IntermediateOutputPath, "GoogleService-Info.plist");

if (string.IsNullOrEmpty(path))
return;

var path = Path.Combine(buildConfig.IntermediateOutputPath, "aritchie-sucks", "GoogleService-Info.plist");
File.WriteAllText(path, json);
// Output to BundleResource Include="path"
var item = new TaskItem(path);
item.SetMetadata("LogicalName", "GoogleService-Info.plist");
_outputs.Add(item);
}

internal static string GetResourcePath(string environmentVariable, string intermediateOutputPath, string logicalName)
{
if (string.IsNullOrEmpty(environmentVariable))
return null;

var variables = Environment.GetEnvironmentVariables().Keys.Cast<string>();
var variableName = variables.FirstOrDefault(x => x.Equals(environmentVariable, StringComparison.InvariantCultureIgnoreCase));

if (string.IsNullOrEmpty(variableName))
return null;

var value = Environment.GetEnvironmentVariable(variableName);
try
{
if (File.Exists(value))
return new FileInfo(value).FullName;
}
catch
{
// Suppress errors
}

var directory = new DirectoryInfo(Path.Combine(intermediateOutputPath, "google-services"));
directory.Create();
var path = Path.Combine(directory.FullName, logicalName);
File.WriteAllText(path, value);
return path;
}
}
}
64 changes: 64 additions & 0 deletions tests/Mobile.BuildTools.Tests/Fixtures/Tasks/GoogleTaskFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mobile.BuildTools.Tasks;
using Xunit;

namespace Mobile.BuildTools.Tests.Fixtures.Tasks
{
public class GoogleTaskFixture
{
private readonly string IntermediateOutputDirectory = new FileInfo(Path.Combine("Generated", "GoogleTask")).FullName;

[Fact]
public void ReturnsIntermediateFileFromVariable()
{
var contents = "SampleGoogleServices";
var environmentName = nameof(ReturnsIntermediateFileFromVariable);
Environment.SetEnvironmentVariable(environmentName, contents, EnvironmentVariableTarget.Process);

var path = GoogleTask.GetResourcePath(environmentName, IntermediateOutputDirectory, "google-services.json");

var expectedPath = Path.Combine(IntermediateOutputDirectory, "google-services", "google-services.json");

Assert.Equal(expectedPath, path);

Assert.Equal(contents, File.ReadAllText(path));
}

[Fact]
public void ReturnsSpecifiedFileFromRelativePath()
{
var contents = "SampleGoogleServices";
var directory = Path.Combine("Generated", "tests", nameof(ReturnsSpecifiedFileFromRelativePath));
Directory.CreateDirectory(directory);
var expectedPath = Path.Combine(directory, "google-test.json");
File.WriteAllText(expectedPath, contents);
var environmentName = nameof(ReturnsSpecifiedFileFromRelativePath);
Environment.SetEnvironmentVariable(environmentName, expectedPath, EnvironmentVariableTarget.Process);

var path = GoogleTask.GetResourcePath(environmentName, IntermediateOutputDirectory, "google-services.json");

Assert.Equal(new FileInfo(expectedPath).FullName, path);
}

[Fact]
public void ReturnsSpecifiedFileFromAbsolutePath()
{
var contents = "SampleGoogleServices";
var directory = Path.Combine("Generated", "tests", nameof(ReturnsSpecifiedFileFromAbsolutePath));
Directory.CreateDirectory(directory);
var expectedPath = new FileInfo(Path.Combine(directory, "google-test.json")).FullName;
File.WriteAllText(expectedPath, contents);
var environmentName = nameof(ReturnsSpecifiedFileFromAbsolutePath);
Environment.SetEnvironmentVariable(environmentName, expectedPath, EnvironmentVariableTarget.Process);

var path = GoogleTask.GetResourcePath(environmentName, IntermediateOutputDirectory, "google-services.json");

Assert.Equal(expectedPath, path);
}
}
}

0 comments on commit cc1970c

Please sign in to comment.