diff --git a/src/AddIns/BackendBindings/AspNet.Mvc/Project/AspNet.Mvc.csproj b/src/AddIns/BackendBindings/AspNet.Mvc/Project/AspNet.Mvc.csproj index 9914c066be..886ea21afe 100644 --- a/src/AddIns/BackendBindings/AspNet.Mvc/Project/AspNet.Mvc.csproj +++ b/src/AddIns/BackendBindings/AspNet.Mvc/Project/AspNet.Mvc.csproj @@ -62,6 +62,14 @@ Create.tt True + + Details.tt + True + + + Details.tt + True + Empty.tt @@ -226,6 +234,12 @@ Create.cs ICSharpCode.AspNet.Mvc.CSHtml + + TextTemplatingFilePreprocessor + ICSharpCode.AspNet.Mvc.CSHtml + Details.cs + Always + Always TextTemplatingFilePreprocessor diff --git a/src/AddIns/BackendBindings/AspNet.Mvc/Project/ItemTemplates/CSharp/CodeTemplates/AddView/CSHTML/Details.cs b/src/AddIns/BackendBindings/AspNet.Mvc/Project/ItemTemplates/CSharp/CodeTemplates/AddView/CSHTML/Details.cs new file mode 100644 index 0000000000..6c9cd49014 --- /dev/null +++ b/src/AddIns/BackendBindings/AspNet.Mvc/Project/ItemTemplates/CSharp/CodeTemplates/AddView/CSHTML/Details.cs @@ -0,0 +1,401 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.239 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ICSharpCode.AspNet.Mvc.CSHtml { + using ICSharpCode.AspNet.Mvc; + using System; + + + public partial class Details : DetailsBase { + + private global::Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost hostValue; + + + #line 47 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + + MvcTextTemplateHost MvcHost { + get { return (MvcTextTemplateHost)Host; } + } + + public string GetModelDirective() + { + string viewDataTypeName = MvcHost.ViewDataTypeName; + if (!String.IsNullOrEmpty(viewDataTypeName)) { + return String.Format("@model {0}", viewDataTypeName); + } + return String.Empty; + } + + #line default + #line hidden + + + public global::Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost Host { + get { + return this.hostValue; + } + set { + this.hostValue = value; + } + } + + public virtual string TransformText() { + this.GenerationEnvironment = null; + + #line 4 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write(this.ToStringHelper.ToStringWithCulture( GetModelDirective() )); + + #line default + #line hidden + + #line 4 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write("\r\n\r\n"); + + #line default + #line hidden + + #line 6 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + + if (MvcHost.IsPartialView) { + // Do nothing. + } else if (MvcHost.IsContentPage) { + + + #line default + #line hidden + + #line 11 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write("@{\r\n\tViewBag.Title = \""); + + #line default + #line hidden + + #line 12 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write(this.ToStringHelper.ToStringWithCulture( MvcHost.ViewName )); + + #line default + #line hidden + + #line 12 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write("\";\r\n\tLayout = \""); + + #line default + #line hidden + + #line 13 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write(this.ToStringHelper.ToStringWithCulture( MvcHost.MasterPageFile )); + + #line default + #line hidden + + #line 13 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write("\";\r\n}\r\n\r\n

"); + + #line default + #line hidden + + #line 16 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write(this.ToStringHelper.ToStringWithCulture( MvcHost.ViewName )); + + #line default + #line hidden + + #line 16 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write("

\r\n\r\n"); + + #line default + #line hidden + + #line 18 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + + } else { + + + #line default + #line hidden + + #line 21 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write("\r\n\r\n\t\r\n\t\t"); + + #line default + #line hidden + + #line 24 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write(this.ToStringHelper.ToStringWithCulture( MvcHost.ViewName )); + + #line default + #line hidden + + #line 24 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write("\r\n\t\r\n\t\r\n"); + + #line default + #line hidden + + #line 27 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + + PushIndent("\t\t"); + } + + + #line default + #line hidden + + #line 31 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write("
\r\n\t"); + + #line default + #line hidden + + #line 32 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write(this.ToStringHelper.ToStringWithCulture( MvcHost.ViewDataType.Name )); + + #line default + #line hidden + + #line 32 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write("\r\n
\r\n

\r\n\t@Html.ActionLink(\"Edit\", \"Edit\") |\r\n\t@Html.ActionLi" + + "nk(\"Back\", \"Index\")\r\n

\r\n"); + + #line default + #line hidden + + #line 38 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + + if (MvcHost.IsPartialView) { + // Do nothing. + } else if (!MvcHost.IsContentPage) { + PopIndent(); + + + #line default + #line hidden + + #line 44 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + this.Write("\t\r\n\r\n"); + + #line default + #line hidden + + #line 46 "D:\projects\dotnet\SharpDevelop.AspNetMvc\src\AddIns\BackendBindings\AspNet.Mvc\Project\ItemTemplates\CSharp\CodeTemplates\AddView\CSHTML\Details.tt" + } + + #line default + #line hidden + return this.GenerationEnvironment.ToString(); + } + + protected virtual void Initialize() { + } + } + + public class DetailsBase { + + private global::System.Text.StringBuilder builder; + + private global::System.Collections.Generic.IDictionary session; + + private global::System.CodeDom.Compiler.CompilerErrorCollection errors; + + private string currentIndent = string.Empty; + + private global::System.Collections.Generic.Stack indents; + + private bool endsWithNewline; + + private ToStringInstanceHelper _toStringHelper = new ToStringInstanceHelper(); + + public virtual global::System.Collections.Generic.IDictionary Session { + get { + return this.session; + } + set { + this.session = value; + } + } + + public global::System.Text.StringBuilder GenerationEnvironment { + get { + if ((this.builder == null)) { + this.builder = new global::System.Text.StringBuilder(); + } + return this.builder; + } + set { + this.builder = value; + } + } + + protected global::System.CodeDom.Compiler.CompilerErrorCollection Errors { + get { + if ((this.errors == null)) { + this.errors = new global::System.CodeDom.Compiler.CompilerErrorCollection(); + } + return this.errors; + } + } + + public string CurrentIndent { + get { + return this.currentIndent; + } + } + + private global::System.Collections.Generic.Stack Indents { + get { + if ((this.indents == null)) { + this.indents = new global::System.Collections.Generic.Stack(); + } + return this.indents; + } + } + + public ToStringInstanceHelper ToStringHelper { + get { + return this._toStringHelper; + } + } + + public void Error(string message) { + this.Errors.Add(new global::System.CodeDom.Compiler.CompilerError(null, -1, -1, null, message)); + } + + public void Warning(string message) { + global::System.CodeDom.Compiler.CompilerError val = new global::System.CodeDom.Compiler.CompilerError(null, -1, -1, null, message); + val.IsWarning = true; + this.Errors.Add(val); + } + + public string PopIndent() { + if ((this.Indents.Count == 0)) { + return string.Empty; + } + int lastPos = (this.currentIndent.Length - this.Indents.Pop()); + string last = this.currentIndent.Substring(lastPos); + this.currentIndent = this.currentIndent.Substring(0, lastPos); + return last; + } + + public void PushIndent(string indent) { + this.Indents.Push(indent.Length); + this.currentIndent = (this.currentIndent + indent); + } + + public void ClearIndent() { + this.currentIndent = string.Empty; + this.Indents.Clear(); + } + + public void Write(string textToAppend) { + if (string.IsNullOrEmpty(textToAppend)) { + return; + } + if ((((this.GenerationEnvironment.Length == 0) + || this.endsWithNewline) + && (this.CurrentIndent.Length > 0))) { + this.GenerationEnvironment.Append(this.CurrentIndent); + } + this.endsWithNewline = false; + char last = textToAppend[(textToAppend.Length - 1)]; + if (((last == '\n') + || (last == '\r'))) { + this.endsWithNewline = true; + } + if ((this.CurrentIndent.Length == 0)) { + this.GenerationEnvironment.Append(textToAppend); + return; + } + int lastNewline = 0; + for (int i = 0; (i + < (textToAppend.Length - 1)); i = (i + 1)) { + char c = textToAppend[i]; + if ((c == '\r')) { + if ((textToAppend[(i + 1)] == '\n')) { + i = (i + 1); + if ((i + == (textToAppend.Length - 1))) { + goto breakLoop; + } + } + } + else { + if ((c != '\n')) { + goto continueLoop; + } + } + i = (i + 1); + int len = (i - lastNewline); + if ((len > 0)) { + this.GenerationEnvironment.Append(textToAppend, lastNewline, (i - lastNewline)); + } + this.GenerationEnvironment.Append(this.CurrentIndent); + lastNewline = i; + continueLoop: + ; + } + breakLoop: + if ((lastNewline > 0)) { + this.GenerationEnvironment.Append(textToAppend, lastNewline, (textToAppend.Length - lastNewline)); + } + else { + this.GenerationEnvironment.Append(textToAppend); + } + } + + public void Write(string format, params object[] args) { + this.Write(string.Format(format, args)); + } + + public void WriteLine(string textToAppend) { + this.Write(textToAppend); + this.GenerationEnvironment.AppendLine(); + this.endsWithNewline = true; + } + + public void WriteLine(string format, params object[] args) { + this.WriteLine(string.Format(format, args)); + } + + public class ToStringInstanceHelper { + + private global::System.IFormatProvider formatProvider = global::System.Globalization.CultureInfo.InvariantCulture; + + public global::System.IFormatProvider FormatProvider { + get { + return this.formatProvider; + } + set { + if ((this.formatProvider == null)) { + throw new global::System.ArgumentNullException("formatProvider"); + } + this.formatProvider = value; + } + } + + public string ToStringWithCulture(object objectToConvert) { + if ((objectToConvert == null)) { + throw new global::System.ArgumentNullException("objectToConvert"); + } + global::System.Type type = objectToConvert.GetType(); + global::System.Type iConvertibleType = typeof(global::System.IConvertible); + if (iConvertibleType.IsAssignableFrom(type)) { + return ((global::System.IConvertible)(objectToConvert)).ToString(this.formatProvider); + } + global::System.Reflection.MethodInfo methInfo = type.GetMethod("ToString", new global::System.Type[] { + iConvertibleType}); + if ((methInfo != null)) { + return ((string)(methInfo.Invoke(objectToConvert, new object[] { + this.formatProvider}))); + } + return objectToConvert.ToString(); + } + } + } +} diff --git a/src/AddIns/BackendBindings/AspNet.Mvc/Project/ItemTemplates/CSharp/CodeTemplates/AddView/CSHTML/Details.tt b/src/AddIns/BackendBindings/AspNet.Mvc/Project/ItemTemplates/CSharp/CodeTemplates/AddView/CSHTML/Details.tt new file mode 100644 index 0000000000..bcd53dc576 --- /dev/null +++ b/src/AddIns/BackendBindings/AspNet.Mvc/Project/ItemTemplates/CSharp/CodeTemplates/AddView/CSHTML/Details.tt @@ -0,0 +1,60 @@ +<#@ template language="C#" HostSpecific="true" #> +<#@ import namespace="ICSharpCode.AspNet.Mvc" #> +<#@ output extension=".cshtml" #> +<#= GetModelDirective() #> + +<# + if (MvcHost.IsPartialView) { + // Do nothing. + } else if (MvcHost.IsContentPage) { +#> +@{ + ViewBag.Title = "<#= MvcHost.ViewName #>"; + Layout = "<#= MvcHost.MasterPageFile #>"; +} + +

<#= MvcHost.ViewName #>

+ +<# + } else { +#> + + + + <#= MvcHost.ViewName #> + + +<# + PushIndent("\t\t"); + } +#> +
+ <#= MvcHost.ViewDataType.Name #> +
+

+ @Html.ActionLink("Edit", "Edit") | + @Html.ActionLink("Back", "Index") +

+<# + if (MvcHost.IsPartialView) { + // Do nothing. + } else if (!MvcHost.IsContentPage) { + PopIndent(); +#> + + +<# } #> +<#+ + MvcTextTemplateHost MvcHost { + get { return (MvcTextTemplateHost)Host; } + } + + public string GetModelDirective() + { + string viewDataTypeName = MvcHost.ViewDataTypeName; + if (!String.IsNullOrEmpty(viewDataTypeName)) { + return String.Format("@model {0}", viewDataTypeName); + } + return String.Empty; + } +#> \ No newline at end of file diff --git a/src/AddIns/BackendBindings/AspNet.Mvc/Project/ItemTemplates/CSharp/CodeTemplates/AddView/CSHTML/Details.txt b/src/AddIns/BackendBindings/AspNet.Mvc/Project/ItemTemplates/CSharp/CodeTemplates/AddView/CSHTML/Details.txt new file mode 100644 index 0000000000..5f282702bb --- /dev/null +++ b/src/AddIns/BackendBindings/AspNet.Mvc/Project/ItemTemplates/CSharp/CodeTemplates/AddView/CSHTML/Details.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/AddIns/BackendBindings/AspNet.Mvc/Test/AspNet.Mvc.Tests.csproj b/src/AddIns/BackendBindings/AspNet.Mvc/Test/AspNet.Mvc.Tests.csproj index fdaaecfdf0..cd449b0a29 100644 --- a/src/AddIns/BackendBindings/AspNet.Mvc/Test/AspNet.Mvc.Tests.csproj +++ b/src/AddIns/BackendBindings/AspNet.Mvc/Test/AspNet.Mvc.Tests.csproj @@ -90,6 +90,7 @@ + diff --git a/src/AddIns/BackendBindings/AspNet.Mvc/Test/Src/CodeTemplates/RazorCSharpDetailsViewTemplateTests.cs b/src/AddIns/BackendBindings/AspNet.Mvc/Test/Src/CodeTemplates/RazorCSharpDetailsViewTemplateTests.cs new file mode 100644 index 0000000000..7569b1ac45 --- /dev/null +++ b/src/AddIns/BackendBindings/AspNet.Mvc/Test/Src/CodeTemplates/RazorCSharpDetailsViewTemplateTests.cs @@ -0,0 +1,152 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) + +using System; +using AspNet.Mvc.Tests.CodeTemplates.Models; +using AspNet.Mvc.Tests.Helpers; +using ICSharpCode.AspNet.Mvc.CSHtml; +using NUnit.Framework; + +namespace AspNet.Mvc.Tests.CodeTemplates +{ + [TestFixture] + public class RazorCSharpDetailsViewTemplateTests + { + Details templatePreprocessor; + TestableMvcTextTemplateHost mvcHost; + + void CreateViewTemplatePreprocessor() + { + mvcHost = new TestableMvcTextTemplateHost(); + templatePreprocessor = new Details(); + templatePreprocessor.Host = mvcHost; + } + + [Test] + public void GetModelDirective_HostViewDataTypeNameIsMyAppMyModel_ReturnsRazorModelFollowedByMyAppMyModel() + { + CreateViewTemplatePreprocessor(); + mvcHost.ViewDataTypeName = "MyApp.MyModel"; + + string modelDirective = templatePreprocessor.GetModelDirective(); + + Assert.AreEqual("@model MyApp.MyModel", modelDirective); + } + + [Test] + public void GetModelDirective_HostViewDataTypeNameIsNull_ReturnsEmptyString() + { + CreateViewTemplatePreprocessor(); + mvcHost.ViewDataTypeName = null; + + string modelDirective = templatePreprocessor.GetModelDirective(); + + Assert.AreEqual(String.Empty, modelDirective); + } + + [Test] + public void GetModelDirective_HostViewDataTypeNameIsEmptyString_ReturnsEmptyString() + { + CreateViewTemplatePreprocessor(); + mvcHost.ViewDataTypeName = String.Empty; + + string modelDirective = templatePreprocessor.GetModelDirective(); + + Assert.AreEqual(String.Empty, modelDirective); + } + + [Test] + public void TransformText_ModelHasNoPropertiesAndNoMasterPage_ReturnsFullHtmlPageWithFormAndFieldSetForModel() + { + CreateViewTemplatePreprocessor(); + Type modelType = typeof(ModelWithNoProperties); + mvcHost.ViewDataType = modelType; + mvcHost.ViewDataTypeName = modelType.FullName; + mvcHost.ViewName = "MyView"; + + string output = templatePreprocessor.TransformText(); + + string expectedOutput = +@"@model AspNet.Mvc.Tests.CodeTemplates.Models.ModelWithNoProperties + + + + + MyView + + +
+ ModelWithNoProperties +
+

+ @Html.ActionLink(""Edit"", ""Edit"") | + @Html.ActionLink(""Back"", ""Index"") +

+ + +"; + Assert.AreEqual(expectedOutput, output); + } + + [Test] + public void TransformText_ModelHasNoPropertiesAndIsPartialView_ReturnsControlWithFormAndFieldSetForModel() + { + CreateViewTemplatePreprocessor(); + mvcHost.IsPartialView = true; + Type modelType = typeof(ModelWithNoProperties); + mvcHost.ViewDataType = modelType; + mvcHost.ViewDataTypeName = modelType.FullName; + mvcHost.ViewName = "MyView"; + + string output = templatePreprocessor.TransformText(); + + string expectedOutput = +@"@model AspNet.Mvc.Tests.CodeTemplates.Models.ModelWithNoProperties + +
+ ModelWithNoProperties +
+

+ @Html.ActionLink(""Edit"", ""Edit"") | + @Html.ActionLink(""Back"", ""Index"") +

+"; + Assert.AreEqual(expectedOutput, output); + } + + [Test] + public void TransformText_ModelHasNoPropertiesAndIsContentPage_ReturnsContentPageWithFormAndFieldSetForModel() + { + CreateViewTemplatePreprocessor(); + mvcHost.IsContentPage = true; + Type modelType = typeof(ModelWithNoProperties); + mvcHost.ViewDataType = modelType; + mvcHost.ViewDataTypeName = modelType.FullName; + mvcHost.ViewName = "MyView"; + mvcHost.MasterPageFile = "~/Views/Shared/Site.master"; + mvcHost.PrimaryContentPlaceHolderID = "Main"; + + string output = templatePreprocessor.TransformText(); + + string expectedOutput = +@"@model AspNet.Mvc.Tests.CodeTemplates.Models.ModelWithNoProperties + +@{ + ViewBag.Title = ""MyView""; + Layout = ""~/Views/Shared/Site.master""; +} + +

MyView

+ +
+ ModelWithNoProperties +
+

+ @Html.ActionLink(""Edit"", ""Edit"") | + @Html.ActionLink(""Back"", ""Index"") +

+"; + Assert.AreEqual(expectedOutput, output); + } + } +}