在NAnt构建期间,是否有一种方法可以提示用户输入?我想执行一个需要密码的命令,但不想将密码放入构建脚本中。
目前我正在使用一个脚本,但如果有现成的预构建方法,我很乐意听取。非常感谢sundar提供的ForegroundColor技巧。
我不确定是使用Project.Log还是直接使用Console.WriteLine()是否重要,任何NAnt高手想教育我吗?
以下是脚本和使用它的示例目标:
<target name="input">
<script language="C#" prefix="password" >
<code><![CDATA[
[Function("ask")]
public string AskPassword(string prompt) {
Project.Log(Level.Info, prompt);
ConsoleColor oldColor = Console.ForegroundColor;
Console.ForegroundColor = Console.BackgroundColor;
try
{
return Console.ReadLine();
}
finally
{
Console.ForegroundColor = oldColor;
}
}
]]></code>
</script>
<echo message="Password is ${password::ask('What is the password?')}"/>
</target>
这将在您输入密码时显示星号:
<code><![CDATA[
[Function("ask")]
public string AskPassword(string prompt) {
Project.Log(Level.Info, prompt);
string password = "";
// get the first character of the password
ConsoleKeyInfo nextKey = Console.ReadKey(true);
while (nextKey.Key != ConsoleKey.Enter)
{
if (nextKey.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
{
password = password.Substring(0, password.Length - 1);
// erase the last * as well
Console.Write(nextKey.KeyChar);
Console.Write(" ");
Console.Write(nextKey.KeyChar);
}
}
else
{
password += nextKey.KeyChar;
Console.Write("*");
}
nextKey = Console.ReadKey(true);
}
Console.WriteLine();
return password;
}
]]></code>
试试这个:
<script language="C#" prefix="test" >
<code>
<![CDATA[
[Function("get-password")]
public static string GetPassword( ) {
Console.WriteLine("Please enter the password");
ConsoleColor oldForegroundColor = Console.ForegroundColor;
Console.ForegroundColor = Console.BackgroundColor;
string password = Console.ReadLine();
Console.ForegroundColor = oldForegroundColor;
return password;
}
]]>
</code>
</script>
<target name="test.password">
<echo message='${test::get-password()}'/>
</target>
-->