本文整理汇总了C#中Microsoft.DotNet.ProjectModel.ProjectContext.CreateExporter方法的典型用法代码示例。如果您正苦于以下问题:C# ProjectContext.CreateExporter方法的具体用法?C# ProjectContext.CreateExporter怎么用?C# ProjectContext.CreateExporter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.DotNet.ProjectModel.ProjectContext
的用法示例。
在下文中一共展示了ProjectContext.CreateExporter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetRuntimeDependencies
private static IEnumerable<string> GetRuntimeDependencies(ProjectContext projectContext, string buildConfiguration)
{
// We collect the full list of runtime dependencies here and pass them back so they can be
// referenced by the REPL environment when seeding the context. It appears that we need to
// explicitly list the dependencies as they may not exist in the output directory (as is the
// for library projects) or they may not exist anywhere on the path (e.g. they may only exist
// in the nuget package that was downloaded for the compilation) or they may be specific to a
// specific target framework.
var runtimeDependencies = new HashSet<string>();
var projectExporter = projectContext.CreateExporter(buildConfiguration);
var projectDependencies = projectExporter.GetDependencies();
foreach (var projectDependency in projectDependencies)
{
var runtimeAssemblies = projectDependency.RuntimeAssemblies;
foreach (var runtimeAssembly in runtimeAssemblies)
{
var runtimeAssemblyPath = runtimeAssembly.ResolvedPath;
runtimeDependencies.Add(runtimeAssemblyPath);
}
}
return runtimeDependencies;
}
示例2: AddProject
private ProjectId AddProject(ProjectContext project)
{
// Create the framework specific project and add it to the workspace
var projectInfo = ProjectInfo.Create(
ProjectId.CreateNewId(),
VersionStamp.Create(),
project.ProjectFile.Name + "+" + project.TargetFramework,
project.ProjectFile.Name,
LanguageNames.CSharp,
project.ProjectFile.ProjectFilePath);
OnProjectAdded(projectInfo);
// TODO: ctor argument?
var configuration = "Debug";
var compilationOptions = project.ProjectFile.GetCompilerOptions(project.TargetFramework, configuration);
var compilationSettings = ToCompilationSettings(compilationOptions, project.TargetFramework, project.ProjectFile.ProjectDirectory);
OnParseOptionsChanged(projectInfo.Id, new CSharpParseOptions(compilationSettings.LanguageVersion, preprocessorSymbols: compilationSettings.Defines));
OnCompilationOptionsChanged(projectInfo.Id, compilationSettings.CompilationOptions);
foreach (var file in project.ProjectFile.Files.SourceFiles)
{
AddSourceFile(projectInfo, file);
}
var exporter = project.CreateExporter(configuration);
foreach (var dependency in exporter.GetDependencies())
{
var projectDependency = dependency.Library as ProjectDescription;
if (projectDependency != null)
{
var projectDependencyContext = ProjectContext.Create(projectDependency.Project.ProjectFilePath, projectDependency.Framework);
var id = AddProject(projectDependencyContext);
OnProjectReferenceAdded(projectInfo.Id, new ProjectReference(id));
}
else
{
foreach (var asset in dependency.CompilationAssemblies)
{
OnMetadataReferenceAdded(projectInfo.Id, GetMetadataReference(asset.ResolvedPath));
}
}
foreach (var file in dependency.SourceReferences)
{
AddSourceFile(projectInfo, file);
}
}
return projectInfo.Id;
}
示例3: GetProjectDependencies
// todo make extension of ProjectContext?
private static List<LibraryExport> GetProjectDependencies(ProjectContext projectContext, string configuration)
{
// Create the library exporter
var exporter = projectContext.CreateExporter(configuration);
// Gather exports for the project
var dependencies = exporter.GetDependencies().ToList();
return dependencies;
}
示例4: CompileProject
private static bool CompileProject(ProjectContext context, CompilerCommandApp args)
{
// Set up Output Paths
string outputPath = context.GetOutputPath(args.ConfigValue, args.OutputValue);
string intermediateOutputPath = context.GetIntermediateOutputPath(args.ConfigValue, args.IntermediateValue, outputPath);
Directory.CreateDirectory(outputPath);
Directory.CreateDirectory(intermediateOutputPath);
// Create the library exporter
var exporter = context.CreateExporter(args.ConfigValue);
// Gather exports for the project
var dependencies = exporter.GetDependencies().ToList();
Reporter.Output.WriteLine($"Compiling {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}");
var sw = Stopwatch.StartNew();
var diagnostics = new List<DiagnosticMessage>();
var missingFrameworkDiagnostics = new List<DiagnosticMessage>();
// Collect dependency diagnostics
foreach (var diag in context.LibraryManager.GetAllDiagnostics())
{
if (diag.ErrorCode == ErrorCodes.DOTNET1011 ||
diag.ErrorCode == ErrorCodes.DOTNET1012)
{
missingFrameworkDiagnostics.Add(diag);
}
diagnostics.Add(diag);
}
if (missingFrameworkDiagnostics.Count > 0)
{
// The framework isn't installed so we should short circuit the rest of the compilation
// so we don't get flooded with errors
PrintSummary(missingFrameworkDiagnostics, sw);
return false;
}
// Get compilation options
var outputName = GetProjectOutput(context.ProjectFile, context.TargetFramework, args.ConfigValue, outputPath);
// Assemble args
var compilerArgs = new List<string>()
{
$"--temp-output:{intermediateOutputPath}",
$"--out:{outputName}"
};
var compilationOptions = context.ProjectFile.GetCompilerOptions(context.TargetFramework, args.ConfigValue);
// Path to strong naming key in environment variable overrides path in project.json
var environmentKeyFile = Environment.GetEnvironmentVariable(EnvironmentNames.StrongNameKeyFile);
if (!string.IsNullOrWhiteSpace(environmentKeyFile))
{
compilationOptions.KeyFile = environmentKeyFile;
}
else if (!string.IsNullOrWhiteSpace(compilationOptions.KeyFile))
{
// Resolve full path to key file
compilationOptions.KeyFile = Path.GetFullPath(Path.Combine(context.ProjectFile.ProjectDirectory, compilationOptions.KeyFile));
}
var references = new List<string>();
// Add compilation options to the args
compilerArgs.AddRange(compilationOptions.SerializeToArgs());
// Add metadata options
compilerArgs.AddRange(AssemblyInfoOptions.SerializeToArgs(AssemblyInfoOptions.CreateForProject(context)));
foreach (var dependency in dependencies)
{
var projectDependency = dependency.Library as ProjectDescription;
if (projectDependency != null)
{
if (projectDependency.Project.Files.SourceFiles.Any())
{
var projectOutputPath = GetProjectOutput(projectDependency.Project, projectDependency.Framework, args.ConfigValue, outputPath);
references.Add(projectOutputPath);
}
}
else
{
references.AddRange(dependency.CompilationAssemblies.Select(r => r.ResolvedPath));
}
compilerArgs.AddRange(dependency.SourceReferences);
}
compilerArgs.AddRange(references.Select(r => $"--reference:{r}"));
if (compilationOptions.PreserveCompilationContext == true)
{
var dependencyContext = DependencyContextBuilder.Build(compilationOptions,
exporter,
//.........这里部分代码省略.........
示例5: Compile
public override bool Compile(ProjectContext context, BuildCommandApp args)
{
// Set up Output Paths
var outputPaths = context.GetOutputPaths(args.ConfigValue, args.BuildBasePathValue);
var outputPath = outputPaths.CompilationOutputPath;
var intermediateOutputPath = outputPaths.IntermediateOutputDirectoryPath;
Directory.CreateDirectory(outputPath);
Directory.CreateDirectory(intermediateOutputPath);
// Create the library exporter
var exporter = context.CreateExporter(args.ConfigValue, args.BuildBasePathValue);
// Gather exports for the project
var dependencies = exporter.GetDependencies().ToList();
Reporter.Output.WriteLine($"Compiling {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}");
var sw = Stopwatch.StartNew();
var diagnostics = new List<DiagnosticMessage>();
var missingFrameworkDiagnostics = new List<DiagnosticMessage>();
// Collect dependency diagnostics
foreach (var diag in context.LibraryManager.GetAllDiagnostics())
{
if (diag.ErrorCode == ErrorCodes.DOTNET1011 ||
diag.ErrorCode == ErrorCodes.DOTNET1012)
{
missingFrameworkDiagnostics.Add(diag);
}
diagnostics.Add(diag);
}
if(diagnostics.Any(d => d.Severity == DiagnosticMessageSeverity.Error))
{
// We got an unresolved dependency or missing framework. Don't continue the compilation.
PrintSummary(diagnostics, sw);
return false;
}
// Get compilation options
var outputName = outputPaths.CompilationFiles.Assembly;
// Assemble args
var compilerArgs = new List<string>()
{
$"--temp-output:{intermediateOutputPath}",
$"--out:{outputName}"
};
var compilationOptions = context.ResolveCompilationOptions(args.ConfigValue);
// Set default platform if it isn't already set and we're on desktop
if (compilationOptions.EmitEntryPoint == true && string.IsNullOrEmpty(compilationOptions.Platform) && context.TargetFramework.IsDesktop())
{
// See https://github.com/dotnet/cli/issues/2428 for more details.
compilationOptions.Platform = RuntimeInformation.ProcessArchitecture == Architecture.X64 ?
"x64" : "anycpu32bitpreferred";
}
var languageId = CompilerUtil.ResolveLanguageId(context);
var references = new List<string>();
// Add compilation options to the args
compilerArgs.AddRange(compilationOptions.SerializeToArgs());
// Add metadata options
compilerArgs.AddRange(AssemblyInfoOptions.SerializeToArgs(AssemblyInfoOptions.CreateForProject(context)));
foreach (var dependency in dependencies)
{
references.AddRange(dependency.CompilationAssemblies.Select(r => r.ResolvedPath));
compilerArgs.AddRange(dependency.SourceReferences.Select(s => s.GetTransformedFile(intermediateOutputPath)));
foreach (var resourceFile in dependency.EmbeddedResources)
{
var transformedResource = resourceFile.GetTransformedFile(intermediateOutputPath);
var resourceName = ResourceManifestName.CreateManifestName(
Path.GetFileName(resourceFile.ResolvedPath), compilationOptions.OutputName);
compilerArgs.Add($"--resource:\"{transformedResource}\",{resourceName}");
}
// Add analyzer references
compilerArgs.AddRange(dependency.AnalyzerReferences
.Where(a => a.AnalyzerLanguage == languageId)
.Select(a => $"--analyzer:{a.AssemblyPath}"));
}
compilerArgs.AddRange(references.Select(r => $"--reference:{r}"));
if (compilationOptions.PreserveCompilationContext == true)
{
var allExports = exporter.GetAllExports().ToList();
var exportsLookup = allExports.ToDictionary(e => e.Library.Identity.Name);
var buildExclusionList = context.GetTypeBuildExclusionList(exportsLookup);
var filteredExports = allExports
.Where(e => e.Library.Identity.Type.Equals(LibraryType.ReferenceAssembly) ||
//.........这里部分代码省略.........
示例6: PublishProjectContext
/// <summary>
/// Publish the project for given 'framework (ex - dnxcore50)' and 'runtimeID (ex - win7-x64)'
/// </summary>
/// <param name="context">project that is to be published</param>
/// <param name="baseOutputPath">Location of published files</param>
/// <param name="configuration">Debug or Release</param>
/// <param name="nativeSubdirectories"></param>
/// <returns>Return 0 if successful else return non-zero</returns>
private bool PublishProjectContext(ProjectContext context, string buildBasePath, string outputPath, string configuration, bool nativeSubdirectories)
{
Reporter.Output.WriteLine($"Publishing {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}/{context.RuntimeIdentifier.Yellow()}");
var options = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
var outputPaths = context.GetOutputPaths(configuration, buildBasePath, outputPath);
if (string.IsNullOrEmpty(outputPath))
{
outputPath = Path.Combine(outputPaths.RuntimeOutputPath, PublishSubfolderName);
}
var contextVariables = new Dictionary<string, string>
{
{ "publish:ProjectPath", context.ProjectDirectory },
{ "publish:Configuration", configuration },
{ "publish:OutputPath", outputPath },
{ "publish:TargetFramework", context.TargetFramework.GetShortFolderName() },
{ "publish:FullTargetFramework", context.TargetFramework.DotNetFrameworkName },
{ "publish:Runtime", context.RuntimeIdentifier },
};
RunScripts(context, ScriptNames.PrePublish, contextVariables);
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
// Compile the project (and transitively, all it's dependencies)
var args = new List<string>() {
"--framework",
$"{context.TargetFramework.DotNetFrameworkName}",
"--runtime",
context.RuntimeIdentifier,
"--configuration",
configuration,
context.ProjectFile.ProjectDirectory
};
if (!string.IsNullOrEmpty(VersionSuffix))
{
args.Add("--version-suffix");
args.Add(VersionSuffix);
}
if (!string.IsNullOrEmpty(buildBasePath))
{
args.Add("--build-base-path");
args.Add(buildBasePath);
}
var result = Build.BuildCommand.Run(args.ToArray());
if (result != 0)
{
return false;
}
// Use a library exporter to collect publish assets
var exporter = context.CreateExporter(configuration);
foreach (var export in exporter.GetAllExports())
{
Reporter.Verbose.WriteLine($"Publishing {export.Library.Identity.ToString().Green().Bold()} ...");
PublishFiles(export.RuntimeAssemblies, outputPath, nativeSubdirectories: false);
PublishFiles(export.NativeLibraries, outputPath, nativeSubdirectories);
export.RuntimeAssets.StructuredCopyTo(outputPath, outputPaths.IntermediateOutputDirectoryPath);
if (options.PreserveCompilationContext.GetValueOrDefault())
{
PublishRefs(export, outputPath);
}
}
var contentFiles = new ContentFiles(context);
contentFiles.StructuredCopyTo(outputPath);
// Publish a host if this is an application
if (options.EmitEntryPoint.GetValueOrDefault())
{
Reporter.Verbose.WriteLine($"Making {context.ProjectFile.Name.Cyan()} runnable ...");
PublishHost(context, outputPath);
}
RunScripts(context, ScriptNames.PostPublish, contextVariables);
Reporter.Output.WriteLine($"Published to {outputPath}".Green().Bold());
return true;
}
示例7: LoadProject
internal Assembly LoadProject(ProjectContext context)
{
var outputPaths = context.GetOutputPaths(Configuration);
var assemblyPath = outputPaths.CompilationFiles.Assembly;
var assembly = LoadFromAssemblyPath(assemblyPath);
PopulateProbingFolder(assemblyPath);
var assemblyFolderPath = outputPaths.CompilationOutputPath;
var libraryExporter = context.CreateExporter(Configuration);
foreach (var dependency in libraryExporter.GetAllExports())
{
var library = dependency.Library as ProjectDescription;
var package = dependency.Library as PackageDescription;
// Check for an unresolved library
if (library != null && !library.Resolved)
{
if (!IsAmbientAssembly(library.Identity.Name))
{
var assetFileName = GetAssemblyFileName(library.Identity.Name);
var assetResolvedPath = ResolveAssemblyPath(assemblyFolderPath, assetFileName);
if (!String.IsNullOrEmpty(assetResolvedPath))
{
if (!IsAssemblyLoaded(library.Identity.Name))
{
LoadFromAssemblyPath(assetResolvedPath);
}
PopulateBinaryFolder(assemblyFolderPath, assetResolvedPath);
PopulateProbingFolder(assetResolvedPath);
var resourceFileName = library.Identity.Name + ".resources.dll";
var assemblyFolderName = PathUtility.GetDirectoryName(assemblyFolderPath);
var resourceAssemblies = Directory.GetFiles(assemblyFolderPath, resourceFileName, SearchOption.AllDirectories)
.Union(Directory.GetFiles(_probingFolderPath, resourceFileName, SearchOption.AllDirectories));
foreach (var asset in resourceAssemblies)
{
var locale = Directory.GetParent(asset).Name
.Replace(assemblyFolderName, String.Empty)
.Replace(ProbingDirectoryName, String.Empty);
PopulateBinaryFolder(assemblyFolderPath, asset, locale);
PopulateProbingFolder(asset, locale);
}
}
}
}
// Check for an unresolved package
else if (package != null && !package.Resolved)
{
foreach (var asset in package.RuntimeAssemblies)
{
var assetName = Path.GetFileNameWithoutExtension(asset.Path);
if (!IsAmbientAssembly(assetName))
{
var assetFileName = Path.GetFileName(asset.Path);
var assetResolvedPath = ResolveAssemblyPath(assemblyFolderPath, assetFileName);
if (!String.IsNullOrEmpty(assetResolvedPath))
{
if (!IsAssemblyLoaded(assetName))
{
LoadFromAssemblyPath(assetResolvedPath);
}
PopulateBinaryFolder(assemblyFolderPath, assetResolvedPath);
PopulateProbingFolder(assetResolvedPath);
}
}
}
if (!IsAmbientAssembly(package.Identity.Name))
{
foreach (var asset in package.ResourceAssemblies)
{
if (asset.Properties.ContainsKey("locale"))
{
var locale = asset.Properties["locale"];
var assetFileName = Path.GetFileName(asset.Path);
var assetResolvedPath = ResolveAssemblyPath(assemblyFolderPath, assetFileName, locale);
if (!String.IsNullOrEmpty(assetResolvedPath))
{
PopulateBinaryFolder(assemblyFolderPath, assetResolvedPath, locale);
PopulateProbingFolder(assetResolvedPath, locale);
}
}
}
}
}
// Check for a precompiled library
else if (library != null && !dependency.RuntimeAssemblyGroups.Any())
{
if (!IsAmbientAssembly(library.Identity.Name))
//.........这里部分代码省略.........
示例8: Compile
public override bool Compile(ProjectContext context, CompilerCommandApp args)
{
// Set up Output Paths
var outputPaths = context.GetOutputPaths(args.ConfigValue, args.BuildBasePathValue);
var outputPath = outputPaths.CompilationOutputPath;
var intermediateOutputPath = outputPaths.IntermediateOutputDirectoryPath;
Directory.CreateDirectory(outputPath);
Directory.CreateDirectory(intermediateOutputPath);
// Create the library exporter
var exporter = context.CreateExporter(args.ConfigValue, args.BuildBasePathValue);
// Gather exports for the project
var dependencies = exporter.GetDependencies().ToList();
Reporter.Output.WriteLine($"Compiling {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}");
var sw = Stopwatch.StartNew();
var diagnostics = new List<DiagnosticMessage>();
var missingFrameworkDiagnostics = new List<DiagnosticMessage>();
// Collect dependency diagnostics
foreach (var diag in context.LibraryManager.GetAllDiagnostics())
{
if (diag.ErrorCode == ErrorCodes.DOTNET1011 ||
diag.ErrorCode == ErrorCodes.DOTNET1012)
{
missingFrameworkDiagnostics.Add(diag);
}
diagnostics.Add(diag);
}
if (missingFrameworkDiagnostics.Count > 0)
{
// The framework isn't installed so we should short circuit the rest of the compilation
// so we don't get flooded with errors
PrintSummary(missingFrameworkDiagnostics, sw);
return false;
}
// Get compilation options
var outputName = outputPaths.CompilationFiles.Assembly;
// Assemble args
var compilerArgs = new List<string>()
{
$"--temp-output:{intermediateOutputPath}",
$"--out:{outputName}"
};
var compilationOptions = context.ResolveCompilationOptions(args.ConfigValue);
var languageId = CompilerUtil.ResolveLanguageId(context);
var references = new List<string>();
// Add compilation options to the args
compilerArgs.AddRange(compilationOptions.SerializeToArgs());
// Add metadata options
compilerArgs.AddRange(AssemblyInfoOptions.SerializeToArgs(AssemblyInfoOptions.CreateForProject(context)));
foreach (var dependency in dependencies)
{
references.AddRange(dependency.CompilationAssemblies.Select(r => r.ResolvedPath));
compilerArgs.AddRange(dependency.SourceReferences.Select(s => s.GetTransformedFile(intermediateOutputPath)));
foreach (var resourceFile in dependency.EmbeddedResources)
{
var transformedResource = resourceFile.GetTransformedFile(intermediateOutputPath);
var resourceName = ResourceManifestName.CreateManifestName(
Path.GetFileName(resourceFile.ResolvedPath), compilationOptions.OutputName);
compilerArgs.Add($"--resource:\"{transformedResource}\",{resourceName}");
}
// Add analyzer references
compilerArgs.AddRange(dependency.AnalyzerReferences
.Where(a => a.AnalyzerLanguage == languageId)
.Select(a => $"--analyzer:{a.AssemblyPath}"));
}
compilerArgs.AddRange(references.Select(r => $"--reference:{r}"));
if (compilationOptions.PreserveCompilationContext == true)
{
var allExports = exporter.GetAllExports().ToList();
var dependencyContext = new DependencyContextBuilder().Build(compilationOptions,
allExports,
allExports,
false, // For now, just assume non-portable mode in the legacy deps file (this is going away soon anyway)
context.TargetFramework,
context.RuntimeIdentifier ?? string.Empty);
var writer = new DependencyContextWriter();
var depsJsonFile = Path.Combine(intermediateOutputPath, compilationOptions.OutputName + "dotnet-compile.deps.json");
using (var fileStream = File.Create(depsJsonFile))
{
writer.Write(dependencyContext, fileStream);
//.........这里部分代码省略.........
示例9: LoadProject
internal Assembly LoadProject(ProjectContext context)
{
var outputPaths = context.GetOutputPaths(Configuration);
var assemblyPath = outputPaths.CompilationFiles.Assembly;
var assembly = LoadFromAssemblyPath(assemblyPath);
PopulateProbingFolder(assemblyPath);
var assemblyFolderPath = outputPaths.CompilationOutputPath;
var libraryExporter = context.CreateExporter(Configuration);
var runtimeIds = GetRuntimeIdentifiers();
foreach (var dependency in libraryExporter.GetAllExports())
{
var library = dependency.Library as ProjectDescription;
var package = dependency.Library as PackageDescription;
// Check for an unresolved library
if (library != null && !library.Resolved)
{
if (!IsAmbientAssembly(library.Identity.Name))
{
var assetFileName = GetAssemblyFileName(library.Identity.Name);
var assetResolvedPath = ResolveAssemblyPath(assemblyFolderPath, assetFileName);
if (!String.IsNullOrEmpty(assetResolvedPath))
{
LoadFromAssemblyPath(assetResolvedPath);
PopulateBinaryFolder(assemblyFolderPath, assetResolvedPath);
PopulateProbingFolder(assetResolvedPath);
var resourceFileName = library.Identity.Name + ".resources.dll";
var assemblyFolderName = PathUtility.GetDirectoryName(assemblyFolderPath);
var resourceAssemblies = Directory.GetFiles(assemblyFolderPath, resourceFileName, SearchOption.AllDirectories)
.Union(Directory.GetFiles(_probingFolderPath, resourceFileName, SearchOption.AllDirectories));
foreach (var asset in resourceAssemblies)
{
var locale = Directory.GetParent(asset).Name
.Replace(assemblyFolderName, String.Empty)
.Replace(ProbingDirectoryName, String.Empty);
PopulateBinaryFolder(assemblyFolderPath, asset, locale);
PopulateProbingFolder(asset, locale);
PopulateRuntimeFolder(asset, locale);
}
}
}
}
// Check for an unresolved package
else if (package != null && !package.Resolved)
{
foreach (var asset in package.RuntimeAssemblies)
{
var assetName = Path.GetFileNameWithoutExtension(asset.Path);
if (!IsAmbientAssembly(assetName))
{
var assetFileName = Path.GetFileName(asset.Path);
var assetResolvedPath = ResolveAssemblyPath(assemblyFolderPath, assetFileName);
if (!String.IsNullOrEmpty(assetResolvedPath))
{
LoadFromAssemblyPath(assetResolvedPath);
PopulateBinaryFolder(assemblyFolderPath, assetResolvedPath);
PopulateProbingFolder(assetResolvedPath);
}
}
}
foreach (var asset in package.RuntimeTargets)
{
var assetName = Path.GetFileNameWithoutExtension(asset.Path);
if (!IsAmbientAssembly(assetName))
{
var assetFileName = Path.GetFileName(asset.Path);
var relativeFolderPath = !String.IsNullOrEmpty(asset.Runtime)
? Path.GetDirectoryName(asset.Path) : String.Empty;
var assetResolvedPath = ResolveAssemblyPath(assemblyFolderPath, assetFileName, relativeFolderPath);
if (!String.IsNullOrEmpty(assetResolvedPath))
{
if (runtimeIds.Contains(asset.Runtime))
{
LoadFromAssemblyPath(assetResolvedPath);
}
PopulateBinaryFolder(assemblyFolderPath, assetResolvedPath, relativeFolderPath);
PopulateProbingFolder(assetResolvedPath, relativeFolderPath);
}
}
}
var runtimeAssets = new HashSet<string>(package.RuntimeAssemblies.Select(x => x.Path), StringComparer.OrdinalIgnoreCase);
//.........这里部分代码省略.........
示例10: GetProjectDependenciesWithSources
//todo make extension of ProjectContext?
//returns map with dependencies: string projectName -> ProjectDescription
private static Dictionary<string, ProjectDescription> GetProjectDependenciesWithSources(ProjectContext projectContext, string configuration)
{
var projects = new Dictionary<string, ProjectDescription>();
// Create the library exporter
var exporter = projectContext.CreateExporter(configuration);
// Gather exports for the project
var dependencies = exporter.GetDependencies().ToList();
// Build project references
foreach (var dependency in dependencies)
{
var projectDependency = dependency.Library as ProjectDescription;
if (projectDependency != null && projectDependency.Project.Files.SourceFiles.Any())
{
projects[projectDependency.Identity.Name] = projectDependency;
}
}
return projects;
}
示例11: PublishProjectContext
/// <summary>
/// Publish the project for given 'framework (ex - netstandardapp1.5)' and 'runtimeID (ex - win7-x64)'
/// </summary>
/// <param name="context">project that is to be published</param>
/// <param name="baseOutputPath">Location of published files</param>
/// <param name="configuration">Debug or Release</param>
/// <param name="nativeSubdirectories"></param>
/// <returns>Return 0 if successful else return non-zero</returns>
private bool PublishProjectContext(ProjectContext context, string buildBasePath, string outputPath, string configuration, bool nativeSubdirectories)
{
var target = context.TargetFramework.DotNetFrameworkName;
if (!string.IsNullOrEmpty(context.RuntimeIdentifier))
{
target = $"{target}/{context.RuntimeIdentifier}";
}
Reporter.Output.WriteLine($"Publishing {context.RootProject.Identity.Name.Yellow()} for {target.Yellow()}");
var options = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
var outputPaths = context.GetOutputPaths(configuration, buildBasePath, outputPath);
if (string.IsNullOrEmpty(outputPath))
{
outputPath = Path.Combine(outputPaths.RuntimeOutputPath, PublishSubfolderName);
}
var contextVariables = new Dictionary<string, string>
{
{ "publish:ProjectPath", context.ProjectDirectory },
{ "publish:Configuration", configuration },
{ "publish:OutputPath", outputPath },
{ "publish:TargetFramework", context.TargetFramework.GetShortFolderName() },
{ "publish:FullTargetFramework", context.TargetFramework.DotNetFrameworkName },
{ "publish:Runtime", context.RuntimeIdentifier },
};
RunScripts(context, ScriptNames.PrePublish, contextVariables);
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
// Compile the project (and transitively, all it's dependencies)
if (ShouldBuild && !InvokeBuildOnProject(context, buildBasePath, configuration))
{
return false;
}
// Use a library exporter to collect publish assets
var exporter = context.CreateExporter(configuration);
var isPortable = string.IsNullOrEmpty(context.RuntimeIdentifier);
// Collect all exports and organize them
var packageExports = exporter.GetAllExports()
.Where(e => e.Library.Identity.Type.Equals(LibraryType.Package))
.ToDictionary(e => e.Library.Identity.Name);
var collectExclusionList = isPortable ? GetExclusionList(context, packageExports) : new HashSet<string>();
var exports = exporter.GetAllExports();
foreach (var export in exports.Where(e => !collectExclusionList.Contains(e.Library.Identity.Name)))
{
Reporter.Verbose.WriteLine($"Publishing {export.Library.Identity.ToString().Green().Bold()} ...");
PublishAssetGroups(export.RuntimeAssemblyGroups, outputPath, nativeSubdirectories: false, includeRuntimeGroups: isPortable);
PublishAssetGroups(export.NativeLibraryGroups, outputPath, nativeSubdirectories, includeRuntimeGroups: isPortable);
export.RuntimeAssets.StructuredCopyTo(outputPath, outputPaths.IntermediateOutputDirectoryPath);
}
if (options.PreserveCompilationContext.GetValueOrDefault())
{
foreach (var export in exports)
{
PublishRefs(export, outputPath, !collectExclusionList.Contains(export.Library.Identity.Name));
}
}
if (context.ProjectFile.HasRuntimeOutput(configuration) && !context.TargetFramework.IsDesktop())
{
// Get the output paths used by the call to `dotnet build` above (since we didn't pass `--output`, they will be different from
// our current output paths)
var buildOutputPaths = context.GetOutputPaths(configuration, buildBasePath);
PublishFiles(
new[] {
buildOutputPaths.RuntimeFiles.DepsJson,
buildOutputPaths.RuntimeFiles.RuntimeConfigJson
},
outputPath);
}
var contentFiles = new ContentFiles(context);
contentFiles.StructuredCopyTo(outputPath);
// Publish a host if this is an application
if (options.EmitEntryPoint.GetValueOrDefault() && !string.IsNullOrEmpty(context.RuntimeIdentifier))
{
Reporter.Verbose.WriteLine($"Copying native host to output to create fully standalone output.");
PublishHost(context, outputPath, options);
}
//.........这里部分代码省略.........
示例12: Compile
public override bool Compile(ProjectContext context, CompilerCommandApp args)
{
var outputPaths = context.GetOutputPaths(args.ConfigValue, args.BuildBasePathValue, args.OutputValue);
var outputPath = outputPaths.RuntimeOutputPath;
var nativeOutputPath = Path.Combine(outputPath, "native");
var intermediateOutputPath =
outputPaths.IntermediateOutputDirectoryPath;
var nativeTempOutput = Path.Combine(intermediateOutputPath, "native");
Directory.CreateDirectory(nativeOutputPath);
Directory.CreateDirectory(nativeTempOutput);
var managedOutput = outputPaths.CompilationFiles.Assembly;
// Create the library exporter
var exporter = context.CreateExporter(args.ConfigValue);
// Gather exports for the project
var exports = exporter.GetAllExports();
// Runtime assemblies.
// TODO: native assets/resources.
var references = exports
.SelectMany(export => export.RuntimeAssemblies)
.Select(r => r.ResolvedPath)
.ToList();
// Setup native args.
var nativeArgs = new List<string>();
// Input Assembly
nativeArgs.Add($"{managedOutput}");
// Add Resolved Assembly References
foreach (var reference in references)
{
nativeArgs.Add("--reference");
nativeArgs.Add(reference);
}
// ILC Args
foreach (var ilcArg in args.IlcArgsValue)
{
nativeArgs.Add("--ilcarg");
nativeArgs.Add($"\"{ilcArg}\"");
}
// ILC Path
if (!string.IsNullOrWhiteSpace(args.IlcPathValue))
{
nativeArgs.Add("--ilcpath");
nativeArgs.Add(args.IlcPathValue);
}
// ILC SDK Path
if (!string.IsNullOrWhiteSpace(args.IlcSdkPathValue))
{
nativeArgs.Add("--ilcsdkpath");
nativeArgs.Add(args.IlcSdkPathValue);
}
// AppDep SDK Path
if (!string.IsNullOrWhiteSpace(args.AppDepSdkPathValue))
{
nativeArgs.Add("--appdepsdk");
nativeArgs.Add(args.AppDepSdkPathValue);
}
// CodeGen Mode
if (args.IsCppModeValue)
{
nativeArgs.Add("--mode");
nativeArgs.Add("cpp");
}
if (!string.IsNullOrWhiteSpace(args.CppCompilerFlagsValue))
{
nativeArgs.Add("--cppcompilerflags");
nativeArgs.Add(args.CppCompilerFlagsValue);
}
// Configuration
if (args.ConfigValue != null)
{
nativeArgs.Add("--configuration");
nativeArgs.Add(args.ConfigValue);
}
// Architecture
if (args.ArchValue != null)
{
nativeArgs.Add("--arch");
nativeArgs.Add(args.ArchValue);
}
// Intermediate Path
nativeArgs.Add("--temp-output");
nativeArgs.Add($"{nativeTempOutput}");
// Output Path
nativeArgs.Add("--output");
//.........这里部分代码省略.........
示例13: PublishProjectContext
/// <summary>
/// Publish the project for given 'framework (ex - dnxcore50)' and 'runtimeID (ex - win7-x64)'
/// </summary>
/// <param name="context">project that is to be published</param>
/// <param name="outputPath">Location of published files</param>
/// <param name="configuration">Debug or Release</param>
/// <returns>Return 0 if successful else return non-zero</returns>
private static bool PublishProjectContext(ProjectContext context, string outputPath, string configuration, bool nativeSubdirectories)
{
Reporter.Output.WriteLine($"Publishing {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}/{context.RuntimeIdentifier.Yellow()}");
var options = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
// Generate the output path
if (string.IsNullOrEmpty(outputPath))
{
outputPath = Path.Combine(
context.ProjectFile.ProjectDirectory,
Constants.BinDirectoryName,
configuration,
context.TargetFramework.GetTwoDigitShortFolderName(),
context.RuntimeIdentifier);
}
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
// Compile the project (and transitively, all it's dependencies)
var result = Command.Create("dotnet-build",
$"--framework \"{context.TargetFramework.DotNetFrameworkName}\" " +
$"--output \"{outputPath}\" " +
$"--configuration \"{configuration}\" " +
"--no-host " +
$"\"{context.ProjectFile.ProjectDirectory}\"")
.ForwardStdErr()
.ForwardStdOut()
.Execute();
if (result.ExitCode != 0)
{
return false;
}
// Use a library exporter to collect publish assets
var exporter = context.CreateExporter(configuration);
foreach (var export in exporter.GetAllExports())
{
// Skip copying project references
if (export.Library is ProjectDescription)
{
continue;
}
Reporter.Verbose.WriteLine($"Publishing {export.Library.Identity.ToString().Green().Bold()} ...");
PublishFiles(export.RuntimeAssemblies, outputPath, false);
PublishFiles(export.NativeLibraries, outputPath, nativeSubdirectories);
}
CopyContents(context, outputPath);
// Publish a host if this is an application
if (options.EmitEntryPoint.GetValueOrDefault())
{
Reporter.Verbose.WriteLine($"Making {context.ProjectFile.Name.Cyan()} runnable ...");
PublishHost(context, outputPath);
}
Reporter.Output.WriteLine($"Published to {outputPath}".Green().Bold());
return true;
}
示例14: CompileProject
private static bool CompileProject(ProjectContext context, CompilerCommandApp args)
{
// Set up Output Paths
var outputPathCalculator = context.GetOutputPathCalculator(args.OutputValue);
var outputPath = outputPathCalculator.GetOutputDirectoryPath(args.ConfigValue);
var intermediateOutputPath =
outputPathCalculator.GetIntermediateOutputDirectoryPath(args.ConfigValue, args.IntermediateValue);
Directory.CreateDirectory(outputPath);
Directory.CreateDirectory(intermediateOutputPath);
// Create the library exporter
var exporter = context.CreateExporter(args.ConfigValue);
// Gather exports for the project
var dependencies = exporter.GetDependencies().ToList();
Reporter.Output.WriteLine($"Compiling {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}");
var sw = Stopwatch.StartNew();
var diagnostics = new List<DiagnosticMessage>();
var missingFrameworkDiagnostics = new List<DiagnosticMessage>();
// Collect dependency diagnostics
foreach (var diag in context.LibraryManager.GetAllDiagnostics())
{
if (diag.ErrorCode == ErrorCodes.DOTNET1011 ||
diag.ErrorCode == ErrorCodes.DOTNET1012)
{
missingFrameworkDiagnostics.Add(diag);
}
diagnostics.Add(diag);
}
if (missingFrameworkDiagnostics.Count > 0)
{
// The framework isn't installed so we should short circuit the rest of the compilation
// so we don't get flooded with errors
PrintSummary(missingFrameworkDiagnostics, sw);
return false;
}
// Get compilation options
var outputName = outputPathCalculator.GetAssemblyPath(args.ConfigValue);
// Assemble args
var compilerArgs = new List<string>()
{
$"--temp-output:\"{intermediateOutputPath}\"",
$"--out:\"{outputName}\""
};
var compilationOptions = CompilerUtil.ResolveCompilationOptions(context, args.ConfigValue);
var languageId = CompilerUtil.ResolveLanguageId(context);
var references = new List<string>();
// Add compilation options to the args
compilerArgs.AddRange(compilationOptions.SerializeToArgs());
// Add metadata options
compilerArgs.AddRange(AssemblyInfoOptions.SerializeToArgs(AssemblyInfoOptions.CreateForProject(context)));
foreach (var dependency in dependencies)
{
references.AddRange(dependency.CompilationAssemblies.Select(r => r.ResolvedPath));
compilerArgs.AddRange(dependency.SourceReferences.Select(s => $"\"{s}\""));
// Add analyzer references
compilerArgs.AddRange(dependency.AnalyzerReferences
.Where(a => a.AnalyzerLanguage == languageId)
.Select(a => $"--analyzer:\"{a.AssemblyPath}\""));
}
compilerArgs.AddRange(references.Select(r => $"--reference:\"{r}\""));
if (compilationOptions.PreserveCompilationContext == true)
{
var dependencyContext = DependencyContextBuilder.Build(compilationOptions,
exporter,
args.ConfigValue,
context.TargetFramework,
context.RuntimeIdentifier);
var writer = new DependencyContextWriter();
var depsJsonFile = Path.Combine(intermediateOutputPath, context.ProjectFile.Name + "dotnet-compile.deps.json");
using (var fileStream = File.Create(depsJsonFile))
{
writer.Write(dependencyContext, fileStream);
}
compilerArgs.Add($"--resource:\"{depsJsonFile},{context.ProjectFile.Name}.deps.json\"");
}
if (!AddNonCultureResources(context.ProjectFile, compilerArgs, intermediateOutputPath))
{
return false;
}
// Add project source files
//.........这里部分代码省略.........
示例15: PublishProjectContext
/// <summary>
/// Publish the project for given 'framework (ex - netcoreapp1.0)' and 'runtimeID (ex - win7-x64)'
/// </summary>
/// <param name="context">project that is to be published</param>
/// <param name="baseOutputPath">Location of published files</param>
/// <param name="configuration">Debug or Release</param>
/// <param name="nativeSubdirectories"></param>
/// <returns>Return 0 if successful else return non-zero</returns>
private bool PublishProjectContext(ProjectContext context, string buildBasePath, string outputPath, string configuration, bool nativeSubdirectories)
{
var target = context.TargetFramework.DotNetFrameworkName;
if (!string.IsNullOrEmpty(context.RuntimeIdentifier))
{
target = $"{target}/{context.RuntimeIdentifier}";
}
Reporter.Output.WriteLine($"Publishing {context.RootProject.Identity.Name.Yellow()} for {target.Yellow()}");
var options = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
var outputPaths = context.GetOutputPaths(configuration, buildBasePath, outputPath);
if (string.IsNullOrEmpty(outputPath))
{
outputPath = Path.Combine(outputPaths.RuntimeOutputPath, PublishSubfolderName);
}
var contextVariables = new Dictionary<string, string>
{
{ "publish:ProjectPath", context.ProjectDirectory },
{ "publish:Configuration", configuration },
{ "publish:OutputPath", outputPath },
{ "publish:TargetFramework", context.TargetFramework.GetShortFolderName() },
{ "publish:FullTargetFramework", context.TargetFramework.DotNetFrameworkName },
{ "publish:Runtime", context.RuntimeIdentifier },
};
RunScripts(context, ScriptNames.PrePublish, contextVariables);
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
// Compile the project (and transitively, all it's dependencies)
if (ShouldBuild && !InvokeBuildOnProject(context, buildBasePath, configuration))
{
return false;
}
// Use a library exporter to collect publish assets
var exporter = context.CreateExporter(configuration, buildBasePath);
// Get the output paths used by the call to `dotnet build` above (since we didn't pass `--output`, they will be different from
// our current output paths)
var buildOutputPaths = context.GetOutputPaths(configuration, buildBasePath);
var exports = exporter.GetAllExports();
var exportsLookup = exports.ToDictionary(e => e.Library.Identity.Name);
var platformExclusionList = context.GetPlatformExclusionList(exportsLookup);
var buildExclusionList = context.GetTypeBuildExclusionList(exportsLookup);
var allExclusionList = new HashSet<string>(platformExclusionList);
allExclusionList.UnionWith(buildExclusionList);
var filteredExports = exports.FilterExports(allExclusionList);
foreach (var export in filteredExports)
{
Reporter.Verbose.WriteLine($"publish: Publishing {export.Library.Identity.ToString().Green().Bold()} ...");
PublishAssetGroups(export.RuntimeAssemblyGroups, outputPath, nativeSubdirectories: false, includeRuntimeGroups: context.IsPortable);
PublishAssetGroups(export.NativeLibraryGroups, outputPath, nativeSubdirectories, includeRuntimeGroups: context.IsPortable);
var runtimeAssetsToCopy = export.RuntimeAssets.Where(a => ShouldCopyExportRuntimeAsset(context, buildOutputPaths, export, a));
runtimeAssetsToCopy.StructuredCopyTo(outputPath, outputPaths.IntermediateOutputDirectoryPath);
foreach (var resourceAsset in export.ResourceAssemblies)
{
var dir = Path.Combine(outputPath, resourceAsset.Locale);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.Copy(resourceAsset.Asset.ResolvedPath, Path.Combine(dir, resourceAsset.Asset.FileName), overwrite: true);
}
}
foreach (var export in exports)
{
if (options.PreserveCompilationContext.GetValueOrDefault())
{
PublishRefs(export, outputPath);
}
}
if (context.ProjectFile.HasRuntimeOutput(configuration) && !context.TargetFramework.IsDesktop())
{
// Make executable in the new location
var executable = new Executable(context, buildOutputPaths, outputPath, buildOutputPaths.IntermediateOutputDirectoryPath, exporter, configuration);
var runtimeExports = filteredExports;
var compilationExports = exports.FilterExports(buildExclusionList);
executable.WriteConfigurationFiles(exports, runtimeExports, compilationExports, includeDevConfig: false);
//.........这里部分代码省略.........