Esempio n. 1
0
        /// <summary>
        /// Gets the text of the type.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string ToString(GeneratedTypeDeclaration type, Language language)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            // return...
            return(ToStringInternal(type, language));
        }
Esempio n. 2
0
        /// <summary>
        /// Saves the given type.
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="type"></param>
        /// <returns>Null if the file was written or doesn't need to be written, otherwise the name of the file that needs to be checked out.</returns>
        private string[] SaveType(Project project, EntityGenerator generator, string folderPath, GeneratedTypeDeclaration type,
                                  bool isBase, bool forWebService, EntityGenerationContext context)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }
            if (generator == null)
            {
                throw new ArgumentNullException("generator");
            }
            if (folderPath == null)
            {
                throw new ArgumentNullException("folderPath");
            }
            if (folderPath.Length == 0)
            {
                throw new ArgumentOutOfRangeException("'folderPath' is zero-length.");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // get the path...
            string path = string.Format("{0}\\{1}.cs", folderPath, type.CodeItem.Name);

            if (project.Settings.TargetVersion != DotNetVersion.V1 && isBase)
            {
                // if we're base, we're actually doing a partial file so change the name...
                path = string.Format("{0}\\{1}.Base.cs", folderPath, type.CodeItem.Name);
            }

            // if we don't want to overwrite it and it exists, go back...
            bool exists = File.Exists(path);

            if (!(isBase) && exists)
            {
                return(null);
            }

            // processing...
            if (this.Log.IsInfoEnabled)
            {
                this.Log.Info("Processing: " + path);
            }

            // get the namespace...
            var ns = generator.GetNamespace(project.Settings.NamespaceName, type, context);

            if (ns == null)
            {
                throw new InvalidOperationException("ns is null.");
            }

            // get the code...
            string code = CodeDomExtender.ToString(ns, Language.CSharp);

            if (code == null)
            {
                throw new InvalidOperationException("'code' is null.");
            }
            if (code.Length == 0)
            {
                throw new InvalidOperationException("'code' is zero-length.");
            }

            // make it partial...  can't do this with the 1.1 CodeDOM.
            //if(project.Settings.TargetVersion != DotNetVersion.V1)
            //	code = CodeDomExtender.AddPartialKeyword(code, project.Settings.Language);

            // do we want to write it?
            bool doWrite = false;

            if (exists)
            {
                // check it...
                byte[] codeHash = this.GetCodeHash(code);
                if (codeHash == null)
                {
                    throw new InvalidOperationException("'codeHash' is null.");
                }
                if (codeHash.Length == 0)
                {
                    throw new InvalidOperationException("'codeHash' is zero-length.");
                }

                // get the file...
                byte[] fileHash = this.GetFileHash(path);
                if (fileHash == null)
                {
                    throw new InvalidOperationException("'fileHash' is null.");
                }
                if (fileHash.Length == 0)
                {
                    throw new InvalidOperationException("'fileHash' is zero-length.");
                }

                // compare...
                if (codeHash.Length != fileHash.Length)
                {
                    throw new InvalidOperationException(string.Format("Length mismatch for 'codeHash' and 'fileHash': {0} cf {1}.", codeHash.Length, fileHash.Length));
                }
                for (int index = 0; index < codeHash.Length; index++)
                {
                    if (codeHash[index] != fileHash[index])
                    {
                        if (this.Log.IsInfoEnabled)
                        {
                            this.Log.Info("\tFile hash has changed");
                        }
                        doWrite = true;
                        break;
                    }
                }
            }
            else
            {
                if (this.Log.IsInfoEnabled)
                {
                    this.Log.Info("\tFile does not exist");
                }
                doWrite = true;
            }

            // write it...
            if (doWrite)
            {
                // can we?
                if (File.Exists(path) && (File.GetAttributes(path) & FileAttributes.ReadOnly) != 0)
                {
                    // mbr - 14-10-2005 - spit the code to a temp file...
                    string tempPath = Runtime.Current.GetTempFilePath(Path.GetExtension(path));
                    using (StreamWriter writer = new StreamWriter(tempPath))
                        writer.Write(code);

                    // require checkout...
                    if (this.Log.IsInfoEnabled)
                    {
                        this.Log.Info("\tFile is read-only");
                    }
                    return(new string[] { path, tempPath });
                }

                // log...
                if (this.Log.IsInfoEnabled)
                {
                    this.Log.Info("\tWriting file");
                }

                // create a file...
                using (StreamWriter writer = new StreamWriter(path))
                    writer.Write(code);
            }

            // ok...
            return(null);
        }