src/Misc/EFTools/EntityDesign/VisualStudio/VsUtils.cs
// <summary>
//     Update the content of files in the project.
//     The method loops through the file names collection. For each file, it will:
//     - Check whether a doc data exist for the document
//     - If yes, write the content to the doc data and continue.
//     - If No, check whether the document is under source control and is not checked out.
//     - If the document is not under the source control, directly write to the file, save, and continue.
//     - If the document is under source control and not checked out, add the file to temporary collection; at the end, the code will check this
//     temporary collection and do bulk checkout for the files and update.
//     We are currently using this method to write build provider information in web.config as well as to update namespaces in the edmx files.
// </summary>
Update the content of files in the project.
The method loops through the file names collection. For each file, it will:
Check whether a doc data exist for the document
If yes, write the content to the doc data and continue.
If No, check whether the document is under source control and is not checked out.
If the document is not under the source control, directly write to the file, save, and continue.
If the document is under source control and not checked out, add the file to temporary collection; at the end, the code will check this
temporary collection and do bulk checkout for the files and update.
We are currently using this method to write build provider information in web.config as well as to update namespaces in the edmx files.
Update the content of files in the project . The method loops through the file names collection . For each file it will : Check whether a doc data exist for the document If yes write the content to the doc data and continue . If No check whether the document is under source control and is not checked out . If the document is not under the source control directly write to the file save and continue . If the document is under source control and not checked out add the file to temporary collection ; at the end the code will check this temporary collection and do bulk checkout for the files and update . We are currently using this method to write build provider information in web . config as well as to update namespaces in the edmx files .
[SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "Microsoft.VisualStudio.Shell.Interop.IVsPersistDocData2.SetDocDataDirty(System.Int32)")]
        internal static void WriteCheckoutFilesInProject(
            IServiceProvider serviceProvider,
            IDictionary<string, object> filesMap,
            GetObjectDataDelegate getObjectDataCallback,
            PersistObjectDataDelegate persistObjectDataCallback)
        {
            var filesToQueryEdit = new Dictionary<string, object>();
            var filesToQuerySave = new Dictionary<string, object>();
            foreach (var filePath in filesMap.Keys)
            {
                var fileDataObject = filesMap[filePath];
                var docCookie = VSConstants.VSCOOKIE_NIL;
                var docData = VSHelpers.GetDocData(serviceProvider, filePath, _VSRDTFLAGS.RDT_NoLock, out docCookie);
                if (docData != null)
                {
                    filesToQueryEdit.Add(filePath, fileDataObject);
                    filesToQuerySave.Add(filePath, fileDataObject); 
                }
                else
                {
                    filesToQuerySave.Add(filePath, fileDataObject);
                }
            }
            if (!VSHelpers.CheckOutFilesIfEditable(serviceProvider, filesToQueryEdit.Keys.ToArray()))
            {
                return;
            }
            if (!VSHelpers.CheckOutFilesIfSaveable(serviceProvider, filesToQuerySave.Keys.ToArray()))
            {
                return;
            }
            foreach (var filePath in filesToQueryEdit.Keys)
            {
                var fileDataObject = filesMap[filePath];
                var docCookie = VSConstants.VSCOOKIE_NIL;
                try
                {
                    var docData = VSHelpers.GetDocData(serviceProvider, filePath, _VSRDTFLAGS.RDT_EditLock, out docCookie);
                    Debug.Assert(docData != null, "Should have retrieved the doc data for the open file");
                    if (docData != null)
                    {
                        var textLines = VSHelpers.GetVsTextLinesFromDocData(docData);
                        Debug.Assert(textLines != null, "unable to get IVsTextLines from doc data");
                        if (textLines != null)
                        {
                            SetTextForVsTextLines(textLines, getObjectDataCallback(fileDataObject));
                            var persistDocData = docData as IVsPersistDocData2;
                            if (persistDocData != null)
                            {
                                persistDocData.SetDocDataDirty(1);
                                persistObjectDataCallback(fileDataObject, filePath);
                            }
                        }
                    }
                }
                finally
                {
                    if (docCookie != VSConstants.VSCOOKIE_NIL)
                    {
                        UnlockRunningDocument(Services.ServiceProvider, docCookie, _VSRDTFLAGS.RDT_EditLock);
                    }
                }
            }
            foreach (var filePath in filesToQuerySave.Keys)
            {
                var fileDataObject = filesMap[filePath];
                persistObjectDataCallback(fileDataObject, filePath);
            }
        }
[ SuppressMessage ( " " , " " , MessageId = " " ) ] internal static void WriteCheckoutFilesInProject ( IServiceProvider serviceProvider , IDictionary < string , object > filesMap , GetObjectDataDelegate getObjectDataCallback , PersistObjectDataDelegate persistObjectDataCallback ) { var filesToQueryEdit = new Dictionary < string , object > ( ) ; var filesToQuerySave = new Dictionary < string , object > ( ) ; foreach ( var filePath in filesMap . Keys ) { var fileDataObject = filesMap [ filePath ] ; var docCookie = VSConstants . VSCOOKIE_NIL ; var docData = VSHelpers . GetDocData ( serviceProvider , filePath , _VSRDTFLAGS . RDT_NoLock , out docCookie ) ; if ( docData != null ) { filesToQueryEdit . Add ( filePath , fileDataObject ) ; filesToQuerySave . Add ( filePath , fileDataObject ) ; } else { filesToQuerySave . Add ( filePath , fileDataObject ) ; } } if ( ! VSHelpers . CheckOutFilesIfEditable ( serviceProvider , filesToQueryEdit . Keys . ToArray ( ) ) ) { return ; } if ( ! VSHelpers . CheckOutFilesIfSaveable ( serviceProvider , filesToQuerySave . Keys . ToArray ( ) ) ) { return ; } foreach ( var filePath in filesToQueryEdit . Keys ) { var fileDataObject = filesMap [ filePath ] ; var docCookie = VSConstants . VSCOOKIE_NIL ; try { var docData = VSHelpers . GetDocData ( serviceProvider , filePath , _VSRDTFLAGS . RDT_EditLock , out docCookie ) ; Debug . Assert ( docData != null , " " ) ; if ( docData != null ) { var textLines = VSHelpers . GetVsTextLinesFromDocData ( docData ) ; Debug . Assert ( textLines != null , " " ) ; if ( textLines != null ) { SetTextForVsTextLines ( textLines , getObjectDataCallback ( fileDataObject ) ) ; var persistDocData = docData as IVsPersistDocData2 ; if ( persistDocData != null ) { persistDocData . SetDocDataDirty ( 1 ) ; persistObjectDataCallback ( fileDataObject , filePath ) ; } } } } finally { if ( docCookie != VSConstants . VSCOOKIE_NIL ) { UnlockRunningDocument ( Services . ServiceProvider , docCookie , _VSRDTFLAGS . RDT_EditLock ) ; } } } foreach ( var filePath in filesToQuerySave . Keys ) { var fileDataObject = filesMap [ filePath ] ; persistObjectDataCallback ( fileDataObject , filePath ) ; } }
Update the content of files in the project.
Update the content of files in the project .
// we will also save the files after editing them // Process all files that need to be edit-queried // Process all files that need to be save-queried // If we've gotten this far, we are ready to process the files. Start with the ones that are open. // do a double-check here to see if a doc data exists. If there is one, we should update // that instead of propagating changes to disk. // Process all files that are not open
serviceProvider
IServiceProvider
filesMap
IDictionary<string, object>
getObjectDataCallback
GetObjectDataDelegate
persistObjectDataCallback
PersistObjectDataDelegate