Exemplo n.º 1
0
 internal void SetNext(ProjectChangeHandler next)
 {
     _next = next;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Starts watching project directory for changes.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// Indicates that project directory contains no resource supported for watching.
        /// </exception>
        public void StartWatching()
        {
            ProjectChangeHandler.Initialize(_domain);

            Dictionary<string, string> config = new Dictionary<string, string>();
            string binaries = Path.Combine(_dir, @"bin");
            if (Directory.Exists(binaries))
                config.Add("*.dll", binaries);

            string styles = Path.Combine(_dir, @"css");
            if (Directory.Exists(styles))
                config.Add(@"*.scss", styles);

            string scripts = Path.Combine(_dir, @"js");
            if (Directory.Exists(scripts))
            {
                config.Add(@"*.js", scripts);
                config.Add(@"*.ts", scripts);
            }

            string views = Path.Combine(_dir, @"Views");
            if (Directory.Exists(views))
                config.Add(@"*.cshtml", views);

            if (config.Count == 0)
                throw new InvalidOperationException(
                    "No resource supported for watching found in project directory.");

            _watcher = new CompositeFileSystemWatcher(config, OnChanged);
            _watcher.BeginMonitoring();

            Trace.TraceInformation("WebSync started watching for changes in {0}", _dir);

            // Using chain of responsibility pattern to manage change notification handlers.
            ProjectChangeHandler lastHandler = null;

            if (_typescript)
            {
                _chain = new TypeScriptChangeHandler(Path.Combine(_dir, "Web.csproj"));
                lastHandler = _chain;
            }

            if (_sass)
            {
                var sassHandler = new SassChangeHandler(_dir);
                if (null == _chain)
                    _chain = sassHandler;
                else
                {
                    lastHandler.SetNext(sassHandler);
                    lastHandler = sassHandler;
                }
            }

            DefaultChangeHandler defaultHandler = new DefaultChangeHandler();
            if (null == _chain)
                _chain = defaultHandler;
            else
                lastHandler.SetNext(defaultHandler);
        }