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;
            }
        }
Exemple #2
0
        static int Main(string[] args)
        {
            bool   errorInUsage     = false;
            bool   mgaIsGiven       = true;
            bool   generateTemplate = false;
            bool   formattedOutput  = false;
            string outputDirectory  = Path.Combine(Environment.CurrentDirectory, "output");
            string statFileName     = "sample_stats.json";
            string inputFilename    = string.Empty;

            Directory.CreateDirectory(outputDirectory);

            // verify the input arguments
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].StartsWith("-"))
                {
                    switch (args[i].Substring(1))
                    {
                    case "p":
                    case "-prettyPrint":
                        formattedOutput = true;
                        break;

                    case "t":
                    case "-template":
                        generateTemplate = true;
                        break;

                    default:
                        errorInUsage = true;
                        break;
                    }
                }
                else
                {
                    inputFilename = args[i];

                    if (i < args.Length - 1)
                    {
                        Console.WriteLine("Arguments after filename are ignored.");
                    }

                    break;
                }
            }

            if (File.Exists(inputFilename))
            {
                if (Path.GetExtension(inputFilename).ToLowerInvariant() == ".mga")
                {
                    mgaIsGiven = true;
                }
                else if (Path.GetExtension(inputFilename).ToLowerInvariant() == ".xme")
                {
                    mgaIsGiven = false;
                }
                else
                {
                    errorInUsage = true;
                }
            }
            else
            {
                errorInUsage = true;
            }


            if (generateTemplate)
            {
                Statistics.Statistics stats = Process(null);
                SerializeStats(stats, Path.Combine(outputDirectory, statFileName), formattedOutput);
                return(0);
            }

            if (errorInUsage)
            {
                Console.WriteLine("Usage: {0} [arguments] inputfilename.[mga|xme]", Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location));
                Console.WriteLine("");
                Console.WriteLine("Arguments:");
                Console.WriteLine("  -p --prettyPrint   Indents the output json file.");
                Console.WriteLine("  -t --template      Generates a sample json file with random numbers.");
                return(1);
            }


            // copy the file to a temp directory
            string tempXmeFile = Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(inputFilename) + ".xme");
            string tempMgaFile = Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(inputFilename) + ".mga");

            statFileName = Path.GetFileNameWithoutExtension(inputFilename) + "_stat.json";

            MgaProject project = new MgaProject();

            if (mgaIsGiven)
            {
                if (inputFilename != tempMgaFile)
                {
                    Console.WriteLine("Copying MGA file to temp directory: {0}", tempMgaFile);
                    File.Copy(inputFilename, tempMgaFile, true);
                }

                Console.WriteLine("Opening project");
                int    mgaversion;
                string paradigmName;
                string paradigmVersion;
                object paradigmGUID;
                bool   ro_mode;

                project.QueryProjectInfo("MGA=" + tempMgaFile, out mgaversion, out paradigmName, out paradigmVersion, out paradigmGUID, out ro_mode);

                project.OpenEx("MGA=" + tempMgaFile, paradigmName, true);

                Console.WriteLine("Saving project as xme: {0}", tempXmeFile);
                // export to xme
                GME.MGA.Parser.MgaDumper dumper = new GME.MGA.Parser.MgaDumper();
                dumper.DumpProject(project, tempXmeFile);
            }
            else
            {
                if (inputFilename != tempXmeFile)
                {
                    Console.WriteLine("Copying XME file to temp directory: {0}", tempXmeFile);
                    File.Copy(inputFilename, tempXmeFile, true);
                }

                GME.MGA.Parser.MgaParser parser = new GME.MGA.Parser.MgaParser();
                string paradigmName;

                parser.GetXMLParadigm(tempXmeFile, out paradigmName);

                Console.WriteLine("Creating project");
                project.CreateEx("MGA=" + tempMgaFile, paradigmName, null);

                // import xme file
                Console.WriteLine("Importing project");

                parser.ParseProject(project, tempXmeFile);

                Console.WriteLine("Saving project as mga: {0}", tempMgaFile);
                project.Save("MGA=" + tempMgaFile);
            }

            if (project == null)
            {
                throw new Exception("Project is null.");
            }


            // process the project
            var statistics = Process(project);

            // read in file stats
            FileInfo mgaInfo = new FileInfo(tempMgaFile);
            FileInfo xmeInfo = new FileInfo(tempXmeFile);

            statistics.MgaSizeInBytes = mgaInfo.Length;
            statistics.XmeSizeInBytes = xmeInfo.Length;

            // serialize the object
            SerializeStats(statistics, Path.Combine(outputDirectory, statFileName), formattedOutput);


            return(0);
        }