public IFile GetTempFile()
        {
            var file = new FileImpl( this, Path.GetTempFileName() );
            file.Parent.Create();

            return file;
        }
        public void OnCsFileRenamed(string[] newPaths, string[] oldPaths)
        {
            if (ExtensionPackage.Instance.Options.TrackSourceFiles == false)
            {
                Log.Debug("Rename skipped {0}", oldPaths?.FirstOrDefault());
                return;
            }

            // Delay to wait for Roslyn to refresh the current Workspace after a change.
            Task.Delay(1000).ContinueWith(task =>
            {
                Enqueue(GenerationType.Rename, newPaths, (path, fileIndex) => new { OldPath = oldPaths[fileIndex], NewPath = path, NewFileMeta = _metadataProvider.GetFile(path) },
                        (item, template) =>
                {
                    if (item.NewFileMeta == null)
                    {
                        // the cs-file was found, but the build-action is not set to compile.
                        return;
                    }

                    var newFile = new FileImpl(item.NewFileMeta);
                    template.RenameFile(newFile, item.OldPath, item.NewPath);
                });
            });
        }
Exemple #3
0
        public static void Generate(CliArgs cliArgs)
        {
            var settings     = new SettingsImpl();
            var template     = new Template(cliArgs.TemplatePath);
            var provider     = new RoslynMetadataProvider();
            var indexBuilder = new StringBuilder();

//detect whether its a directory or file
            foreach (var path in GetFiles(cliArgs.SourcePath, cliArgs.Recursive, cliArgs.Regex))
            {
                var file       = new FileImpl(provider.GetFile(path, settings, null));
                var outputPath = template.RenderFile(file);
                if (outputPath != null)
                {
                    indexBuilder.Append(ExportStatement(outputPath));
                }
            }

            if (cliArgs.GenerateIndex)
            {
                var @join = Path.Join(Path.GetDirectoryName(cliArgs.TemplatePath), "index.ts");
                Console.WriteLine($"Outputting to {@join}");
                File.WriteAllText(@join, indexBuilder.ToString(), new UTF8Encoding(true));
            }
        }
        public void OnCsFileChanged(string[] paths)
        {
            if (ExtensionPackage.Instance.Options.TrackSourceFiles == false)
            {
                Log.Debug("Render skipped {0}", paths?.FirstOrDefault());
                return;
            }

            // Delay to wait for Roslyn to refresh the current Workspace after a change.
            Task.Delay(1000).ContinueWith(task =>
            {
                Enqueue(GenerationType.Render, paths, path => _metadataProvider.GetFile(path), (fileMeta, template) =>
                {
                    if (fileMeta == null)
                    {
                        // the cs-file was found, but the build-action is not set to compile.
                        return;
                    }

                    var file = new FileImpl(fileMeta);
                    if (template.ShouldRenderFile(file.FullName))
                    {
                        template.RenderFile(file);
                    }
                });
            });
        }
        private void Render(Template template, GenerationEvent generationEvent)
        {
            try
            {
                var stopwatch = Stopwatch.StartNew();

                var path = generationEvent.Paths[0];
                Log.Debug("Render {0}", path);

                var metadata = metadataProvider.GetFile(path);
                var file     = new FileImpl(metadata);

                var success = template.RenderFile(file, false);

                if (success == false)
                {
                    solutionMonitor.TriggerFileChanged(path);
                }

                stopwatch.Stop();
                Log.Debug("Render completed in {0} ms", stopwatch.ElapsedMilliseconds);
            }
            catch (Exception exception)
            {
                Log.Error("Render Exception: {0}, {1}", exception.Message, exception.StackTrace);
            }
        }
Exemple #6
0
 protected override void Arrange()
 {
     base.Arrange();
     base.Act();
     FileInformation1.Stub(x => x.Path).Return(source);
     FileInformation1.Stub(x => x.Name).Return(name);
     PathImpl.Stub(x => x.Combine(destinationPath, name)).Return(FQPath);
     FileImpl.Stub(x => x.MoveFile(source, FQPath));
 }
Exemple #7
0
        static void Main(string[] args)
        {
            //var stopwatch = Stopwatch.StartNew();
            var showHelp = args == null || args.Length == 0;

            string templatePath = null;
            string sourcePath   = null;

            var p = new OptionSet  {
                { "t|template=", "full path to template (*.tst) file.", v => templatePath = v },
                { "s|source=", "full path to source (*.cs) file.", v => sourcePath = v },
                { "h|help", "show this message and exit", v => showHelp = v != null }
            };

            try
            {
                p.Parse(args);
            }
            catch (OptionException e) {
                Console.Write("TypewriterCli: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `dotnet TypewriterCli.dll --help' for more information.");
                return;
            }

            try
            {
                if (showHelp)
                {
                    ShowHelp(p);
                    return;
                }

                if (templatePath == null)
                {
                    throw new InvalidOperationException("Missing required option -t|template");
                }

                if (sourcePath == null)
                {
                    throw new InvalidOperationException("Missing required option -s|source");
                }

                var settings = new SettingsImpl();
                var template = new Template(templatePath);
                var provider = new RoslynMetadataProvider();
                var file     = new FileImpl(provider.GetFile(sourcePath, settings, null));

                template.RenderFile(file);
                //Console.WriteLine("Convert {0} ms", stopwatch.ElapsedMilliseconds);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error: {e.Message}");
            }
        }
        public void OnTemplateChanged(string templatePath, bool force = false)
        {
            Log.Debug("{0} queued {1}", GenerationType.Template, templatePath);

            ErrorList.Clear();

            ThreadHelper.JoinableTaskFactory.Run(async() =>
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                var projectItem = _dte.Solution.FindProjectItem(templatePath);

                var template = _templateController.GetTemplate(projectItem);

                if (force == false && ExtensionPackage.Instance.RenderOnSave == false)
                {
                    Log.Debug("Render skipped {0}", templatePath);
                    return;
                }

                var filesToRender = template.GetFilesToRender();
                Log.Debug(" Will Check/Render {0} .cs files in referenced projects", filesToRender.Count);


                // Delay to wait for Roslyn to refresh the current Workspace after a change.
                await Task.Delay(1000).ConfigureAwait(true);
                _eventQueue.Enqueue(() =>
                {
                    var stopwatch = Stopwatch.StartNew();

                    foreach (var path in filesToRender)
                    {
                        var metadata = _metadataProvider.GetFile(path, template.Settings, null);
                        if (metadata == null)
                        {
                            // the cs-file was found, but the build-action is not set to compile.
                            continue;
                        }

                        var file = new FileImpl(metadata);

                        template.RenderFile(file);

                        if (template.HasCompileException)
                        {
                            break;
                        }
                    }

                    template.SaveProjectFile();

                    stopwatch.Stop();
                    Log.Debug("{0} processed {1} in {2}ms", GenerationType.Template, templatePath,
                              stopwatch.ElapsedMilliseconds);
                });
            });
        }
Exemple #9
0
            public void SetData(string name, int data)
            {
                name = name.ToLower();
                FileImpl f = files[name];

                f.SetLength(0);
                StreamWriter w = new StreamWriter(f);

                w.Write(data.ToString());
                w.Flush();
                foreach (FileSystemWatcherImpl wtch in watchers)
                {
                    wtch.FireChanged(Path.Combine(baseDir, name));
                }
            }
Exemple #10
0
        private void Render(GenerationEvent generationEvent)
        {
            try
            {
                var templates = templateController.Templates;
                if (templates.Any() == false)
                {
                    return;
                }

                var stopwatch = Stopwatch.StartNew();
                Log.Debug("{0} {1}", generationEvent.Type, string.Join(" -> ", generationEvent.Paths));

                switch (generationEvent.Type)
                {
                case GenerationType.Render:
                    var metadata = metadataProvider.GetFile(generationEvent.Paths[0]);
                    var file     = new FileImpl(metadata);

                    foreach (var template in templates)
                    {
                        template.RenderFile(file, true);
                    }
                    break;

                case GenerationType.Delete:
                    foreach (var template in templates)
                    {
                        template.DeleteFile(generationEvent.Paths[0], true);
                    }
                    break;

                case GenerationType.Rename:
                    foreach (var template in templates)
                    {
                        template.RenameFile(generationEvent.Paths[0], generationEvent.Paths[1], true);
                    }
                    break;
                }

                stopwatch.Stop();
                Log.Debug("{0} completed in {1} ms", generationEvent.Type, stopwatch.ElapsedMilliseconds);
            }
            catch (Exception exception)
            {
                Log.Error("{0} Exception: {1}, {2}", generationEvent.Type, exception.Message, exception.StackTrace);
            }
        }
Exemple #11
0
        private void RenderFile(string[] paths)
        {
            // Delay to wait for Roslyn to refresh the current Workspace after a change.
            Task.Delay(1000).ContinueWith(task =>
            {
                Enqueue(GenerationType.Render, paths, (path, template) => _metadataProvider.GetFile(path, template.Settings, RenderFile), (fileMeta, template) =>
                {
                    if (fileMeta == null)
                    {
                        // the cs-file was found, but the build-action is not set to compile.
                        return;
                    }

                    var file = new FileImpl(fileMeta);
                    if (template.ShouldRenderFile(file.FullName))
                    {
                        template.RenderFile(file);
                    }
                });
            });
        }
        public void OnTemplateChanged(string templatePath)
        {
            Log.Debug("{0} queued {1}", GenerationType.Template, templatePath);

            ErrorList.Clear();

            var projectItem = _dte.Solution.FindProjectItem(templatePath);

            var template      = _templateController.GetTemplate(projectItem);
            var filesToRender = template.GetFilesToRender();

            Log.Debug(" Will Check/Render {0} .cs files in referenced projects", filesToRender.Count);

            // Delay to wait for Roslyn to refresh the current Workspace after a change.
            Task.Delay(1000).ContinueWith(task =>
            {
                _eventQueue.Enqueue(() =>
                {
                    var stopwatch = Stopwatch.StartNew();

                    foreach (var path in filesToRender)
                    {
                        var metadata = _metadataProvider.GetFile(path);
                        var file     = new FileImpl(metadata);

                        template.RenderFile(file);

                        if (template.HasCompileException)
                        {
                            break;
                        }
                    }

                    template.SaveProjectFile();

                    stopwatch.Stop();
                    Log.Debug("{0} processed {1} in {2}ms", GenerationType.Template, templatePath, stopwatch.ElapsedMilliseconds);
                });
            });
        }
        private void RenderFile(string[] paths)
        {
            ThreadHelper.JoinableTaskFactory.Run(async() =>
            {
                // Delay to wait for Roslyn to refresh the current Workspace after a change.
                await Task.Delay(1000).ConfigureAwait(true);

                Enqueue(GenerationType.Render, paths, (path, template) => _metadataProvider.GetFile(path, template.Settings, RenderFile), (fileMeta, template) =>
                {
                    if (fileMeta == null)
                    {
                        // the cs-file was found, but the build-action is not set to compile.
                        return;
                    }

                    var file = new FileImpl(fileMeta);
                    if (template.ShouldRenderFile(file.FullName))
                    {
                        template.RenderFile(file);
                    }
                });
            });
        }
Exemple #14
0
 public Copier(FileImpl fileImpl, PathImpl pathImpl)
 {
     _fileImpl = fileImpl;
     _pathImpl = pathImpl;
 }
Exemple #15
0
 public FileWriter(FileImpl fileImpl)
 {
     _fileImpl = fileImpl;
 }
Exemple #16
0
        public void OnTemplateChanged(string templatePath, bool force = false)
        {
            Log.Debug("{0} queued {1}", GenerationType.Template, templatePath);

            ErrorList.Clear();

            var projectItem = _dte.Solution.FindProjectItem(templatePath);

            var template = _templateController.GetTemplate(projectItem);

            if (force == false && ExtensionPackage.Instance.Options.RenderOnSave == false)
            {
                Log.Debug("Render skipped {0}", templatePath);
                return;
            }

            var filesToRender = template.GetFilesToRender();

            Log.Debug(" Will Check/Render {0} .cs files in referenced projects", filesToRender.Count);

            if (Path.GetExtension(templatePath) == Constants.TstXTemplateExtension)
            {
                Task.Delay(1000).ContinueWith(task =>
                {
                    _eventQueue.Enqueue(() =>
                    {
                        var stopwatch = Stopwatch.StartNew();

                        var allProjectFiles = new RootContextImpl(filesToRender.Select(file =>
                                                                                       _metadataProvider.GetFile(file, template.Settings, null)));

                        template.RenderProjectFiles(allProjectFiles);

                        template.SaveProjectFile();

                        stopwatch.Stop();
                        Log.Debug("{0} processed {1} in {2}ms", GenerationType.Template, templatePath,
                                  stopwatch.ElapsedMilliseconds);
                    });
                });
            }
            else
            {
                Task.Delay(1000).ContinueWith(task =>
                {
                    _eventQueue.Enqueue(() =>
                    {
                        var stopwatch = Stopwatch.StartNew();

                        foreach (var path in filesToRender)
                        {
                            var metadata = _metadataProvider.GetFile(path, template.Settings, null);
                            if (metadata == null)
                            {
                                // the cs-file was found, but the build-action is not set to compile.
                                continue;
                            }

                            var file = new FileImpl(metadata);

                            template.RenderFile(file);

                            if (template.HasCompileException)
                            {
                                break;
                            }
                        }

                        template.SaveProjectFile();

                        stopwatch.Stop();
                        Log.Debug("{0} processed {1} in {2}ms", GenerationType.Template, templatePath, stopwatch.ElapsedMilliseconds);
                    });
                });
            }
        }
Exemple #17
0
 public FileWriter(FileImpl fileImpl)
 {
     _fileImpl = fileImpl;
 }
Exemple #18
0
 protected override void Arrange()
 {
     base.Arrange();
     base.Act();
     FileImpl.Stub(x => x.ReadAllLines(Path)).Return(Expected);
 }
 public StartpointCompression(ZipFileImpl zipFileImpl, DirectoryImpl directoryImpl, FileImpl fileImpl)
 {
     _zipFile       = zipFileImpl;
     _directoryImpl = directoryImpl;
     _fileImpl      = fileImpl;
 }
Exemple #20
0
 public void It_Should_Copy_File()
 {
     FileImpl.AssertWasCalled(x => x.MoveFile(source, FQPath));
 }
Exemple #21
0
 public void It_Should_Return_All_Lines()
 {
     FileImpl.AssertWasCalled(x => x.WriteLines(Path, lines));
 }