public RazorBuildTaskFile(string relativePath, RazorBuildPathProvider pathProvider)
        {
            this.VirtualPath = relativePath.Replace(pathProvider.RealPathSeparator, pathProvider.VirtualPathSeparator);
            this.RealPath = pathProvider.RootPath + pathProvider.VirtualPathSeparator + relativePath;
            this.RealPath = this.RealPath.Replace(pathProvider.VirtualPathSeparator, pathProvider.RealPathSeparator);

            this.VirtualPathProvider = pathProvider;
        }
Пример #2
0
        public RazorBuildTaskFile(string relativePath, RazorBuildPathProvider pathProvider)
        {
            this.VirtualPath = relativePath.Replace(pathProvider.RealPathSeparator, pathProvider.VirtualPathSeparator);
            this.RealPath    = pathProvider.RootPath + pathProvider.VirtualPathSeparator + relativePath;
            this.RealPath    = this.RealPath.Replace(pathProvider.VirtualPathSeparator, pathProvider.RealPathSeparator);

            this.VirtualPathProvider = pathProvider;
        }
        public override bool Execute()
        {
            if (!this.InputFiles.Any())
                return true;

            Instance = new RazorGeneratorBuildTask();

            if (AppConfigPath == null)
            {
                var configNames = new[] {
                    PathUtils.CombinePaths(ProjectDir, "Web.config"),
                    PathUtils.CombinePaths(ProjectDir, "App.config"),
                    PathUtils.CombinePaths(ProjectDir, "web.config"), //unix
                    PathUtils.CombinePaths(ProjectDir, "app.config"),
                };
                AppConfigPath = configNames.FirstOrDefault(File.Exists);
            }

            // Use the task's parent project's web/app configuration file
            using (AppConfigScope.Change(AppConfigPath))
            {
                var allowedConfigs = ConfigurationManager.AppSettings[ConfigurationAppKeyName] ?? this.AllowedConfigurations;

                // If specified, only generate source code if the Project Configuration matches the given Configuration from the user
                if (!ConfigurationMatches(this.ProjectConfiguration, allowedConfigs))
                    return true;

                var pageBaseTypeName = GetPageBaseTypeName();
                var pathProvider = new RazorBuildPathProvider(this.ProjectDir);
                var transformer = new RazorViewPageTransformer(pageBaseTypeName);

                for (int i = 0; i < this.InputFiles.Length; i++)
                {
                    var file = new RazorBuildTaskFile(this.InputFiles[i].ItemSpec, pathProvider);
                    var pageHost = new RazorPageHost(pathProvider, file, transformer, new CSharpCodeProvider(), new Dictionary<string, string>())
                    {
                        RootNamespace = this.ProjectRootNamespace
                    };

                    var fileName = this.OutputFiles[i].ItemSpec = ToUniqueFilePath(this.OutputFiles[i].ItemSpec, pageHost.DefaultNamespace);
                    var sourceCode = pageHost.GenerateSourceCode();

                    File.WriteAllText(fileName, sourceCode);
                }

                return true;
            }
        }