/// <summary> /// Create an empty <see cref="WorkspaceContext" /> using the default <see cref="ProjectReaderSettings" />, with the specified Version Suffix /// </summary> /// <param name="versionSuffix">The suffix to use to replace any '-*' snapshot tokens in Project versions.</param> /// <returns></returns> public static BuildWorkspace Create(string versionSuffix) { var settings = ProjectReaderSettings.ReadFromEnvironment(); if (!string.IsNullOrEmpty(versionSuffix)) { settings.VersionSuffix = versionSuffix; } return(new BuildWorkspace(settings)); }
public static ProjectReaderSettings ReadFromEnvironment() { var settings = new ProjectReaderSettings { VersionSuffix = Environment.GetEnvironmentVariable("DOTNET_BUILD_VERSION"), AssemblyFileVersion = Environment.GetEnvironmentVariable("DOTNET_ASSEMBLY_FILE_VERSION") }; return(settings); }
public Project ReadProject(string projectPath, ProjectReaderSettings settings) { projectPath = ProjectPathHelper.NormalizeProjectFilePath(projectPath); var name = Path.GetFileName(Path.GetDirectoryName(projectPath)); using (var stream = new FileStream(projectPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { return(ReadProject(stream, name, projectPath, settings)); } }
public static bool TryGetProject(string path, out Project project, ProjectReaderSettings settings = null) { project = null; string projectPath = null; if (string.Equals(Path.GetFileName(path), Project.FileName, StringComparison.OrdinalIgnoreCase)) { projectPath = path; path = Path.GetDirectoryName(path); } else if (!HasProjectFile(path)) { return(false); } else { projectPath = Path.Combine(path, Project.FileName); } // Assume the directory name is the project name if none was specified var projectName = PathUtility.GetDirectoryName(Path.GetFullPath(path)); projectPath = Path.GetFullPath(projectPath); if (!File.Exists(projectPath)) { return(false); } try { using (var stream = File.OpenRead(projectPath)) { var reader = new ProjectReader(); project = reader.ReadProject(stream, projectName, projectPath, settings); } } catch (Exception ex) { throw FileFormatException.Create(ex, projectPath); } return(true); }
public Project ReadProject(Stream stream, string projectName, string projectPath, ProjectReaderSettings settings = null) { settings = settings ?? new ProjectReaderSettings(); var project = new Project(); var reader = new StreamReader(stream); JObject rawProject; using (var jsonReader = new JsonTextReader(reader)) { rawProject = JObject.Load(jsonReader); // Try to read another token to ensure we're at the end of the document. // This will no-op if we are, and throw a JsonReaderException if there is additional content (which is what we want) jsonReader.Read(); } if (rawProject == null) { throw FileFormatException.Create( "The JSON file can't be deserialized to a JSON object.", projectPath); } // Meta-data properties project.Name = rawProject.Value <string>("name") ?? projectName; project.ProjectFilePath = Path.GetFullPath(projectPath); var version = rawProject.Value <string>("version"); if (version == null) { project.Version = new NuGetVersion("1.0.0"); } else { try { var buildVersion = settings.VersionSuffix; project.Version = SpecifySnapshot(version, buildVersion); } catch (Exception ex) { throw FileFormatException.Create(ex, version, project.ProjectFilePath); } } var fileVersion = settings.AssemblyFileVersion; if (string.IsNullOrWhiteSpace(fileVersion)) { project.AssemblyFileVersion = project.Version.Version; } else { try { var simpleVersion = project.Version.Version; project.AssemblyFileVersion = new Version(simpleVersion.Major, simpleVersion.Minor, simpleVersion.Build, int.Parse(fileVersion)); } catch (FormatException ex) { throw new FormatException("The assembly file version is invalid: " + fileVersion, ex); } } project.Description = rawProject.Value <string>("description"); project.Copyright = rawProject.Value <string>("copyright"); project.Title = rawProject.Value <string>("title"); project.EntryPoint = rawProject.Value <string>("entryPoint"); project.TestRunner = rawProject.Value <string>("testRunner"); project.Authors = rawProject.Value <JToken>("authors")?.Values <string>().ToArray() ?? EmptyArray <string> .Value; project.Language = rawProject.Value <string>("language"); // REVIEW: Move this to the dependencies node? project.EmbedInteropTypes = rawProject.Value <bool>("embedInteropTypes"); project.Dependencies = new List <ProjectLibraryDependency>(); project.Tools = new List <ProjectLibraryDependency>(); project.Runtimes = new List <string>(); // Project files project.Files = new ProjectFilesCollection(rawProject, project.ProjectDirectory, project.ProjectFilePath); AddProjectFilesDeprecationDiagnostics(rawProject, project); ConvertDeprecatedToSupportedFormat(rawProject); var commands = rawProject.Value <JToken>("commands") as JObject; if (commands != null) { foreach (var command in commands) { var commandValue = command.Value.Type == JTokenType.String ? command.Value.Value <string>() : null; if (commandValue != null) { project.Commands[command.Key] = commandValue; } } } var scripts = rawProject.Value <JToken>("scripts") as JObject; if (scripts != null) { foreach (var script in scripts) { var stringValue = script.Value.Type == JTokenType.String ? script.Value.Value <string>() : null; if (stringValue != null) { project.Scripts[script.Key] = new string[] { stringValue }; continue; } var arrayValue = script.Value.Type == JTokenType.Array ? script.Value.Values <string>().ToArray() : null; if (arrayValue != null) { project.Scripts[script.Key] = arrayValue; continue; } throw FileFormatException.Create( string.Format("The value of a script in {0} can only be a string or an array of strings", Project.FileName), script.Value, project.ProjectFilePath); } } project.PackOptions = GetPackOptions(rawProject, project) ?? new PackOptions(); project.RuntimeOptions = GetRuntimeOptions(rawProject) ?? new RuntimeOptions(); project.PublishOptions = GetPublishInclude(rawProject, project); BuildTargetFrameworksAndConfigurations(project, rawProject); PopulateDependencies( project.ProjectFilePath, project.Dependencies, rawProject, "dependencies", isGacOrFrameworkReference: false); PopulateDependencies( project.ProjectFilePath, project.Tools, rawProject, "tools", isGacOrFrameworkReference: false); PopulateRuntimes(project.Runtimes, rawProject); JToken runtimeOptionsToken; if (rawProject.TryGetValue("runtimeOptions", out runtimeOptionsToken)) { var runtimeOptions = runtimeOptionsToken as JObject; if (runtimeOptions == null) { throw FileFormatException.Create("The runtimeOptions must be an object", runtimeOptionsToken); } project.RawRuntimeOptions = runtimeOptions.ToString(); } return(project); }
public static Project GetProject(string projectPath, ProjectReaderSettings settings = null) { return(new ProjectReader().ReadProject(projectPath, settings)); }
/// <summary> /// Creates a project context for each target located in the project at <paramref name="projectPath"/> /// </summary> public static IEnumerable <ProjectContext> CreateContextForEachTarget(string projectPath, ProjectReaderSettings settings = null) { var project = ProjectReader.GetProject(projectPath); return(new ProjectContextBuilder() .WithProjectReaderSettings(settings) .WithProject(project) .BuildAllTargets()); }
/// <summary> /// Creates a project context for each framework located in the project at <paramref name="projectPath"/> /// </summary> public static IEnumerable <ProjectContext> CreateContextForEachFramework(string projectPath, ProjectReaderSettings settings = null, IEnumerable <string> runtimeIdentifiers = null) { if (!projectPath.EndsWith(Project.FileName)) { projectPath = Path.Combine(projectPath, Project.FileName); } var project = ProjectReader.GetProject(projectPath, settings); foreach (var framework in project.GetTargetFrameworks()) { yield return(new ProjectContextBuilder() .WithProject(project) .WithTargetFramework(framework.FrameworkName) .WithProjectReaderSettings(settings) .WithRuntimeIdentifiers(runtimeIdentifiers ?? Enumerable.Empty <string>()) .Build()); } }
public DesignTimeWorkspace(ProjectReaderSettings settings) : base(settings, true) { }
public BuildWorkspace(ProjectReaderSettings settings) : base(settings, false) { }
public ProjectContextBuilder WithProjectReaderSettings(ProjectReaderSettings projectReaderSettings) { ProjectReaderSettings = projectReaderSettings; return(this); }
protected Workspace(ProjectReaderSettings settings, bool designTime) { _settings = settings; _lockFileReader = new LockFileFormat(); _designTime = designTime; }