I submitted this back in March and received a few comments from Gert. I've
not had time (until now) to act on them. Please see attached file.

--Neil

// NAnt - A .NET build tool
// Copyright (C) 2001-2002 Gerry Shaw
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

// Neil Cowburn ([EMAIL PROTECTED])

using System;
using System.Diagnostics;
using Microsoft.Win32;
using System.Collections;
using System.IO;
using NAnt.Core;
using NAnt.Core.Tasks;
using NAnt.Core.Attributes;

namespace NAnt.Contrib.Tasks {

    /// <summary>
    /// Generates CAB files using CabWiz.exe
    /// </summary>
    /// <example>
    ///   <para>
    ///   Build CABs for ARM and MIPS devices.
    ///   </para>
    ///   <code>
    ///     <![CDATA[
    /// <cabwiz device="PocketPC" inf="MyApp.inf" dest="C:\MyApp\Cabs\" 
err="C:\MyApp\Cabs\ErrLog.txt" cpu="ARM MIPS" />
    ///     ]]>
    ///   </code>
    /// </example>
    [TaskName("cabwiz")]
    public class CabWizTask : ExternalProgramBase {

        public enum Platform {
            PocketPC2002    = 0,
            PocketPC2003    = 1,
            Smartphone2003  = 2,
        }

        private readonly string REG_PLATFORMS_KEY = @"SOFTWARE\Microsoft\Windows CE 
Tools\Platform Manager\{F384D888-F9AA-11D1-BB9E-00A0C9C9CCEE}";
        private readonly string REG_INSTALL_KEY = 
@"{F384D894-F9AA-11D1-BB9E-00A0C9C9CCEE}\{C5C00C80-30C2-11D3-99DD-00105A0DF099}";
        private readonly string REG_INSTALLDIR_VAL = "InstallDir";

        private Hashtable _sdks = null;
        private string _cabwizPath = string.Empty;
        private FileInfo _inf;
        private DirectoryInfo _dest;
        private FileInfo _err;
        private string _cpu;
        private Platform _platformType;
        private string _args = string.Empty;

        /// <summary>
        /// The path of the INF file.
        /// </summary>
        [TaskAttribute("inf",Required=true)]
        [StringValidator(AllowEmpty=false)]
        public FileInfo InfFile {
            get { return _inf; }
            set { _inf = value; }
        }

        /// <summary>
        /// Absolute destination directory for the CABs.
        /// </summary>
        [TaskAttribute("dest",Required=true)]
        [StringValidator(AllowEmpty=false)]
        public DirectoryInfo DestDir {
            get { return _dest; }
            set { _dest = value; }
        }


        /// <summary>
        /// Absolute path of the error log.
        /// </summary>
        [TaskAttribute("err",Required=true)]
        [StringValidator(AllowEmpty=false)]
        public FileInfo ErrorLogFile {
            get { return _err; }
            set { _err = value; }
        }

        /// <summary>
        /// The target device type. Valid values are "PocketPC", and "Smartphone".
        /// </summary>
        [TaskAttribute("platform",Required=true)]
        [StringValidator(AllowEmpty=false)]
        public Platform PlatformType {
            get { return _platformType; }
            set { _platformType = value; }
        }

        /// <summary>
        /// CPUs to support in the INF file. Separate multiple CPUs with spaces.
        /// </summary>
        [TaskAttribute("cpu",Required=true)]
        [StringValidator(AllowEmpty=false)]
        public string CpuSupport {
            get { return _cpu; }
            set { _cpu = value; }
        }

        /// <summary>
        /// Arguments of program to execute
        /// </summary>
        public override string ProgramArguments {
            get {
                return _args;
            }
        }

        public CabWizTask() {
            _sdks = new Hashtable();
        }

        private void EnumerateInstalledSDKs() {
            // Has the Smart Device Programmability options been installed?
            RegistryKey netcfKey = 
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\.NETCompactFramework");
            string netcfInstallDir = (string)netcfKey.GetValue("sdkInstallRoot");
            netcfKey.Close();

            _sdks[Platform.PocketPC2002] = new 
FileInfo(Path.GetFullPath(netcfInstallDir + @"v1.0.5000\bin"));

            RegistryKey platformsKey = 
Registry.LocalMachine.OpenSubKey(REG_PLATFORMS_KEY);
            if(platformsKey.SubKeyCount > 0) {
                string[] platforms = platformsKey.GetSubKeyNames();
                foreach(string platform in platforms){
                    string platformName = null;
                    string installDir = null;

                    RegistryKey platformKey = platformsKey.OpenSubKey(platform);
                    platformName = platformKey.GetValue("").ToString();
                    RegistryKey propsKey = platformKey.OpenSubKey(REG_INSTALL_KEY);
                    if(propsKey != null) {
                        byte[] stringBuffer = 
(byte[])propsKey.GetValue(REG_INSTALLDIR_VAL);
                        installDir = 
System.Text.Encoding.Unicode.GetString(stringBuffer, 0, stringBuffer.Length);
                        propsKey.Close();
                    }
                    platformKey.Close();

                    switch(platformName.ToLower()){
                        case "pocket pc 2003":
                            _sdks[Platform.PocketPC2003] = new 
FileInfo(Path.GetFullPath(installDir + "Tools\\"));
                            break;
                        case "smartphone 2003":
                            _sdks[Platform.Smartphone2003] = new 
FileInfo(Path.GetFullPath(installDir + "Tools\\"));
                            break;
                        default:
                            break;
                    }
                }
            }
            platformsKey.Close();

        }

        private void ThrowInvalidDeviceType() {
            throw new BuildException(string.Format("Device Type of {0} is invalid. 
Accepted types are: PocketPC, Smartphone",_platformType));
        }

        protected override void ExecuteTask() {
            Log(Level.Info, LogPrefix + "Building CABs...");

            EnumerateInstalledSDKs();
            switch(_platformType) {
                case Platform.PocketPC2003:
                case Platform.Smartphone2003:
                    break;
                default:
                    ThrowInvalidDeviceType();
                    break;
            }

            if(_platformType == Platform.Smartphone2003){
                Process.Start(_sdks[_platformType] + "cabwizsp.exe", 
string.Format("{0} /dest {1} /err {2} /cpu {3}",_inf,_dest,_err,_cpu));
            }
            else {
                Process.Start(_sdks[_platformType] + "cabwiz.exe", string.Format("{0} 
/dest {1} /err {2} /cpu {3}",_inf,_dest,_err,_cpu));
            }
        }
    }
}

Reply via email to