Exemplo n.º 1
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);
            }
        }
        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);
        }