2017-12-20 12:47:50 +03:00
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
{
[Required]
2018-06-11 12:56:17 +03:00
public ITaskItem [ ] CSSFiles { get ; set ; }
2017-12-20 12:47:50 +03:00
2018-06-11 12:56:17 +03:00
[Required]
public ITaskItem [ ] OutputFiles { get ; set ; }
2017-12-20 12:47:50 +03:00
public string Language { get ; set ; }
public string AssemblyName { get ; set ; }
public override bool Execute ( )
{
bool success = true ;
2018-02-08 16:31:35 +03:00
Log . LogMessage ( MessageImportance . Normal , "Generating assembly attributes for CSS files" ) ;
2018-06-11 12:56:17 +03:00
if ( CSSFiles = = null | | OutputFiles = = null ) {
2018-02-08 16:31:35 +03:00
Log . LogMessage ( MessageImportance . Low , "Skipping CssG" ) ;
2017-12-20 12:47:50 +03:00
return true ;
}
2018-06-11 12:56:17 +03:00
if ( CSSFiles . 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." , CSSFiles . Length , OutputFiles . Length , "CSSFiles" , "OutputFiles" ) ;
return false ;
}
for ( var i = 0 ; i < CSSFiles . Length ; i + + ) {
var cssFile = CSSFiles [ i ] ;
var outputFile = OutputFiles [ i ] . ItemSpec ;
var generator = new CssGenerator ( cssFile , Language , AssemblyName , outputFile , Log ) ;
2017-12-20 12:47:50 +03:00
try {
2018-06-11 12:56:17 +03:00
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 ) ;
}
2017-12-20 12:47:50 +03:00
}
catch ( XmlException xe ) {
2018-06-11 12:56:17 +03:00
Log . LogError ( null , null , null , cssFile . ItemSpec , xe . LineNumber , xe . LinePosition , 0 , 0 , xe . Message , xe . HelpLink , xe . Source ) ;
2017-12-20 12:47:50 +03:00
success = false ;
}
catch ( Exception e ) {
2018-06-11 12:56:17 +03:00
Log . LogError ( null , null , null , cssFile . ItemSpec , 0 , 0 , 0 , 0 , e . Message , e . HelpLink , e . Source ) ;
2017-12-20 12:47:50 +03:00
success = false ;
}
}
return success ;
}
}
}