Esempio n. 1
0
        public void CreatedObjectWithDefaultNamesShouldNotHaveSpaces()
        {
            var mgaReference = "MGA=" + mgaFile;

            MgaProject project = new MgaProject();

            project.EnableAutoAddOns(true);
            project.OpenEx(mgaReference, "CyPhyML", null);

            List <IMgaObject> createdObjects = new List <IMgaObject>();

            try
            {
                var terr = project.BeginTransactionInNewTerr(transactiontype_enum.TRANSACTION_NON_NESTED);

                // turn off CyPhySignalBlocksAddOn
                var addons = project.AddOnComponents.Cast <IMgaComponentEx>().Where(x => x.ComponentName == "CyPhySignalBlocksAddOnAddon");
                foreach (var addon in addons)
                {
                    try
                    {
                        addon.Enable(false);
                    }
                    catch (Exception ex)
                    {
                        // if one fails keep trying the other ones.
                    }
                }

                // Use default names empty suffix
                createdObjects = this.CreateObjectHierarchy(project, "");

                project.CommitTransaction();

                project.BeginTransaction(terr, transactiontype_enum.TRANSACTION_NON_NESTED);

                // check renames
                foreach (var obj in createdObjects)
                {
                    // this will fail on the first one
                    // FIXME: should we collect everything and print a detailed message about failures?
                    Assert.False(obj.Name.Contains(' '), string.Format("Name contains space, but it should not contain any spaces. {0} [{1}]", obj.Name, obj.MetaBase.DisplayedName));
                }

                project.AbortTransaction();
            }
            finally
            {
                project.Close(false);
            }

            Assert.True(File.Exists(mgaReference.Substring("MGA=".Length)));
        }
        private void AttachLibrary(LibraryInfo libraryInfo)
        {
            string mgaPath = metaPath + "\\" + libraryInfo.MgaName + ".mga";

            if ((project.ProjectStatus & PROJECT_STATUS_OPEN) == PROJECT_STATUS_OPEN)
            {
                if (!File.Exists(mgaPath))
                {
                    GMEConsole.Error.WriteLine("Path '" + mgaPath + "' does not exist. Cannot attach " + libraryInfo.MgaName);
                    return;
                }

                project.Notify(globalevent_enum.APPEVENT_LIB_ATTACH_BEGIN);
                try
                {
                    project.BeginTransaction(project.ActiveTerritory);

                    IMgaFolder oldLibFolder = project.RootFolder.ChildFolders.Cast <IMgaFolder>()
                                              .Where(x => string.IsNullOrWhiteSpace(x.LibraryName) == false && (x.Name.Contains(libraryInfo.DisplayName) || x.Name.Contains(libraryInfo.DisplayName))).FirstOrDefault();

                    bool needAttach;
                    if (oldLibFolder == null)
                    {
                        needAttach = true;

                        if (libraryPaths.Contains(Path.GetFullPath(project.ProjectConnStr.Substring("MGA=".Length))))
                        {
                            // Don't attach libraries to themselves
                            needAttach = false;
                        }
                    }
                    else
                    {
                        DateTime oldModTime;
                        long     loldModTime;
                        if (long.TryParse(oldLibFolder.RegistryValue["modtime"], out loldModTime))
                        {
                            oldModTime = DateTime.FromFileTimeUtc(loldModTime);
                        }
                        else
                        {
                            oldModTime = DateTime.MinValue;
                        }
                        needAttach = File.GetLastWriteTimeUtc(mgaPath).CompareTo(oldModTime) > 0;
                        if (!needAttach)
                        {
                            GMEConsole.Info.WriteLine("Library is up-to-date: embedded library modified " + oldModTime.ToString() +
                                                      ", " + libraryInfo.MgaName + " modified " + File.GetLastWriteTimeUtc(mgaPath).ToString());
                        }
                    }

                    if (needAttach)
                    {
                        MgaProject proj = (MgaProject)Activator.CreateInstance(Type.GetTypeFromProgID("Mga.MgaProject"));
                        int        mgaVersion;
                        string     paradigmName;
                        string     paradigmVersion;
                        object     paradigmGuid;
                        bool       readOnly;
                        proj.QueryProjectInfo("MGA=" + mgaPath, out mgaVersion, out paradigmName, out paradigmVersion, out paradigmGuid, out readOnly);

                        Guid guidP1     = ConvertToGUID(paradigmGuid);
                        Guid guidP2     = ConvertToGUID(project.RootMeta.GUID);
                        bool guidsEqual = guidP1.Equals(guidP2);

                        if (paradigmName != project.MetaName || !guidsEqual)
                        {
                            GMEConsole.Info.WriteLine("Skipping refresh of " + libraryInfo.DisplayName + " because it uses a different metamodel version than the current project.");
                            project.AbortTransaction();
                            // not true, but don't try again
                            libraryInfo.attachedLibrary = true;
                            return;
                        }

                        // GMEConsole.Info.WriteLine("Attaching library " + mgaPath);
                        RootFolder newLibFolder = Common.Classes.RootFolder.GetRootFolder(project).AttachLibrary("MGA=" + mgaPath);
                        DateTime   modtime      = File.GetLastWriteTimeUtc(mgaPath);
                        ((newLibFolder as ISIS.GME.Common.Classes.RootFolder).Impl as GME.MGA.IMgaFolder).RegistryValue["modtime"] =
                            modtime.ToFileTimeUtc().ToString();

                        if (oldLibFolder != null)
                        {
                            ReferenceSwitcher.Switcher sw = new ReferenceSwitcher.Switcher(oldLibFolder, newLibFolder.Impl, null);
                            sw.UpdateSublibrary();
                            oldLibFolder.DestroyObject();
                        }
                        ((newLibFolder as ISIS.GME.Common.Classes.RootFolder).Impl as GME.MGA.IMgaFolder).LibraryName = libraryInfo.DisplayName;
                        GMEConsole.Info.WriteLine((oldLibFolder == null ? "Attached " : "Refreshed ") + libraryInfo.MgaName + ".mga library.");
                    }
                    project.CommitTransaction();
                }
                catch (Exception e)
                {
                    GMEConsole.Error.WriteLine("Error refreshing library: " + SecurityElement.Escape(e.Message));
                    project.AbortTransaction();
                }
                finally
                {
                    project.Notify(globalevent_enum.APPEVENT_LIB_ATTACH_END);
                }

                libraryInfo.attachedLibrary = true;
            }
        }