/// <summary>
        /// Writes out the project file to a given directory.
        /// </summary>
        /// <param name="directory">The directory to save the file.</param>
        public void Save(DirectoryInfo directory)
        {
            // Set up the project macros we'll be expanding.
            ProjectMacros macros = SetupMacros(directory);

            // Validate the state.
            string projectFilename = macros.ExpandMacros("{ProjectFilename}");

            if (string.IsNullOrWhiteSpace(projectFilename))
            {
                throw new InvalidOperationException(
                          "Project filename is not defined in Settings property.");
            }

            // We need a write lock on the blocks to avoid changes. This also prevents
            // any background tasks from modifying the blocks during the save process.
            ProjectBlockCollection blocks = Project.Blocks;

            using (blocks.AcquireLock(RequestLock.Write))
            {
                // Create a new project writer and write out the results.
                var projectWriter = new FilesystemPersistenceProjectWriter(
                    Project, Settings, macros);
                var projectFile = new FileInfo(projectFilename);

                projectWriter.Write(projectFile);
            }
        }
        public void ExpandValue()
        {
            // Arrange
            var macros = new ProjectMacros();

            macros.Substitutions["ProjectDir"] = "pd";

            // Act
            string results = macros.ExpandMacros("{ProjectDir}");

            // Assert
            Assert.AreEqual("pd", results);
        }
        /// <summary>
        /// Gets the macro or project XML writer. If the given variable expands to
        /// a value, an XML writer is created and returned. Otherwise, the given
        /// project writer is used instead.
        /// </summary>
        /// <param name="projectWriter">The project writer.</param>
        /// <param name="macros">The macros.</param>
        /// <param name="variable">The variable.</param>
        /// <param name="createdWriter">if set to <c>true</c> [created writer].</param>
        /// <returns></returns>
        protected static XmlWriter GetXmlWriter(
            XmlWriter projectWriter,
            ProjectMacros macros,
            string variable,
            out bool createdWriter)
        {
            // Expand the variable to get the filename.
            string filename = macros.ExpandMacros(variable);

            // If the value is null, then we use the project writer.
            if (string.IsNullOrWhiteSpace(filename))
            {
                createdWriter = false;
                return(projectWriter);
            }

            // Create the writer and return it.
            var       file   = new FileInfo(filename);
            XmlWriter writer = GetXmlWriter(file);

            createdWriter = true;

            return(writer);
        }