Inheritance: System.CodeDom.CodeDirective
Ejemplo n.º 1
0
		public void Constructor1 ()
		{
			string fileName = "mono";
			Guid algorithmId = Guid.NewGuid();
			byte[] data = new byte[] {0,1};

			CodeChecksumPragma ccp = new CodeChecksumPragma (fileName, algorithmId, data);
			Assert.AreEqual (algorithmId, ccp.ChecksumAlgorithmId, "#1");
			Assert.AreEqual (data, ccp.ChecksumData, "#2");
			Assert.AreEqual (fileName, ccp.FileName, "#3");
			Assert.AreSame (data, ccp.ChecksumData, "#4");
			Assert.AreSame (fileName, ccp.FileName, "#5");

			ccp.ChecksumAlgorithmId = Guid.Empty;
			Assert.AreEqual (Guid.Empty, ccp.ChecksumAlgorithmId, "#6");

			ccp.ChecksumData = null;
			Assert.IsNull (ccp.ChecksumData, "#7");

			ccp.FileName = null;
			Assert.IsNotNull (ccp.FileName, "#8");
			Assert.AreEqual (string.Empty, ccp.FileName, "#9");

			ccp = new CodeChecksumPragma ((string) null, Guid.Empty, (byte[]) null);
			Assert.AreEqual (Guid.Empty, ccp.ChecksumAlgorithmId, "#10");
			Assert.IsNull (ccp.ChecksumData, "#11");
			Assert.IsNotNull (ccp.FileName, "#12");
			Assert.AreEqual (string.Empty, ccp.FileName, "#13");
		}
Ejemplo n.º 2
0
		public void Constructor0_Deny_Unrestricted ()
		{
			CodeChecksumPragma ccp = new CodeChecksumPragma ();
			Assert.AreEqual (Guid.Empty, ccp.ChecksumAlgorithmId, "ChecksumAlgorithmId");
			ccp.ChecksumAlgorithmId = Guid.NewGuid ();
			Assert.IsNull (ccp.ChecksumData, "ChecksumData");
			ccp.ChecksumData = new byte [1];
			Assert.AreEqual (String.Empty, ccp.FileName, "FileName");
		}
Ejemplo n.º 3
0
		public void Constructor1_Deny_Unrestricted ()
		{
			Guid g = Guid.NewGuid ();
			CodeChecksumPragma ccp = new CodeChecksumPragma ("mono", g, new byte [1]);
			Assert.AreEqual (g, ccp.ChecksumAlgorithmId, "ChecksumAlgorithmId");
			ccp.ChecksumAlgorithmId = Guid.Empty;
			Assert.AreEqual (1, ccp.ChecksumData.Length, "ChecksumData");
			ccp.ChecksumData = new byte[0];
			Assert.AreEqual ("mono", ccp.FileName, "FileName");
		}
Ejemplo n.º 4
0
		public void Constructor0 ()
		{
			CodeChecksumPragma ccp = new CodeChecksumPragma ();
			Assert.AreEqual (Guid.Empty, ccp.ChecksumAlgorithmId, "#1");
			Assert.IsNull (ccp.ChecksumData, "#2");
			Assert.IsNotNull (ccp.FileName, "#3");
			Assert.AreEqual (string.Empty, ccp.FileName, "#4");

			ccp.FileName = null;
			Assert.IsNotNull (ccp.FileName, "#5");
			Assert.AreEqual (string.Empty, ccp.FileName, "#6");
		}
Ejemplo n.º 5
0
        private void GenerateSource()
        {
            Debug.Assert(_codeContexts.Count == 0);

            CodeNamespace cnsImports = IsLanguageCSharp ? new CodeNamespace() : _ccRoot.CodeNS;

            if (IsCodeNeeded)
            {
                cnsImports.Imports.Add(new CodeNamespaceImport("System"));

                if (_usingNS != null)
                {
                    foreach (string u in _usingNS)
                    {
                        cnsImports.Imports.Add(new CodeNamespaceImport(u));
                    }

                    _usingNS.Clear();
                    _usingNS = null;
                }

                //  } end SubClass
                _ccRoot.CodeNS.Types.Add(_ccRoot.CodeClass);
            }

            if (IsCompilingEntryPointClass)
            {
                GenerateAppEntryPoint();
            }

            if (IsCodeNeeded)
            {
                MemoryStream codeMemStream = new MemoryStream();

                // using Disposes the StreamWriter when it ends.  Disposing the StreamWriter
                // also closes the underlying MemoryStream.  Furthermore, don't add BOM here since
                // TaskFileService.WriteGeneratedCodeFileFile adds it.
                using (StreamWriter codeStreamWriter = new StreamWriter(codeMemStream, new UTF8Encoding(false)))
                {
                    CodeGeneratorOptions o = new CodeGeneratorOptions();

                    // } end namespace
                    CodeCompileUnit ccu = new CodeCompileUnit();

                    // generate pragma checksum data
                    if (s_md5HashAlgorithm == null)
                    {
                        s_md5HashAlgorithm = new MD5CryptoServiceProvider();
                        s_hashMD5Guid = new Guid(0x406ea660, 0x64cf, 0x4c82, 0xb6, 0xf0, 0x42, 0xd4, 0x81, 0x72, 0xa7, 0x99);
                    }

                    CodeChecksumPragma csPragma = new CodeChecksumPragma();
                    csPragma.FileName = ParentFolderPrefix + SourceFileInfo.RelativeSourceFilePath + XAML;
                    csPragma.ChecksumAlgorithmId = s_hashMD5Guid;
                    csPragma.ChecksumData = TaskFileService.GetChecksum(SourceFileInfo.OriginalFilePath, s_hashMD5Guid);
                    ccu.StartDirectives.Add(csPragma);

                    if (cnsImports != _ccRoot.CodeNS)
                    {
                        ccu.Namespaces.Add(cnsImports);
                    }

                    ccu.Namespaces.Add(_ccRoot.CodeNS);

                    CodeDomProvider codeProvider = EnsureCodeProvider();

                    if (codeProvider.Supports(GeneratorSupport.PartialTypes) && _ccRoot.SubClass.Length == 0)
                    {
                        _ccRoot.CodeClass.IsPartial = true;
                    }

                    codeProvider.GenerateCodeFromCompileUnit(ccu, codeStreamWriter, o);

                    codeStreamWriter.Flush();
                    TaskFileService.WriteGeneratedCodeFile(codeMemStream.ToArray(),
                        TargetPath + SourceFileInfo.RelativeSourceFilePath,
                        SharedStrings.GeneratedExtension, SharedStrings.IntellisenseGeneratedExtension,
                        LanguageSourceExtension);
                }
            }

            // Generate the InternalTypeHelper class in a separate code file only once and on an as
            // needed basis for the current assembly being built. This class provides support for
            // accessing legitimate internal types and properties that are present in the same (local)
            // or a friend assembly and it is generated only when any such internals are actually
            // encountered in any of the xaml files in the project.
            GenerateInternalTypeHelperImplementation();
        }
Ejemplo n.º 6
0
 private void GenerateChecksumPragma(CodeChecksumPragma checksumPragma)
 {
     base.Output.Write("#ExternalChecksum(\"");
     base.Output.Write(checksumPragma.FileName);
     base.Output.Write("\",\"");
     base.Output.Write(checksumPragma.ChecksumAlgorithmId.ToString("B", CultureInfo.InvariantCulture));
     base.Output.Write("\",\"");
     if (checksumPragma.ChecksumData != null)
     {
         foreach (byte num in checksumPragma.ChecksumData)
         {
             base.Output.Write(num.ToString("X2", CultureInfo.InvariantCulture));
         }
     }
     base.Output.WriteLine("\")");
 }
Ejemplo n.º 7
0
		internal void ConstructType ()
		{
			unit = new CodeCompileUnit ();
			byte[] md5checksum = parser.MD5Checksum;

			if (md5checksum != null) {
				CodeChecksumPragma pragma = new CodeChecksumPragma ();
				pragma.FileName = parser.InputFile;
				pragma.ChecksumAlgorithmId = HashMD5;
				pragma.ChecksumData = md5checksum;

				unit.StartDirectives.Add (pragma);
			}

			if (parser.IsPartial) {
				string partialns = null;
				string partialclasstype = parser.PartialClassName;

				int partialdot = partialclasstype.LastIndexOf ('.');
				if (partialdot != -1) {
					partialns = partialclasstype.Substring (0, partialdot);
					partialclasstype = partialclasstype.Substring (partialdot + 1);
				}
				
				CodeNamespace partialNS = new CodeNamespace (partialns);
				partialClass = new CodeTypeDeclaration (partialclasstype);
				partialClass.IsPartial = true;
				partialClassExpr = new CodeTypeReferenceExpression (parser.PartialClassName);
				
				unit.Namespaces.Add (partialNS);
				partialClass.TypeAttributes = TypeAttributes.Public;
				partialNS.Types.Add (partialClass);
			}

			string mainclasstype = parser.ClassName;
			string mainns = DEFAULT_NAMESPACE;
			int maindot = mainclasstype.LastIndexOf ('.');
			if (maindot != -1) {
				mainns = mainclasstype.Substring (0, maindot);
				mainclasstype = mainclasstype.Substring (maindot + 1);
			}

			mainNS = new CodeNamespace (mainns);
			mainClass = new CodeTypeDeclaration (mainclasstype);
			CodeTypeReference baseTypeRef;
			if (partialClass != null) {
				baseTypeRef = new CodeTypeReference (parser.PartialClassName);
				baseTypeRef.Options |= CodeTypeReferenceOptions.GlobalReference;
			} else {
				baseTypeRef = new CodeTypeReference (parser.BaseType.FullName);
				if (parser.BaseTypeIsGlobal)
					baseTypeRef.Options |= CodeTypeReferenceOptions.GlobalReference;
			}
			mainClass.BaseTypes.Add (baseTypeRef);

			mainClassExpr = new CodeTypeReferenceExpression (mainns + "." + mainclasstype);

			unit.Namespaces.Add (mainNS);
			mainClass.TypeAttributes = TypeAttributes.Public;
			mainNS.Types.Add (mainClass);

			foreach (object o in parser.Imports.Keys) {
				if (o is string)
					mainNS.Imports.Add (new CodeNamespaceImport ((string) o));
			}

			// StringCollection.Contains has O(n) complexity, but
			// considering the number of comparisons we make on
			// average and the fact that using an intermediate array
			// would be even more costly, this is fine here.
			StringCollection refAsm = unit.ReferencedAssemblies;
			string asmName;
			if (parser.Assemblies != null) {
				foreach (object o in parser.Assemblies) {
					asmName = o as string;
					if (asmName != null && !refAsm.Contains (asmName))
						refAsm.Add (asmName);
				}
			}

			ArrayList al = WebConfigurationManager.ExtraAssemblies;
			if (al != null && al.Count > 0) {
				foreach (object o in al) {
					asmName = o as string;
					if (asmName != null && !refAsm.Contains (asmName))
						refAsm.Add (asmName);
				}
			}

			IList list = BuildManager.CodeAssemblies;
			if (list != null && list.Count > 0) {
				Assembly asm;
				foreach (object o in list) {
					asm = o as Assembly;
					if (o == null)
						continue;
					asmName = asm.Location;
					if (asmName != null && !refAsm.Contains (asmName))
						refAsm.Add (asmName);
				}
			}

			// Late-bound generators specifics (as for MonoBASIC/VB.NET)
			unit.UserData["RequireVariableDeclaration"] = parser.ExplicitOn;
			unit.UserData["AllowLateBound"] = !parser.StrictOn;

			InitializeType ();
			AddInterfaces ();
			AddClassAttributes ();
			CreateStaticFields ();
			AddApplicationAndSessionObjects ();
			AddScripts ();
			CreateMethods ();
			CreateConstructor (null, null);
		}
 private void GenerateChecksumPragma(CodeChecksumPragma checksumPragma) {
     Output.Write("#pragma checksum \"");
     Output.Write(checksumPragma.FileName);
     Output.Write("\" \"");
     Output.Write(checksumPragma.ChecksumAlgorithmId.ToString("B", CultureInfo.InvariantCulture));
     Output.Write("\" \"");
     if (checksumPragma.ChecksumData != null) {
         foreach(Byte b in checksumPragma.ChecksumData) {
             Output.Write(b.ToString("X2", CultureInfo.InvariantCulture));
         }
     }
     Output.WriteLine("\"");            
 }
Ejemplo n.º 9
0
 private static void ValidateChecksumPragma(CodeChecksumPragma e) {
     if (e.FileName.IndexOfAny(Path.GetInvalidPathChars()) != -1)
         throw new ArgumentException(SR.GetString(SR.InvalidPathCharsInChecksum, e.FileName));
 }
Ejemplo n.º 10
0
		void GenerateCodeChecksumPragma (CodeChecksumPragma pragma)
		{
			Output.Write ("#pragma checksum ");
			Output.Write (QuoteSnippetString (pragma.FileName));
			Output.Write (" \"");
			Output.Write (pragma.ChecksumAlgorithmId.ToString ("B"));
			Output.Write ("\" \"");
			if (pragma.ChecksumData != null) {
				foreach (byte b in pragma.ChecksumData) {
					Output.Write (b.ToString ("X2"));
				}
			}
			Output.WriteLine ("\"");
		}
Ejemplo n.º 11
0
        // Create a CodeDOM graph.
        static void CreateGraph(CodeCompileUnit cu)
        {
            cu.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start,
                                                           "Compile Unit Region"));
            cu.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End,
                                                         string.Empty));
            CodeChecksumPragma pragma1 = new CodeChecksumPragma();

            pragma1.FileName            = "c:\\temp\\test\\OuterLinePragma.txt";
            pragma1.ChecksumAlgorithmId = HashMD5;
            pragma1.ChecksumData        = new byte[] { 0xAA, 0xAA };
            cu.StartDirectives.Add(pragma1);
            CodeChecksumPragma pragma2 = new CodeChecksumPragma("test.txt", HashSHA1, new byte[] { 0xBB, 0xBB, 0xBB });

            cu.StartDirectives.Add(pragma2);

            CodeNamespace ns = new CodeNamespace("Namespace1");

            ns.Imports.Add(new CodeNamespaceImport("System"));
            ns.Imports.Add(new CodeNamespaceImport("System.IO"));
            cu.Namespaces.Add(ns);
            ns.Comments.Add(new CodeCommentStatement("Namespace Comment"));
            CodeTypeDeclaration cd = new CodeTypeDeclaration("Class1");

            ns.Types.Add(cd);

            cd.Comments.Add(new CodeCommentStatement("Outer Type Comment"));
            cd.LinePragma = new CodeLinePragma("c:\\temp\\test\\OuterLinePragma.txt", 300);

            CodeMemberMethod method1 = new CodeMemberMethod();

            method1.Name       = "Method1";
            method1.Attributes = (method1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;


            CodeMemberMethod method2 = new CodeMemberMethod();

            method2.Name       = "Method2";
            method2.Attributes = (method2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            method2.Comments.Add(new CodeCommentStatement("Method 2 Comment"));

            cd.Members.Add(method1);
            cd.Members.Add(method2);

            cd.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start,
                                                           "Outer Type Region"));

            cd.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End,
                                                         string.Empty));

            CodeMemberField field1 = new CodeMemberField(typeof(String), "field1");

            cd.Members.Add(field1);
            field1.Comments.Add(new CodeCommentStatement("Field 1 Comment"));

            CodeRegionDirective codeRegionDirective1 = new CodeRegionDirective(CodeRegionMode.Start,
                                                                               "Field Region");

            field1.StartDirectives.Add(codeRegionDirective1);
            CodeRegionDirective codeRegionDirective2 = new CodeRegionDirective(CodeRegionMode.End,
                                                                               "");

            codeRegionDirective2.RegionMode = CodeRegionMode.End;
            codeRegionDirective2.RegionText = string.Empty;
            field1.EndDirectives.Add(codeRegionDirective2);

            CodeSnippetStatement snippet1 = new CodeSnippetStatement();

            snippet1.Value = "            Console.WriteLine(field1);";

            CodeRegionDirective regionStart = new CodeRegionDirective(CodeRegionMode.End, "");

            regionStart.RegionText = "Snippet Region";
            regionStart.RegionMode = CodeRegionMode.Start;
            snippet1.StartDirectives.Add(regionStart);
            snippet1.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));

            // CodeStatement example
            CodeConstructor constructor1 = new CodeConstructor();

            constructor1.Attributes = (constructor1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            CodeStatement codeAssignStatement1 = new CodeAssignStatement(
                new CodeFieldReferenceExpression(
                    new CodeThisReferenceExpression(),
                    "field1"),
                new CodePrimitiveExpression("value1"));

            codeAssignStatement1.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Statements Region"));
            cd.Members.Add(constructor1);
            codeAssignStatement1.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));
            method2.Statements.Add(codeAssignStatement1);
            method2.Statements.Add(snippet1);
        }
Ejemplo n.º 12
0
 private void GenerateChecksumPragma(CodeChecksumPragma checksumPragma) {
     // the syntax is: #ExternalChecksum("FileName","GuidChecksum","ChecksumValue")
     Output.Write("#ExternalChecksum(\"");
     Output.Write(checksumPragma.FileName);
     Output.Write("\",\"");
     Output.Write(checksumPragma.ChecksumAlgorithmId.ToString("B", CultureInfo.InvariantCulture));
     Output.Write("\",\"");
     if (checksumPragma.ChecksumData != null) {
         foreach(Byte b in checksumPragma.ChecksumData) {
             Output.Write(b.ToString("X2", CultureInfo.InvariantCulture));
         }
     }
     Output.WriteLine("\")");            
 }
Ejemplo n.º 13
0
    // Add a checksum pragma.  This is used for improved debugging experience.
    private void AddChecksumPragma(BuildProvider buildProvider, CodeCompileUnit compileUnit) {

        // If we can't get a virtual path, do nothing
        if (buildProvider == null || buildProvider.VirtualPath == null)
            return;

        // Only do this if we're compiling in debug mode
        if (!_compilerType.CompilerParameters.IncludeDebugInformation)
            return;

        string physicalPath = HostingEnvironment.MapPathInternal(buildProvider.VirtualPath);

        // Only do this is the file physically exists, which it would not in the
        // case of a non-file based VirtualPathProvider.  In such case, there is
        // no point in putting the pragma, since the debugger could not locate
        // the file anyway.
        if (!File.Exists(physicalPath))
            return;

        CodeChecksumPragma pragma = new CodeChecksumPragma() {
            ChecksumAlgorithmId = s_codeChecksumSha1Id
        };

        if (_compConfig.UrlLinePragmas) {
            pragma.FileName = ErrorFormatter.MakeHttpLinePragma(buildProvider.VirtualPathObject.VirtualPathString);
        }
        else {
            pragma.FileName = physicalPath;
        }

        // Generate a SHA1 hash from the contents of the file

        // The VS debugger uses a cryptographic hash of the file being debugged so that it doesn't accidentally
        // display to the user the wrong version of the file. This is merely a convenience feature for debugging
        // purposes and is not security-related in any way. Since VS only supports MD5 and SHA1 hashes, we just
        // use SHA1 and suppress the [Obsolete] warning.
#pragma warning disable 618
        using (Stream stream = new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
            using (SHA1 hashAlgorithm = CryptoAlgorithms.CreateSHA1()) {
                pragma.ChecksumData = hashAlgorithm.ComputeHash(stream);
            }
        }
#pragma warning restore 618

        // Add the pragma to the CodeCompileUnit
        compileUnit.StartDirectives.Add(pragma);
    }
Ejemplo n.º 14
0
        protected virtual CodeChecksumPragma Rewrite(CodeChecksumPragma source, ref bool didRewrite)
        {
            if (source == null)
            {
                return source;
            }

            bool didChildRewrite = false;
            CodeChecksumPragma result = new CodeChecksumPragma();
            result.FileName = source.FileName;
            result.ChecksumAlgorithmId = source.ChecksumAlgorithmId;
            result.ChecksumData = source.ChecksumData;
            this.Rewrite(result.UserData, source.UserData, ref didChildRewrite);
            if (didChildRewrite)
            {
                didRewrite = true;
                return result;
            }
            else
            {
                return source;
            }
        }
 private void AddChecksumPragma(System.Web.Compilation.BuildProvider buildProvider, CodeCompileUnit compileUnit)
 {
     if (((buildProvider != null) && (buildProvider.VirtualPath != null)) && this._compilerType.CompilerParameters.IncludeDebugInformation)
     {
         string path = HostingEnvironment.MapPathInternal(buildProvider.VirtualPath);
         if (File.Exists(path))
         {
             if (!s_hashMD5Guid.HasValue)
             {
                 s_hashMD5Guid = new Guid(0x406ea660, 0x64cf, 0x4c82, 0xb6, 240, 0x42, 0xd4, 0x81, 0x72, 0xa7, 0x99);
             }
             CodeChecksumPragma pragma = new CodeChecksumPragma();
             if (this._compConfig.UrlLinePragmas)
             {
                 pragma.FileName = ErrorFormatter.MakeHttpLinePragma(buildProvider.VirtualPathObject.VirtualPathString);
             }
             else
             {
                 pragma.FileName = path;
             }
             pragma.ChecksumAlgorithmId = s_hashMD5Guid.Value;
             using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
             {
                 pragma.ChecksumData = this.ComputeHash(stream);
             }
             compileUnit.StartDirectives.Add(pragma);
         }
     }
 }
 /// <summary>
 /// Visits a <see cref="CodeChecksumPragma"/>.
 /// </summary>
 /// <param name="codeChecksumPragma">The <see cref="CodeChecksumPragma"/> to visit.</param>
 protected virtual void VisitCodeChecksumPragma(CodeChecksumPragma codeChecksumPragma)
 {
 }