Esempio n. 1
0
        public void Run_RunsMethod()
        {
            // Arrange
            string solutionDir      = Path.GetFullPath(typeof(MethodRunnerIntegrationTests).GetTypeInfo().Assembly.Location + "../../../../../../..");
            string projectDir       = "StubProject.OlderFramework";
            string projectName      = projectDir;
            string projectAbsSrcDir = $"{solutionDir}/test/{projectDir}";
            string outputDir        = $"{projectAbsSrcDir}/bin/debug/netcoreapp1.0";
            string assemblyFilePath = $"{outputDir}/{projectName}.dll";
            string entryClassName   = $"{projectName}.EntryStubClass";
            int    testExitCode     = 10; // Arbitrary

            string[] stubArgs = new string[] { testExitCode.ToString() };

            DirectoryAssemblyLoadContext dalc = new DirectoryAssemblyLoadContext(outputDir);
            Assembly assembly = dalc.LoadFromAssemblyPath(assemblyFilePath);

            IServiceCollection services = new ServiceCollection();

            services.AddProjectHost();
            IServiceProvider serviceProvider = services.BuildServiceProvider();
            IMethodRunner    runner          = serviceProvider.GetService <IMethodRunner>();

            // Act
            int result = runner.Run(assembly, entryClassName, args: stubArgs);

            // Assert
            Assert.Equal(testExitCode, result);
            (serviceProvider as IDisposable).Dispose();
        }
Esempio n. 2
0
        /// <summary>
        ///     Execute the task.
        /// </summary>
        /// <returns>
        ///     <c>true</c>, if the task executed succesfully; otherwise, <c>false</c>.
        /// </returns>
        public override bool Execute()
        {
            FileInfo moduleAssemblyFile = new FileInfo(
                ModuleAssembly.GetMetadata("FullPath")
                );

            Log.LogMessage(MessageImportance.Low, "Scanning assembly '{0}'...",
                           moduleAssemblyFile.FullName
                           );

            DirectoryAssemblyLoadContext assemblyLoadContext = new DirectoryAssemblyLoadContext(
                fallbackDirectory: moduleAssemblyFile.Directory.FullName
                );

            HelpItems     help      = new HelpItems();
            MamlGenerator generator = new MamlGenerator();

            Assembly moduleAssembly = assemblyLoadContext.LoadFromAssemblyPath(moduleAssemblyFile.FullName);

            foreach (Type cmdletType in Reflector.GetCmdletTypes(moduleAssembly))
            {
                CmdletAttribute cmdletAttribute = cmdletType.GetTypeInfo().GetCustomAttribute <CmdletAttribute>();

                Log.LogMessage(MessageImportance.Low, "Generating help for cmdlet '{0}-{1}' ('{2}').",
                               cmdletAttribute.VerbName,
                               cmdletAttribute.NounName,
                               cmdletType.FullName
                               );

                help.Commands.Add(
                    generator.Generate(cmdletType)
                    );
            }

            FileInfo helpFile = new FileInfo(
                HelpFile.GetMetadata("FullPath")
                );

            if (helpFile.Exists)
            {
                helpFile.Delete();
            }

            using (StreamWriter writer = helpFile.CreateText())
            {
                help.WriteTo(writer);
            }

            Log.LogMessage(MessageImportance.Normal, "'{0}' -> '{1}'",
                           moduleAssemblyFile.Name,
                           helpFile.Name
                           );

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        ///     The main program entry-point.
        /// </summary>
        /// <param name="args">
        ///     Command-line arguments.
        /// </param>
        static void Main(string[] args)
        {
            if (args.Length < 2 || args.Length > 3 || args[0] != "gen-help")
            {
                Console.WriteLine("Usage:\n\tdotnet reptile gen-help <Module.dll> [Module.dll-Help.xml]");

                return;
            }

            try
            {
                string modulePath = Path.GetFullPath(args[1]);

                // If the module's dependencies are not available from
                // the usual places (e.g. dotnet-reptile's base directory)
                // then load them from the module directory.
                DirectoryAssemblyLoadContext loadContext = new DirectoryAssemblyLoadContext(
                    Path.GetDirectoryName(modulePath)
                    );
                Assembly  moduleAssembly = loadContext.LoadFromAssemblyPath(modulePath);
                HelpItems help           = new MamlGenerator().Generate(moduleAssembly);

                FileInfo helpFile = new FileInfo(
                    fileName: args.Length == 3 ? Path.GetFullPath(args[2]) : modulePath + "-Help.xml"
                    );
                if (helpFile.Exists)
                {
                    helpFile.Delete();
                }

                using (StreamWriter writer = helpFile.CreateText())
                {
                    help.WriteTo(writer);
                }

                Console.WriteLine($"Generated '{helpFile.FullName}'.");
            }
            catch (ReflectionTypeLoadException typeLoadError)
            {
                Console.WriteLine(typeLoadError);
                Console.WriteLine(
                    new String('=', 80)
                    );
                foreach (Exception loaderException in typeLoadError.LoaderExceptions)
                {
                    Console.WriteLine(loaderException);
                }
            }
            catch (Exception unexpectedError)
            {
                Console.WriteLine(unexpectedError);
            }
        }
        public void DirectoryAssemblyLoadContext_ResolvesImplicitAndExplicitLoads()
        {
            // Arrange
            string solutionDir = Path.GetFullPath(typeof(DirectoryAssemblyLoadContextIntegrationTests).GetTypeInfo().Assembly.Location + "../../../../../../..");
            string projectDir  = "StubProject.Referencer";
            string publishDir  = $"{solutionDir}/test/{projectDir}/bin/debug/netstandard1.3/publish";

            DirectoryAssemblyLoadContext dalc = new DirectoryAssemblyLoadContext(publishDir);

            // Act
            Assembly referencer                 = dalc.LoadFromAssemblyName(new AssemblyName("StubProject.Referencer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"));
            Type     referencerStubType         = referencer.GetTypes().First();
            object   referencerStubTypeInstance = Activator.CreateInstance(referencerStubType);

            // Assert
            // Ensure that StubProject.Referencee.dll was loaded implicitly (dotnetcore1.1 does not expose any methods to explicitly check if an assembly
            // is loaded)
            Assert.NotNull(referencerStubTypeInstance);
        }