4

I have an xml file (resource.xml) in my asp.net mvc project and a T4 file (resource.tt) to convert that file to json in a .js file (resource.js).

The issue is i want to run t4 file automatically when resource.xml file changes or saved.

I know that in asp.net has a .resx file that when it changes, a custom tool automatically generate a file,

I want something like that

Update: In my project I have an xml file in /Resources/Resource.fr.xml and a t4 file that read the xml file and generate json object in /Resources/Resource.fr.js file. I want to t4 file generate the .js file when xml file saved or changes.

Omid Shariati
  • 1,904
  • 5
  • 22
  • 44
  • This is a duplicate of another question you posted here http://stackoverflow.com/questions/14997308/how-can-set-the-custom-tool-property-of-an-xml-file-to-a-t4-file/15037150#15037150 – Oleg Sych Feb 23 '13 at 04:48

2 Answers2

2

I have just answered this sort of question in this thread

Check this out: https://github.com/thomaslevesque/AutoRunCustomTool or https://visualstudiogallery.msdn.microsoft.com/ecb123bf-44bb-4ae3-91ee-a08fc1b9770e From the readme:
After you install the extension, you should see a new Run custom tool on property on each project item. Just edit this property to add the name(s) of the target file(s). That's it!
"target" files are your .tt files
Community
  • 1
  • 1
Orif Khodjaev
  • 1,080
  • 1
  • 13
  • 34
1

Take a look at the FileSystemWatcher class. It monitors changes to a file or even to a folder.

Look at this example:

using System; using System.IO; using System.Security.Permissions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Run(@"C:\Users\Hanlet\Desktop\Watcher\ConsoleApplication1\bin\Debug");  
        }
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static void Run(string path)
        {

            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path =path;
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            watcher.Filter = "*.xml";

            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);

            watcher.EnableRaisingEvents = true;

            Console.WriteLine("Press \'q\' to quit the sample.");
            while (Console.Read() != 'q') ;
        }

        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            if(e.FullPath.IndexOf("resource.xml") > - 1)
                Console.WriteLine("The file was: " + e.ChangeType);
        }
    }
}

This monitors and catches everytime the resource.xml file suffers some kind of change (created, deleted or updated). Good luck!

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75