This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathCssGenerator.cs
96 lines (85 loc) · 2.62 KB
/
CssGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;
using IOPath = System.IO.Path;
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(MessageImportance.Low, "Source: {0}", CssFile);
Logger?.LogMessage(MessageImportance.Low, " Language: {0}", Language);
Logger?.LogMessage(MessageImportance.Low, " ResourceID: {0}", ResourceId);
Logger?.LogMessage(MessageImportance.Low, " TargetPath: {0}", TargetPath);
Logger?.LogMessage(MessageImportance.Low, " AssemblyName: {0}", AssemblyName);
Logger?.LogMessage(MessageImportance.Low, " OutputFile {0}", OutputFile);
GenerateCode();
return true;
}
void GenerateCode()
{
//Create the target directory if required
Directory.CreateDirectory(IOPath.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.Replace('\\', '/'))),
new CodeAttributeArgument(new CodePrimitiveExpression(null))
));
//write the result
using (var writer = new StreamWriter(OutputFile))
Provider.GenerateCodeFromCompileUnit(ccu, writer, new CodeGeneratorOptions());
}
}
}