﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Cake.VisualStudio.Helpers;

namespace Cake.VisualStudio.TaskRunner
{
    class TaskParser
    {
        private static string _loadPattern = "#load \"";
        public static SortedList<string, string> LoadTasks(string configPath)
        {
            var list = new SortedList<string, string>();

            try
            {
                var script = new ScriptContent(configPath);
                script.Parse(_loadPattern, s => s.Replace("#load", string.Empty).Trim().Trim('"', ';'));
                var document = script.ToString();
                var r = new Regex("Task\\([\\w\\\"](.+)\\b\\\"*\\)");
                var matches = r.Matches(document);
                var taskNames = matches.Cast<Match>().Select(m => m.Groups[1].Value);
                foreach (var name in taskNames)
                {
                    list.Add(name, $"-Target=\"{name}\"");
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }

            return list;
        }
    }
}
﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Cake.VisualStudio.Helpers;

namespace Cake.VisualStudio.TaskRunner
{
    class TaskParser
    {
        private static string _loadPattern = "#load \"";
        public static SortedList<string, string> LoadTasks(string configPath)
        {
            var list = new SortedList<string, string>();

            try
            {
                var script = new ScriptContent(configPath);
                script.Parse(_loadPattern, s => s.Replace("#load", string.Empty).Trim().Trim('"', ';'));
                var document = script.ToString();
                var r = new Regex("Task\\s*\\(\\s*\\\"(.+)\\b\\\"\\s*\\)");
                var matches = r.Matches(document);
                var taskNames = matches.Cast<Match>().Select(m => m.Groups[1].Value);
                foreach (var name in taskNames)
                {
                    list.Add(name, $"-Target=\"{name}\"");
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }

            return list;
        }
    }
}
Rework regex to allow whitespace
task-runner: Rework regex to allow whitespace

Some projects may enforce special coding style guidelines on their
projects. To be more flexible in this area the new regex will now allow
whitespace before and in between the brackets. Following some examples of
allowed task name definitions.

    Task("F_0$0")
    Task( "F_0$0" )
    Task ( "F_0$0" )
    Task ( "F_0$0" )
    Task(
      "F_0$0"
    )
    Task (
      "F_0$0"
    )
    Task (
    	"F_0$0"
    )

This fixes #52
