Skip to content

Instantly share code, notes, and snippets.

@kos59125
Created September 1, 2012 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kos59125/3564010 to your computer and use it in GitHub Desktop.
Save kos59125/3564010 to your computer and use it in GitHub Desktop.
NUnit のプロジェクトファイルは, Windows 環境の GUI で作成すると,対象アセンブリのパスのディレクトリ区切り文字がバックスラッシュになって Linux や Mac OS の環境でうまく動かないのでスラッシュに変換する。
// Usage:
// 1. Fsi.exe NUnitAssemblyPathConvertor.fsx BarTest.nunit
// 2. Get-Content FooTest.nunit | Fsi.exe NUnitAssemblyPathConvertor.fsx
#r "System.Xml.Linq.dll"
open System
open System.IO
open System.Xml.Linq
open Microsoft.FSharp.Core.LanguagePrimitives
type Trial<'a, 'b> =
| Success of 'a
| Failure of 'b
type Error =
| None = 0
| IO = 1
| InvalidArgument = 2
| InvalidInput = 3
let exit = EnumToValue >> Environment.Exit
let doEdit edit =
function
| Success input ->
try
Success (edit input)
with
| ex -> Failure (ex, Error.InvalidInput)
| Failure (ex, error) ->
Failure (ex, error)
let getUserInput () =
match fsi.CommandLineArgs with
| [| _ |] ->
try
Success <| Console.In.ReadToEnd ()
with
| ex -> Failure (ex, Error.IO)
| [| _; path |] ->
try
Success <| File.ReadAllText (path)
with
| ex -> Failure (ex, Error.IO)
| args ->
let filename = Path.GetFileName (args.[0])
let message = sprintf "Usage: Fsi.exe %s [project.nunit]" filename
let ex = ApplicationException (message) :> exn
Failure (ex, Error.InvalidArgument)
let editProject rawProjectXml =
let project = XDocument.Parse rawProjectXml
query {
for assembly in project.Descendants (XName.Get ("assembly")) do
select (assembly.Attribute (XName.Get ("path")))
}
// UNIX だとディレクトリの区切り文字は '\' だとうまくいかないが Windows だと '/' でも OK。
|> Seq.iter (fun attribute -> attribute.Value <- attribute.Value.Replace ('\\', '/'))
project
getUserInput ()
|> doEdit editProject
|> function
| Success result ->
Console.WriteLine (result)
Error.None
| Failure (ex, error) ->
Console.Error.WriteLine (ex.Message)
error
|> exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment