コード例 #1
0
        public void InstallFiles(int packageId, string tempDir)
        {
            using (DisposableTimer.DebugDuration <Installer>(
                       () => "Installing package files for package id " + packageId + " into temp folder " + tempDir,
                       () => "Package file installation complete for package id " + packageId))
            {
                //retrieve the manifest to continue installation
                var insPack = InstalledPackage.GetById(packageId);

                //TODO: Depending on some files, some files should be installed differently.
                //i.e. if stylsheets should probably be installed via business logic, media items should probably use the media IFileSystem!

                // Move files
                //string virtualBasePath = System.Web.HttpContext.Current.Request.ApplicationPath;
                string basePath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

                foreach (XmlNode n in Config.DocumentElement.SelectNodes("//file"))
                {
                    //we enclose the whole file-moving to ensure that the entire installer doesn't crash
                    try
                    {
                        var destPath   = GetFileName(basePath, XmlHelper.GetNodeValue(n.SelectSingleNode("orgPath")));
                        var sourceFile = GetFileName(tempDir, XmlHelper.GetNodeValue(n.SelectSingleNode("guid")));
                        var destFile   = GetFileName(destPath, XmlHelper.GetNodeValue(n.SelectSingleNode("orgName")));

                        // Create the destination directory if it doesn't exist
                        if (Directory.Exists(destPath) == false)
                        {
                            Directory.CreateDirectory(destPath);
                        }
                        //If a file with this name exists, delete it
                        else if (File.Exists(destFile))
                        {
                            File.Delete(destFile);
                        }

                        // Move the file
                        File.Move(sourceFile, destFile);

                        //PPH log file install
                        insPack.Data.Files.Add(XmlHelper.GetNodeValue(n.SelectSingleNode("orgPath")) + "/" + XmlHelper.GetNodeValue(n.SelectSingleNode("orgName")));
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error <Installer>("Package install error", ex);
                    }
                }

                // log that a user has install files
                if (_currentUserId > -1)
                {
                    Audit.Add(AuditTypes.PackagerInstall,
                              string.Format("Package '{0}' installed. Package guid: {1}", insPack.Data.Name, insPack.Data.PackageGuid),
                              _currentUserId, -1);
                }

                insPack.Save();
            }
        }
コード例 #2
0
        public void InstallBusinessLogic(int packageId, string tempDir)
        {
            using (DisposableTimer.DebugDuration <Installer>(
                       () => "Installing business logic for package id " + packageId + " into temp folder " + tempDir,
                       () => "Package business logic installation complete for package id " + packageId))
            {
                InstalledPackage insPack;
                try
                {
                    //retrieve the manifest to continue installation
                    insPack = InstalledPackage.GetById(packageId);
                    //bool saveNeeded = false;

                    // Get current user, with a fallback
                    var currentUser = new User(0);

                    //if there's a context, try to resolve the user - this will return null if there is a context but no
                    // user found when there are old/invalid cookies lying around most likely during installation.
                    // in that case we'll keep using the admin user
                    if (string.IsNullOrEmpty(BasePages.UmbracoEnsuredPage.umbracoUserContextID) == false)
                    {
                        if (BasePages.UmbracoEnsuredPage.ValidateUserContextID(BasePages.UmbracoEnsuredPage.umbracoUserContextID))
                        {
                            var userById = User.GetCurrent();
                            if (userById != null)
                            {
                                currentUser = userById;
                            }
                        }
                    }


                    //Xml as XElement which is used with the new PackagingService
                    var rootElement      = Config.DocumentElement.GetXElement();
                    var packagingService = ApplicationContext.Current.Services.PackagingService;

                    //Perhaps it would have been a good idea to put the following into methods eh?!?

                    #region Stylesheets
                    foreach (XmlNode n in Config.DocumentElement.SelectNodes("Stylesheets/Stylesheet"))
                    {
                        StyleSheet s = StyleSheet.Import(n, currentUser);

                        insPack.Data.Stylesheets.Add(s.Id.ToString(CultureInfo.InvariantCulture));
                        //saveNeeded = true;
                    }

                    //if (saveNeeded) { insPack.Save(); saveNeeded = false; }
                    #endregion

                    #region Templates
                    var templateElement = rootElement.Descendants("Templates").FirstOrDefault();
                    if (templateElement != null)
                    {
                        var templates = packagingService.ImportTemplates(templateElement, currentUser.Id);
                        foreach (var template in templates)
                        {
                            insPack.Data.Templates.Add(template.Id.ToString(CultureInfo.InvariantCulture));
                        }
                    }
                    #endregion

                    #region Languages
                    var languageItemsElement = rootElement.Descendants("Languages").FirstOrDefault();
                    if (languageItemsElement != null)
                    {
                        var insertedLanguages = packagingService.ImportLanguages(languageItemsElement);
                        insPack.Data.Languages.AddRange(insertedLanguages.Select(l => l.Id.ToString(CultureInfo.InvariantCulture)));
                    }

                    #endregion

                    #region Dictionary items
                    var dictionaryItemsElement = rootElement.Descendants("DictionaryItems").FirstOrDefault();
                    if (dictionaryItemsElement != null)
                    {
                        var insertedDictionaryItems = packagingService.ImportDictionaryItems(dictionaryItemsElement);
                        insPack.Data.DictionaryItems.AddRange(insertedDictionaryItems.Select(d => d.Id.ToString(CultureInfo.InvariantCulture)));
                    }
                    #endregion

                    #region DataTypes
                    var dataTypeElement = rootElement.Descendants("DataTypes").FirstOrDefault();
                    if (dataTypeElement != null)
                    {
                        var dataTypeDefinitions = packagingService.ImportDataTypeDefinitions(dataTypeElement, currentUser.Id);
                        foreach (var dataTypeDefinition in dataTypeDefinitions)
                        {
                            insPack.Data.DataTypes.Add(dataTypeDefinition.Id.ToString(CultureInfo.InvariantCulture));
                        }
                    }
                    #endregion

                    #region Macros
                    var macroItemsElement = rootElement.Descendants("Macros").FirstOrDefault();
                    if (macroItemsElement != null)
                    {
                        var insertedMacros = packagingService.ImportMacros(macroItemsElement);
                        insPack.Data.Macros.AddRange(insertedMacros.Select(m => m.Id.ToString(CultureInfo.InvariantCulture)));
                    }
                    #endregion

                    #region DocumentTypes
                    //Check whether the root element is a doc type rather then a complete package
                    var docTypeElement = rootElement.Name.LocalName.Equals("DocumentType") ||
                                         rootElement.Name.LocalName.Equals("DocumentTypes")
                        ? rootElement
                        : rootElement.Descendants("DocumentTypes").FirstOrDefault();

                    if (docTypeElement != null)
                    {
                        var contentTypes = packagingService.ImportContentTypes(docTypeElement, currentUser.Id);
                        foreach (var contentType in contentTypes)
                        {
                            insPack.Data.Documenttypes.Add(contentType.Id.ToString(CultureInfo.InvariantCulture));
                            //saveNeeded = true;
                        }
                    }
                    #endregion

                    #region Documents
                    var documentElement = rootElement.Descendants("DocumentSet").FirstOrDefault();
                    if (documentElement != null)
                    {
                        var content          = packagingService.ImportContent(documentElement, -1, currentUser.Id);
                        var firstContentItem = content.First();
                        insPack.Data.ContentNodeId = firstContentItem.Id.ToString(CultureInfo.InvariantCulture);
                    }
                    #endregion

                    #region Package Actions
                    foreach (XmlNode n in Config.DocumentElement.SelectNodes("Actions/Action"))
                    {
                        if (n.Attributes["undo"] == null || n.Attributes["undo"].Value == "true")
                        {
                            insPack.Data.Actions += n.OuterXml;
                        }

                        //Run the actions tagged only for 'install'

                        if (n.Attributes["runat"] != null && n.Attributes["runat"].Value == "install")
                        {
                            var alias = n.Attributes["alias"] != null ? n.Attributes["alias"].Value : "";

                            if (alias.IsNullOrWhiteSpace() == false)
                            {
                                PackageAction.RunPackageAction(insPack.Data.Name, alias, n);
                            }
                        }
                    }
                    #endregion

                    // Trigger update of Apps / Trees config.
                    // (These are ApplicationStartupHandlers so just instantiating them will trigger them)
                    new ApplicationRegistrar();
                    new ApplicationTreeRegistrar();

                    insPack.Save();
                }
                catch (Exception exception)
                {
                    LogHelper.Error <Installer>("Error installing businesslogic", exception);
                    throw;
                }

                OnPackageBusinessLogicInstalled(insPack);
                OnPackageInstalled(insPack);
            }
        }
コード例 #3
0
        public void InstallFiles(int packageId, string tempDir)
        {
            using (DisposableTimer.DebugDuration <Installer>(
                       () => "Installing package files for package id " + packageId + " into temp folder " + tempDir,
                       () => "Package file installation complete for package id " + packageId))
            {
                //retrieve the manifest to continue installation
                var insPack = InstalledPackage.GetById(packageId);

                //TODO: Depending on some files, some files should be installed differently.
                //i.e. if stylsheets should probably be installed via business logic, media items should probably use the media IFileSystem!

                // Move files
                //string virtualBasePath = System.Web.HttpContext.Current.Request.ApplicationPath;
                string basePath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

                try
                {
                    foreach (XmlNode n in Config.DocumentElement.SelectNodes("//file"))
                    {
                        var destPath   = GetFileName(basePath, XmlHelper.GetNodeValue(n.SelectSingleNode("orgPath")));
                        var sourceFile = GetFileName(tempDir, XmlHelper.GetNodeValue(n.SelectSingleNode("guid")));
                        var destFile   = GetFileName(destPath, XmlHelper.GetNodeValue(n.SelectSingleNode("orgName")));

                        // Create the destination directory if it doesn't exist
                        if (Directory.Exists(destPath) == false)
                        {
                            Directory.CreateDirectory(destPath);
                        }
                        //If a file with this name exists, delete it
                        else if (File.Exists(destFile))
                        {
                            File.Delete(destFile);
                        }

                        // Copy the file
                        // SJ: Note - this used to do a move but some packages included the same file to be
                        // copied to multiple locations like so:
                        //
                        // <file>
                        //   <guid>my-icon.png</guid>
                        //   <orgPath>/umbraco/Images/</orgPath>
                        //   <orgName>my-icon.png</orgName>
                        // </file>
                        // <file>
                        //   <guid>my-icon.png</guid>
                        //   <orgPath>/App_Plugins/MyPlugin/Images</orgPath>
                        //   <orgName>my-icon.png</orgName>
                        // </file>
                        //
                        // Since this file unzips as a flat list of files, moving the file the first time means
                        // that when you try to do that a second time, it would result in a FileNotFoundException
                        File.Copy(sourceFile, destFile);

                        //PPH log file install
                        insPack.Data.Files.Add(XmlHelper.GetNodeValue(n.SelectSingleNode("orgPath")) + "/" + XmlHelper.GetNodeValue(n.SelectSingleNode("orgName")));
                    }

                    // Once we're done copying, remove all the files
                    foreach (XmlNode n in Config.DocumentElement.SelectNodes("//file"))
                    {
                        var sourceFile = GetFileName(tempDir, XmlHelper.GetNodeValue(n.SelectSingleNode("guid")));
                        if (File.Exists(sourceFile))
                        {
                            File.Delete(sourceFile);
                        }
                    }
                }
                catch (Exception exception)
                {
                    LogHelper.Error <Installer>("Package install error", exception);
                    throw;
                }

                // log that a user has install files
                if (_currentUserId > -1)
                {
                    Audit.Add(AuditTypes.PackagerInstall,
                              string.Format("Package '{0}' installed. Package guid: {1}", insPack.Data.Name, insPack.Data.PackageGuid),
                              _currentUserId, -1);
                }

                insPack.Save();
            }
        }