Skip to content

Commit

Permalink
Updating to support paths from _preprocess.xml. Related to #64. Delet…
Browse files Browse the repository at this point in the history
…ing ReadMe Item Template
  • Loading branch information
sayedihashimi committed Dec 25, 2013
1 parent bd67f7b commit b2c520a
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 71 deletions.
14 changes: 0 additions & 14 deletions content/ItemTemplates/Misc/ReadmeMd/_Definitions/CSharp.vstemplate

This file was deleted.

14 changes: 0 additions & 14 deletions content/ItemTemplates/Misc/ReadmeMd/_Definitions/VB.vstemplate

This file was deleted.

15 changes: 0 additions & 15 deletions content/ItemTemplates/Misc/ReadmeMd/_Definitions/Web.VB.vstemplate

This file was deleted.

This file was deleted.

Binary file removed content/ItemTemplates/Misc/ReadmeMd/icon.pdn
Binary file not shown.
Binary file removed content/ItemTemplates/Misc/ReadmeMd/icon.png
Binary file not shown.
9 changes: 0 additions & 9 deletions content/ItemTemplates/Misc/ReadmeMd/readme.md

This file was deleted.

Expand Up @@ -41,6 +41,22 @@ public class GetItemTemplateNameFromVSTemplatePath : Microsoft.Build.Utilities.T
public string OutputPathWithFileName { get; set; }
#endregion

private string GetCustomOutputPathFolder(string templateRoot) {
if (string.IsNullOrEmpty(templateRoot)) { throw new ArgumentNullException("templateRoot"); }

string result = null;
// see if there is a _preprocess.xml file in the root of the folder
FileInfo preprocessFi = new FileInfo(Path.Combine(templateRoot, "_preprocess.xml"));
if (preprocessFi.Exists) {
// parse the file
TemplateInfo templateInfo = TemplateInfo.BuildTemplateInfoFrom(preprocessFi.FullName);
if (!string.IsNullOrEmpty(templateInfo.OverridePath)) {
result = templateInfo.OverridePath;
}
}

return result;
}
public override bool Execute() {
Log.LogMessage("GetItemTemplateNameFromVSTemplatePath Starting");
System.IO.FileInfo vsTemplateFileInfo = new System.IO.FileInfo(VstemplateFilePath);
Expand All @@ -60,7 +76,9 @@ public class GetItemTemplateNameFromVSTemplatePath : Microsoft.Build.Utilities.T

string itRootFileName = di.Parent.Name;
string subFolder = this.CustomTemplatesFolder;


string customOutputPathFolder = GetCustomOutputPathFolder(ItemTemplateFolder);

// set OutputFolder
// if the name is
// 'CSharp.vstemplate' -> CSharp\
Expand All @@ -69,7 +87,7 @@ public class GetItemTemplateNameFromVSTemplatePath : Microsoft.Build.Utilities.T
// 'Web.VB.vstemplate' -> VisualBasic\Web\
// 'fsharp.vstemplate' -> FSharp\
if (string.Compare(@"CSharp.vstemplate", vsTemplateFileInfo.Name, StringComparison.OrdinalIgnoreCase) == 0) {
ItemTemplateName = string.Format("{0}.csharp", itRootFileName);
ItemTemplateName = string.Format("{0}.csharp", itRootFileName);
OutputPathFolder = string.Format(@"{0}CSharp\{1}\{2}", ItemTemplateZipRootFolder, templateRelPath, subFolder);
}
else if (string.Compare(@"Web.CSharp.vstemplate", vsTemplateFileInfo.Name, StringComparison.OrdinalIgnoreCase) == 0) {
Expand Down Expand Up @@ -103,6 +121,10 @@ public class GetItemTemplateNameFromVSTemplatePath : Microsoft.Build.Utilities.T
return false;
}

if (!string.IsNullOrEmpty(customOutputPathFolder)) {
OutputPathFolder = string.Format(@"{0}"+customOutputPathFolder, ItemTemplateZipRootFolder, templateRelPath, subFolder);
}

OutputPathWithFileName = string.Format(@"{0}\{1}{2}", OutputPathFolder, itRootFileName,di.Parent.Extension);

return Success;
Expand Down
21 changes: 20 additions & 1 deletion src/LigerShark.TemplateBuilder.Tasks/TemplateInfo.cs
Expand Up @@ -10,17 +10,29 @@ public class TemplateInfo {
public TemplateInfo() {
this.Replacements = new Dictionary<string, string>();
}
public string OverridePath { get; set; }
public string Include { get; set; }
public string Exclude { get; set; }
public IDictionary<string,string> Replacements { get; set; }

public static TemplateInfo BuildTemplateInfoFrom(string filePath){

public static string SafeGetAttributeValue(XElement element, XName attributeName) {
if (element == null || attributeName == null) {
return string.Empty;
}

var attrib = element.Attribute(attributeName);

return attrib != null ? attrib.Value : string.Empty;
}
public static TemplateInfo BuildTemplateInfoFrom(string filePath){
if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException("filePath"); }
if (!File.Exists(filePath)) { throw new FileNotFoundException("Template info file not found.", filePath); }

XDocument doc = XDocument.Load(filePath);
var result = (from r in doc.Root.Elements("Replacements")
select new {
// OverridePath = SafeGetAttributeValue(t,"Path"),
Include = r.Attribute("Include").Value,
Exclude = r.Attribute("Exclude").Value,
Replacements = (
Expand All @@ -31,8 +43,15 @@ public class TemplateInfo {
}
)
}).Single();

// there is probably a better way to parse this file in one pass but this is OK for now
var templateInfo = (from t in doc.Root.Elements("TemplateInfo")
select new {
OverridePath = SafeGetAttributeValue(t, "Path")
}).SingleOrDefault();

var tempInfo = new TemplateInfo {
OverridePath = (templateInfo != null ? templateInfo.OverridePath : string.Empty),
Include = result.Include,
Exclude = result.Exclude
};
Expand Down
2 changes: 1 addition & 1 deletion template-builder.nuspec
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>TemplateBuilder</id>
<version>1.0.3.32-beta</version>
<version>1.0.3.33-beta</version>
<authors>Sayed Ibrahim Hashimi</authors>
<owners>Sayed Ibrahim Hashimi</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
Expand Down

0 comments on commit b2c520a

Please sign in to comment.