1
// NAnt - A .NET build tool
2
// Copyright (C) 2001-2003 Gerry Shaw
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// Ian Maclean (imaclean@gmail.com)
19
// Jaroslaw Kowalski (jkowalski@users.sourceforge.net)
20
// Gert Driesen (gert.driesen@ardatis.com)
23
using System.ComponentModel;
25
using System.Collections;
26
using System.Reflection;
27
using System.Globalization;
30
using NAnt.Core.Attributes;
31
using NAnt.Core.Tasks;
32
using NAnt.Core.Types;
35
namespace NAnt.Win32.Functions {
37
/// Groups a set of functions that convert Windows native filenames to
38
/// Cygwin POSIX-style pathnames and vice versa.
41
/// It can be used when a Cygwin program needs to pass a file name to a
42
/// native Windows program, or expects to get a file name from a native
45
[FunctionSet("cygpath", "Unix/Cygwin")]
46
public class CygpathFunctions : FunctionSetBase {
47
#region Public Instance Constructors
50
/// Initializes a new instance of the <see cref="CygpathFunctions" />
51
/// class with the specified <see cref="Project" /> and properties.
53
/// <param name="project">The <see cref="Project" /> in which the class is used.</param>
54
/// <param name="properties">The set of properties to use for macro expansion.</param>
55
public CygpathFunctions(Project project, PropertyDictionary properties) : base(project, properties) {
58
#endregion Public Instance Constructors
60
#region Public Instance Methods
63
/// Gets the DOS (short) form of the specified path.
65
/// <param name="path">The path to convert.</param>
67
/// The DOS (short) form of the specified path.
69
/// <exception cref="Win32Exception"><c>cygpath</c> could not be started.</exception>
70
/// <exception cref="ArgumentException"><paramref name="path" /> could not be converted to a short form.</exception>
71
[Function("get-dos-path")]
72
public string GetDosPath(string path) {
73
return RunCygpathString(new Argument[] {
74
new Argument("--dos \"" + path + "\"") });
78
/// Gets the Unix form of the specified path.
80
/// <param name="path">The path to convert.</param>
82
/// The Unix form of the specified path.
84
/// <exception cref="Win32Exception"><c>cygpath</c> could not be started.</exception>
85
/// <exception cref="ArgumentException"><paramref name="path" /> could not be converted to a Unix form.</exception>
86
[Function("get-unix-path")]
87
public string GetUnixPath(string path) {
88
return RunCygpathString(new Argument[] {
89
new Argument("--unix \"" + path + "\"") });
93
/// Gets the Windows form of the specified path.
95
/// <param name="path">The path to convert.</param>
97
/// The Windows form of the specified path.
99
/// <exception cref="Win32Exception"><c>cygpath</c> could not be started.</exception>
100
/// <exception cref="ArgumentException"><paramref name="path" /> could not be converted to a Windows form.</exception>
101
[Function("get-windows-path")]
102
public string GetWindowsPath(string path) {
103
return RunCygpathString(new Argument[] {
104
new Argument("--windows \"" + path + "\"") });
107
#endregion Public Instance Methods
109
#region Private Instance Methods
112
/// Runs cygpath with the specified arguments and returns the result
113
/// as a <see cref="string" />.
115
/// <param name="args">The arguments to pass to cygpath.</param>
117
/// The result of running cygpath with the specified arguments.
119
private string RunCygpathString(Argument[] args) {
120
MemoryStream ms = new MemoryStream();
122
ExecTask execTask = GetTask(ms);
123
execTask.Arguments.AddRange(args);
128
StreamReader sr = new StreamReader(ms);
129
string output = sr.ReadLine();
132
} catch (Exception ex) {
134
StreamReader sr = new StreamReader(ms);
135
string output = sr.ReadToEnd();
138
if (output.Length != 0) {
139
throw new BuildException(output, ex);
147
/// Factory method to return a new instance of ExecTask
149
/// <param name="stream"></param>
150
/// <returns></returns>
151
private ExecTask GetTask(Stream stream) {
152
ExecTask execTask = new ExecTask();
153
execTask.Parent = Project;
154
execTask.Project = Project;
155
execTask.FileName = "cygpath";
156
execTask.Threshold = Level.None;
157
execTask.ErrorWriter = execTask.OutputWriter = new StreamWriter(stream);
161
#endregion Private Instance Methods