Skip to content

Commit

Permalink
add Google Services
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Mar 17, 2021
1 parent cf871a8 commit 46fdda6
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Mobile.BuildTools/Models/BuildToolsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public string Schema
[JsonProperty("environment")]
public EnvironmentSettings Environment { get; set; }

[JsonProperty("google")]
public GoogleConfig Google { get; set; }

[JsonProperty("debug")]
public bool Debug { get; set; }
}
Expand Down
9 changes: 9 additions & 0 deletions src/Mobile.BuildTools/Models/GoogleConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Mobile.BuildTools.Models
{
public class GoogleConfig
{
public string ServicesJson { get; set; }

public string InfoPlist { get; set; }
}
}
20 changes: 19 additions & 1 deletion src/Mobile.BuildTools/MonoAndroid.targets
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,22 @@
</ItemGroup>
</Target>

</Project>
<Target Name="MBTBundleGoogleServices"
BeforeTargets="Compile;CoreCompile">
<GoogleTask ConfigurationPath="$(BuildToolsConfigFilePath)"
ProjectName="$(MSBuildProjectName)"
ProjectDirectory="$(MSBuildProjectDirectory)"
SolutionDirectory="$(SolutionDir)"
Configuration="$(Configuration)"
IntermediateOutputPath="$(IntermediateOutputPath)"
TargetFrameworkIdentifier="$(TargetFrameworkIdentifier)">
<Output ItemName="GeneratedFile"
TaskParameter="GoogleOutput"/>
<Output ItemName="FilesWrite"
TaskParameter="GoogleOutput"/>
<Output ItemName="GoogleServicesJson"
TaskParameter="GoogleOutput" />
</GoogleTask>
</Target>

</Project>
58 changes: 58 additions & 0 deletions src/Mobile.BuildTools/Tasks/GoogleTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Mobile.BuildTools.Build;
using Mobile.BuildTools.Configuration;
using Mobile.BuildTools.Utils;

namespace Mobile.BuildTools.Tasks
{
public class GoogleTask : BuildToolsTaskBase
{
private List<ITaskItem> _outputs = new List<ITaskItem>();
[Output]
public ITaskItem[] GoogleOutput => _outputs.ToArray();

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

private void GetGoogleServicesJson(IBuildConfiguration buildConfig)
{
var json = Environment.GetEnvironmentVariable(buildConfig.Configuration.Google.ServicesJson);
if (string.IsNullOrEmpty(json))
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");
_outputs.Add(item);
}

private void GetGoogleInfoPlist(IBuildConfiguration buildConfig)
{
var json = Environment.GetEnvironmentVariable(buildConfig.Configuration.Google.InfoPlist);
if (string.IsNullOrEmpty(json))
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);
}
}
}
18 changes: 18 additions & 0 deletions src/Mobile.BuildTools/iOS.targets
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,22 @@

</Target>

<Target Name="MBTBundleGoogleServices"
BeforeTargets="Compile;CoreCompile">
<GoogleTask ConfigurationPath="$(BuildToolsConfigFilePath)"
ProjectName="$(MSBuildProjectName)"
ProjectDirectory="$(MSBuildProjectDirectory)"
SolutionDirectory="$(SolutionDir)"
Configuration="$(Configuration)"
IntermediateOutputPath="$(IntermediateOutputPath)"
TargetFrameworkIdentifier="$(TargetFrameworkIdentifier)">
<Output ItemName="GeneratedFile"
TaskParameter="GoogleOutput"/>
<Output ItemName="FilesWrite"
TaskParameter="GoogleOutput"/>
<Output ItemName="BundleResource"
TaskParameter="GoogleOutput" />
</GoogleTask>
</Target>

</Project>

0 comments on commit 46fdda6

Please sign in to comment.