Exemplo n.º 1
0
        /// <summary>
        /// Harvest a directory.
        /// </summary>
        /// <param name="argument">The path of the directory.</param>
        /// <returns>The harvested directory.</returns>
        public override Wix.Fragment[] Harvest(string argument)
        {
            if (null == argument)
            {
                throw new ArgumentNullException("argument");
            }

            Wix.IParentElement harvestParent = this.HarvestDirectory(argument, true, this.GenerateType);
            Wix.ISchemaElement harvestElement;

            if (this.GenerateType == GenerateType.PayloadGroup)
            {
                Wix.PayloadGroup payloadGroup = (Wix.PayloadGroup)harvestParent;
                payloadGroup.Id = this.RootedDirectoryRef;
                harvestElement  = payloadGroup;
            }
            else
            {
                Wix.Directory directory = (Wix.Directory)harvestParent;

                Wix.DirectoryRef directoryRef = new Wix.DirectoryRef();
                directoryRef.Id = this.RootedDirectoryRef;

                if (this.SuppressRootDirectory)
                {
                    foreach (Wix.ISchemaElement element in directory.Children)
                    {
                        directoryRef.AddChild(element);
                    }
                }
                else
                {
                    directoryRef.AddChild(directory);
                }
                harvestElement = directoryRef;
            }

            Wix.Fragment fragment = new Wix.Fragment();
            fragment.AddChild(harvestElement);

            return(new Wix.Fragment[] { fragment });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Harvest a directory.
        /// </summary>
        /// <param name="path">The path of the directory.</param>
        /// <param name="harvestChildren">The option to harvest child directories and files.</param>
        /// <param name="generateType">The type to generate.</param>
        /// <returns>The harvested directory.</returns>
        private Wix.IParentElement HarvestDirectory(string path, bool harvestChildren, GenerateType generateType)
        {
            if (File.Exists(path))
            {
                throw new WixException(ErrorMessages.ExpectedDirectoryGotFile("dir", path));
            }

            if (null == this.RootedDirectoryRef)
            {
                this.RootedDirectoryRef = "TARGETDIR";
            }

            // use absolute paths
            path = Path.GetFullPath(path);

            // Remove any trailing separator to ensure Path.GetFileName() will return the directory name.
            path = path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

            Wix.IParentElement harvestParent;
            if (generateType == GenerateType.PayloadGroup)
            {
                harvestParent = new Wix.PayloadGroup();
            }
            else
            {
                Wix.Directory directory = new Wix.Directory();
                directory.Name       = Path.GetFileName(path);
                directory.FileSource = path;

                if (this.SetUniqueIdentifiers)
                {
                    if (this.SuppressRootDirectory)
                    {
                        directory.Id = this.Core.GenerateIdentifier(DirectoryPrefix, this.RootedDirectoryRef);
                    }
                    else
                    {
                        directory.Id = this.Core.GenerateIdentifier(DirectoryPrefix, this.RootedDirectoryRef, directory.Name);
                    }
                }
                harvestParent = directory;
            }

            if (harvestChildren)
            {
                try
                {
                    int fileCount = this.HarvestDirectory(path, "SourceDir\\", harvestParent, generateType);

                    if (generateType != GenerateType.PayloadGroup)
                    {
                        // its an error to not harvest anything with the option to keep empty directories off
                        if (0 == fileCount && !this.KeepEmptyDirectories)
                        {
                            throw new WixException(HarvesterErrors.EmptyDirectory(path));
                        }
                    }
                }
                catch (DirectoryNotFoundException)
                {
                    throw new WixException(HarvesterErrors.DirectoryNotFound(path));
                }
            }

            return(harvestParent);
        }