Esempio n. 1
0
        public void Extensions_Safe_Catch2()
        {
            var ex0    = new Ex("ex");
            var ex1    = new Ex1("ex");
            var ex2    = new Ex2("ex");
            var ex3    = new Ex3("ex");
            var baseEx = new BaseEx("ex");
            var subEx  = new SubEx("ex");

            var err1 = Safe.Try <bool, Ex1, Ex2>(() => { throw ex1; });
            var err2 = Safe.Try <bool, Ex1, Ex2>(() => { throw ex2; });

            Assert.IsFalse(err1.HasValue);
            Assert.IsFalse(err2.HasValue);

            Assert.IsInstanceOfType(err1.Match(x => null, ex => ex), typeof(Ex1));
            Assert.IsInstanceOfType(err2.Match(x => null, ex => ex), typeof(Ex2));

            Assert.AreEqual(err1.Match(x => null, ex => ex), ex1);
            Assert.AreEqual(err2.Match(x => null, ex => ex), ex2);

            CustomAssert.Throws <Ex>(() => Safe.Try <bool, Ex2, Ex3>(() => { throw ex0; }));
            CustomAssert.Throws <Ex1>(() => Safe.Try <bool, Ex2, Ex3>(() => { throw ex1; }));

            Safe.Try <bool, BaseEx, Ex2>(() => { throw subEx; });
            Safe.Try <bool, Ex, Ex2>(() => { throw ex0; });
            Safe.Try <bool, Ex, Ex2>(() => { throw ex1; });

            var success = Safe.Try <bool, Ex1, Ex2>(() => true);

            Assert.IsTrue(success.ValueOr(false));
        }
Esempio n. 2
0
        public void Extensions_Safe_Catch5()
        {
            var ex0    = new Ex("ex");
            var ex1    = new Ex1("ex");
            var ex2    = new Ex2("ex");
            var ex3    = new Ex3("ex");
            var ex4    = new Ex4("ex");
            var ex5    = new Ex5("ex");
            var ex6    = new Ex6("ex");
            var baseEx = new BaseEx("ex");
            var subEx  = new SubEx("ex");

            var err1 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex1; });
            var err2 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex2; });
            var err3 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex3; });
            var err4 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex4; });
            var err5 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex5; });

            Assert.IsFalse(err1.HasValue);
            Assert.IsFalse(err2.HasValue);
            Assert.IsFalse(err3.HasValue);
            Assert.IsFalse(err4.HasValue);
            Assert.IsFalse(err5.HasValue);

            Assert.IsInstanceOf(typeof(Ex1), err1.Match(x => null, ex => ex));
            Assert.IsInstanceOf(typeof(Ex2), err2.Match(x => null, ex => ex));
            Assert.IsInstanceOf(typeof(Ex3), err3.Match(x => null, ex => ex));
            Assert.IsInstanceOf(typeof(Ex4), err4.Match(x => null, ex => ex));
            Assert.IsInstanceOf(typeof(Ex5), err5.Match(x => null, ex => ex));

            Assert.AreEqual(err1.Match(x => null, ex => ex), ex1);
            Assert.AreEqual(err2.Match(x => null, ex => ex), ex2);
            Assert.AreEqual(err3.Match(x => null, ex => ex), ex3);
            Assert.AreEqual(err4.Match(x => null, ex => ex), ex4);
            Assert.AreEqual(err5.Match(x => null, ex => ex), ex5);

            CustomAssert.Throws <Ex>(() => Safe.Try <bool, Ex2, Ex3, Ex4, Ex5, Ex6>(() => { throw ex0; }));
            CustomAssert.Throws <Ex1>(() => Safe.Try <bool, Ex2, Ex3, Ex4, Ex5, Ex6>(() => { throw ex1; }));

            Safe.Try <bool, BaseEx, Ex2, Ex3, Ex4, Ex5>(() => { throw subEx; });
            Safe.Try <bool, Ex, Ex2, Ex3, Ex4, Ex5>(() => { throw ex0; });
            Safe.Try <bool, Ex, Ex2, Ex3, Ex4, Ex5>(() => { throw ex1; });

            var success = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => true);

            Assert.IsTrue(success.ValueOr(false));
        }
Esempio n. 3
0
        /// <summary>
        /// Finds and/or compiles all script files and assemblies.
        /// </summary>
        /// <param name="ScriptsForProjectFileName">Path to the current project. May be null, in which case we compile scripts for all projects.</param>
        /// <param name="AdditionalScriptsFolders">Additional script fodlers to look for source files in.</param>
        public void FindAndCompileAllScripts(string ScriptsForProjectFileName, List <string> AdditionalScriptsFolders)
        {
            bool DoCompile = false;

            if (GlobalCommandLine.Compile)
            {
                if (CommandUtils.IsEngineInstalled())
                {
                    CommandUtils.LogWarning("Ignoring -Compile argument because engine is installed.");
                }
                else
                {
                    DoCompile = true;
                }
            }

            // Compile only if not disallowed.
            if (DoCompile && !String.IsNullOrEmpty(CommandUtils.CmdEnv.MsBuildExe))
            {
                FindAndCompileScriptModules(ScriptsForProjectFileName, AdditionalScriptsFolders);
            }

            var ScriptAssemblies = new List <Assembly>();

            LoadPreCompiledScriptAssemblies(ScriptAssemblies);

            // Setup platforms
            Platform.InitializePlatforms(ScriptAssemblies.ToArray());

            // Instantiate all the automation classes for interrogation
            Log.TraceVerbose("Creating commands.");
            ScriptCommands = new Dictionary <string, Type>(StringComparer.InvariantCultureIgnoreCase);
            foreach (var CompiledScripts in ScriptAssemblies)
            {
                try
                {
                    foreach (var ClassType in CompiledScripts.GetTypes())
                    {
                        if (ClassType.IsSubclassOf(typeof(BuildCommand)) && ClassType.IsAbstract == false)
                        {
                            if (ScriptCommands.ContainsKey(ClassType.Name) == false)
                            {
                                ScriptCommands.Add(ClassType.Name, ClassType);
                            }
                            else
                            {
                                bool IsSame = string.Equals(ClassType.AssemblyQualifiedName, ScriptCommands[ClassType.Name].AssemblyQualifiedName);

                                if (IsSame == false)
                                {
                                    Log.TraceWarning("Unable to add command {0} twice. Previous: {1}, Current: {2}", ClassType.Name,
                                                     ClassType.AssemblyQualifiedName, ScriptCommands[ClassType.Name].AssemblyQualifiedName);
                                }
                            }
                        }
                    }
                }
                catch (ReflectionTypeLoadException LoadEx)
                {
                    foreach (var SubEx in LoadEx.LoaderExceptions)
                    {
                        Log.TraceWarning("Got type loader exception: {0}", SubEx.ToString());
                    }
                    throw new AutomationException("Failed to add commands from {0}. {1}", CompiledScripts, LoadEx);
                }
                catch (Exception Ex)
                {
                    throw new AutomationException("Failed to add commands from {0}. {1}", CompiledScripts, Ex);
                }
            }
        }
        /// <summary>
        /// Finds and/or compiles all script files and assemblies.
        /// </summary>
        /// <param name="ScriptsForProjectFileName">Path to the current project. May be null, in which case we compile scripts for all projects.</param>
        /// <param name="AdditionalScriptsFolders">Additional script fodlers to look for source files in.</param>
        public static void FindAndCompileAllScripts(string ScriptsForProjectFileName, List <string> AdditionalScriptsFolders)
        {
            // Find all the project files
            Stopwatch            SearchTimer  = Stopwatch.StartNew();
            List <FileReference> ProjectFiles = FindAutomationProjects(ScriptsForProjectFileName, AdditionalScriptsFolders);

            Log.TraceLog("Found {0} project files in {1:0.000}s", ProjectFiles.Count, SearchTimer.Elapsed.TotalSeconds);

            // Get the default properties for compiling the projects
            Dictionary <string, string> MsBuildProperties = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            MsBuildProperties.Add("Platform", "AnyCPU");
            MsBuildProperties.Add("Configuration", BuildConfig);
            MsBuildProperties.Add("EngineDir", CommandUtils.EngineDirectory.FullName);

            // Read all the projects
            Stopwatch ParsingTimer = Stopwatch.StartNew();

            CsProjectInfo[] Projects = new CsProjectInfo[ProjectFiles.Count];
            Parallel.For(0, ProjectFiles.Count, Idx => Projects[Idx] = CsProjectInfo.Read(ProjectFiles[Idx], MsBuildProperties));
            Log.TraceLog("Parsed project files in {0:0.000}s", ParsingTimer.Elapsed.TotalSeconds);

            // Compile only if not disallowed.
            if (GlobalCommandLine.Compile && !String.IsNullOrEmpty(CommandUtils.CmdEnv.MsBuildExe))
            {
                List <CsProjectInfo> CompileProjects = new List <CsProjectInfo>(Projects);
                if (CommandUtils.IsEngineInstalled())
                {
                    CompileProjects.RemoveAll(x => x.ProjectPath.IsUnderDirectory(CommandUtils.EngineDirectory));
                }
                CompileAutomationProjects(CompileProjects, MsBuildProperties);
            }

            // Get all the build artifacts
            BuildProducts = new HashSet <FileReference>();

            HashSet <DirectoryReference> OutputDirs = new HashSet <DirectoryReference>();

            OutputDirs.Add(DirectoryReference.Combine(CommandUtils.EngineDirectory, "Binaries", "DotNET"));             // Don't want any artifacts from this directory (just AutomationTool.exe and AutomationScripts.dll)

            foreach (CsProjectInfo Project in Projects)
            {
                DirectoryReference OutputDir;
                if (!Project.TryGetOutputDir(out OutputDir))
                {
                    throw new AutomationException("Unable to get output directory for {0}", Project.ProjectPath);
                }

                if (OutputDirs.Add(OutputDir))
                {
                    BuildProducts.UnionWith(DirectoryReference.EnumerateFiles(OutputDir));
                }
            }

            // Load everything
            Stopwatch       LoadTimer  = Stopwatch.StartNew();
            List <Assembly> Assemblies = LoadAutomationAssemblies(Projects);

            Log.TraceLog("Loaded assemblies in {0:0.000}s", LoadTimer.Elapsed.TotalSeconds);

            // Setup platforms
            Platform.InitializePlatforms(Assemblies.ToArray());

            // Instantiate all the automation classes for interrogation
            Log.TraceVerbose("Creating commands.");
            ScriptCommands = new Dictionary <string, Type>(StringComparer.InvariantCultureIgnoreCase);
            foreach (Assembly CompiledScripts in Assemblies)
            {
                try
                {
                    foreach (Type ClassType in CompiledScripts.GetTypes())
                    {
                        if (ClassType.IsSubclassOf(typeof(BuildCommand)) && ClassType.IsAbstract == false)
                        {
                            if (ScriptCommands.ContainsKey(ClassType.Name) == false)
                            {
                                ScriptCommands.Add(ClassType.Name, ClassType);
                            }
                            else
                            {
                                bool IsSame = string.Equals(ClassType.AssemblyQualifiedName, ScriptCommands[ClassType.Name].AssemblyQualifiedName);

                                if (IsSame == false)
                                {
                                    Log.TraceWarning("Unable to add command {0} twice. Previous: {1}, Current: {2}", ClassType.Name,
                                                     ClassType.AssemblyQualifiedName, ScriptCommands[ClassType.Name].AssemblyQualifiedName);
                                }
                            }
                        }
                    }
                }
                catch (ReflectionTypeLoadException LoadEx)
                {
                    foreach (Exception SubEx in LoadEx.LoaderExceptions)
                    {
                        Log.TraceWarning("Got type loader exception: {0}", SubEx.ToString());
                    }
                    throw new AutomationException("Failed to add commands from {0}. {1}", CompiledScripts, LoadEx);
                }
                catch (Exception Ex)
                {
                    throw new AutomationException("Failed to add commands from {0}. {1}", CompiledScripts, Ex);
                }
            }
        }