2019-12-03 18:44:25 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Unosquare.Swan.Components {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a CsProjFile (and FsProjFile) parser.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Based on https://github.com/maartenba/dotnetcli-init.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <typeparam name="T">The type of <c>CsProjMetadataBase</c>.</typeparam>
|
|
|
|
|
/// <seealso cref="System.IDisposable" />
|
|
|
|
|
public class CsProjFile<T>
|
|
|
|
|
: IDisposable
|
|
|
|
|
where T : CsProjMetadataBase {
|
|
|
|
|
private readonly Stream _stream;
|
|
|
|
|
private readonly Boolean _leaveOpen;
|
|
|
|
|
private readonly XDocument _xmlDocument;
|
|
|
|
|
|
2019-02-17 14:08:57 +01:00
|
|
|
|
/// <summary>
|
2019-12-03 18:44:25 +01:00
|
|
|
|
/// Initializes a new instance of the <see cref="CsProjFile{T}"/> class.
|
2019-02-17 14:08:57 +01:00
|
|
|
|
/// </summary>
|
2019-12-03 18:44:25 +01:00
|
|
|
|
/// <param name="filename">The filename.</param>
|
|
|
|
|
public CsProjFile(String filename = null)
|
|
|
|
|
: this(OpenFile(filename)) {
|
|
|
|
|
// placeholder
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="CsProjFile{T}"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="stream">The stream.</param>
|
|
|
|
|
/// <param name="leaveOpen">if set to <c>true</c> [leave open].</param>
|
|
|
|
|
/// <exception cref="ArgumentException">Project file is not of the new .csproj type.</exception>
|
|
|
|
|
public CsProjFile(Stream stream, Boolean leaveOpen = false) {
|
|
|
|
|
this._stream = stream;
|
|
|
|
|
this._leaveOpen = leaveOpen;
|
|
|
|
|
|
|
|
|
|
this._xmlDocument = XDocument.Load(stream);
|
|
|
|
|
|
|
|
|
|
XElement projectElement = this._xmlDocument.Descendants("Project").FirstOrDefault();
|
|
|
|
|
XAttribute sdkAttribute = projectElement?.Attribute("Sdk");
|
|
|
|
|
String sdk = sdkAttribute?.Value;
|
|
|
|
|
if(sdk != "Microsoft.NET.Sdk" && sdk != "Microsoft.NET.Sdk.Web") {
|
|
|
|
|
throw new ArgumentException("Project file is not of the new .csproj type.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Metadata = Activator.CreateInstance<T>();
|
|
|
|
|
this.Metadata.SetData(this._xmlDocument);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the metadata.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>
|
|
|
|
|
/// The nu get metadata.
|
|
|
|
|
/// </value>
|
|
|
|
|
public T Metadata {
|
|
|
|
|
get;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves this instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Save() {
|
|
|
|
|
this._stream.SetLength(0);
|
|
|
|
|
this._stream.Position = 0;
|
|
|
|
|
|
|
|
|
|
this._xmlDocument.Save(this._stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void Dispose() {
|
|
|
|
|
if(!this._leaveOpen) {
|
|
|
|
|
this._stream?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static FileStream OpenFile(String filename) {
|
|
|
|
|
if(filename == null) {
|
|
|
|
|
filename = Directory
|
|
|
|
|
.EnumerateFiles(Directory.GetCurrentDirectory(), "*.csproj", SearchOption.TopDirectoryOnly)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(filename == null) {
|
|
|
|
|
filename = Directory
|
|
|
|
|
.EnumerateFiles(Directory.GetCurrentDirectory(), "*.fsproj", SearchOption.TopDirectoryOnly)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(String.IsNullOrWhiteSpace(filename)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(filename));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-17 14:08:57 +01:00
|
|
|
|
}
|