Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Xamarin.Forms.Android.sln
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ Global
{D597E3C6-1A50-4042-99FA-3E7CE28E4819}.Release|x86.ActiveCfg = Release|Any CPU
{D597E3C6-1A50-4042-99FA-3E7CE28E4819}.Release|x86.Build.0 = Release|Any CPU
{4B14D295-C09B-4C38-B880-7CC768E50585}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B14D295-C09B-4C38-B880-7CC768E50585}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B14D295-C09B-4C38-B880-7CC768E50585}.Debug|ARM.ActiveCfg = Debug|Any CPU
{4B14D295-C09B-4C38-B880-7CC768E50585}.Debug|ARM.Build.0 = Debug|Any CPU
{4B14D295-C09B-4C38-B880-7CC768E50585}.Debug|iPhone.ActiveCfg = Debug|Any CPU
Expand Down
36 changes: 21 additions & 15 deletions .nuspec/Xamarin.Forms.targets
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,23 @@
</CoreCompileDependsOn>
</PropertyGroup>

<Target Name="XamlG" BeforeTargets="BeforeCompile" DependsOnTargets="PrepareResourceNames" Condition="'$(_XamlGAlreadyExecuted)'!='true'">
<PropertyGroup>
<_XamlGAlreadyExecuted>true</_XamlGAlreadyExecuted>
</PropertyGroup>
<Target Name="_FindXamlGFiles" DependsOnTargets="PrepareResourceNames">
<ItemGroup>
<_XamlGInputs Include="@(EmbeddedResource)" Condition="'%(Extension)' == '.xaml' AND '$(DefaultLanguageSourceExtension)' == '.cs' AND '%(TargetPath)' != ''" />
<_XamlGOutputs Include="@(_XamlGInputs->'$(IntermediateOutputPath)%(TargetPath).g.cs')" />
</ItemGroup>
</Target>

<Target Name="XamlG" BeforeTargets="BeforeCompile" DependsOnTargets="_FindXamlGFiles" Inputs="@(_XamlGInputs)" Outputs="@(_XamlGOutputs)">
<XamlGTask
XamlFiles="@(EmbeddedResource)" Condition="'%(Extension)' == '.xaml' AND '$(DefaultLanguageSourceExtension)' == '.cs'"
Language = "$(Language)"
AssemblyName = "$(AssemblyName)"
OutputPath = "$(IntermediateOutputPath)">
<Output ItemName="FilesWrite" TaskParameter="GeneratedCodeFiles" />
<Output ItemName="Compile" TaskParameter="GeneratedCodeFiles" />
</XamlGTask>
XamlFiles="@(_XamlGInputs)"
OutputFiles="@(_XamlGOutputs)"
Language="$(Language)"
AssemblyName="$(AssemblyName)" />
<ItemGroup>
<FileWrites Include="@(_XamlGOutputs)" />
<Compile Include="@(_XamlGOutputs)" />
</ItemGroup>
</Target>

<!-- XamlC -->
Expand All @@ -94,9 +99,10 @@
DebugSymbols = "$(DebugSymbols)"
DebugType = "$(DebugType)"
KeepXamlResources = "$(XFKeepXamlResources)" />
<Touch Files="$(IntermediateOutputPath)XamlC.stamp" AlwaysCreate="True">
<Output TaskParameter="TouchedFiles" ItemName="FileWrites"/>
</Touch>
<Touch Files="$(IntermediateOutputPath)XamlC.stamp" AlwaysCreate="True" />
<ItemGroup>
<FileWrites Include="$(IntermediateOutputPath)XamlC.stamp" />
</ItemGroup>
</Target>

<!-- CssG -->
Expand All @@ -115,7 +121,7 @@
Language = "$(Language)"
AssemblyName = "$(AssemblyName)"
OutputPath = "$(IntermediateOutputPath)">
<Output ItemName="FilesWrite" TaskParameter="GeneratedCodeFiles" />
<Output ItemName="FileWrites" TaskParameter="GeneratedCodeFiles" />
<Output ItemName="Compile" TaskParameter="GeneratedCodeFiles" />
</CssGTask>
</Target>
Expand Down
32 changes: 16 additions & 16 deletions Xamarin.Forms.Build.Tasks/XamlGTask.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

Expand All @@ -10,44 +9,45 @@ namespace Xamarin.Forms.Build.Tasks
{
public class XamlGTask : Task
{
List<ITaskItem> _generatedCodeFiles = new List<ITaskItem>();

[Required]
public ITaskItem[] XamlFiles { get; set; }

[Output]
public ITaskItem[] GeneratedCodeFiles => _generatedCodeFiles.ToArray();
[Required]
public ITaskItem[] OutputFiles { get; set; }

public string Language { get; set; }
public string AssemblyName { get; set; }
public string OutputPath { get; set; }

public override bool Execute()
{
bool success = true;
Log.LogMessage(MessageImportance.Normal, "Generating code behind for XAML files");

if (XamlFiles == null) {
//NOTE: should not happen due to [Required], but there appears to be a place this is class is called directly
if (XamlFiles == null || OutputFiles == null) {
Log.LogMessage("Skipping XamlG");
return true;
}

foreach (var xamlFile in XamlFiles) {
//when invoked from `UpdateDesigntimeXaml` target, the `TargetPath` isn't set, use a random one instead
var targetPath = xamlFile.GetMetadata("TargetPath");
if (string.IsNullOrWhiteSpace(targetPath))
targetPath = $".{Path.GetRandomFileName()}";
if (XamlFiles.Length != OutputFiles.Length) {
Log.LogError("\"{2}\" refers to {0} item(s), and \"{3}\" refers to {1} item(s). They must have the same number of items.", XamlFiles.Length, OutputFiles.Length, "XamlFiles", "OutputFiles");
return false;
}

var outputFile = Path.Combine(OutputPath, $"{targetPath}.g.cs");
for (int i = 0; i < XamlFiles.Length; i++) {
var xamlFile = XamlFiles[i];
var outputFile = OutputFiles[i].ItemSpec;
if (Path.DirectorySeparatorChar == '/' && outputFile.Contains(@"\"))
outputFile = outputFile.Replace('\\','/');
else if (Path.DirectorySeparatorChar == '\\' && outputFile.Contains(@"/"))
outputFile = outputFile.Replace('/', '\\');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to investigate if this is still an issue (#1250)


var generator = new XamlGenerator(xamlFile, Language, AssemblyName, outputFile, Log);
try {
if (generator.Execute())
_generatedCodeFiles.Add(new TaskItem(Microsoft.Build.Evaluation.ProjectCollection.Escape(outputFile)));
if (!generator.Execute()) {
//If Execute() fails, the file still needs to exist because it is added to the <Compile/> ItemGroup
File.WriteAllText (outputFile, string.Empty);
}
}
catch (XmlException xe) {
Log.LogError(null, null, null, xamlFile.ItemSpec, xe.LineNumber, xe.LinePosition, 0, 0, xe.Message, xe.HelpLink, xe.Source);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Build.Framework;

namespace Xamarin.Forms.Xaml.UnitTests
{
public class DummyBuildEngine : IBuildEngine
{
public List<BuildErrorEventArgs> Errors { get; } = new List<BuildErrorEventArgs> ();

public List<BuildWarningEventArgs> Warnings { get; } = new List<BuildWarningEventArgs> ();

public List<BuildMessageEventArgs> Messages { get; } = new List<BuildMessageEventArgs> ();

public void LogErrorEvent (BuildErrorEventArgs e)
{
Errors.Add (e);
}

public void LogWarningEvent (BuildWarningEventArgs e)
{
Warnings.Add (e);
}

public void LogMessageEvent (BuildMessageEventArgs e)
{
Messages.Add (e);
}

public void LogCustomEvent (CustomBuildEventArgs e)
Expand Down
Loading