Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support VS 2017 .NET Core csproj Projects #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/LigerShark.TemplateBuilder.Tasks/CreateTemplateTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private HashSet<string> GetFiles(string projectDirectoryPath) {

return files;
}

private void RecurseItems(XElement projectItemContainer, HashSet<string> takenSourceFileNames, HashSet<string> takenTargetFileNames) {
RecurseItems(projectItemContainer, null, null, takenSourceFileNames, takenTargetFileNames);
}
Expand Down Expand Up @@ -110,14 +110,14 @@ private string GetProjectFile(XDocument vstemplate) {
result = System.IO.Path.Combine(vsTemplateFi.Directory.FullName, projfilename);
}

return result;
return result;
}

public override bool Execute() {
var vstemplate = XDocument.Load(VsTemplateShell);
var workingTemplate = XDocument.Load(VsTemplateShell);
// var workingTemplate = XDocument.Parse(@"<VSTemplate Version=""3.0.0"" xmlns=""http://schemas.microsoft.com/developer/vstemplate/2005"" Type=""Project"" />");

if (vstemplate.Root == null || workingTemplate.Root == null) {
return false;
}
Expand Down Expand Up @@ -180,7 +180,7 @@ public override bool Execute() {
templateContentElement.Add(customParameters);
}

if (UpdateProjectElement) {
if (UpdateProjectElement) {
projectElement.SetAttributeValue(XName.Get("TargetFileName"), "$safeprojectname$" + projectExtension);
projectElement.SetAttributeValue(XName.Get("File"), realProjectFile);
projectElement.SetAttributeValue(XName.Get("ReplaceParameters"), true);
Expand All @@ -196,7 +196,8 @@ public override bool Execute() {
_filesToExclude = new List<string>();
var itemsToMerge = new List<string>();

if (string.Equals(projectExtension, ".xproj", StringComparison.OrdinalIgnoreCase)) {
if (string.Equals(projectExtension, ".xproj", StringComparison.OrdinalIgnoreCase) ||
(string.Equals(projectExtension, ".csproj", StringComparison.OrdinalIgnoreCase) && IsNewCsproj(project.FullPath))) {
var files = GetFiles(Path.GetDirectoryName(project.FullPath));
foreach (var file in files) {
if (!CanExcludeFile(file.ToLower(), _filesToExclude) &&
Expand Down Expand Up @@ -241,7 +242,7 @@ public override bool Execute() {
//Copy all non-mutated sections
var mutatedTemplateSections = new[] {"TemplateContent", "TemplateData"};

// In commit f668a11df0f403520ae1457d3fd5a872ace2107d the entire file is copied first,
// In commit f668a11df0f403520ae1457d3fd5a872ace2107d the entire file is copied first,
// so this should no longer be needed.
// var elementsToCopyDirectly = vstemplate.Root.Elements().Where(x => !mutatedTemplateSections.Contains(x.Name.LocalName));
//foreach (var element in elementsToCopyDirectly) {
Expand All @@ -265,12 +266,20 @@ public override bool Execute() {
return true;
}

private bool IsNewCsproj(string filePath)
{
var xml = File.ReadAllText(filePath);
var document = XDocument.Parse(xml);
var sdk = document.Element("Project")?.Attribute("Sdk")?.Value;
return !string.IsNullOrEmpty(sdk);
}

private bool CanExcludeFile(string fileName, IList<string> filesToExclude) {
bool exclude = false;

if (FilesExclude != null) {
foreach (var item in FilesExclude) {
if (item != null &&
if (item != null &&
!string.IsNullOrEmpty(item.ItemSpec) &&
string.Equals(fileName, item.ItemSpec.ToLower(), StringComparison.Ordinal)) {
exclude = true;
Expand Down