4

特定のディレクトリにある最新のフォルダの名前を取得するために、カスタム タスクを記述せずに nant に比較的簡単な方法はありますか? 再帰は必要ありません。私は directory::get-creation-time と foreach ループと if ステートメント、yada yada でそれをやろうとしています。複雑すぎるので、代わりにカスタム タスクを作成しようとしています。ただし、既存の nant 機能を使用して簡単に実行できる方法があると思います。

4

1 に答える 1

6

これを純粋なナント方式で行うと、特にプロパティがナントで機能する方法が面倒になる可能性があると述べたのは正しいと思います。カスタム タスクを作成したくない場合は、いつでもスクリプト タスクを使用できます。例えば:

<?xml version="1.0"?>
<project name="testing" basedir=".">

    <script language="C#" prefix="test" >
        <code>
            <![CDATA[
            [Function("find-newest-dir")]
            public static string FindNewestDir( string startDir ) {
                string theNewestDir = string.Empty;
                DateTime theCreateTime = new DateTime();
                DateTime theLastCreateTime = new DateTime();
                string[] theDirs = Directory.GetDirectories( startDir );
                for ( int theCurrentIdx = 0; theCurrentIdx < theDirs.Length; ++theCurrentIdx )
                {
                    if ( theCurrentIdx != 0 )
                    {
                        DateTime theCurrentDirCreateTime = Directory.GetCreationTime( theDirs[ theCurrentIdx ] );
                        if ( theCurrentDirCreateTime >= theCreateTime )
                        {
                            theNewestDir = theDirs[ theCurrentIdx ];
                            theCreateTime = theCurrentDirCreateTime;
                        }
                    }
                    else
                    {
                        theNewestDir = theDirs[ theCurrentIdx ];
                        theCreateTime = Directory.GetCreationTime( theDirs[ theCurrentIdx ] );
                    }
                }
                return theNewestDir;
            }
            ]]>
        </code>
    </script>

    <property name="dir" value="" overwrite="false"/>
    <echo message="The newest directory is: ${test::find-newest-dir( dir )}"/>

</project>

これにより、関数を呼び出して最新のディレクトリを取得できるはずです。実際の関数の実装は何にでも変更できます (もう少し最適化するなど) が、スクリプト タスクの使用方法の参照用に簡単なものを含めました。次のような出力が生成されます。

ナント -D:dir=c:\

NAnt 0.85 (ビルド 0.85.2478.0; リリース; 2006 年 10 月 14 日)
Copyright (C) 2001-2006 ゲリー・ショー
http://nant.sourceforge.net

ビルドファイル: file:///C:/tmp/NAnt.build
対象フレームワーク: Microsoft .NET Framework 2.0

   [スクリプト] アセンブリ「jdrgmbuy」をスキャンして拡張機能を探します。
     [echo] 最新のディレクトリは次のとおりです: C:\tmp

ビルド成功

合計時間: 0.3 秒。
于 2008-10-30T18:06:21.427 に答える