internal static void UpdateServiceDefinition()

in Nodejs/Product/Nodejs/NodejsProject.cs [831:942]


        internal static void UpdateServiceDefinition(
            IVsHierarchy project,
            string roleType,
            string projectName,
            System.IServiceProvider site
        )
        {
            Utilities.ArgumentNotNull(nameof(project), project);

            ErrorHandler.ThrowOnFailure(project.GetProperty(
                (uint)VSConstants.VSITEMID.Root,
                (int)__VSHPROPID.VSHPROPID_FirstChild,
                out var obj
            ));

            while (TryGetItemId(obj, out var id))
            {

                if (ErrorHandler.Succeeded(project.GetGuidProperty(id, (int)__VSHPROPID.VSHPROPID_TypeGuid, out var itemType)) &&
                    itemType == VSConstants.GUID_ItemType_PhysicalFile &&
                    ErrorHandler.Succeeded(project.GetProperty(id, (int)__VSHPROPID.VSHPROPID_Name, out obj)) &&
                    "ServiceDefinition.csdef".Equals(obj as string, StringComparison.InvariantCultureIgnoreCase) &&
                    ErrorHandler.Succeeded(project.GetCanonicalName(id, out var mkDoc)) &&
                    !string.IsNullOrEmpty(mkDoc)
                )
                {
                    // We have found the file
                    var rdt = site.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;

                    var updateFileOnDisk = true;

                    if (ErrorHandler.Succeeded(rdt.FindAndLockDocument(
                        (uint)_VSRDTFLAGS.RDT_EditLock,
                        mkDoc,
                        out var docHier,
                        out var docId,
                        out var pDocData,
                        out var docCookie
                    )))
                    {
                        try
                        {
                            if (pDocData != IntPtr.Zero)
                            {
                                try
                                {
                                    // File is open, so edit it through the document
                                    UpdateServiceDefinition(
                                        Marshal.GetObjectForIUnknown(pDocData) as IVsTextLines,
                                        roleType,
                                        projectName
                                    );

                                    ErrorHandler.ThrowOnFailure(rdt.SaveDocuments(
                                        (uint)__VSRDTSAVEOPTIONS.RDTSAVEOPT_ForceSave,
                                        docHier,
                                        docId,
                                        docCookie
                                    ));

                                    updateFileOnDisk = false;
                                }
                                catch (ArgumentException)
                                {
                                }
                                catch (InvalidOperationException)
                                {
                                }
                                catch (COMException)
                                {
                                }
                                finally
                                {
                                    Marshal.Release(pDocData);
                                }
                            }
                        }
                        finally
                        {
                            ErrorHandler.ThrowOnFailure(rdt.UnlockDocument(
                                (uint)_VSRDTFLAGS.RDT_Unlock_SaveIfDirty | (uint)_VSRDTFLAGS.RDT_RequestUnlock,
                                docCookie
                            ));
                        }
                    }

                    if (updateFileOnDisk)
                    {
                        // File is not open, so edit it on disk
                        FileStream stream = null;
                        try
                        {
                            UpdateServiceDefinition(mkDoc, roleType, projectName);
                        }
                        finally
                        {
                            if (stream != null)
                            {
                                stream.Close();
                            }
                        }
                    }

                    break;
                }

                if (ErrorHandler.Failed(project.GetProperty(id, (int)__VSHPROPID.VSHPROPID_NextSibling, out obj)))
                {
                    break;
                }
            }
        }