T4模板生成解决方案文件的相对路径

时间:2012-08-16 10:17:46

标签: c# visual-studio-2010 visual-studio t4

我需要一个T4模板或其他东西与Visual Studio集成,以生成解决方案中文件的相对路径。

他们的任何加载项还是t4? 我正在使用Visual Studio 2012。

类似的东西:

    public static class Scripts
    {
        public static class AdminSkin
        {
            public static class Css
            {
                public const string Site = "/Scripts/adminskin/css/site.css";
                public const string SiteFa = "/Scripts/adminskin/css/site-fa.css"; 
                public const string Reset = "/Scripts/adminskin/css/reset.css";
                public const string SimpleList = "/Scripts/adminskin/css/simple-lists.css";
                public const string SpecialPages = "/Scripts/adminskin/css/special-pages.css";

            }

            public static class Js
            {
                public const string Site = "/Scripts/adminskin/js/site.js";
                public const string JqueryaccessibleList = "/Scripts/adminskin/js/jquery.accessibleList.js";
                public const string Jquerytip = "/Scripts/adminskin/js/jquery.tip.js";
                public const string List = "/Scripts/adminskin/js/list.js";
                public const string Oldbrowsers = "/Scripts/adminskin/js/old-browsers.js";
                public const string LiveControl = "/Scripts/adminskin/js/live-control.js";
            }
        }

        public static class Calendar
        {
            public static class Js
            {
                public const string Calendar = "/Scripts/calendar/calendar.js";
                public const string CalendarSetup = "/Scripts/calendar/calendar-setup.js";

                public const string CalendarFa = "/Scripts/calendar/calendar-fa.js";
                public const string CalendarEn = "/Scripts/calendar/calendar-en.js";
            }

            public static class Css
            {
                public const string Theme = "/Scripts/calendar/aqua/theme.css";
            }
        }

        public static class HighChart
        {
            public static class Js
            {
                public const string Highstock = "/Scripts/highchart/highstock.js";
                public const string HighstockModified = "/Scripts/highchart/highstock.src.js";
                public const string Theme = "/Scripts/highchart/themes/mytheme.js";
                public const string Exporting = "/Scripts/highchart/modules/exporting.src.js";
            }
        }

        public static class ImageGallery
        {
            public static class Js
            {
                public const string JqueryEasing = "/Scripts/ImageGallery/js/jquery.easing.1.3.min.js";
                public const string JqueryWtRotator = "/Scripts/ImageGallery/js/jquery.wt-rotator.min.js";
                public const string Preview = "/Scripts/ImageGallery/js/preview.js";
            }

            public static class Css
            {
                public const string Preview = "/Scripts/ImageGallery/preview.css";
                public const string JqueryWtRotator = "/Scripts/ImageGallery/wt-rotator.css";
            }
        }

        public static class JQuery
        {
            public static class Js
            {
                public const string JqueryCdn = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js ";
                public const string JqueryUiCdn = "http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.20/jquery-ui.min.js";
                public const string JqueryUiCssCdn = "http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.20/themes/ui-lightness/jquery-ui.css";

                public const string JqueryLocal = "/Scripts/jquery/jquery-1.7.min.js";
                public const string JqueryUiLocal = "/Scripts/jquery-ui/js/jquery-ui-1.8.20.custom.min.js";
                public const string JqueryUiCssLocal = "/Scripts/jquery-ui/css/ui-lightness/jquery-ui-1.8.20.custom.css";
            }
        }

        public static class Linq
        {
            public static class Js
            {
                public const string Linq = "/Scripts/linq.js/linq.min.js";
            }
        }


        public static class Moment
        {
            public static class Js
            {
                public const string Moment = "/Scripts/moment/moment.min.js";
            }
        }

        public static class Lang
        {
            public static class Js
            {
                public const string Jalali = "/Scripts/lang/jalali.js";
                public const string LangFa = "/Scripts/lang/lang-fa.js";
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

T4MVC可能就是你要找的东西。它是一个T4 template for ASP.NET MVC apps,可以创建强类型的帮助器。它将生成一组可以使用的类,而不是使用字符串。对于诸如javascript文件之类的静态文件,将有一组类,例如:

Links.Scripts.jquery_validate_js

这将返回“〜/ Scripts / jquery.validate.js”形式的字符串。

对于名为AspPathGuru.tt的WebForms项目David Ebbo created a simple T4 template。目前仅支持.aspx,.ascx和.master文件。但是,如果编辑IgnoreFile方法,如下所示,则可以为其他文件生成代码:

bool IgnoreFile(FileInfo file) {
    // Only look for a few specific extensions
    switch (file.Extension.ToLowerInvariant()) {
        case ".aspx":
        case ".ascx":
        case ".js":
        case ".css":
        case ".master":
            return false;
    }

    return true;
}

这将生成一个Paths类,您可以使用如下所示:

Paths.Scripts.jquery_1_4_1_js