This repository has been archived by the owner on May 1, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43143bd
commit 1b1f6ce
Showing
35 changed files
with
1,499 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
Xamarin.Forms.Build.Tasks/CompiledConverters/StyleSheetConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Mono.Cecil.Cil; | ||
|
||
using static Mono.Cecil.Cil.Instruction; | ||
using static Mono.Cecil.Cil.OpCodes; | ||
|
||
using Xamarin.Forms.Build.Tasks; | ||
using Xamarin.Forms.Xaml; | ||
|
||
namespace Xamarin.Forms.Core.XamlC | ||
{ | ||
class StyleSheetConverter : ICompiledTypeConverter | ||
{ | ||
public IEnumerable<Instruction> ConvertFromString(string value, ILContext context, BaseNode node) | ||
{ | ||
var module = context.Body.Method.Module; | ||
var body = context.Body; | ||
|
||
INode rootNode = node; | ||
while (!(rootNode is ILRootNode)) | ||
rootNode = rootNode.Parent; | ||
|
||
var rootTargetPath = RDSourceTypeConverter.GetPathForType(module, ((ILRootNode)rootNode).TypeReference); | ||
var uri = new Uri(value, UriKind.Relative); | ||
|
||
var resourceId = ResourceDictionary.RDSourceTypeConverter.GetResourceId(uri, rootTargetPath, s => RDSourceTypeConverter.GetResourceIdForPath(module, s)); | ||
|
||
//return StyleSheet.Parse(rootObjectType.GetTypeInfo().Assembly, resourceId, lineInfo); | ||
|
||
var getTypeFromHandle = module.ImportReference(typeof(Type).GetMethod("GetTypeFromHandle", new[] { typeof(RuntimeTypeHandle) })); | ||
var getAssembly = module.ImportReference(typeof(Type).GetProperty("Assembly").GetGetMethod()); | ||
yield return Create(Ldtoken, module.ImportReference(((ILRootNode)rootNode).TypeReference)); | ||
yield return Create(Call, module.ImportReference(getTypeFromHandle)); | ||
yield return Create(Callvirt, module.ImportReference(getAssembly)); //assembly | ||
|
||
yield return Create(Ldstr, resourceId); //resourceId | ||
|
||
foreach (var instruction in node.PushXmlLineInfo(context)) | ||
yield return instruction; //lineinfo | ||
|
||
var styleSheetParse = module.ImportReference(typeof(StyleSheets.StyleSheet).GetMethods().FirstOrDefault(mi => mi.Name == "Parse" && mi.GetParameters().Length == 3)); | ||
yield return Create(Call, module.ImportReference(styleSheetParse)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Xml; | ||
|
||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Xamarin.Forms.Build.Tasks | ||
{ | ||
public class CssGTask : Task | ||
{ | ||
readonly List<ITaskItem> _generatedCodeFiles = new List<ITaskItem>(); | ||
|
||
[Required] | ||
public ITaskItem[] XamlFiles { get; set; } | ||
|
||
[Output] | ||
public ITaskItem[] GeneratedCodeFiles => _generatedCodeFiles.ToArray(); | ||
|
||
public string Language { get; set; } | ||
public string AssemblyName { get; set; } | ||
public string OutputPath { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
bool success = true; | ||
|
||
if (XamlFiles == null) { | ||
Log.LogMessage("Skipping CssG"); | ||
return true; | ||
} | ||
|
||
foreach (var xamlFile in XamlFiles) { | ||
var outputFile = Path.Combine(OutputPath, $"{xamlFile.GetMetadata("TargetPath")}.g.cs"); | ||
var generator = new CssGenerator(xamlFile, Language, AssemblyName, outputFile, Log); | ||
try { | ||
if (generator.Execute()) | ||
_generatedCodeFiles.Add(new TaskItem(Microsoft.Build.Evaluation.ProjectCollection.Escape(outputFile))); | ||
} | ||
catch (XmlException xe) { | ||
Log.LogError(null, null, null, xamlFile.ItemSpec, xe.LineNumber, xe.LinePosition, 0, 0, xe.Message, xe.HelpLink, xe.Source); | ||
|
||
success = false; | ||
} | ||
catch (Exception e) { | ||
Log.LogError(null, null, null, xamlFile.ItemSpec, 0, 0, 0, 0, e.Message, e.HelpLink, e.Source); | ||
success = false; | ||
} | ||
} | ||
|
||
return success; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System.CodeDom; | ||
using System.CodeDom.Compiler; | ||
using System.IO; | ||
|
||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
using Microsoft.CSharp; | ||
|
||
using Xamarin.Forms.Xaml; | ||
|
||
namespace Xamarin.Forms.Build.Tasks | ||
{ | ||
class CssGenerator | ||
{ | ||
internal CssGenerator() | ||
{ | ||
} | ||
|
||
public CssGenerator( | ||
ITaskItem taskItem, | ||
string language, | ||
string assemblyName, | ||
string outputFile, | ||
TaskLoggingHelper logger) | ||
: this( | ||
taskItem.ItemSpec, | ||
language, | ||
taskItem.GetMetadata("ManifestResourceName"), | ||
taskItem.GetMetadata("TargetPath"), | ||
assemblyName, | ||
outputFile, | ||
logger) | ||
{ | ||
} | ||
|
||
internal static CodeDomProvider Provider = new CSharpCodeProvider(); | ||
|
||
public string CssFile { get; } | ||
public string Language { get; } | ||
public string ResourceId { get; } | ||
public string TargetPath { get; } | ||
public string AssemblyName { get; } | ||
public string OutputFile { get; } | ||
public TaskLoggingHelper Logger { get; } | ||
|
||
public CssGenerator( | ||
string cssFile, | ||
string language, | ||
string resourceId, | ||
string targetPath, | ||
string assemblyName, | ||
string outputFile, | ||
TaskLoggingHelper logger = null) | ||
{ | ||
CssFile = cssFile; | ||
Language = language; | ||
ResourceId = resourceId; | ||
TargetPath = targetPath; | ||
AssemblyName = assemblyName; | ||
OutputFile = outputFile; | ||
Logger = logger; | ||
} | ||
|
||
//returns true if a file is generated | ||
public bool Execute() | ||
{ | ||
Logger?.LogMessage("Source: {0}", CssFile); | ||
Logger?.LogMessage(" Language: {0}", Language); | ||
Logger?.LogMessage(" ResourceID: {0}", ResourceId); | ||
Logger?.LogMessage(" TargetPath: {0}", TargetPath); | ||
Logger?.LogMessage(" AssemblyName: {0}", AssemblyName); | ||
Logger?.LogMessage(" OutputFile {0}", OutputFile); | ||
|
||
GenerateCode(); | ||
|
||
return true; | ||
} | ||
|
||
void GenerateCode() | ||
{ | ||
//Create the target directory if required | ||
Directory.CreateDirectory(Path.GetDirectoryName(OutputFile)); | ||
|
||
var ccu = new CodeCompileUnit(); | ||
ccu.AssemblyCustomAttributes.Add( | ||
new CodeAttributeDeclaration(new CodeTypeReference($"global::{typeof(XamlResourceIdAttribute).FullName}"), | ||
new CodeAttributeArgument(new CodePrimitiveExpression(ResourceId)), | ||
new CodeAttributeArgument(new CodePrimitiveExpression(TargetPath)), | ||
new CodeAttributeArgument(new CodePrimitiveExpression(null)) | ||
)); | ||
|
||
//write the result | ||
using (var writer = new StreamWriter(OutputFile)) | ||
Provider.GenerateCodeFromCompileUnit(ccu, writer, new CodeGeneratorOptions()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
|
||
using NUnit.Framework; | ||
|
||
using Xamarin.Forms.Core.UnitTests; | ||
|
||
namespace Xamarin.Forms.StyleSheets.UnitTests | ||
{ | ||
[TestFixture] | ||
public class IStylableTest | ||
{ | ||
[SetUp] | ||
public void SetUp() | ||
{ | ||
Device.PlatformServices = new MockPlatformServices(); | ||
Internals.Registrar.RegisterAll(new Type[0]); | ||
} | ||
|
||
[TestCase] | ||
public void GetBackgroundColor() | ||
{ | ||
var label = new Label(); | ||
var bp = ((IStylable)label).GetProperty("background-color"); | ||
Assert.AreSame(VisualElement.BackgroundColorProperty, bp); | ||
} | ||
|
||
[TestCase] | ||
public void GetLabelColor() | ||
{ | ||
var label = new Label(); | ||
var bp = ((IStylable)label).GetProperty("color"); | ||
Assert.AreSame(Label.TextColorProperty, bp); | ||
} | ||
|
||
[TestCase] | ||
public void GetEntryColor() | ||
{ | ||
var entry = new Entry(); | ||
var bp = ((IStylable)entry).GetProperty("color"); | ||
Assert.AreSame(Entry.TextColorProperty, bp); | ||
} | ||
|
||
[TestCase] | ||
public void GetGridColor() | ||
{ | ||
var grid = new Grid(); | ||
var bp = ((IStylable)grid).GetProperty("color"); | ||
Assert.Null(bp); | ||
} | ||
} | ||
} |
Oops, something went wrong.