﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace AsyncRewriter
{
    /// <summary>
    /// </summary>
    /// <remarks>
    /// http://stackoverflow.com/questions/2961753/how-to-hide-files-generated-by-custom-tool-in-visual-studio
    /// </remarks>
    public class RewriteAsync : Microsoft.Build.Utilities.Task
    {
        [Required]
        public ITaskItem[] InputFiles { get; set; }
        [Required]
        public ITaskItem OutputFile { get; set; }

        readonly Rewriter _rewriter;

        public RewriteAsync()
        {
            _rewriter = new Rewriter(new TaskLoggingAdapter(Log));
        }

        public override bool Execute()
        {
            var asyncCode = _rewriter.RewriteAndMerge(InputFiles.Select(f => f.ItemSpec).ToArray());
            File.WriteAllText(OutputFile.ItemSpec, asyncCode);
            return true;
        }
    }
}

﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace AsyncRewriter
{
    /// <summary>
    /// </summary>
    /// <remarks>
    /// http://stackoverflow.com/questions/2961753/how-to-hide-files-generated-by-custom-tool-in-visual-studio
    /// </remarks>
    public class RewriteAsync : Microsoft.Build.Utilities.Task
    {
        [Required]
        public ITaskItem[] InputFiles { get; set; }
        [Required]
        public ITaskItem OutputFile { get; set; }

        readonly Rewriter _rewriter;

        public RewriteAsync()
        {
            _rewriter = Log == null ? new Rewriter() : new Rewriter(new TaskLoggingAdapter(Log));
        }

        public override bool Execute()
        {
            var asyncCode = _rewriter.RewriteAndMerge(InputFiles.Select(f => f.ItemSpec).ToArray());
            File.WriteAllText(OutputFile.ItemSpec, asyncCode);
            return true;
        }
    }
}

Fix null reference exception when running under Mono
Fix null reference exception when running under Mono

When running under Mono, Log property is null and then, Rewriter throws NullReferenceExceptions when trying to use the underlying Log. 

When Log is null, use the default Rewriter constructor which uses a Console logger.