public static int Main(String[] args) { if (args.Length < 2 || args.Length > 4) { usage(); return(1); } MgaProject mgaProject; List <String> lp_FilesToImport = new List <string>(); string avmFilePath = ""; string mgaProjectPath = ""; string componentReplacementPath = ""; bool rOptionUsed = false; bool pOptionUsed = false; for (int ix = 0; ix < args.Length; ++ix) { if (args[ix].ToLower() == "-r") { if (pOptionUsed) { usage(); return(1); } rOptionUsed = true; if (++ix >= args.Length) { usage(); return(1); } if (avmFilePath != null && avmFilePath != "") { if (mgaProjectPath != null && mgaProjectPath != "") { usage(); return(1); } mgaProjectPath = avmFilePath; avmFilePath = ""; lp_FilesToImport.Clear(); } String sImportDirectory = args[ix]; String startingDirectory = Path.GetFullPath(sImportDirectory); string[] xmlFiles = Directory.GetFiles(startingDirectory, "*.acm", SearchOption.AllDirectories); foreach (String p_XMLFile in xmlFiles) { lp_FilesToImport.Add(Path.GetFullPath(p_XMLFile)); } } else if (args[ix].ToLower() == "-p") { if (rOptionUsed) { usage(); return(1); } pOptionUsed = true; if (++ix >= args.Length) { usage(); return(1); } componentReplacementPath = args[ix]; } else if (lp_FilesToImport.Count == 0 && avmFilePath == "") { avmFilePath = args[ix]; try { lp_FilesToImport.Add(Path.GetFullPath(avmFilePath)); } catch (System.ArgumentException ex) { Console.Out.WriteLine(ex.Message); Console.Out.WriteLine(avmFilePath); throw ex; } } else { if (mgaProjectPath != null && mgaProjectPath != "") { usage(); return(1); } mgaProjectPath = args[ix]; } } mgaProject = GetProject(mgaProjectPath); try { bool bExceptionOccurred = false; if (mgaProject != null) { MgaGateway mgaGateway = new MgaGateway(mgaProject); mgaGateway.PerformInTransaction(delegate { string libroot = Path.GetDirectoryName(Path.GetFullPath(mgaProjectPath)); CyPhyML.RootFolder cyPhyMLRootFolder = ISIS.GME.Common.Utils.CreateObject <CyPhyMLClasses.RootFolder>(mgaProject.RootFolder as MgaObject); #region Attach QUDT library if needed IMgaFolder oldQudt = mgaProject.RootFolder.ChildFolders.Cast <IMgaFolder>().Where(x => x.LibraryName != "" && (x.Name.ToLower().Contains("qudt"))).FirstOrDefault(); string mgaQudtPath = Meta_Path + "\\meta\\CyPhyMLQudt.mga"; bool needAttach = false; if (oldQudt == null) { needAttach = true; } else { long loldModTime; DateTime oldModTime = long.TryParse(oldQudt.RegistryValue["modtime"], out loldModTime) ? DateTime.FromFileTimeUtc(loldModTime) : DateTime.MinValue; needAttach = System.IO.File.GetLastWriteTimeUtc(mgaQudtPath).CompareTo(oldModTime) > 0; if (!needAttach) { Console.Error.WriteLine("QUDT is up-to-date: embedded library modified " + oldModTime.ToString() + ", CyPhyMLQudt.mga modified " + System.IO.File.GetLastWriteTimeUtc(mgaQudtPath).ToString()); } } if (needAttach) { Console.Error.WriteLine("Attaching library " + mgaQudtPath); ISIS.GME.Common.Interfaces.RootFolder newQudt = ISIS.GME.Common.Classes.RootFolder.GetRootFolder(mgaProject).AttachLibrary("MGA=" + mgaQudtPath); DateTime modtime = System.IO.File.GetLastWriteTimeUtc(mgaQudtPath); ((newQudt as ISIS.GME.Common.Classes.RootFolder).Impl as GME.MGA.IMgaFolder).RegistryValue["modtime"] = modtime.ToFileTimeUtc().ToString(); if (oldQudt != null) { ReferenceSwitcher.Switcher sw = new ReferenceSwitcher.Switcher(oldQudt, newQudt.Impl, null); sw.UpdateSublibrary(); oldQudt.DestroyObject(); } ((newQudt as ISIS.GME.Common.Classes.RootFolder).Impl as GME.MGA.IMgaFolder).LibraryName = "UnitLibrary QUDT"; Console.Error.WriteLine((oldQudt == null ? "Attached " : "Refreshed") + " Qudt library."); } #endregion var importer = new CyPhyComponentImporter.CyPhyComponentImporterInterpreter(); importer.Initialize(cyPhyMLRootFolder.Impl.Project); importer.ImportFiles(cyPhyMLRootFolder.Impl.Project, libroot, lp_FilesToImport.ToArray(), true); importer.DisposeLogger(); bExceptionOccurred = importer.Errors.Count > 0; }, abort: false); mgaProject.Save(); if (bExceptionOccurred) { return(-1); } } } finally { mgaProject.Close(true); } return(0); }
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; } }