// ReSharper disable once SuggestBaseTypeForParameter
            public ProjectFile(List <FileToWrite> files, bool updateProjectFile, VsTfsSourceControlProvider tfs)
            {
                if (updateProjectFile && files.Count > 0)
                {
                    ProjectFiles = new SortedDictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                    // ReSharper disable once AssignNullToNotNullAttribute
                    var file = GetProjectPath(new DirectoryInfo(files[0].Directory));
                    ProjectFound = file != null;
                    if (file == null)
                    {
                        return;
                    }
                    ProjectPath           = file.FullName;
                    Lines                 = File.ReadAllLines(ProjectPath).ToList();
                    ProjectDir            = Path.GetDirectoryName(ProjectPath);
                    ProjectFileIndexStart = Lines.FindIndex(l => l.Contains("<Compile Include="));
                    foreach (var line in Lines.Skip(ProjectFileIndexStart).TakeWhile(l => l.Contains("<Compile Include=")))
                    {
                        ProjectFiles.Add(line, line);
                    }
                    ProjectFileIndexEnd = ProjectFileIndexStart + ProjectFiles.Count;

                    // Determine Line Format, defaulting if none currently exist
                    var first = ProjectFiles.Keys.FirstOrDefault() ?? "    <Compile Include=\"\" />";
                    LineFormat = first.Substring(0, first.IndexOf("\"", StringComparison.Ordinal) + 1) + "{0}" + first.Substring(first.LastIndexOf("\"", StringComparison.Ordinal), first.Length - first.LastIndexOf("\"", StringComparison.Ordinal));
                }

                UpdateProjectFile = updateProjectFile;
                ProjectUpdated    = false;
                Tfs = tfs;
            }
Exemplo n.º 2
0
        public void Save(string filePath)
        {
            var undoCheckoutIfUnchanged = FileRequiresUndoCheckout(filePath);

            var serializer        = new XmlSerializer(typeof(EarlyBoundGeneratorConfig));
            var xmlWriterSettings = new XmlWriterSettings
            {
                Indent = true,
                NewLineOnAttributes = true,
            };

            using (var xmlWriter = XmlWriter.Create(filePath, xmlWriterSettings))
            {
                serializer.Serialize(xmlWriter, this);
                xmlWriter.Close();
            }

            // Put pipe delimited values on new lines to make it easier to see changes in source control
            var xml = File.ReadAllText(filePath);

            xml = xml.Replace("|", "|" + Environment.NewLine);
            File.WriteAllText(filePath, xml);

            if (undoCheckoutIfUnchanged)
            {
                var tfs = new VsTfsSourceControlProvider();
                tfs.UndoCheckoutIfUnchanged(filePath);
            }
        }
Exemplo n.º 3
0
        public void Save(string filePath)
        {
            var undoCheckoutIfUnchanged = false;
            var attributes = File.GetAttributes(filePath);

            if (attributes.HasFlag(FileAttributes.ReadOnly))
            {
                attributes = attributes & ~FileAttributes.ReadOnly;
                if (this.ExtensionConfig.UseTfsToCheckoutFiles)
                {
                    try
                    {
                        var tfs = new VsTfsSourceControlProvider();
                        tfs.Checkout(filePath);
                        if (File.GetAttributes(filePath).HasFlag(FileAttributes.ReadOnly))
                        {
                            // something failed, just make it editable.
                            File.SetAttributes(filePath, attributes);
                        }
                        else
                        {
                            undoCheckoutIfUnchanged = true;
                        }
                    }
                    catch
                    {
                        // eat it and just make it editable.
                        File.SetAttributes(filePath, attributes);
                    }
                }
                else
                {
                    File.SetAttributes(filePath, attributes);
                }
            }

            var serializer        = new XmlSerializer(typeof(EarlyBoundGeneratorConfig));
            var xmlWriterSettings = new XmlWriterSettings
            {
                Indent = true,
                NewLineOnAttributes = true,
            };

            using (var xmlWriter = XmlWriter.Create(filePath, xmlWriterSettings))
            {
                serializer.Serialize(xmlWriter, this);
                xmlWriter.Close();
            }

            if (undoCheckoutIfUnchanged)
            {
                var tfs = new VsTfsSourceControlProvider();
                tfs.UndoCheckoutIfUnchanged(filePath);
            }
        }
Exemplo n.º 4
0
            // ReSharper disable once SuggestBaseTypeForParameter
            public ProjectFile(List <FileToWrite> files, bool updateProjectFile, VsTfsSourceControlProvider tfs)
            {
                if (updateProjectFile && files.Count > 0)
                {
                    ProjectFiles = new SortedDictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                    // ReSharper disable once AssignNullToNotNullAttribute
                    var file = GetProjectPath(new DirectoryInfo(files[0].Directory));
                    ProjectFound = file != null;
                    if (file == null)
                    {
                        return;
                    }
                    ProjectPath = file.FullName;
                    Lines       = File.ReadAllLines(ProjectPath).ToList();
                    ProjectDir  = Path.GetDirectoryName(ProjectPath);
                    if (!Lines.Any(l => l.Contains("<Compile Include=")))
                    {
                        ProjectFileIndexStart = Lines.FindLastIndex(l => l.Contains("</PropertyGroup>")) + 1;
                        Lines.Insert(ProjectFileIndexStart, "</ItemGroup>");
                        Lines.Insert(ProjectFileIndexStart, "<ItemGroup>");
                        ProjectFileIndexStart++;
                    }
                    else
                    {
                        ProjectFileIndexStart = Lines.FindIndex(l => l.Contains("<Compile Include="));
                    }
                    foreach (var line in Lines.Skip(ProjectFileIndexStart).TakeWhile(l => l.Contains("<Compile Include=") && !ProjectFiles.ContainsKey(l)))
                    {
                        ProjectFiles.Add(line, line);
                    }
                    ProjectFileIndexEnd = ProjectFileIndexStart + ProjectFiles.Count;

                    // Determine Line Format, defaulting if none currently exist
                    var first = ProjectFiles.Keys.FirstOrDefault() ?? (ProjectPath.EndsWith("projitems")
                                    ? "    <Compile Include=\"$(MSBuildThisFileDirectory)\" />"
                                    : "    <Compile Include=\"\" />");

                    var startEndIndex = first.Contains("$(")
                        ? first.IndexOf(")", first.IndexOf("$(", StringComparison.Ordinal), StringComparison.Ordinal) + 1 // Path contains Ms Build Variable
                        : first.IndexOf("\"", StringComparison.Ordinal) + 1;
                    LineFormat = first.Substring(0, startEndIndex) + "{0}" + first.Substring(first.LastIndexOf("\"", StringComparison.Ordinal), first.Length - first.LastIndexOf("\"", StringComparison.Ordinal));
                }

                UpdateProjectFile = updateProjectFile;
                ProjectUpdated    = false;
                Tfs = tfs;
            }
        private bool FileRequiresUndoCheckout(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return(false);
            }
            var attributes = File.GetAttributes(filePath);

            var undoCheckoutIfUnchanged = false;

            if (!attributes.HasFlag(FileAttributes.ReadOnly))
            {
                return(false);
            }

            attributes = attributes & ~FileAttributes.ReadOnly;
            if (ExtensionConfig.UseTfsToCheckoutFiles)
            {
                try
                {
                    var tfs = new VsTfsSourceControlProvider();
                    tfs.Checkout(filePath);
                    if (File.GetAttributes(filePath).HasFlag(FileAttributes.ReadOnly))
                    {
                        // something failed, just make it editable.
                        File.SetAttributes(filePath, attributes);
                    }
                    else
                    {
                        undoCheckoutIfUnchanged = true;
                    }
                }
                catch
                {
                    // eat it and just make it editable.
                    File.SetAttributes(filePath, attributes);
                }
            }
            else
            {
                File.SetAttributes(filePath, attributes);
            }

            return(undoCheckoutIfUnchanged);
        }
        public void Save(string filePath)
        {
            var undoCheckoutIfUnchanged = FileRequiresUndoCheckout(filePath);

            var serializer        = new XmlSerializer(typeof(EarlyBoundGeneratorConfig));
            var xmlWriterSettings = new XmlWriterSettings
            {
                Indent = true,
                NewLineOnAttributes = true,
            };

            using (var xmlWriter = XmlWriter.Create(filePath, xmlWriterSettings))
            {
                serializer.Serialize(xmlWriter, this);
                xmlWriter.Close();
            }

            if (undoCheckoutIfUnchanged)
            {
                var tfs = new VsTfsSourceControlProvider();
                tfs.UndoCheckoutIfUnchanged(filePath);
            }
        }
Exemplo n.º 7
0
        protected virtual void WriteInternal(IOrganizationMetadata organizationMetadata, string language, string outputFile, string targetNamespace, IServiceProvider services)
        {
            Metadata        = organizationMetadata;
            ServiceProvider = services;
            if (UseTfsToCheckoutFiles)
            {
                Tfs = new VsTfsSourceControlProvider(null, new ProcessExecutorInfo
                {
                    OnOutputReceived = Log,
                    OnErrorReceived  = DisplayMessage
                });
            }
            DisplayMessage("Ensuring Context File is Accessible");
            EnsureFileIsAccessible(outputFile);

            Log("Creating Temp file");
            var tempFile = Path.GetTempFileName();

            Log("File " + tempFile + " Created");

            // Write the file out as normal
            DisplayMessage($"Writing file {Path.GetFileName(outputFile)} to {tempFile}");
            DefaultService.Write(organizationMetadata, language, tempFile, targetNamespace, services);
            Log("Completed writing file {0} to {1}", Path.GetFileName(outputFile), tempFile);

            // Check if the Header needs to be updated and or the file needs to be split
            if (!string.IsNullOrWhiteSpace(CommandLineText) || RemoveRuntimeVersionComment)
            {
                var lines = GetFileTextWithUpdatedClassComment(tempFile, CommandLineText, RemoveRuntimeVersionComment);
                if (CreateOneFilePerCodeUnit)
                {
                    DisplayMessage($"Splitting File {outputFile} By Code Unit");
                    SplitFileByCodeUnit(SplitByCodeUnit, outputFile, lines);
                }
                else
                {
                    DisplayMessage($"Updating File {outputFile}");
                    File.WriteAllLines(outputFile, lines);
                    if (UndoCheckoutIfUnchanged(outputFile))
                    {
                        Console.WriteLine(outputFile + " was unchanged.");
                    }
                }
            }
            else if (CreateOneFilePerCodeUnit)
            {
                DisplayMessage($"Splitting File {outputFile} By Code Unit");
                SplitFileByCodeUnit(SplitByCodeUnit, outputFile, File.ReadAllLines(tempFile));
            }
            else
            {
                DisplayMessage($"Copying File {outputFile}");
                File.Copy(tempFile, outputFile, true);
                if (UndoCheckoutIfUnchanged(outputFile))
                {
                    Console.WriteLine(outputFile + " was unchanged.");
                }
            }

            DisplayMessage($"Cleaning up Temporary File {tempFile}");
            File.Delete(tempFile);
            Log("Completed cleaning up Temporary File");
            DisplayMessage(tempFile + " Moved To: " + outputFile);
        }