Hi folks

I've written a task that wrappers the command-line utility, CabWiz.exe which
is used to generate CAB installation files for Pocket PC and Smartphone
applications. 

Please review and feedback. Be kind, it's my first task :)

Example usage: 

<cabwiz 
  device="PocketPC" 
  inf="MyApp.inf" 
  dest="C:\MyApp\Cabs\" 
  err="C:\MyApp\Cabs\ErrLog.txt" 
  cpu="ARM MIPS" 
/>

All attributes are required. Device can either be "PocketPC" or
"Smartphone".

Cheers
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 NAnt.Core;
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 : Task {

        private enum Platform {
            PocketPC    = 1,
            Smartphone  = 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 string _inf;
        private string _dest;
        private string _err;
        private string _cpu;
        private string _deviceType;
        private Platform _platform;
        
        /// <summary>
        /// The path of the INF file.
        /// </summary>
        [TaskAttribute("inf",Required=true)]
        [StringValidator(AllowEmpty=false)]
        public string InfFile {
            get { return _inf; }
            set { _inf = value; }
        }

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


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

        /// <summary>
        /// The target device type. Valid values are "PocketPC", and "Smartphone".
        /// </summary>
        [TaskAttribute("device",Required=true)]
        [StringValidator(AllowEmpty=false)]
        public string DeviceType {
            get { return _deviceType; }
            set { _deviceType = 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; }
        }

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

        private void EnumerateInstalledSDKs() {
            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.PocketPC] = installDir;
                            break;
                        case "smartphone 2003":
                            _sdks[Platform.Smartphone] = installDir;
                            break;
                        default:
                            break;
                    }
                }
            }
            platformsKey.Close();
        }

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

        protected override void ExecuteTask() {
            Log(Level.Info, LogPrefix + "Building CABs...");
            
            EnumerateInstalledSDKs();
            switch(_deviceType) {
                case "PocketPC":
                    _platform = Platform.PocketPC;
                    break;
                case "Smartphone":
                    _platform = Platform.Smartphone;
                    break;
                default:
                    ThrowInvalidDeviceType();
                    break;
            }
            
            _cabwizPath = _sdks[_platform] + @"Tools\";

            if(_platform == Platform.PocketPC) {
                Process.Start(_cabwizPath + "cabwiz.exe", string.Format("{0} /dest {1} 
/err {2} /cpu {3}",_inf,_dest,_err,_cpu));
            }
            if(_platform == Platform.Smartphone){
                Process.Start(_cabwizPath + "cabwizsp.exe", string.Format("{0} /dest 
{1} /err {2} /cpu {3}",_inf,_dest,_err,_cpu));
            }
        }
    }
}

Reply via email to