public void TestCreateWithRequiredServices() { var commandLine = @"goo.cs"; var ws = new AdhocWorkspace(DesktopMefHostServices.DefaultServices); // includes non-portable services too _ = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.CSharp, commandLine, @"C:\ProjectDirectory", ws); }
private ProjectInfo GetCommandLineProject(string projectPath, string projectName, string languageName, string[] args) { var projectDirectory = Path.GetDirectoryName(projectPath); string outputPath; if (languageName == LanguageNames.VisualBasic) { var vbArgs = Microsoft.CodeAnalysis.VisualBasic.VisualBasicCommandLineParser.Default.Parse(args, projectPath, sdkDirectory: null); outputPath = Path.Combine(vbArgs.OutputDirectory, vbArgs.OutputFileName); } else { var csArgs = Microsoft.CodeAnalysis.CSharp.CSharpCommandLineParser.Default.Parse(args, projectPath, sdkDirectory: null); outputPath = Path.Combine(csArgs.OutputDirectory, csArgs.OutputFileName); } var projectInfo = CommandLineProject.CreateProjectInfo( projectName: projectName, language: languageName, commandLineArgs: args, projectDirectory: projectDirectory, workspace: workspace); projectInfo = projectInfo.WithOutputFilePath(outputPath).WithFilePath(projectPath); return(projectInfo); }
public async Task TestAddProject_CommandLineProjectAsync() { CreateFiles(GetSimpleCSharpSolutionFiles()); string commandLine = @"CSharpClass.cs /out:foo.dll /target:library"; var baseDirectory = Path.Combine(this.SolutionDirectory.Path, "CSharpProject"); using (var ws = new AdhocWorkspace()) { var info = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.CSharp, commandLine, baseDirectory); ws.AddProject(info); var project = ws.CurrentSolution.GetProject(info.Id); Assert.Equal("TestProject", project.Name); Assert.Equal("foo", project.AssemblyName); Assert.Equal(OutputKind.DynamicallyLinkedLibrary, project.CompilationOptions.OutputKind); Assert.Equal(1, project.Documents.Count()); var fooDoc = project.Documents.First(d => d.Name == "CSharpClass.cs"); Assert.Equal(0, fooDoc.Folders.Count); var expectedPath = Path.Combine(baseDirectory, "CSharpClass.cs"); Assert.Equal(expectedPath, fooDoc.FilePath); var text = (await fooDoc.GetTextAsync()).ToString(); Assert.NotEqual("", text); var tree = await fooDoc.GetSyntaxRootAsync(); Assert.Equal(false, tree.ContainsDiagnostics); var compilation = await project.GetCompilationAsync(); } }
public void TestLoadProjectFromCommandLine() { string commandLine = @"goo.cs subdir\bar.cs /out:goo.dll /target:library"; var info = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.CSharp, commandLine, @"C:\ProjectDirectory"); var ws = new AdhocWorkspace(); ws.AddProject(info); var project = ws.CurrentSolution.GetProject(info.Id); Assert.Equal("TestProject", project.Name); Assert.Equal("goo", project.AssemblyName); Assert.Equal(OutputKind.DynamicallyLinkedLibrary, project.CompilationOptions.OutputKind); Assert.Equal(2, project.Documents.Count()); var gooDoc = project.Documents.First(d => d.Name == "goo.cs"); Assert.Equal(0, gooDoc.Folders.Count); Assert.Equal(@"C:\ProjectDirectory\goo.cs", gooDoc.FilePath); var barDoc = project.Documents.First(d => d.Name == "bar.cs"); Assert.Equal(1, barDoc.Folders.Count); Assert.Equal("subdir", barDoc.Folders[0]); Assert.Equal(@"C:\ProjectDirectory\subdir\bar.cs", barDoc.FilePath); }
private static Solution CreateSolution( string commandLineArguments, string projectName, string language, string projectSourceFolder, string outputAssemblyPath) { var workspace = CreateWorkspace(); var projectInfo = CommandLineProject.CreateProjectInfo( projectName, language, commandLineArguments, projectSourceFolder, workspace); var solution = workspace.CurrentSolution.AddProject(projectInfo); solution = RemoveNonExistingFiles(solution); solution = AddAssemblyAttributesFile(language, outputAssemblyPath, solution); solution = DisambiguateSameNameLinkedFiles(solution); solution = DeduplicateProjectReferences(solution); solution.Workspace.WorkspaceFailed += WorkspaceFailed; return(solution); }
public void TestCreateWithRequiredServices() { var commandLine = @"goo.cs"; var ws = new AdhocWorkspace(); _ = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.CSharp, commandLine, @"C:\ProjectDirectory", ws); }
public void TestCommandLineProjectWithRelativePathOutsideProjectCone() { var commandLine = @"..\goo.cs"; var info = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.CSharp, commandLine, @"C:\ProjectDirectory"); var docInfo = info.Documents.First(); Assert.Equal(0, docInfo.Folders.Count); Assert.Equal("goo.cs", docInfo.Name); }
public void TestRootedPathInsideProjectCone() { string commandLine = @"c:\ProjectDirectory\goo.cs"; var info = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.CSharp, commandLine, @"C:\ProjectDirectory"); var docInfo = info.Documents.First(); Assert.Equal(0, docInfo.Folders.Count); Assert.Equal("goo.cs", docInfo.Name); }
public void TestDuplicateReferenceInVisualBasicWithVbRuntimeFlag() { var pathToAssembly = typeof(object).Assembly.Location; var quotedPathToAssembly = '"' + pathToAssembly + '"'; var commandLine = $"goo.vb /r:{quotedPathToAssembly} /vbruntime:{quotedPathToAssembly}"; var info = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.VisualBasic, commandLine, baseDirectory: @"C:\ProjectDirectory"); // The compiler may add other references automatically, so we'll only assert a single reference for the one we're interested in Assert.Single(info.MetadataReferences.OfType <PortableExecutableReference>(), r => r.FilePath == pathToAssembly); }
public void TestCreateWithoutRequiredServices() { string commandLine = @"goo.cs"; Assert.Throws <InvalidOperationException>(delegate { var ws = new AdhocWorkspace(new MefHostServices(new ContainerConfiguration().CreateContainer())); // no services var info = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.CSharp, commandLine, @"C:\ProjectDirectory", ws); }); }
public void TestCreateWithoutRequiredServices() { string commandLine = @"foo.cs"; Assert.Throws <InvalidOperationException>(delegate { var ws = new AdhocWorkspace(); // only includes portable services var info = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.CSharp, commandLine, @"C:\ProjectDirectory", ws); }); }
public void TestAdditionalFiles() { string commandLine = @"goo.cs /additionalfile:bar.cs"; var info = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.CSharp, commandLine, @"C:\ProjectDirectory"); var firstDoc = info.Documents.Single(); var secondDoc = info.AdditionalDocuments.Single(); Assert.Equal("goo.cs", firstDoc.Name); Assert.Equal("bar.cs", secondDoc.Name); }
public void TestUnrootedSubPathInsideProjectCone() { string commandLine = @"subdir\foo.cs"; var info = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.CSharp, commandLine, @"C:\ProjectDirectory"); var docInfo = info.Documents.First(); Assert.Equal(1, docInfo.Folders.Count); Assert.Equal("subdir", docInfo.Folders[0]); Assert.Equal("foo.cs", docInfo.Name); }
public void TestUnrootedPathOutsideProjectCone() { string commandLine = @"..\foo.cs"; var ws = new CustomWorkspace(); var info = CommandLineProject.CreateProjectInfo(ws, "TestProject", LanguageNames.CSharp, commandLine, @"C:\ProjectDirectory"); var docInfo = info.Documents.First(); Assert.Equal(0, docInfo.Folders.Count); Assert.Equal("foo.cs", docInfo.Name); }
public void TestAnalyzerReferences() { var pathToAssembly = typeof(object).Assembly.Location; var assemblyBaseDir = Path.GetDirectoryName(pathToAssembly); var relativePath = Path.Combine(".", Path.GetFileName(pathToAssembly)); string commandLine = @"goo.cs /a:" + relativePath; var info = CommandLineProject.CreateProjectInfo("TestProject", LanguageNames.CSharp, commandLine, assemblyBaseDir); var firstDoc = info.Documents.Single(); var analyzerRef = info.AnalyzerReferences.First(); Assert.Equal("goo.cs", firstDoc.Name); Assert.Equal(pathToAssembly, analyzerRef.FullPath); }
private Project CreateProject(AdhocWorkspace workspace) { var projectInfo = CommandLineProject.CreateProjectInfo(CscArgs.OutputFileName, "C#", Environment.CommandLine, _precompilationCommandLineArgs.BaseDirectory, workspace); projectInfo = projectInfo .WithDocuments( projectInfo .Documents .Select(d => Path.GetExtension(d.FilePath) == ".cshtml" ? d.WithTextLoader(new RazorParser(this, d.TextLoader, workspace)) : d)); return(workspace.AddProject(projectInfo)); }
private Project CreateProject(AdhocWorkspace workspace, List <IDocumentExtender> documentExtenders) { var projectInfo = CommandLineProject.CreateProjectInfo(CscArgs.OutputFileName, "C#", Environment.CommandLine, _precompilationCommandLineArgs.BaseDirectory, workspace); projectInfo = projectInfo .WithCompilationOptions(CscArgs.CompilationOptions .WithSourceReferenceResolver(new SourceFileResolver(CscArgs.SourcePaths, CscArgs.BaseDirectory, CscArgs.PathMap))) // required for path mapping support .WithDocuments( projectInfo .Documents .Select(d => documentExtenders.Aggregate(d, (doc, ex) => ex.Extend(doc)))); return(workspace.AddProject(projectInfo)); }
public Project OpenCommandLineProject(string responseFile, string language) { // This line deserves better error handling, but the tools current model is just throwing exception for most errors. // Issue: #90 string rspContents = File.ReadAllText(responseFile); var projectInfo = CommandLineProject.CreateProjectInfo( projectName: Path.GetFileNameWithoutExtension(responseFile), language: language, commandLine: rspContents, baseDirectory: Path.GetDirectoryName(Path.GetFullPath(responseFile)), workspace: this); this.OnProjectAdded(projectInfo); return(this.CurrentSolution.GetProject(projectInfo.Id)); }
public async Task TestAddProject_CommandLineProjectAsync() { using var tempRoot = new TempRoot(); var tempDirectory = tempRoot.CreateDirectory(); var tempFile = tempDirectory.CreateFile("CSharpClass.cs"); tempFile.WriteAllText("class CSharpClass { }"); using var ws = new AdhocWorkspace(); var commandLine = @"CSharpClass.cs /out:goo.dll /target:library"; var info = CommandLineProject.CreateProjectInfo( "TestProject", LanguageNames.CSharp, commandLine, tempDirectory.Path, ws ); ws.AddProject(info); var project = ws.CurrentSolution.GetProject(info.Id); Assert.Equal("TestProject", project.Name); Assert.Equal("goo", project.AssemblyName); Assert.Equal( OutputKind.DynamicallyLinkedLibrary, project.CompilationOptions.OutputKind ); Assert.Equal(1, project.Documents.Count()); var gooDoc = project.Documents.First(d => d.Name == "CSharpClass.cs"); Assert.Equal(0, gooDoc.Folders.Count); Assert.Equal(tempFile.Path, gooDoc.FilePath); var text = (await gooDoc.GetTextAsync()).ToString(); Assert.Equal(tempFile.ReadAllText(), text); var tree = await gooDoc.GetSyntaxRootAsync(); Assert.False(tree.ContainsDiagnostics); var compilation = await project.GetCompilationAsync(); }
private Project CreateProject(AdhocWorkspace workspace, RazorParser razorParser) { var projectInfo = CommandLineProject.CreateProjectInfo(CscArgs.OutputFileName, "C#", Environment.CommandLine, _precompilationCommandLineArgs.BaseDirectory, workspace); projectInfo = projectInfo .WithCompilationOptions(CscArgs.CompilationOptions .WithSourceReferenceResolver(new SourceFileResolver(CscArgs.SourcePaths, CscArgs.BaseDirectory, CscArgs.PathMap))) // required for path mapping support .WithDocuments( projectInfo .Documents .Select(d => Path.GetExtension(d.FilePath) == ".cshtml" ? razorParser.Wrap(d) : d)); //projectInfprojectInfo.WithCompilationOptions(CscArgs.CompilationOptions.WithSourceReferenceResolver(new SourceFileResolver(CscArgs.SourcePaths, CscArgs.BaseDirectory, CscArgs.PathMap))); //Console.WriteLine(projectInfo.CompilationOptions.SourceReferenceResolver.NormalizePath()); return workspace.AddProject(projectInfo); }
static void Main(string[] args) { var workspace = new AdhocWorkspace(DesktopMefHostServices.DefaultServices); var projectInfo = CommandLineProject.CreateProjectInfo("cs2ts", LanguageNames.CSharp, args, Environment.CurrentDirectory, workspace); var project = workspace.AddProject(projectInfo); var compilation = (CSharpCompilation)project.GetCompilationAsync().GetAwaiter().GetResult(); var result = TypeScriptTranslator.Translate(compilation); if (result.Diagnostics.Count > 0) { foreach (var dx in result.Diagnostics) { Console.Out.WriteLine(dx); } } else { Console.Out.Write(result.Text); } }
/// <summary> /// Creates the project info from the msBuild project /// </summary> /// <param name="msBuildProj">The ms Build project</param> /// <returns>The project info</returns> protected override ProjectInfo CreateProjectInfo(MsBuild.Project msBuildProj) { AnalyzerManager mgr = new AnalyzerManager(); ProjectAnalyzer projAnalyzer = mgr.GetProject(ProjectPath); AdhocWorkspace workspace = projAnalyzer.GetWorkspace(true); // This doesn't seems to work either way Project proj = workspace.CurrentSolution.Projects.First(); StringBuilder builder = new StringBuilder(); IEnumerable <MsBuild.ProjectItem> compile = msBuildProj.Items.Where(itm => itm.ItemType == "Compile"); foreach (MsBuild.ProjectItem item in compile) { builder.Append($"{item.EvaluatedInclude} "); } // /out provides a name builder.Append($"/out:{new FileInfo(msBuildProj.FullPath).Name} "); // target: TODO: What does this do? builder.Append("/target:library "); ProjectInfo info = CommandLineProject.CreateProjectInfo( proj.Name, LanguageNames.CSharp, CommandLineParser.SplitCommandLineIntoArguments(builder.ToString(), true), msBuildProj.DirectoryPath, null); // Add the references found by buildalyzer info = info.WithFilePath(msBuildProj.FullPath) .WithMetadataReferences(proj.MetadataReferences) .WithAnalyzerReferences(proj.AnalyzerReferences); return(info); }