65.9K
CodeProject 正在变化。 阅读更多。
Home

DNN 模块打包器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.61/5 (11投票s)

2006年11月22日

CPOL

2分钟阅读

viewsIcon

65909

downloadIcon

1027

一个用于创建 DotNetNuke 清单文件并打包模块的工具。

Sample Image - screen1.jpg

引言

DNN 模块打包器是一个用于创建 DotNetNuke 清单文件并将模块打包成 ZIP 文件,以便部署到 DotNetNuke 3.x 或 DotNetNuke 4.x 托管网站的工具。您可以在打包之前编辑清单文件,并添加模块在 DotNetNuke 环境中正常运行所需的任何其他文件。DNN 清单文件只是一个包含有关模块的信息的 XML 文件,包括其 DNN 接口控件(如查看、编辑和设置控件)以及模块所需的所有文件夹、文件和二进制文件的列表。

使用 DNN 模块打包器

DNN 模块打包器需要 Visual Studio 中 DotNetNuke 模块模板自动创建的基本 .DNN 清单文件。如果您没有 .DNN 清单文件,可以随时从 DotNetNuke 安装中复制一个并将其修改为自己的模块。

打包 DNN 模块的基本步骤是:

  1. 选择 .DNN 清单文件。
  2. 选择模块目录的根目录。
  3. 选择目标 Zip 输出目录和文件名。
  4. 生成清单(并在需要时对清单进行编辑)。
  5. 添加包中所需的任何其他 DLL、代码文件或其他文件。
  6. 生成包。

另一个有用的功能是能够自动排除模块目录及其子目录中具有特定扩展名的任何文件,这些文件您不希望包含在清单或包中(例如 VSS 和其他源代码管理文件)。

以下是打包过程以及 DNN 模块打包器中各种屏幕/选项卡的快速概述

包含附加文件的示例 DNN 模块包

Template

示例 DNN 模块包清单

Generated

DNN 模块打包结果

Generated

DNN 模块打包器的工作原理

DNN 模块打包器的主要引擎/处理器是 DnnGenerator 类。其主要功能是枚举模块根目录(及其子目录)下的所有文件,并将它们作为文件夹/文件 XML 节点添加到 DNN 清单中。DnnGenerator 类还维护着包中所有文件的列表,并负责组装最终产品:压缩的 DNN 模块包。

class DnnGenerator
{
    /// <summary>
    /// Generates a .DNN manifest file for a DNN Module project.
    /// Primarily, this is used to enumerate all the 
    /// filenames and paths in the project
    /// and put them in the .dnn manifest file properly

    /// </summary>
    /// <param name="sOldDnnFilePath">The original module DNN file</param>
    /// <param name="sProjectPath">Path to the DNN Module/files</param>
    /// <param name="aryOtherFiles">Array of binary files
    /// to include with the project</param>

    /// <returns></returns>

    public static string GenerateProjectDnnConfig(string sOldDnnFilePath, 
           string sProjectPath, string sExcludeExts, ArrayList aryOtherFiles)
    {
        string sNewDnnFile = string.Empty;

        if (File.Exists(sOldDnnFilePath) == false)
        {
            throw new Exception("Specified .DNN manifest file does not exist!");           
        }

        if (Directory.Exists(sProjectPath) == false)
        {
            throw new Exception("Specified DNN Module project path does not exist!");
        }

        // reads in default .dnn file

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(sOldDnnFilePath);

        // removes all the <files> elems

        XmlNode node = xmlDoc.SelectSingleNode("/dotnetnuke/folders/folder/files");
        node.RemoveAll();

        // get a list of all the files under the project path

        ArrayList aryFiles = FileUtilities.GetFilesInFolder(sProjectPath, true);

        // get the list of extensions to include

        string[] _ExcludeExts = sExcludeExts.Split(',');
        ArrayList aryExcludeExts = new ArrayList();
        foreach (string sExt in _ExcludeExts)
        {
            aryExcludeExts.Add(sExt.Trim().ToLower());
        }
        
        // regenerates the <files> elems with current file/relpath info

        foreach (string sfile in aryFiles)
        {
            FileInfo fi = new FileInfo(sfile);
            if (aryExcludeExts.Contains(fi.Extension.Trim().ToLower()) == true)
            {
                continue;
            }

            XmlElement xnfile = xmlDoc.CreateElement("file");
            node.AppendChild(xnfile);

            string srelpath = FileUtilities.RelativePathTo(sProjectPath, 
                                                           fi.DirectoryName);
            if (srelpath.Length > 0)
            {
                // relative path to project with no preceding slashes            

                XmlElement xnpath = xmlDoc.CreateElement("path");
                xnfile.AppendChild(xnpath);
                xnpath.InnerText = srelpath;

                // TEST:  need to check multiple Sub dirs with DNN

                // may need to change '\' to a '/' in path

            }                
            
            XmlElement xnname = xmlDoc.CreateElement("name");
            xnfile.AppendChild(xnname);
            xnname.InnerText = fi.Name;
        }

        // regenerates the <files> elems for any added assemblies 

        foreach (string sotherfile in aryOtherFiles)
        {
            FileInfo fi = new FileInfo(sotherfile);

            XmlElement xnfile = xmlDoc.CreateElement("file");
            node.AppendChild(xnfile);
            
            if (fi.FullName.ToUpper().IndexOf("\\APP_CODE\\") >= 0)
            {
                // relative path to project with no preceding slashes            
                XmlElement xnpath = xmlDoc.CreateElement("path");
                xnfile.AppendChild(xnpath);
                xnpath.InnerText = "[app_code]"; 
            }  

            XmlElement xnname = xmlDoc.CreateElement("name");
            xnfile.AppendChild(xnname);
            xnname.InnerText = fi.Name;
        }        
        
        return xmlDoc.InnerXml;
    }

    /// <summary>
    /// Backs-up the old DNN manifest and writes the specified contenst to a new DNN file
    /// </summary>
    /// <param name="sOldDnnFilePath"></param>
    /// <param name="sNewContents"></param>
    public static void SaveNewDnnConfigFile(string sOldDnnFilePath, string sNewContents)
    { 
        string sNewDnnFilePath = Path.GetTempFileName();
        FileUtilities.WriteFileContents(sNewDnnFilePath, sNewContents, true);

        // copy the existing dnn file to a backup file
        //if(File.Exists(sOldDnnFilePath + ".backup") == false)
        //{
          //  File.Copy(sOldDnnFilePath, sOldDnnFilePath + ".backup");
        //}

        File.Delete(sOldDnnFilePath);
        File.Move(sNewDnnFilePath, sOldDnnFilePath);            
    }

    /// <summary>
    /// Creates a DNN module package from the project path, other files, 
    /// and then stores it as a ZIP file
    /// </summary>
    /// <param name="sZipDestFile"></param>
    /// <param name="sProjectPath"></param>
    /// <param name="aryOtherFiles"></param>
    public static void GenerateDnnModulePackage(string sZipDestFile, 
           string sProjectPath, string sExcludeExts, ArrayList aryOtherFiles)
    { 
         // get a list of all the files under the project path
        ArrayList aryFiles = FileUtilities.GetFilesInFolder(sProjectPath, true);

        // add a list of other files to be included in bin dir (like dlls)
        aryFiles.AddRange(aryOtherFiles);

        // remove any files that have excluded extensions
        aryFiles = DnnGenerator.RemoveExcludedFiles(sExcludeExts, aryFiles);

        // zip everything up to where you save it to
        FileUtilities.ZipFiles(sZipDestFile, null, aryFiles);
    }

    /// <summary>
    /// Returns an arraylist of the files that have non-excluded extensions
    /// </summary>
    /// <param name="sExcludeExts"></param>
    /// <param name="aryOtherFiles"></param>
    /// <returns></returns>
    private static ArrayList RemoveExcludedFiles(string sExcludeExts, 
                                                 ArrayList aryOtherFiles)
    {
        ArrayList aryFiles = new ArrayList();
        ArrayList aryExcludeExts = new ArrayList();

        // get the list of extensions to include
        string[] _ExcludeExts = sExcludeExts.Split(',');
        foreach (string sExt in _ExcludeExts)
        {
            aryExcludeExts.Add(sExt.Trim().ToLower());
        }
        
        // only transfer the files with non-excluded extensions
        foreach (string sfile in aryOtherFiles)
        {
            FileInfo fi = new FileInfo(sfile);
            if (aryExcludeExts.Contains(fi.Extension.Trim().ToLower()) == true)
            {
                continue;
            }

            aryFiles.Add(sfile);
        }

        return aryFiles;
    }
}

结论

我希望您觉得这篇文章和工具有用 - 我发现它使打包和部署 DNN 模块变得轻而易举!

© . All rights reserved.