~ubuntu-branches/ubuntu/feisty/nant/feisty

« back to all changes in this revision

Viewing changes to src/NAnt.Win32/Functions/CygpathFunctions.cs

  • Committer: Bazaar Package Importer
  • Author(s): Dave Beckett
  • Date: 2006-06-12 23:30:36 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060612233036-a1uwh0949z0218ep
Tags: 0.84+0.85-rc4-1
* New upstream release
* Acknowledge NMU (Closes: #372588)
* Standards-Version 3.7.2
* Update to latest CLI policy package split.  Build-Depends-Indep: on
  cli-common-dev, libmono-winforms1.0-cil, libmono-winforms2.0-cil and
  mono-gmcs to get 1.0 and 2.0 packages
* Removed patches no longer needed:
  - 01-AssemblyInfoTask.cs.patch
  - 02-ScriptTask.cs.patch
  - 03-XmlResultFormatter.cs.patch
  - 04-SourceControl.patch
  - 05-ExceptionTest.cs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// NAnt - A .NET build tool
 
2
// Copyright (C) 2001-2003 Gerry Shaw
 
3
//
 
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.
 
8
//
 
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.
 
13
//
 
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
 
17
//
 
18
// Ian Maclean (imaclean@gmail.com)
 
19
// Jaroslaw Kowalski (jkowalski@users.sourceforge.net)
 
20
// Gert Driesen (gert.driesen@ardatis.com)
 
21
 
 
22
using System;
 
23
using System.ComponentModel;
 
24
using System.IO;
 
25
using System.Collections;
 
26
using System.Reflection;
 
27
using System.Globalization;
 
28
 
 
29
using NAnt.Core;
 
30
using NAnt.Core.Attributes;
 
31
using NAnt.Core.Tasks;
 
32
using NAnt.Core.Types;
 
33
using NAnt.Core.Util;
 
34
 
 
35
namespace NAnt.Win32.Functions {
 
36
    /// <summary>
 
37
    /// Groups a set of functions that convert Windows native filenames to 
 
38
    /// Cygwin POSIX-style pathnames and vice versa.
 
39
    /// </summary>
 
40
    /// <remarks>
 
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 
 
43
    /// Windows program.
 
44
    /// </remarks>
 
45
    [FunctionSet("cygpath", "Unix/Cygwin")]
 
46
    public class CygpathFunctions : FunctionSetBase {
 
47
        #region Public Instance Constructors
 
48
 
 
49
        /// <summary>
 
50
        /// Initializes a new instance of the <see cref="CygpathFunctions" />
 
51
        /// class with the specified <see cref="Project" /> and properties.
 
52
        /// </summary>
 
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) {
 
56
        }
 
57
 
 
58
        #endregion Public Instance Constructors
 
59
 
 
60
        #region Public Instance Methods
 
61
 
 
62
        /// <summary>
 
63
        /// Gets the DOS (short) form of the specified path.
 
64
        /// </summary>
 
65
        /// <param name="path">The path to convert.</param>
 
66
        /// <returns>
 
67
        /// The DOS (short) form of the specified path.
 
68
        /// </returns>
 
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 + "\"") });
 
75
        }
 
76
 
 
77
        /// <summary>
 
78
        /// Gets the Unix form of the specified path.
 
79
        /// </summary>
 
80
        /// <param name="path">The path to convert.</param>
 
81
        /// <returns>
 
82
        /// The Unix form of the specified path.
 
83
        /// </returns>
 
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 + "\"") });
 
90
        }
 
91
 
 
92
        /// <summary>
 
93
        /// Gets the Windows form of the specified path.
 
94
        /// </summary>
 
95
        /// <param name="path">The path to convert.</param>
 
96
        /// <returns>
 
97
        /// The Windows form of the specified path.
 
98
        /// </returns>
 
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 + "\"") });
 
105
        }
 
106
 
 
107
        #endregion Public Instance Methods
 
108
 
 
109
        #region Private Instance Methods
 
110
        
 
111
        /// <summary>
 
112
        /// Runs cygpath with the specified arguments and returns the result 
 
113
        /// as a <see cref="string" />.
 
114
        /// </summary>
 
115
        /// <param name="args">The arguments to pass to cygpath.</param>
 
116
        /// <returns>
 
117
        /// The result of running cygpath with the specified arguments.
 
118
        /// </returns>
 
119
        private string RunCygpathString(Argument[] args) {
 
120
            MemoryStream ms = new MemoryStream();
 
121
 
 
122
            ExecTask execTask = GetTask(ms);
 
123
            execTask.Arguments.AddRange(args);
 
124
 
 
125
            try {
 
126
                execTask.Execute();
 
127
                ms.Position = 0;
 
128
                StreamReader sr = new StreamReader(ms);
 
129
                string output = sr.ReadLine();
 
130
                sr.Close();
 
131
                return output;
 
132
            } catch (Exception ex) {
 
133
                ms.Position = 0;
 
134
                StreamReader sr = new StreamReader(ms);
 
135
                string output = sr.ReadToEnd();
 
136
                sr.Close();
 
137
 
 
138
                if (output.Length != 0) {
 
139
                    throw new BuildException(output, ex);
 
140
                } else {
 
141
                    throw;
 
142
                }
 
143
            }
 
144
        }
 
145
        
 
146
        /// <summary>
 
147
        /// Factory method to return a new instance of ExecTask
 
148
        /// </summary>
 
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);
 
158
            return execTask;
 
159
        }
 
160
 
 
161
        #endregion Private Instance Methods
 
162
    }
 
163
}