//*************************************************************************
		//*	Private																																*
		//*************************************************************************
		//*************************************************************************
		//*	Protected																															*
		//*************************************************************************
		//*************************************************************************
		//*	Public																																*
		//*************************************************************************

		//*-----------------------------------------------------------------------*
		//*	ParseProject																													*
		//*-----------------------------------------------------------------------*
		/// <summary>
		/// Parse the project text content into an abstracted definition
		/// collection.
		/// </summary>
		/// <param name="content">
		/// JSON string content to be deserialized.
		/// </param>
		/// <param name="projectPath">
		/// Path to the local working directory.
		/// </param>
		/// <param name="configs">
		/// Configuration tables loaded in this session.
		/// </param>
		/// <param name="components">
		/// Metadata component pages loaded in this session.
		///	</param>
		///	<param name="templates">
		///	Template definitions loaded in this session.
		///	</param>
		/// <returns>
		/// Newly created and resolved JSON Template collection.
		/// </returns>
		/// <remarks>
		/// The project and template files both use generic templates.
		/// </remarks>
		public static void Parse(string content,
			string projectPath, ConfigurationCollection configs,
			ComponentCollection components, TemplateCollection templates)
		{
			ComponentItem iComponent = null;
			ConfigurationItem iConfig = null;
			TemplateItem iTemplate = null;
			JsonTemplateCollection project = JsonConvert.
					DeserializeObject<JsonTemplateCollection>(content);
			string typeName = "";

			foreach(JsonTemplateItem template in project)
			{
				typeName = template.TypeName.ToLower();
				switch(typeName)
				{
					case "componentpage":
						iComponent = ComponentItem.Parse(template.Name,
							template.Definition, projectPath);
						components.Add(iComponent);
						break;
					case "configuration":
						iConfig =
							ConfigurationItem.Parse(template.Name,
							template.Definition, projectPath);
						configs.Add(iConfig);
						break;
					case "template":
						iTemplate = TemplateItem.Parse(template.Name,
							template.Definition, projectPath);
						templates.Add(iTemplate);
						break;
				}
			}
		}
Esempio n. 2
0
        //*-----------------------------------------------------------------------*

        //*-----------------------------------------------------------------------*
        //*	Run																																		*
        //*-----------------------------------------------------------------------*
        /// <summary>
        /// Run the configured application.
        /// </summary>
        public void Run()
        {
            ActionNodeTree tree    = new ActionNodeTree();
            string         content = "";
            DirectoryInfo  idir    = null;
            FileInfo       ifile   = new FileInfo(mProjectFilename);
            FileInfo       ofile   = null;

            if (mProjectPath?.Length > 0)
            {
                //	If a project path was specified, then set it.
                idir = new DirectoryInfo(mProjectPath);
                Directory.SetCurrentDirectory(mProjectPath);
            }
            else if (ifile.DirectoryName?.Length > 0)
            {
                //	If the project file used a directory name, then that
                //	will also be our project folder.
                if (ifile.DirectoryName.IndexOf(":") > -1 ||
                    ifile.DirectoryName.IndexOf("\\\\") > -1)
                {
                    //	Absolute path in project filename overrides project path
                    //	name.
                    Directory.SetCurrentDirectory(ifile.DirectoryName);
                    mProjectPath = ifile.DirectoryName;
                }
                else
                {
                    //	Project filename path is relative.
                    mProjectPath = Path.Combine(mProjectPath, ifile.DirectoryName);
                    Directory.SetCurrentDirectory(mProjectPath);
                }
                mProjectFilename = ifile.Name;
            }
            else
            {
                //	Use the current directory as the project path.
                mProjectPath = Directory.GetCurrentDirectory();
            }

            ifile = new FileInfo(Path.Combine(mProjectPath, mProjectFilename));
            if (mOutputFilename?.Length > 0)
            {
                if (Tools.IsRelative(mOutputFilename))
                {
                    ofile = new FileInfo(Path.Combine(mProjectPath, mOutputFilename));
                }
                else
                {
                    ofile = new FileInfo(mOutputFilename);
                }
            }

            if (ifile.Exists)
            {
                //	Read the project file.
                content = File.ReadAllText(ifile.FullName);
                switch (mMode)
                {
                case ProgramModeTypeEnum.ConvertToTemplate:
                    Console.WriteLine("Mode: Convert source to template...");
                    content =
                        JsonTemplateItem.MakeTemplate(content,
                                                      mTabCharacter, mTabCount);
                    if (ofile != null)
                    {
                        Directory.CreateDirectory(ofile.DirectoryName);
                        File.WriteAllText(ofile.FullName, content);
                        Console.WriteLine(
                            string.Format("Template file written to {0}...",
                                          ofile.Name));
                    }
                    break;

                case ProgramModeTypeEnum.InventoryDetail:
                    Console.WriteLine(
                        "Mode: Create inventory detail report from project...");
                    JsonTemplateCollection.Parse(content, mProjectPath,
                                                 mConfigurations, mComponents, mTemplates);
                    tree.Configurations = mConfigurations;
                    tree.Components     = mComponents;
                    tree.ProjectPath    = mProjectPath;
                    tree.RenderedContent.Clear();
                    foreach (TemplateItem template in mTemplates)
                    {
                        tree.InventoryDetail(template);
                    }
                    if (tree.SaveFile(ofile))
                    {
                        Console.WriteLine(
                            string.Format(
                                "File {0} saved...", ofile.Name));
                    }
                    break;

                case ProgramModeTypeEnum.InventorySummary:
                    Console.WriteLine(
                        "Mode: Create inventory summary report from project...");
                    JsonTemplateCollection.Parse(content, mProjectPath,
                                                 mConfigurations, mComponents, mTemplates);
                    tree.Configurations = mConfigurations;
                    tree.Components     = mComponents;
                    tree.ProjectPath    = mProjectPath;
                    tree.RenderedContent.Clear();
                    tree.InventorySummary(mTemplates);
                    if (tree.SaveFile(ofile))
                    {
                        Console.WriteLine(
                            string.Format(
                                "File {0} saved...", ofile.Name));
                    }
                    break;

                case ProgramModeTypeEnum.ListNodes:
                    Console.WriteLine("Mode: List nodes in project...");
                    JsonTemplateCollection.Parse(content, mProjectPath,
                                                 mConfigurations, mComponents, mTemplates);
                    tree.Configurations = mConfigurations;
                    tree.Components     = mComponents;
                    tree.ProjectPath    = mProjectPath;
                    tree.RenderedContent.Clear();
                    foreach (TemplateItem template in mTemplates)
                    {
                        tree.ListTemplate(template);
                    }
                    if (tree.SaveFile(ofile))
                    {
                        Console.WriteLine(
                            string.Format(
                                "File {0} saved...", ofile.Name));
                    }
                    break;

                case ProgramModeTypeEnum.RenderProject:
                    Console.WriteLine("Mode: Render documents from project...");
                    JsonTemplateCollection.Parse(content, mProjectPath,
                                                 mConfigurations, mComponents, mTemplates);
                    tree.Configurations = mConfigurations;
                    tree.Components     = mComponents;
                    tree.ProjectPath    = mProjectPath;
                    tree.Verbose        = mVerbose;
                    foreach (TemplateItem template in mTemplates)
                    {
                        Console.WriteLine(
                            string.Format("Rendering template: {0}",
                                          template.Name));
                        if (tree.RenderTemplate(template))
                        {
                            if (tree.RenderedContent.IsDirty)
                            {
                                //	Save the file if possible.
                                if (tree.SaveFile())
                                {
                                    Console.WriteLine(
                                        string.Format(
                                            "File {0} saved...",
                                            mConfigurations.GetValue(
                                                template.Name + ".OutputFilename")));
                                }
                            }
                        }
                    }
                    break;
                }
            }
            else
            {
                Console.WriteLine("Error: Project File [" + ifile.FullName +
                                  "] does not exist...");
            }
        }