2012-11-09 06:03:14 +01:00
|
|
|
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
module BuildTasks
|
|
|
|
|
|
|
|
open System
|
|
|
|
open System.Diagnostics
|
|
|
|
open Microsoft.Build.Evaluation
|
|
|
|
open Microsoft.Build.Framework
|
|
|
|
open Microsoft.Build.Utilities
|
|
|
|
|
2012-11-09 22:33:03 +01:00
|
|
|
/// Read all of the defined properties from the calling project file and stuff
|
|
|
|
/// them in a Map
|
2012-11-09 06:03:14 +01:00
|
|
|
let propertyMap (be : IBuildEngine) =
|
|
|
|
use reader = Xml.XmlReader.Create(be.ProjectFileOfTaskNode)
|
|
|
|
let project = new Project(reader)
|
|
|
|
project.AllEvaluatedProperties
|
|
|
|
|> Seq.filter (fun x -> not x.IsEnvironmentProperty)
|
|
|
|
|> Seq.filter (fun x -> not x.IsGlobalProperty)
|
|
|
|
|> Seq.filter (fun x -> not x.IsReservedProperty)
|
|
|
|
|> Seq.map (fun x -> (x.Name, x.EvaluatedValue))
|
|
|
|
|> Map.ofSeq
|
|
|
|
|
2012-11-15 21:36:23 +01:00
|
|
|
/// Convert an absolute windows path to an msys path
|
|
|
|
let mungePath path =
|
2012-11-09 22:33:03 +01:00
|
|
|
let matchre pat str =
|
|
|
|
let m = System.Text.RegularExpressions.Regex.Match(str, pat)
|
|
|
|
if m.Success
|
2012-11-10 04:30:31 +01:00
|
|
|
then List.tail [ for g in m.Groups -> g.Value ]
|
2012-11-09 22:33:03 +01:00
|
|
|
else []
|
2012-11-15 21:36:23 +01:00
|
|
|
match matchre "([A-Za-z]):\\\\(.*)" path with
|
2012-11-09 22:33:03 +01:00
|
|
|
| drive :: path :: [] -> sprintf "/%s/%s" drive (path.Replace('\\', '/'))
|
2012-11-15 21:36:23 +01:00
|
|
|
| _ -> path
|
2012-11-09 22:33:03 +01:00
|
|
|
|
2012-11-15 21:36:23 +01:00
|
|
|
type ShellWrapper(conf : ITaskItem) =
|
2012-11-10 04:30:31 +01:00
|
|
|
inherit ToolTask()
|
2012-11-09 06:03:14 +01:00
|
|
|
|
2012-11-10 04:30:31 +01:00
|
|
|
member val Arguments = "" with get, set
|
|
|
|
member val WorkingDirectory = "" with get, set
|
2012-11-09 06:03:14 +01:00
|
|
|
|
2012-11-10 04:30:31 +01:00
|
|
|
// ToolTask overrides
|
|
|
|
override val ToolName = "sh.exe" with get
|
2012-11-15 21:36:23 +01:00
|
|
|
override this.GenerateFullPathToTool() = conf.GetMetadata "Sh"
|
2012-11-10 04:30:31 +01:00
|
|
|
override this.GenerateCommandLineCommands() = this.Arguments
|
|
|
|
override this.GetWorkingDirectory() = this.WorkingDirectory
|
2012-11-09 22:33:03 +01:00
|
|
|
|
2012-11-10 04:30:31 +01:00
|
|
|
override this.Execute() =
|
|
|
|
if this.GenerateFullPathToTool() |> IO.File.Exists |> not then
|
|
|
|
failwith "sh.exe not found. Make sure the MSYS root is set to a correct location."
|
|
|
|
|
2012-11-15 21:36:23 +01:00
|
|
|
if not <| IO.Directory.Exists this.WorkingDirectory then ignore <| IO.Directory.CreateDirectory this.WorkingDirectory
|
|
|
|
|
2012-11-10 04:30:31 +01:00
|
|
|
this.UseCommandProcessor <- false
|
|
|
|
this.StandardOutputImportance <- "High"
|
2012-11-15 21:36:23 +01:00
|
|
|
this.EnvironmentVariables <- [| for x in ["CC"; "CPP"; "CFLAGS"; "PATH"; "INCLUDE"; "LIB"]
|
2013-12-11 21:30:27 +01:00
|
|
|
-> sprintf "%s=%s" <| x <| conf.GetMetadata(x).Replace("\n", "").Replace(" ", "") |]
|
2012-11-10 04:30:31 +01:00
|
|
|
base.Execute()
|
2012-11-09 06:03:14 +01:00
|
|
|
|
|
|
|
type ExecShellScript() =
|
|
|
|
inherit Task()
|
|
|
|
|
2012-11-10 04:30:31 +01:00
|
|
|
// Task arguments
|
2012-11-15 21:36:23 +01:00
|
|
|
[<Required>] member val WorkingDirectory = "" with get, set
|
|
|
|
[<Required>] member val Command = "" with get, set
|
|
|
|
[<Required>] member val Configuration : ITaskItem = null with get, set
|
2012-11-09 22:33:03 +01:00
|
|
|
member val Arguments = "" with get, set
|
2012-11-09 06:03:14 +01:00
|
|
|
|
2012-11-15 21:36:23 +01:00
|
|
|
member private this.realArgs () =
|
2012-11-12 22:10:00 +01:00
|
|
|
let cleanArgs = this.Arguments.Replace("\r", "").Replace('\n', ' ')
|
2012-11-15 21:36:23 +01:00
|
|
|
sprintf "-c '%s %s'" (mungePath this.Command) cleanArgs
|
2012-11-10 04:30:31 +01:00
|
|
|
|
2012-11-09 06:03:14 +01:00
|
|
|
override this.Execute() =
|
|
|
|
try
|
2012-11-15 21:36:23 +01:00
|
|
|
let sw = ShellWrapper(this.Configuration,
|
2012-11-10 04:30:31 +01:00
|
|
|
BuildEngine = this.BuildEngine,
|
|
|
|
HostObject = this.HostObject,
|
2012-11-15 21:36:23 +01:00
|
|
|
Arguments = this.realArgs(),
|
2012-11-10 04:30:31 +01:00
|
|
|
WorkingDirectory = this.WorkingDirectory)
|
|
|
|
|
|
|
|
sw.Execute()
|
|
|
|
with
|
|
|
|
| Failure(e) -> this.Log.LogError(e); false
|
|
|
|
| e -> this.Log.LogErrorFromException(e); false
|
2012-11-09 22:33:03 +01:00
|
|
|
|
|
|
|
type MsysPath() =
|
|
|
|
inherit Task()
|
|
|
|
|
|
|
|
member val Path = "" with get, set
|
2012-11-15 21:36:23 +01:00
|
|
|
[<Output>] member val Result = "" with get, set
|
2012-11-09 22:33:03 +01:00
|
|
|
|
|
|
|
override this.Execute() =
|
|
|
|
try
|
2012-11-15 21:36:23 +01:00
|
|
|
this.Result <- mungePath this.Path
|
2012-11-09 06:03:14 +01:00
|
|
|
true
|
2012-11-09 22:33:03 +01:00
|
|
|
with Failure(e) ->
|
2012-11-09 06:03:14 +01:00
|
|
|
this.Log.LogError(e)
|
|
|
|
false
|
2012-11-09 22:33:03 +01:00
|
|
|
|
|
|
|
type UpdateFile() =
|
|
|
|
inherit Task()
|
|
|
|
|
|
|
|
member val File = "" with get, set
|
|
|
|
member val Find = "" with get, set
|
|
|
|
member val Replacement = "" with get, set
|
|
|
|
|
|
|
|
override this.Execute() =
|
|
|
|
try
|
|
|
|
this.Log.LogMessage("Replacing '{0}' with '{1}' in '{2}'", this.Find, this.Replacement, this.File)
|
|
|
|
let text = IO.File.ReadAllText(this.File).Replace(this.Find, this.Replacement)
|
|
|
|
IO.File.WriteAllText(this.File, text)
|
|
|
|
true
|
|
|
|
with e ->
|
|
|
|
this.Log.LogErrorFromException e
|
|
|
|
false
|