Пример #1
0
        public static ProjectContext ProjectContext(string projectPath)
        {
            if (string.IsNullOrEmpty(projectPath))
            {
                throw new ArgumentException($"{nameof(projectPath)} is required.");
            }

            // Check for uri paths
            if (projectPath.StartsWith("file:"))
            {
                projectPath = new Uri(projectPath).LocalPath;
            }

            // Search up the folders from the path provided and find a project.json
            var foundProjectJsonPath = "";
            var foundProjectJsonFile = "";
            var curDirectory         = new DirectoryInfo(projectPath);
            var rootDirectory        = curDirectory.Root.FullName;

            while (curDirectory.FullName != rootDirectory)
            {
                var files = curDirectory.EnumerateFiles("*.csproj", SearchOption.TopDirectoryOnly);
                if (files.Count() == 1)
                {
                    foundProjectJsonPath = curDirectory.FullName;
                    foundProjectJsonFile = files.Single().FullName;
                    break;
                }
                curDirectory = curDirectory.Parent;
            }
            if (string.IsNullOrEmpty(foundProjectJsonPath))
            {
                throw new ArgumentException("Project path not found.");
            }

            var configuration = "Debug";

#if RELEASE
            configuration = "Release";
#endif

            var    tempFile   = Path.GetTempFileName();
            var    assembly   = Assembly.GetExecutingAssembly();
            string sourceFile = assembly.GetName().Name + ".Microsoft.VisualStudio.Web.CodeGeneration.Tools.targets";
            var    stream     = assembly.GetManifestResourceStream(sourceFile);
            var    output     = File.OpenWrite(tempFile);
            stream.CopyTo(output);
            output.Close();

            var context = MsBuildProjectContextBuilder.Build(
                foundProjectJsonPath,
                foundProjectJsonFile,
                tempFile,
                configuration);

            File.Delete(tempFile);

            return(context);
        }
Пример #2
0
        protected async Task <CSharpCompilation> GetCompilationAsync(string projectPath)
        {
            Out.WriteLine("Analyzing project...");
            var stopwatch = Stopwatch.StartNew();

            var obj = Path.Combine(Path.GetDirectoryName(projectPath), "obj");

            Directory.CreateDirectory(obj);

            var inject = Path.Combine(obj, $"{Path.GetFileName(projectPath)}.InjectSupR.targets");

            try
            {
                if (!File.Exists(inject))
                {
                    using (var stream = typeof(Program).Assembly.GetManifestResourceStream("SuperScaffolding.InjectSupR.targets"))
                    {
                        using (var file = File.OpenWrite(inject))
                        {
                            stream.CopyTo(file);
                        }
                    }
                }

                var builder = new MsBuildProjectContextBuilder(projectPath, Path.GetDirectoryName(typeof(Program).Assembly.Location));
                var context = builder.Build();

                var workspace   = new RoslynWorkspace(context);
                var project     = workspace.CurrentSolution.Projects.Single();
                var compilation = (CSharpCompilation)await project.GetCompilationAsync().ConfigureAwait(false);

                Out.WriteLine("Created compilation in: {0}ms", stopwatch.ElapsedMilliseconds);
                Out.WriteLine();

                return(compilation);
            }
            finally
            {
                try
                {
                    File.Delete(inject);
                }
                catch
                {
                }
            }
        }
Пример #3
0
        private async Task <int> Execute()
        {
            var projectPath = Project.HasValue() ? Project.Value() : Directory.GetCurrentDirectory();

            if (Directory.Exists(projectPath))
            {
                projectPath = Path.Combine(Path.GetFullPath(projectPath), Path.GetFileName(projectPath) + ".csproj");
            }

            if (!File.Exists(projectPath))
            {
                Console.WriteLine($"Project file {projectPath} not found.");
                return(1);
            }

            Console.WriteLine("Analyzing project...");
            var builder = new MsBuildProjectContextBuilder(projectPath, Program.TargetDirectory);
            var context = builder.Build();

            var workspace   = new RoslynWorkspace(context);
            var project     = workspace.CurrentSolution.Projects.Single();
            var compilation = await project.GetCompilationAsync().ConfigureAwait(false);

            var startup = await StartupAnalysis.AnalyzeCompilation(compilation).ConfigureAwait(false);

            Console.WriteLine("Found services:");
            foreach (var service in startup.Services)
            {
                Console.WriteLine($"{service}");
            }
            Console.WriteLine();

            Console.WriteLine("Found middleware:");
            foreach (var middleware in startup.Middleware)
            {
                Console.WriteLine($"{middleware.ToString()}");
            }
            Console.WriteLine();
            return(0);
        }