示例#1
0
        /// <summary>
        /// Process cab file
        /// </summary>
        /// <returns></returns>
        public bool ProcessCabFile(string fileName, SolutionInformation solutionInfo)
        {
            bool returnValue = true;
            try
            {
                if (File.Exists("manifest.xml"))
                {
                    CabType ct = GetCabType(fileName);

                    switch (ct)
                    {
                        case CabType.WebPart:
                            {
                                WebPartManifest wpm = new WebPartManifest();

                                //// A FileStream is needed to read the XML document.
                                using (FileStream fs = new FileStream("manifest.xml", FileMode.Open, FileAccess.Read))
                                {
                                    using (XmlReader reader = new XmlTextReader(fs))
                                    {
                                        // Declare an object variable of the type to be deserialized.
                                        // Create an instance of the XmlSerializer specifying type and namespace.
                                        XmlSerializer serializer = new XmlSerializer(typeof(WebPartManifest));

                                        wpm = (WebPartManifest)serializer.Deserialize(reader);
                                        foreach (object o in wpm.DWPFiles)
                                        {
                                            WebPartManifestDwpFiles dwpFiles = (WebPartManifestDwpFiles)o;
                                            foreach (WebPartManifestDwpFilesDwpFile dwpFile in dwpFiles.DwpFiles)
                                            {
                                                WebPartInfo wp = new WebPartInfo();
                                                wp.File = dwpFile.FileName;
                                                wp.Type = GetTypeOfWebPart(dwpFile.FileName);
                                                solutionInfo.WebParts.Add(wp);

                                            }
                                        }
                                        foreach (object o in wpm.Assemblies)
                                        {
                                            WebPartManifestAssemblies assemblies = (WebPartManifestAssemblies)o;
                                            foreach (WebPartManifestAssembliesAssembly assembly in assemblies.Assemblies)
                                            {
                                                AssemblyInfo assInfo = new AssemblyInfo();
                                                AssemblyFileReference afr = new AssemblyFileReference();
                                                afr.Location = assembly.FileName;
                                                ProcessAssembly(afr, solutionInfo, Path.GetDirectoryName(fileName));
                                            }
                                        }
                                    }
                                }

                                break;
                            }
                        case CabType.SiteTemplate:
                            {
                                //// A FileStream is needed to read the XML document.
                                using (FileStream fs = new FileStream("manifest.xml", FileMode.Open, FileAccess.Read))
                                {
                                    using (StreamReader reader = new StreamReader(fs))
                                    {
                                        using (StreamReader reader2 = new StreamReader(fs))
                                        {
                                            while (!reader2.EndOfStream)
                                            {
                                                string s = reader2.ReadLine().TrimStart();
                                                if (s.StartsWith("<TemplateTitle>"))
                                                {
                                                    s = s.Substring(s.IndexOf(">") + 1);
                                                    s = s.Substring(0, s.IndexOf("<"));
                                                    solutionInfo.SiteTemplates.Add(s);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }

                                break;
                            }
                        case CabType.ListTemplate:
                            {
                                //// A FileStream is needed to read the XML document.
                                using (FileStream fs = new FileStream("manifest.xml", FileMode.Open, FileAccess.Read))
                                {
                                    using (StreamReader reader = new StreamReader(fs))
                                    {
                                        using (StreamReader reader2 = new StreamReader(fs))
                                        {
                                            while (!reader2.EndOfStream)
                                            {
                                                string s = reader2.ReadLine().TrimStart();
                                                if (s.StartsWith("<TemplateTitle>"))
                                                {
                                                    s = s.Substring(s.IndexOf(">") + 1);
                                                    s = s.Substring(0, s.IndexOf("<"));
                                                    solutionInfo.ListTemplates.Add(s);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }

                                break;
                            }
                    }
                }
            }
            catch (Exception ex)
            {
                returnValue = false;
                solutionInfo.Error = ex.Message;

                //ExceptionCSV_SolutionAnalyzer.WriteException(Path.GetFileName(solutionInfo.Name), "ProcessCabFile", ex.Message, ex.ToString(), ex.GetType().ToString(), "N/A");
            }

            return returnValue;
        }
示例#2
0
        /// <summary>
        /// Look inside an assembly
        /// </summary>
        private void ProcessAssembly(AssemblyFileReference assemblyReference, SolutionInformation solutionInfo, string folder)
        {
            try
            {
                StringBuilder sb = new StringBuilder();

                FileInfo fi = new FileInfo(Path.Combine(folder, assemblyReference.Location));
                if (_verbose) { Console.WriteLine("Processing assembly :{0}", fi.FullName); }
                sb.AppendFormat("{0} size {1}\r\n", assemblyReference.Location, fi.Length);
                _numberOfAssemblies = _numberOfAssemblies + 1;

                Mono.Cecil.ModuleDefinition module = Mono.Cecil.ModuleDefinition.ReadModule(Path.Combine(folder, assemblyReference.Location));

                AssemblyInfo assemblyInfo = new AssemblyInfo();
                assemblyInfo.File = fi.Name;
                assemblyInfo.Size = (int)fi.Length;
                int pos = module.FullyQualifiedName.IndexOf("Version=");
                if (pos > 0)
                {
                    assemblyInfo.Version = module.FullyQualifiedName.Substring(pos + 8);
                    pos = assemblyInfo.Version.IndexOf(",");
                    assemblyInfo.Version = assemblyInfo.Version.Substring(0, pos);
                }
                solutionInfo.Assemblies.Add(assemblyInfo);

                // Check if this is an assembly of  Microsoft SharePoint
                if (!module.Assembly.FullName.Contains("PublicKeyToken=71e9bce111e9429c"))
                {
                    if (assemblyReference.SafeControls != null)
                    {
                        sb.AppendFormat("\tSafeControlEntries: {0}\r\n", assemblyReference.SafeControls.GetLength(0));

                        foreach (SafeControlDefinition scd in assemblyReference.SafeControls)
                        {
                            if (scd.SafeSpecified)
                            {
                                string s =
                                    String.Format(
                                        "\t\t<SafeControl Assembly=\"{0}\" Namespace=\"{1}\" TypeName=\"{2}\" Safe=\"{3}\" />\r\n",
                                        scd.Assembly, scd.Namespace, scd.TypeName, scd.Safe);
                                sb.Append(s);
                                assemblyInfo.SafeConfigEntries.Add(s);
                                solutionInfo.SafeControlEntriesCount = solutionInfo.SafeControlEntriesCount + 1;
                            }
                            else
                            {
                                string s =
                                    String.Format(
                                        "\t\t<SafeControl Assembly=\"{0}\" Namespace=\"{1}\" TypeName=\"{2}\" />\r\n",
                                        scd.Assembly, scd.Namespace, scd.TypeName);
                                sb.Append(s);
                                assemblyInfo.SafeConfigEntries.Add(s);
                            }
                            _numberOfSafeControlEntries = _numberOfSafeControlEntries + 1;
                        }
                    }

                    if (assemblyReference.ClassResources != null)
                    {
                        _numberOfClassResources += assemblyReference.ClassResources.GetLength(0);
                        solutionInfo.ClassResourcesCount += assemblyReference.ClassResources.GetLength(0);
                    }

                    var modules = module.ModuleReferences;
                    sb.AppendFormat("\tModules: {0}\r\n", modules.Count + 1);

                    sb.AppendFormat("\t\t{0}\r\n", module.Name);
                    assemblyInfo.Modules.Add(module.Name);
                    foreach (var m in module.ModuleReferences)
                    {
                        sb.AppendFormat("\t\t{0}\r\n", m.Name);
                        assemblyInfo.Modules.Add(m.Name);
                    }
                    var refs = module.AssemblyReferences;

                    sb.AppendFormat("\tReferenced assemblies: {0}\r\n", refs.Count);
                    foreach (var an in refs)
                    {
                        sb.AppendFormat("\t\t{0}\r\n", an.Name);
                        assemblyInfo.ReferencedAssemblies.Add(an.Name);
                    }

                    var types = module.Types;
                    sb.AppendFormat("\tTypes: {0}\r\n", types.Count);
                    foreach (var t in types)
                    {
                        try
                        {
                            // Skip the default module node
                            if (t.FullName.Equals("<Module>", StringComparison.InvariantCultureIgnoreCase))
                            {
                                continue;
                            }

                            Class c = new Class();
                            c.Name = t.FullName;
                            if (t.BaseType != null)
                            {
                                c.InheritsFrom = t.BaseType.FullName;
                            }

                            switch (c.InheritsFrom)
                            {
                                case "Microsoft.SharePoint.Administration.SPJobDefinition":
                                    {
                                        solutionInfo.TimerJobs.Add(c.Name);
                                        break;
                                    }
                                case "Microsoft.SharePoint.SPItemEventReceiver":
                                    {
                                        solutionInfo.EventHandlers.Add(c.Name);
                                        break;
                                    }
                                case "System.Web.UI.UserControl":
                                    {
                                        solutionInfo.UserControls.Add(c.Name);
                                        break;
                                    }
                            }

                            sb.AppendFormat("\t\t{0}\r\n", t.FullName);

                            var methods = t.Methods;
                            sb.AppendFormat("\t\t Methods:{0}\r\n", methods.Count);
                            c.NrOfMethods = methods.Count;
                            if (_verbose)
                            {
                                foreach (var m in methods)
                                {
                                    c.Methods.Add(new MethodInformation() { Name = m.Name, CodeSize = m.Body.CodeSize });
                                    sb.AppendFormat("\t\t\t{0}\r\n", m.Name);
                                }
                            }

                            var props = t.Properties;

                            sb.AppendFormat("\t\t Properties:{0}\r\n", props.Count);
                            c.NrOfProperties = props.Count;
                            if (_verbose)
                            {
                                foreach (var p in props)
                                {
                                    sb.AppendFormat("\t\t\t{0}\r\n", p.Name);
                                    c.Properties.Add(p.Name);
                                }
                            }

                            assemblyInfo.Classes.Add(c);

                        }
                        catch { }

                    }

                    var resources = module.Resources;
                    sb.AppendFormat("\tResources:{0}\r\n", resources.Count);

                    assemblyInfo.NrOfResources = resources.Count;
                    foreach (var resource in resources)
                    {
                        sb.AppendFormat("\t\t\t{0}", resource.Name);
                        assemblyInfo.Resources.Add(resource.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                if (_verbose)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error when processing assembly {0},Error: {1}", assemblyReference.Location,
                        ex.Message);
                    Console.ResetColor();
                }
                string comments = string.Empty;
                comments = "Error when processing assembly : " + assemblyReference.Location;
                //ExceptionCSV_SolutionAnalyzer.WriteException(Path.GetFileName(solutionInfo.Name), "ProcessAssembly", ex.Message, ex.ToString(), ex.GetType().ToString(), comments);
            }
        }