Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FakeSearchCategory.fs « MonoDevelop.FSharpBinding « fsharpbinding « external « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f8867d3909f3ba46065136adbdc9a0403d4fdae1 (plain)
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
namespace MonoDevelop.FSharp

open System.Diagnostics
open System.IO
open System.Text.RegularExpressions
open System.Threading
open System.Threading.Tasks
open MonoDevelop.Core
open MonoDevelop.Core.Text
open MonoDevelop.Components.MainToolbar
open MonoDevelop.Ide
open MonoDevelop.Projects
open MonoDevelop.Core.Execution
open MonoDevelop.Ide.Gui

type FakeSearchResult(solution: Solution, match', matchedString, rank, scriptPath) =
    inherit SearchResult(match', matchedString, rank)

    override x.SearchResultType = SearchResultType.Type

    override x.Description =
        sprintf "Runs the FAKE %s task in %s" matchedString solution.Name

    override x.PlainText = "FAKE " + matchedString

    override x.Icon = getImage "md-command"

    override x.CanActivate = true

    override x.Activate() =
        let monitor = IdeApp.Workbench.ProgressMonitors.GetRunProgressMonitor ("FAKE")
        let processStartInfo = Runtime.ProcessService.CreateProcessStartInfo(scriptPath, matchedString, string solution.BaseDirectory, true)
        let fakeProcess = Process.Start processStartInfo

        fakeProcess.EnableRaisingEvents <- true
        fakeProcess.BeginOutputReadLine()
        fakeProcess.BeginErrorReadLine()
        fakeProcess.OutputDataReceived.Add(fun de -> monitor.Log.WriteLine de.Data)
        fakeProcess.ErrorDataReceived.Add(fun de -> monitor.ErrorLog.WriteLine de.Data)

        let pad = IdeApp.Workbench.ProgressMonitors.GetPadForMonitor monitor
        pad.BringToFront()

        fakeProcess.Exited.Add(fun _ -> monitor.Dispose())

type FakeSearchCategory() =
    inherit SearchCategory("FAKE", sortOrder = SearchCategory.FirstCategoryOrder)

    override x.get_Tags() = [|"fake"|]

    override x.IsValidTag _tag = true

    override x.GetResults(searchCallback, pattern, token) =

        let addResult (solution: Solution, m: Match, rank, scriptPath) =
            if token.IsCancellationRequested then ()
            else
                let sr = FakeSearchResult(solution, pattern.Pattern, m.Groups.[1].Value, rank, scriptPath)
                searchCallback.ReportResult sr

        Task.Run(
          (fun () ->
              let matcher = StringMatcher.GetMatcher (pattern.Pattern, false)
              async {
                  for solution in IdeApp.Workspace.GetAllSolutions() do

                    let launcherScript = if Platform.IsWindows then
                                              "build.cmd"
                                          else
                                              "build.sh"

                    let fakeScriptPath = Path.Combine([|string solution.BaseDirectory; "build.fsx"|])
                    let launcherScriptPath = Path.Combine([|string solution.BaseDirectory; launcherScript|])
                    if File.Exists(fakeScriptPath) && File.Exists(launcherScriptPath)  then
                        let fakeScript = File.ReadAllText fakeScriptPath

                        Regex.Matches(fakeScript, "Target \"([\\w.]+)\"")
                        |> Seq.cast<Match>
                        |> Seq.choose(fun x -> let (matched, rank) = matcher.CalcMatchRank ("FAKE " + x.Groups.[1].Value)
                                               match matched with
                                               | true -> Some (solution, x, rank, launcherScriptPath)
                                               | _ -> None)
                        |> Seq.iter addResult }
              |> Async.StartAndLogException ), token)