Arild さんのプロフィールRandom thoughts about An...ブログリスト ツール ヘルプ

ブログ


    7月3日

    Powershell script blocks as .NET event handlers

    Scott Hanselman posts an interesting script where he uses the NSvn library (the .NET wrapper library for Subversion we wrote for AnkhSVN) directly from Powershell to either checkout from a Subversion repository or update an existing working copy. This is a great way to use NSvn and one we definitely didn’t have in mind when writing it.

    He also asks: “How do you do a delegate from within PowerShell if a .NET Assembly needs a delegate with a certain signature? This library will call me back with status updates I'd like to broker to write-progress.”

    NSvn’s NSvn.Core.Client class has an event called Notification, which is raised for every notification callback we get from the underlying Subversion C library. Subversion calls back for various reasons, including whenever a file has been downloaded as part of an update operation (in Subversion, a checkout is really just a special case of an update where the working copy has none of the files). In the Notification event, the NotificationEventArgs object’s Path property contains the path to the file or directory that has been updated, while the .Action property indicates what kind of operation has been performed against the path. will usually contain NotifyAction.UpdateUpdate or NotifyAction.UpdateAdd (in the case of a checkout, all callbacks will be of the NotifyAction.UpdateAdd type). See here for a listing of the various action types.

    Now, the question is, how does one consume this event from a Powershell script? Powershell supports anonymous functions called script blocks. These are roughly equivalent to C#’s anonymous delegates. Script blocks can be stored in a variable and invoked at any given time using the & operator:

    PS D:\tmp> $helloworld = { write-host "Hello world" }

    PS D:\tmp> & $helloworld

    Hello world

    It turns out that a script block can be cast to any arbitrary delegate and used as an event handler:

    PS D:\tmp> $handler = [System.EventHandler]{ write-host "Hello world" }

    PS D:\tmp> $handler.GetType().FullName

    System.EventHandler

    PS D:\tmp> $handler.Invoke($null, [System.EventArgs]::Empty)

    Hello world

     

    In our case, we want a delegate of type NotificationDelegate (ok, not a good name for an event handler delegate, or so FXCop tells me, but anyway…), which has the following signature:

    public delegate void NotificationDelegate( Object sender, NotificationEventArgs args );

    The one thing that’s not obvious about scriptblocks as event handlers is how you get hold of the eventargs instance. The documentation does not go out of its way to tell you about it either, so I started experimenting. My first attempt looked like this:

    PS D:\tmp> $notificationcallback = [NSvn.Core.NotificationDelegate]{ param($sender, $e) write-host $e.Path }

    The param syntax is one of the ways you declare parameters to a regular function or script. If it worked as expected, it would have printed out the path for each item being updated or checked out. Of course, “as expected” being the operative phrase here. $e turned out to be null.

    Another way in Powershell to get hold of arguments to a function is through the special $args array. Unfortunately, this didn’t seem to work either:

    PS D:\tmp> $event = [System.EventHandler]{ write-host "Args: $args" }

    PS D:\tmp> $event.Invoke($null, [EventArgs]::Empty)

    Args:

    PS D:\tmp>

    Another Reading of The Fine Manual (why is this thing in a Word document, anyway?) finally turned up an example, in which they use the $_ special variable to get hold of the EventArgs instance passed as a second parameter to all (most?) .NET events. This seems like a weird choice to me, $_ is generally used to hold the “current” object in a pipeline. Why did they overload it like this? To make things even worse, the sender argument is available in another special variable called $this. Why “$this”? It’s not like the usage in any way resembles the way the this keyword is used in other languages.

    Anyway, armed with this hard-won knowledge, rewriting Scott’s script to “broker” the notification events to Write-Progress proves to be rather simple. This is the revised script:

        
      param ([string]$svnurl       = $(read-host "Please specify the path to SVN"),
            [string]$svnlocalpath = $(read-host "Please specify the local path") 
            )
    
        if ([System.IO.Path]::IsPathRooted($svnlocalpath) -eq $false)
        {
            throw "Please specific a local absolute path"
        }
        
        [System.Reflection.Assembly]::LoadFrom((join-Path "$env:PROGRAMFILES\AnkhSVN" -childPath NSvn.Common.dll))
        [System.Reflection.Assembly]::LoadFrom((join-Path "$env:PROGRAMFILES\AnkhSVN" -childPath NSvn.Core.dll))
        
        
        
        $PRIVATE:svnclient = new-object NSvn.Core.Client
        
        $PRIVATE:notificationcallback = [NSvn.Core.NotificationDelegate]{
            Write-Progress -status ("{0}: {1}" -f ($_.Action, $_.Path)) -activity "Updating Working Copy"
        }
        
        $PRIVATE:svnclient.add_Notification($notificationcallback)    
            
        $PRIVATE:svnclient.AuthBaton.Add( [NSvn.Core.AuthenticationProvider]::GetWindowsSimpleProvider() )
        if ((test-Path $svnlocalpath) -eq $true )
        {
            write-progress -status "Updating from $svnurl" -activity "Updating Working Copy"
            $PRIVATE:svnclient.Update($svnlocalpath, [NSvn.Core.Revision]::Head, $true)
        }
        else
        {
            write-progress -status "Checkout from $svnurl" -activity "Updating Working Copy"
            $PRIVATE:svnclient.Checkout($svnurl, $svnlocalpath, [NSvn.Core.Revision]::Head, $true)
        }
    

    コメント (8 件)

    しばらくお待ちください。
    入力されたコメントは長すぎます。短くしてください。
    何も入力されていません。もう一度やり直してください。
    現在、コメントを追加できません。後でもう一度やり直してください。
    コメントと書くには、保護者 (ほごしゃ) の方の許可 (きょか) をもらってください。許可をリクエストする
    保護者 (ほごしゃ) の方が、あなたがコメントを書けないようにしています。
    現在、コメントを削除できません。後でもう一度やり直してください。
    1 日に投稿できるコメントの最大数を超えました。24 時間経過してから、もう一度やり直してください。
    あなたが他のユーザーに対して迷惑行為を行っている可能性があると確認されたため、お使いのアカウントによるコメントの投稿を無効にしています。誤って無効にされたと思われる場合は、Windows Live のサポートにお問い合わせください。
    コメントを投稿する前に、以下のセキュリティ チェックを完了してください。
    セキュリティ チェックに入力する文字は、画像に表示されている文字または音声で流れた文字と一致していなければいけません。

    コメントを投稿するには、お使いの Windows Live ID でサインインしてください (Hotmail、Messenger、または Xbox LIVE を既に使用している場合は、そのアカウントが Windows Live ID です)。サインイン


    Windows Live ID をお持ちでない場合は、アカウントを新規登録してください。

    名前なしさんの投稿:
    http://www.batteryfast.com/dell/inspiron-9300.htm dell inspiron 9300 battery
    http://www.batteryfast.com/hp/dv8000.htm hp dv8000 battery
    http://www.batteryfast.com/hp/dv1000.htm hp dv1000 battery
    http://www.batteryfast.com/hp/dv4000.htm hp dv4000 battery
    http://www.batteryfast.com/hp/r3000.htm hp r3000 battery
    http://www.batteryfast.com/dell/inspiron-mini-9.htm dell inspiron mini 9 battery
    http://www.batteryfast.com/dell/inspiron-9400.htm dell inspiron 9400 battery
    http://www.batteryfast.com/acer/aspire-5520.htm acer aspire 5520 battery
    http://www.batteryfast.com/acer/aspire-5920.htm acer aspire 5920 battery
    http://www.batteryfast.com/dell/inspiron-6400.htm dell inspiron 6400 battery
    http://www.batteryfast.com/ibm/thinkpad-x60.htm ibm thinkpad x60 battery
    http://www.batteryfast.com/ibm/thinkpad-x60s.htm ibm thinkpad x60s battery
    http://www.batteryfast.com/hp/zt3000.htm hp zt3000 battery
    http://www.batteryfast.com/hp/pavilion-zv5000.htm hp pavilion zv5000 battery
    http://www.batteryfast.com/hp/pavilion-zv6000.htm hp pavilion zv6000 battery
    http://www.batteryfast.com/hp/pavilion-zd8000.htm hp pavilion zd8000 battery
    http://www.batteryfast.com/hp/pavilion-zx5000.htm hp pavilion zx5000 battery
    http://www.batteryfast.com/hp/presario-v4000.htm hp presario v4000 battery
    http://www.batteryfast.com/hp/presario-v2000.htm hp presario v2000 battery
    http://www.batteryfast.com/hp/presario-m2000.htm hp presario m2000 battery
    http://www.batteryfast.com/compaq/presario-v4000.htm compaq presario v4000 battery
    http://www.batteryfast.com/compaq/presario-v2000.htm compaq presario v2000 battery
    http://www.batteryfast.com/compaq/presario-v3000.htm compaq presario v3000 battery
    http://www.batteryfast.com/compaq/presario-v6000.htm compaq presario v6000 battery
    http://www.batteryfast.com/compaq/presario-c700.htm compaq presario c700 battery
    http://www.batteryfast.com/dell/fk890.htm dell fk890 battery
    http://www.batteryfast.com/toshiba/pa3593u-1bas.htm toshiba pa3593u-1bas battery
    http://www.batteryfast.com/hp/m2000.htm hp m2000 battery
    http://www.batteryfast.com/hp/338794-001.htm hp 338794-001 battery
    http://www.batteryfast.com/hp/395789-001.htm hp 395789-001 battery
    http://www.batteryfast.com/compaq/116314-001.htm compaq 116314-001 battery
    http://www.batteryfast.com/toshiba/satellite-a70.htm toshiba satellite a70 battery
    http://www.batteryfast.com/compaq/319411-001.htm compaq 319411-001 battery
    http://www.batteryfast.com/hp/346970-001.htm hp 346970-001 battery
    http://www.batteryfast.com/hp/367759-001.htm hp 367759-001 battery
    http://www.batteryfast.com/hp/411462-421.htm hp 411462-421 battery
    http://www.batteryfast.com/hp/ev088aa.htm hp ev088aa battery
    http://www.batteryfast.com/hp/ex941aa.htm hp ex941aa battery
    http://www.batteryfast.com/laptop battery
    http://www.batteryfast.com/laptop batteries
    http://www.batteryfast.com/dell laptop batteries/
    http://www.batteryfast.com/dell/wr050.htm dell wr050 battery
    http://www.batteryfast.com/hp laptop batteries/
    http://www.batteryfast.com/laptop ac adapter/
    11 月 11 日
    名前なしさんの投稿:
    http://www.batteryfast.com/gateway/m360.htm gateway m360 battery
    http://www.batteryfast.com/gateway/m460.htm gateway m460 battery
    http://www.batteryfast.com/hp/nc8430.htm hp nc8430 battery
    http://www.batteryfast.com/hp/dv6000.htm hp dv6000 battery
    http://www.batteryfast.com/hp/hstnn-db42.htm hp hstnn-db42 battery
    http://www.batteryfast.com/hp/dv9000.htm hp dv9000 battery
    http://www.batteryfast.com/hp/dv9700.htm hp dv9700 battery
    http://www.batteryfast.com/hp/hstnn-ib44.htm hp hstnn-ib44 battery
    http://www.batteryfast.com/hp/510.htm hp 510 battery
    http://www.batteryfast.com/hp/530.htm hp 530 battery
    http://www.batteryfast.com/sony/vgp-bps2a.htm sony vgp-bps2a battery
    http://www.batteryfast.com/sony/vgp-bps2b.htm sony vgp-bps2b battery
    http://www.batteryfast.com/sony/vgp-bps2.htm sony vgp-bps2 battery
    http://www.batteryfast.com/sony/vgp-bps2c.htm sony vgp-bps2c battery
    http://www.batteryfast.com/toshiba/pa3399u-1brs.htm toshiba pa3399u-1brs battery
    http://www.batteryfast.com/toshiba/satellite-a100.htm toshiba satellite a100 battery
    http://www.batteryfast.com/toshiba/pa3536u.htm toshiba pa3536u battery
    http://www.batteryfast.com/dell/inspiron-1300.htm dell inspiron 1300 battery
    http://www.batteryfast.com/dell/xps-1330.htm dell xps 1330 battery
    http://www.batteryfast.com/dell/xps-m1330.htm dell xps m1330 battery
    http://www.batteryfast.com/dell/inspiron-6000.htm dell inspiron 6000 battery
    http://www.batteryfast.com/dell/inspiron-9300.htm dell inspiron 9300 battery
    http://www.batteryfast.com/dell/e1705.htm dell e1705 battery
    http://www.batteryfast.com/hp/nc8230.htm hp nc8230 battery
    http://www.batteryfast.com/hp/dv9500.htm hp dv9500 battery
    http://www.batteryfast.com/hp/dv2000.htm hp dv2000 battery
    http://www.batteryfast.com/dell/latitude-d620.htm dell latitude d620 battery
    http://www.batteryfast.com/hp/dv6000.htm hp dv6000 battery
    http://www.batteryfast.com/hp/dv2700.htm hp dv2700 battery
    http://www.batteryfast.com/dell/inspiron-b130.htm dell inspiron b130 battery
    http://www.batteryfast.com/toshiba/pa3534u-1brs.htm toshiba pa3534u-1brs battery
    http://www.batteryfast.com/dell/inspiron-1525.htm dell inspiron 1525 battery
    http://www.batteryfast.com/dell/d9200.php Notebook Laptop Battery for Dell D9200 D5318 G5260 laptop battery ,
    http://www.batteryfast.com/acer/tm4200.php Battery ACER BATBL50L6 Travelmate 4200 4203 4230 4260 4280 laptop battery ,
    http://www.batteryfast.com/dell/inspiron-1200.htm dell inspiron 1200 battery
    http://www.batteryfast.com/toshiba/pa3534u-1brs.htm toshiba pa3534u-1brs battery
    http://www.batteryfast.com/toshiba/pa3399u-1bas.htm toshiba pa3399u-1bas battery
    http://www.batteryfast.com/toshiba/pa3399u-2bas.htm toshiba pa3399u-2bas battery
    http://www.batteryfast.com/gateway/m320.htm gateway m320 battery
    http://www.batteryfast.com/toshiba/satellite-a105.htm toshiba satellite a105 battery
    http://www.batteryfast.com/compaq/nc4400.htm compaq nc4400 battery
    http://www.batteryfast.com/compaq/tc4200.htm compaq tc4200 battery
    http://www.batteryfast.com/compaq/nc4200.htm compaq nc4200 battery
    http://www.batteryfast.com/dell/e1405.htm dell e1405 battery
    http://www.batteryfast.com/hp/hstnn-db02.htm hp hstnn-db02 battery
    11 月 5 日
    名前なしさんの投稿:
    Welcome to wow gold our wow Gold and wow power leveling store. We wow gold are specilized, wow power leveling professional and reliable wow power leveling website for wow power leveling selling and wow gold service. By the World of Warcraft gold same token,we offer wow power leveling the best WoW service wow power leveling for our long-term and wow powerleveling loyal customers. wow powerleveling You will find wow powerleveling the benefits and value powerleveling we created powerleveling different from other sites. As to most people, power leveling they are unwilling to power leveling spend most of wow power leveling the time wow gold grinding money Rolex for mounts or rolex replica repair when replica rolex they can purchase Watches Rolex what they Rolex Watches are badly need. The Watch Rolex only way is to look Rolex Watch for the best place rs gold to buy cheap WOW gold. Yes! You find it here! Our WoW Gold supplying service has already accumulated a high reputation and credibility. We have plenty of Gold suppliers, which will guarantee our delivery instant. Actually, we have been getting Runescape Gold tons of postive feedbacks from our loyal RuneScape Money customers who really appreciate our service.
    11 月 14 日
    名前なしさんの投稿:
    HTML clipboard在現今社會中情趣用品已經是人們生活中不可或缺的一部份,情趣是你我都需要的東西,如果沒有情趣用品夫妻床趣的情趣真的會減少很多,當今的社會情趣已經不像是以前不敢表達,情趣用品現在要購買也更加容易了,網路上有各種情趣精品,各國情趣用品,男歡女愛情趣,甚至無聊時可以去AIO交友愛情館裡面有許多正妹,情人歡愉用品最新的潮流,最正的妹美女視訊,最騷的情色交友,最夯的情人用品性哥,免費的視訊交友,體貼的辣妹視訊,孤寂時美女交友,讓你享受性愛,找圖片時嘟嘟成人網是你最好的選擇,各種按摩棒,震動按摩棒,微調按摩棒,情趣按摩棒,逼真按摩棒都是夫妻情趣生活中不可或缺的,G點一直是眾人在探討的,跳蛋他能幫助你達到高潮,各種跳蛋,無線跳蛋,性感小惡魔性感內衣,超逼真的飛機杯,真人版充氣娃娃,情趣娃娃,無聊時幻想的角色扮演,舒服舒適的性感睡衣,刺激的後庭區,狂亂的SM,順利進行的潤滑液,七夕快到了你選好情趣禮物了嗎,各種威而柔,香水,精油,芳香精油,我想心都是你送禮的最好選擇,寂寞夜晚時自慰,可以使用自慰套,來讓你達到高潮性感吊帶襪滿足男生的幻想,情趣用品加盟讓你可以賺錢不落人後,有空可以去情人歡愉用品,或者是跳蛋情人娜娜看看,常常更新的情趣用品讓你一看在看,情人節禮物決定好了嗎,情人節就快到了,或者吊帶襪也是一個不錯的選擇,有空去辣妹視訊看看,保證美女交友,讓你打發一天的時間情色交友,或是成人交友,免費的視訊聊天室,都有很多美女視訊,不看視訊美女你一定會睡不著,在多情色視訊也滿足不了你,免費視訊聊天讓你不花錢也可以享受道,純情的視訊交友,無聊時去視訊聊天,看看年輕辣妹的AIO交友愛情館,嘟嘟成人網裡面有各種圖片影片讓你看完精疲力進,走吧一起去成人貼圖最近很流行的自拍裡面都有,AIO交友愛情館最近很少上去,線在迷上情色,因為始終這才是最真實的情色貼圖,有時看看情色文學也很不錯,裡面很多別人的情色交友經驗,或去色情聊天室裡面,看色情小說都是不錯的選擇,七夕情人節祝福大家,色情是男人都會,多去看看情色視訊,在去看情色電影之後去看,色情網站就這樣一天就過去了,雖然辣妹視訊很好看,但是視訊聊天室更好玩,太多情色視訊,或者是免費視訊聊天都是很好的地方,每天去看視訊聊天,美女視訊,讓你大飽眼福視訊美女,射到無力的美女交友,在多美女也不會比,情色交友更吸引我,哪天趣成人交友,看別人自拍,計得找我本土自拍過去的讓人很懷念....
    9 月 30 日
    9 月 8 日
    匿名 の表示アイコン
    Jeffrey Snover さんの投稿:
    [Experimenting with a folkonomy: use of the tage PSFAQ to improve the signal-to-noise ratio of searches]
     
    PSFAQ: How can you use Windows PowerShell to provide a delegate with a specific signature?
     
    See Above.
     
    Thanks for the data!
     
    Jeffrey Snover
    Windows PowerShell Architect
    Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
    7 月 4 日
    匿名 の表示アイコン
    Arild Fines さんの投稿:
    Best guess, libdb4*.dll. Running Dependency Walker on NSvn.Core.dll is usually the best way to figure out missing dependencies (it's a mixed-mode managed c++ DLL, so it has a bunch of dependencies on unmanaged DLLs). 0x8007007e is "module not found" or something like that, IIRC.
    7 月 4 日
    匿名 の表示アイコン
    Scott Hanselman さんの投稿:
    Great stuff! I really think this opens up an opportunity for the NSvn.Core library.

    Question - on a FRESH Windows Machine with only Tortoise installed, when we copy in NSvn.* we get a COM error 0x8007007e while using the NSvn and are unable to continue. It works on a half the devs machines we try and on a bunch it doesn't. This has prevented me from using my new library on non-dev machines.

    What dependancy am I missing?
    7 月 3 日

    トラックバック (1 件)

    この記事のトラックバックの URL は次のとおりです。
    http://arildf.spaces.live.com/blog/cns!E99F8B43533149B0!127.trak
    この記事を参照しているブログ