Пример #1
1
        public bool RenderFile(File file)
        {
            bool success;
            var output = Render(file, out success);

            if (success)
            {
                if (output == null)
                {
                    DeleteFile(file.FullName);
                }
                else
                {
                    SaveFile(file.FullName, output);
                }
            }

            return success;
        }
Пример #2
1
        private string GetOutputFilename(File file, string sourcePath)
        {
            var sourceFilename = Path.GetFileNameWithoutExtension(sourcePath);
            var extension = GetOutputExtension();

            try
            {
                if (_configuration.Value.OutputFilenameFactory != null)
                {
                    var filename = _configuration.Value.OutputFilenameFactory(file);

                    filename = filename
                        .Replace("<", "-")
                        .Replace(">", "-")
                        .Replace(":", "-")
                        .Replace("\"", "-")
                        //.Replace("/", "-")
                        //.Replace("\\", "-")
                        .Replace("|", "-")
                        .Replace("?", "-")
                        .Replace("*", "-");

                    if (filename.Contains(".") == false)
                        filename += extension;

                    return filename;
                }
            }
            catch (Exception exception)
            {
                Log.Warn($"Can't get output filename for '{sourcePath}' ({exception.Message})");
            }

            return sourceFilename + extension;
        }
Пример #3
0
        protected virtual void SaveFile(File file, string output, ref bool success)
        {
            ProjectItem item;
            var         outputPath = GetOutputPath(file);

            if (string.Equals(file.FullName, outputPath, StringComparison.InvariantCultureIgnoreCase))
            {
                Log.Error("Output filename cannot match source filename.");
                success = false;
                return;
            }

            if (HasChanged(outputPath, output))
            {
                CheckOutFileFromSourceControl(outputPath);

                System.IO.File.WriteAllText(outputPath, output);
                item = FindProjectItem(outputPath) ?? _projectItem.ProjectItems.AddFromFile(outputPath);
            }
            else
            {
                item = FindProjectItem(outputPath);
            }

            SetMappedSourceFile(item, file.FullName);
        }
Пример #4
0
        private string GetOutputPath(File file)
        {
            var path       = file.FullName;
            var directory  = Path.GetDirectoryName(_templatePath);
            var filename   = GetOutputFilename(file, path);
            var outputPath = Path.Combine(directory, filename);

            for (var i = 1; i < 1000; i++)
            {
                var item = FindProjectItem(outputPath);
                if (item == null)
                {
                    return(outputPath);
                }

                var mappedSourceFile = GetMappedSourceFile(item);
                if (mappedSourceFile == null || path.Equals(mappedSourceFile, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(outputPath);
                }

                var name = filename.EndsWith(".d.ts", StringComparison.OrdinalIgnoreCase) ?
                           filename.Substring(0, filename.Length - 5) :
                           filename.Substring(0, filename.LastIndexOf(".", StringComparison.Ordinal));

                var extension = filename.EndsWith(".d.ts", StringComparison.OrdinalIgnoreCase) ?
                                ".d.ts" :
                                filename.Substring(filename.LastIndexOf(".", StringComparison.Ordinal));

                outputPath = Path.Combine(directory, $"{name} ({i}){extension}");
            }

            throw new Exception("GetOutputPath");
        }
Пример #5
0
        public void DeleteFile(File file)
        {
            var outputPath = GetOutputPath(file);

            //var item = GetExistingItem(path);
            //item?.Delete();
            if (System.IO.File.Exists(outputPath))
            {
                System.IO.File.Delete(outputPath);
                Console.WriteLine($"Deleted: {outputPath}");
            }
        }
Пример #6
0
        protected virtual string SaveFile(File file, string output, ref bool success)
        {
            //ProjectItem item;
            var outputPath = GetOutputPath(file);

            if (string.Equals(file.FullName, outputPath, StringComparison.InvariantCultureIgnoreCase))
            {
                //Log.Error(");
                Console.WriteLine($"Output filename cannot match source filename.");
                success = false;
                return(outputPath);
            }

            var hasChanged = HasChanged(outputPath, output);

//            if (ExtensionPackage.Instance.Options.AddGeneratedFilesToProject == false)
//            {
//                if (hasChanged)
//                {
//                    WriteFile(outputPath, output);
//                    Log.Debug($"Output file '{outputPath}' saved.");
//                }
//                return;
//            }

            if (hasChanged)
            {
                //CheckOutFileFromSourceControl(outputPath);
                WriteFile(outputPath, output);
                Console.WriteLine($"Generated: {outputPath}");
//                item = FindProjectItem(outputPath);
//
//                if (item == null)
//                {
//                    try
//                    {
//                        item = _projectItem.ProjectItems.AddFromFile(outputPath);
//                    }
//                    catch (Exception exception)
//                    {
//                        Log.Error($"Unable to add '{outputPath}' to project. {exception.Message}");
//                    }
//                }
            }
            else
            {
                Console.WriteLine($"No changes: {outputPath}");
            }

            return(outputPath);
            //SetMappedSourceFile(item, file.FullName);
        }
Пример #7
0
 public string Render(File file, out bool success)
 {
     try
     {
         return(Parser.Parse(_projectItem, file.FullName, _template.Value, _customExtensions, file, out success));
     }
     catch (Exception ex)
     {
         Log.Error(ex.Message + " Template: " + _templatePath);
         success = false;
         return(null);
     }
 }
Пример #8
0
        protected virtual void SaveFile(File file, string output, ref bool success)
        {
            ProjectItem item;
            var         outputPath = GetOutputPath(file);

            if (string.Equals(file.FullName, outputPath, StringComparison.InvariantCultureIgnoreCase))
            {
                Log.Error("Output filename cannot match source filename.");
                success = false;
                return;
            }

            var hasChanged = HasChanged(outputPath, output);

            if (ExtensionPackage.Instance.AddGeneratedFilesToProject == false ||
                _configuration.Value.SkipAddingGeneratedFilesToProject)
            {
                if (hasChanged)
                {
                    WriteFile(outputPath, output);
                    Log.Debug($"Output file '{outputPath}' saved.");
                }
                return;
            }

            if (hasChanged)
            {
                CheckOutFileFromSourceControl(outputPath);
                WriteFile(outputPath, output);
                item = FindProjectItem(outputPath);

                if (item == null)
                {
                    try
                    {
                        item = _projectItem.ProjectItems.AddFromFile(outputPath);
                    }
                    catch (Exception exception)
                    {
                        Log.Error($"Unable to add '{outputPath}' to project. {exception.Message}");
                    }
                }
            }
            else
            {
                item = FindProjectItem(outputPath);
            }

            SetMappedSourceFile(item, file.FullName);
        }
Пример #9
0
        public bool RenderFile(File file)
        {
            var output = Render(file, out var success);

            if (success)
            {
                if (output == null)
                {
                    DeleteFile(file.FullName);
                }
                else
                {
                    SaveFile(file, output, ref success);
                }
            }

            return(success);
        }
Пример #10
0
        public void RenameFile(File file, string oldPath, string newPath)
        {
            var item = GetExistingItem(oldPath);

            if (item != null)
            {
                if (Path.GetFileName(oldPath)?.Equals(Path.GetFileName(newPath)) ?? false)
                {
                    SetMappedSourceFile(item, newPath);
                    return;
                }

                var newOutputPath = GetOutputPath(file);

                item.Name = Path.GetFileName(newOutputPath);
                SetMappedSourceFile(item, newPath);
            }
        }
Пример #11
0
        public bool RenderFile(File file, bool saveProjectFile)
        {
            bool success;
            var  output = Render(file, out success);

            if (success)
            {
                if (output == null)
                {
                    DeleteFile(file.FullName, saveProjectFile);
                }
                else
                {
                    SaveFile(file.FullName, output, saveProjectFile);
                }
            }

            return(success);
        }
Пример #12
0
        protected virtual void SaveFile(File file, string output)
        {
            ProjectItem item;
            var         outputPath = GetOutputPath(file);

            if (HasChanged(outputPath, output))
            {
                CheckOutFileFromSourceControl(outputPath);

                System.IO.File.WriteAllText(outputPath, output);
                item = FindProjectItem(outputPath) ?? _projectItem.ProjectItems.AddFromFile(outputPath);
            }
            else
            {
                item = FindProjectItem(outputPath);
            }

            SetMappedSourceFile(item, file.FullName);
        }
Пример #13
0
        public string RenderFile(File file)
        {
            bool success;
            var  output = Render(file, out success);

            if (success)
            {
                if (output == null)
                {
                    //DeleteFile(file);
                }
                else
                {
                    return(SaveFile(file, output, ref success));
                }
            }

            return(null);
        }
Пример #14
0
        private string GetOutputFilename(File file, string sourcePath)
        {
            var sourceFilename = Path.GetFileNameWithoutExtension(sourcePath);
            var extension      = GetOutputExtension();

            try
            {
                if (_configuration.Value.OutputFilenameFactory != null)
                {
                    var filename = _configuration.Value.OutputFilenameFactory(file);

                    filename = filename
                               .Replace("<", "-")
                               .Replace(">", "-")
                               .Replace(":", "-")
                               .Replace("\"", "-")
                               //.Replace("/", "-")
                               //.Replace("\\", "-")
                               .Replace("|", "-")
                               .Replace("?", "-")
                               .Replace("*", "-");

                    if (filename.Contains(".") == false)
                    {
                        filename += extension;
                    }

                    return(filename);
                }
            }
            catch (Exception exception)
            {
                //Log.Warn($"Can't get output filename for '{sourcePath}' ({exception.Message})");
                Console.WriteLine($"Warn: Can't get output filename for '{sourcePath}' ({exception.Message})");
            }

            return(sourceFilename + extension);
        }
 protected override void SaveFile(File file, string output)
 {
     Trace.WriteLine("SaveFile intercepted");
     Trace.WriteLine("Output: ");
     Trace.WriteLine(output);
 }
Пример #16
0
 public string Render(File file, out bool success)
 {
     return(Parser.Parse(_template, _customExtensions, file, out success));
 }
Пример #17
0
 protected override void SaveFile(File file, string output)
 {
     Trace.WriteLine("SaveFile intercepted");
     Trace.WriteLine("Output: ");
     Trace.WriteLine(output);
 }
Пример #18
0
        public string Render(File file, out bool success)
        {
            try
            {
                return Parser.Parse(_projectItem, file.FullName, _template.Value, _customExtensions, file, out success);

            }
            catch (Exception ex)
            {
                Log.Error(ex.Message + " Template: " + _templatePath);
                success = false;
                return null;
            }
        }
Пример #19
0
        protected virtual void SaveFile(File file, string output)
        {

            ProjectItem item;
            var outputPath = GetOutputPath(file);

            if (HasChanged(outputPath, output))
            {
                CheckOutFileFromSourceControl(outputPath);

                System.IO.File.WriteAllText(outputPath, output);
                item = FindProjectItem(outputPath) ?? _projectItem.ProjectItems.AddFromFile(outputPath);
            }
            else
            {
                item = FindProjectItem(outputPath);
            }

            SetMappedSourceFile(item, file.FullName);


        }
Пример #20
0
 protected FileTests(ITestFixture fixture) : base(fixture)
 {
     fileInfo = GetFile(@"Tests\CodeModel\Support\FileInfo.cs");
 }
Пример #21
0
        public void RenameFile(File file, string oldPath, string newPath)
        {
            var item = GetExistingItem(oldPath);

            if (item != null)
            {
                if (Path.GetFileName(oldPath)?.Equals(Path.GetFileName(newPath)) ?? false)
                {
                    SetMappedSourceFile(item, newPath);

                    return;
                }

                var newOutputPath = GetOutputPath(file);

                item.Name = Path.GetFileName(newOutputPath);
                SetMappedSourceFile(item, newPath);

            }

        }
Пример #22
0
        private string GetOutputPath(File file)
        {
            var path = file.FullName;
            var directory = Path.GetDirectoryName(_templatePath);
            var filename = GetOutputFilename(file, path);
            var outputPath = Path.Combine(directory, filename);

            for (var i = 1; i < 1000; i++)
            {
                var item = FindProjectItem(outputPath);
                if (item == null) return outputPath;

                var mappedSourceFile = GetMappedSourceFile(item);
                if (mappedSourceFile == null || path.Equals(mappedSourceFile, StringComparison.InvariantCultureIgnoreCase)) return outputPath;

                var name = filename.EndsWith(".d.ts", StringComparison.OrdinalIgnoreCase) ?
                    filename.Substring(0, filename.Length - 5) :
                    filename.Substring(0, filename.LastIndexOf(".", StringComparison.Ordinal));

                var extension = filename.EndsWith(".d.ts", StringComparison.OrdinalIgnoreCase) ?
                    ".d.ts" :
                    filename.Substring(filename.LastIndexOf(".", StringComparison.Ordinal));

                outputPath = Path.Combine(directory, $"{name} ({i}){extension}");
            }

            throw new Exception("GetOutputPath");
        }
Пример #23
0
 public string Render(File file, out bool success)
 {
     return Parser.Parse(_template, _customExtensions, file, out success);
 }
Пример #24
0
 protected FileTests(ITestFixture fixture, GlobalServiceProvider sp) : base(fixture, sp)
 {
     fileInfo = GetFile(@"Tests\CodeModel\Support\FileInfo.cs");
 }