}         // End CreateNewVariableType

        #region File Creation
        /// <summary>
        /// Creates a script from the given template.
        ///
        /// This function does very little checking...it assumes that all names
        /// are valid C# for identifiers.
        ///
        /// Be wary that this may throw exceptions. (See StreamReader.ReadToEnd(),
        /// StreamWriter.Write(), StreamReader's contructor, and StreamWriter's
        /// constructor.)
        ///
        /// This does not issue a refresh, nor does it add any labels.
        /// </summary>
        /// <returns>
        /// The relative path (starting with the project root) to the newly
        /// created file. If it wasn't created (i.e. referability doesn't match
        /// with the types supported by the template), and empty string is
        /// returned instead.
        /// </returns>
        /// <param name="readableName">Human readable name.</param>
        /// <param name="typeName">C# name of type to be supported.</param>
        /// <param name="referability">
        /// Referability mode associated with the C# type named by typeName.
        /// </param>
        /// <param name="template">Info for the template.</param>
        /// <param name="normalPath">Path for non-editor scripts.</param>
        /// <param name="editorPath">Path for editor scripts.</param>
        private static string CreateScriptFromTemplate(
            string readableName,
            string typeName,
            ReferabilityMode referability,
            TemplateInfo template,
            string normalPath,
            string editorPath
            )
        {
            string templatePath = "";               // Path of the template file
            string newFileName  = "";               // Name of the new file
            string newFilePath  = "";               // Full path of new file (including name)

            // Before attempting to copy the template, we'll check if it
            // even matches what we need.
            if (template.IsCompatibleWith(referability))
            {
                templatePath = template.path;

                newFileName = ReplaceTemplatePlaceholders(
                    Path.GetFileNameWithoutExtension(templatePath),
                    readableName, typeName, referability.ToString()
                    ) + ".cs";

                newFilePath = UnityPathUtils.Combine(
                    (template.IsEngineTemplate ? normalPath : editorPath),
                    newFileName
                    );

                if (File.Exists(newFilePath))
                {
                    throw new IOException(newFilePath + " already exists!");
                }

                StreamReader templateReader   = null;
                string       templateContents = "";
                try {
                    // After changing the conde in this block, revise the
                    // exception documentaion above.
                    templateReader   = new StreamReader(templatePath);
                    templateContents = templateReader.ReadToEnd();
                }
                finally {
                    if (templateReader != null)
                    {
                        templateReader.Close();
                    }
                }


                string newScriptContents = ReplaceTemplatePlaceholders(
                    templateContents,
                    readableName, typeName, referability.ToString()
                    );


                StreamWriter scriptWriter = null;
                try {
                    // After changing the conde in this block, revise the
                    // exception documentaion above.
                    scriptWriter = new StreamWriter(newFilePath);
                    scriptWriter.Write(newScriptContents);
                }
                finally {
                    if (scriptWriter != null)
                    {
                        scriptWriter.Close();
                    }
                }
            }             // End if( ... )

            return(newFilePath);
        }         // End CreateScriptFromTemplate
Exemplo n.º 2
0
        }         // End CreateNewVariableType

        #region File Creation
        /// <summary>
        /// Creates a script from the given template.
        ///
        /// This function does very little checking...it assumes that all names
        /// are valid C# for identifiers.
        ///
        /// Be wary that this may throw exceptions. (See StreamReader.ReadToEnd(),
        /// StreamWriter.Write(), StreamReader's contructor, and StreamWriter's
        /// constructor.)
        ///
        /// This does not issue a refresh, nor does it add any labels.
        /// </summary>
        /// <returns>
        /// The relative path (starting with the project root) to the newly
        /// created file. If it wasn't created (i.e. referability doesn't match
        /// with the types supported by the template), and empty string is
        /// returned instead.
        /// </returns>
        /// <param name="metaData">Data used to populate the template.</param>
        /// <param name="template">Info for the template.</param>
        /// <param name="normalPath">Path for non-editor scripts.</param>
        /// <param name="editorPath">Path for editor scripts.</param>
        private static string CreateScriptFromTemplate(
            ScriptMetaData metaData,
            TemplateInfo template,
            string normalPath,
            string editorPath,
            bool overrideExisting = false
            )
        {
            //string templatePath = "";   // Path of the template file
            //string newFileName = "";    // Name of the new file
            string newFilePath = "";                // Full path of new file (including name)

            // Before attempting to copy the template, we'll check if it
            // even matches what we need.
            if (template.IsCompatibleWith(metaData.ParsedReferability))
            {
                string templateName = Path.GetFileNameWithoutExtension(template.path);
                string newFileName  = metaData.ApplyReplacements(templateName) + ".cs";

                newFilePath = UnityPathUtils.Combine(
                    (template.IsEngineTemplate ? normalPath : editorPath),
                    newFileName
                    );

                if (File.Exists(newFilePath) && !overrideExisting)
                {
                    throw new IOException(newFilePath + " already exists!");
                }

                StreamReader templateReader   = null;
                string       templateContents = "";
                try {
                    // After changing the conde in this block, revise the
                    // exception documentaion above.
                    templateReader   = new StreamReader(template.path);
                    templateContents = templateReader.ReadToEnd();
                }
                finally {
                    if (templateReader != null)
                    {
                        templateReader.Close();
                    }
                }


                string newScriptContents = metaData.ApplyReplacements(templateContents);


                StreamWriter scriptWriter = null;
                try {
                    // After changing the conde in this block, revise the
                    // exception documentaion above.
                    scriptWriter = new StreamWriter(newFilePath, append: false);
                    scriptWriter.Write(newScriptContents + TemplateNewLine);

                    // Append the meta data.
                    scriptWriter.Write(ScriptSetManager.DataHeader + TemplateNewLine);
                    scriptWriter.Write(metaData.ToJson() + TemplateNewLine);
                    scriptWriter.Write(ScriptSetManager.DataFooter + TemplateNewLine);
                }
                finally {
                    if (scriptWriter != null)
                    {
                        scriptWriter.Close();
                    }
                }
            }             // End if( ... )

            return(newFilePath);
        }         // End CreateScriptFromTemplate