Пример #1
0
 public void Initialize(InstallScript script)
 {
     _log.Length = 0;
     _script     = script;
     _lines      = _script.Lines.Where(l => l.Script != null && l.Type != InstallType.Warning).ToList();
     _currLine   = -1;
 }
Пример #2
0
        public InstallScript ConvertManifestXml(XmlDocument doc, string name)
        {
            ExportProcessor.EnsureSystemData(_conn, ref _itemTypes);

            foreach (var elem in doc.ElementsByXPath("//Item[@action='add']").ToList())
            {
                elem.SetAttribute("action", "merge");
            }
            ItemType itemType;

            foreach (var elem in doc.ElementsByXPath("//Item[@type and @id]").ToList())
            {
                if (_itemTypes.TryGetValue(elem.Attribute("type", "").ToLowerInvariant(), out itemType) && itemType.IsVersionable)
                {
                    elem.SetAttribute(XmlFlags.Attr_ConfigId, elem.Attribute("id"));
                    elem.SetAttribute("where", string.Format("[{0}].[config_id] = '{1}'", itemType.Name.Replace(' ', '_'), elem.Attribute("id")));
                    elem.RemoveAttribute("id");
                }
            }

            var result = new InstallScript();

            result.Title = name;
            _exportTools.Export(result, doc);
            return(result);
        }
Пример #3
0
        public virtual InstallScript Read()
        {
            var result = new InstallScript();

            XmlDocument doc;
            var         scripts  = new List <InstallItem>();
            var         manifest = new XmlDocument();
            string      path;

            manifest.Load(GetExistingStream(null));

            if (manifest.DocumentElement.HasAttribute("created"))
            {
                result.Created = DateTime.Parse(manifest.DocumentElement.GetAttribute("created"));
            }
            result.Creator     = manifest.DocumentElement.GetAttribute("creator");
            result.Description = manifest.DocumentElement.GetAttribute("description");
            if (manifest.DocumentElement.HasAttribute("modified"))
            {
                result.Modified = DateTime.Parse(manifest.DocumentElement.GetAttribute("modified"));
            }
            result.Version = manifest.DocumentElement.GetAttribute("revision");
            result.Title   = manifest.DocumentElement.GetAttribute("title");
            if (manifest.DocumentElement.HasAttribute("website"))
            {
                result.Website = new Uri(manifest.DocumentElement.GetAttribute("website"));
            }

            foreach (var child in manifest.DocumentElement.ChildNodes.OfType <XmlElement>())
            {
                if (child.LocalName == "Item")
                {
                    scripts.Add(InstallItem.FromScript(child));
                }
                else
                {
                    path = child.GetAttribute("path");
                    if (!string.IsNullOrEmpty(path))
                    {
                        if (path.EndsWith(".xslt", StringComparison.OrdinalIgnoreCase))
                        {
                            doc = ReadReport(path);
                        }
                        else
                        {
                            doc = new XmlDocument();
                            doc.Load(GetExistingStream(path));
                        }

                        foreach (var item in doc.DocumentElement.Elements("Item"))
                        {
                            scripts.Add(InstallItem.FromScript(item));
                        }
                    }
                }
            }
            result.Lines = scripts;

            return(result);
        }
Пример #4
0
        public override void Write(InstallScript script)
        {
            _package.PackageProperties.Created     = script.Created;
            _package.PackageProperties.Creator     = script.Creator;
            _package.PackageProperties.Description = script.Description;
            _package.PackageProperties.Modified    = script.Modified;
            _package.PackageProperties.Revision    = script.Version;
            _package.PackageProperties.Title       = script.Title;
            if (script.Website != null)
            {
                _package.PackageProperties.Identifier = script.Website.ToString();
            }

            base.Write(script);
        }
Пример #5
0
 public static IEnumerable <InstallItemDiff> GetDiffs(InstallScript left, InstallScript right)
 {
     return(GetDiffs(left.Lines, right.Lines));
 }
Пример #6
0
        public virtual void Write(InstallScript script)
        {
            string newPath;
            var    existingPaths = new HashSet <string>();

            // Record the import order
            var settings = new XmlWriterSettings();

            settings.OmitXmlDeclaration = true;
            settings.Indent             = true;
            settings.IndentChars        = "  ";
            using (var manifestStream = GetNewStream(null))
            {
                using (var manifestWriter = XmlWriter.Create(manifestStream, settings))
                {
                    manifestWriter.WriteStartElement("Import");

                    if (script.Created.HasValue)
                    {
                        manifestWriter.WriteAttributeString("created", script.Created.Value.ToString("s"));
                    }
                    manifestWriter.WriteAttributeString("creator", script.Creator);
                    manifestWriter.WriteAttributeString("description", script.Description);
                    if (script.Modified.HasValue)
                    {
                        manifestWriter.WriteAttributeString("modified", script.Modified.Value.ToString("s"));
                    }
                    manifestWriter.WriteAttributeString("revision", script.Version);
                    manifestWriter.WriteAttributeString("title", script.Title);
                    if (script.Website != null)
                    {
                        manifestWriter.WriteAttributeString("website", script.Website.ToString());
                    }

                    InstallItem first;
                    foreach (var group in script.GroupLines())
                    {
                        first = group.First();
                        if (first.Type == InstallType.DependencyCheck)
                        {
                            foreach (var line in group)
                            {
                                line.Script.WriteTo(manifestWriter);
                            }
                        }
                        else
                        {
                            switch (first.Reference.Type)
                            {
                            case "Report":
                                newPath = first.Reference.Type + "\\" + CleanFileName(first.Reference.KeyedName ?? first.Reference.Unique) + ".xslt";
                                if (existingPaths.Contains(newPath))
                                {
                                    newPath = first.Reference.Type + "\\" + CleanFileName((first.Reference.KeyedName ?? "") + "_" + first.Reference.Unique) + ".xslt";
                                }

                                WriteReport(group, newPath);
                                break;

                            default:
                                newPath = first.Reference.Type + "\\" + CleanFileName(first.Reference.KeyedName ?? first.Reference.Unique) + ".xml";
                                if (existingPaths.Contains(newPath))
                                {
                                    newPath = first.Reference.Type + "\\" + CleanFileName((first.Reference.KeyedName ?? "") + "_" + first.Reference.Unique) + ".xml";
                                }

                                using (var stream = GetNewStream(newPath))
                                {
                                    using (var writer = GetWriter(stream))
                                    {
                                        writer.WriteStartElement("AML");
                                        foreach (var line in group)
                                        {
                                            line.Script.WriteTo(writer);
                                        }
                                        writer.WriteEndElement();
                                    }
                                }
                                break;
                            }

                            existingPaths.Add(newPath);
                            manifestWriter.WriteStartElement("Path");
                            manifestWriter.WriteAttributeString("path", newPath);
                            manifestWriter.WriteEndElement();
                        }
                    }
                    manifestWriter.WriteEndElement();
                }
            }
        }
Пример #7
0
        public void Write(InstallScript script)
        {
            using (var xml = XmlTextWriter.Create(_path, _settings))
            {
                xml.WriteStartElement("imports");
                xml.WriteStartElement("package");
                xml.WriteAttributeString("name", script.Title);

                if (script.Title.StartsWith("com.aras.innovator"))
                {
                    if (script.Title.StartsWith("com.aras.innovator.solution."))
                    {
                        _baseFolderPath = InnovatorPackage.CleanFileName(script.Title).Substring(28).Replace('.', '\\') + "\\Import";
                        xml.WriteAttributeString("path", _baseFolderPath);
                    }
                    else
                    {
                        _baseFolderPath = InnovatorPackage.CleanFileName(script.Title).Replace('.', '\\');
                        xml.WriteAttributeString("path", ".\\");
                    }
                }
                else
                {
                    _baseFolderPath = InnovatorPackage.CleanFileName(script.Title) + "\\Import";
                    xml.WriteAttributeString("path", _baseFolderPath);
                }

                xml.WriteEndElement();
                xml.WriteEndElement();
            }

            _baseFolderPath = Path.Combine(Path.GetDirectoryName(_path), _baseFolderPath);

            XmlWriter   writer;
            InstallItem first;
            var         existingPaths = new HashSet <string>();
            string      newPath;

            foreach (var group in script.GroupLines(i => i.Type != InstallType.DependencyCheck))
            {
                first   = group.First();
                newPath = first.Reference.Type + "\\" + InnovatorPackage.CleanFileName(first.Reference.KeyedName ?? first.Reference.Unique) + ".xml";
                if (existingPaths.Contains(newPath))
                {
                    newPath = first.Reference.Type + "\\" + InnovatorPackage.CleanFileName((first.Reference.KeyedName ?? "") + "_" + first.Reference.Unique) + ".xml";
                }

                writer = GetWriter(newPath);
                try
                {
                    writer.WriteStartElement("AML");
                    foreach (var line in group)
                    {
                        line.Script.WriteTo(writer);
                    }
                    writer.WriteEndElement();
                    writer.Flush();
                }
                finally
                {
                    writer.Close();
                }

                existingPaths.Add(newPath);
            }
        }
Пример #8
0
        public bool Write(InstallScript script,
                          Func <string, DatabasePackageAction> errorHandler = null,
                          Action <int, string> reportProgress = null)
        {
            var cont       = true;
            var typeGroups = from l in script.Lines
                             where l.Type == InstallType.Create
                             group l by l.Reference.Type into typeGroup
                             select typeGroup;
            var    cnt           = typeGroups.Count();
            var    idx           = 0;
            var    packageGroups = new HashSet <string>();
            string currPackageId = null;

            while (cont)
            {
                IEnumerable <XmlElement> elements;
                foreach (var typeGroup in typeGroups)
                {
                    if (reportProgress != null)
                    {
                        reportProgress((int)(idx * 50.0 / cnt), string.Format("Checking for existing package elements ({0} of {1}) ", idx + 1, cnt));
                    }

                    if (typeGroup.First().Reference.Unique.IsGuid())
                    {
                        elements = _conn.GetItems("ApplyItem",
                                                  "<Item type=\"PackageElement\" action=\"get\" select=\"element_id,name,source_id\"><element_type>"
                                                  + typeGroup.Key
                                                  + "</element_type><element_id condition=\"in\">'"
                                                  + typeGroup.Select(i => i.Reference.Unique).Aggregate((p, c) => p + "','" + c)
                                                  + "'</element_id></Item>");
                    }
                    else
                    {
                        elements = _conn.GetItems("ApplyItem",
                                                  "<Item type=\"PackageElement\" action=\"get\" select=\"element_id,name,source_id\"><element_type>"
                                                  + typeGroup.Key
                                                  + "</element_type><element_id condition=\"in\">(select id from innovator.["
                                                  + typeGroup.Key.Replace(' ', '_')
                                                  + "] where "
                                                  + typeGroup.Select(i => i.Reference.Unique).Aggregate((p, c) => p + " or " + c)
                                                  + ")</element_id></Item>");
                    }

                    packageGroups.UnionWith(elements.Select(e => e.Element("source_id", "")));
                    idx++;
                }

                var packages = _conn.GetItems("ApplyItem",
                                              "<Item type=\"PackageDefinition\" action=\"get\" select=\"name\"><id condition=\"in\">(select SOURCE_ID FROM innovator.PACKAGEGROUP where id in ('"
                                              + packageGroups.Aggregate((p, c) => p + "','" + c)
                                              + "'))</id></Item>");
                currPackageId = packages.Where(p => p.Element("name", "") == script.Title).SingleOrDefault().Attribute("id");

                cont = false;
                if (packages.Any(p => p.Element("name", "") != script.Title))
                {
                    if (errorHandler != null)
                    {
                        var packageList = (from p in packages
                                           where p.Element("name", "") != script.Title
                                           select p.Element("name", ""))
                                          .Aggregate((p, c) => p + ", " + c);
                        switch (errorHandler("The package cannot be created because one or more elements exist in the packages: " + packageList))
                        {
                        case DatabasePackageAction.TryAgain:
                            cont = true;
                            break;

                        case DatabasePackageAction.RemoveElementsFromPackages:
                            foreach (var typeGroup in typeGroups)
                            {
                                if (reportProgress != null)
                                {
                                    reportProgress((int)(idx * 50.0 / cnt), string.Format("Removing package elements ({0} of {1}) ", idx + 1, cnt));
                                }

                                if (typeGroup.First().Reference.Unique.IsGuid())
                                {
                                    elements = _conn.GetItems("ApplyItem",
                                                              "<Item type=\"PackageElement\" action=\"purge\" where=\"[PackageElement].[element_type] = '"
                                                              + typeGroup.Key
                                                              + "' and [PackageElement].[element_id] in ('"
                                                              + typeGroup.Select(i => i.Reference.Unique).Aggregate((p, c) => p + "','" + c)
                                                              + "')\" />");
                                }
                                else
                                {
                                    elements = _conn.GetItems("ApplyItem",
                                                              "<Item type=\"PackageElement\" action=\"purge\" where=\"[PackageElement].[element_type] = '"
                                                              + typeGroup.Key
                                                              + "' and [PackageElement].[element_id] in (select id from innovator.["
                                                              + typeGroup.Key.Replace(' ', '_')
                                                              + "] where "
                                                              + typeGroup.Select(i => i.Reference.Unique).Aggregate((p, c) => p + " or " + c)
                                                              + ")\" />");
                                }

                                idx++;
                            }


                            break;

                        default:
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            // Try one more time to get the package
            if (string.IsNullOrEmpty(currPackageId))
            {
                var packages = _conn.GetItems("ApplyItem",
                                              "<Item type=\"PackageDefinition\" action=\"get\" select=\"name\"><name>" + script.Title + "</name></Item>");
                currPackageId = packages.SingleOrDefault().Attribute("id");
            }

            // Add the package
            if (string.IsNullOrEmpty(currPackageId))
            {
                var packages = _conn.GetItems("ApplyItem",
                                              "<Item type=\"PackageDefinition\" action=\"add\" ><name>" + script.Title + "</name></Item>", true);
                currPackageId = packages.SingleOrDefault().Attribute("id");
            }

            string groupId;

            foreach (var typeGroup in typeGroups)
            {
                if (reportProgress != null)
                {
                    reportProgress((int)(50 + idx * 50.0 / cnt), string.Format("Adding package elements of type ({0} of {1}) ", idx + 1, cnt));
                }

                groupId = _conn.GetItems("ApplyItem",
                                         "<Item type=\"PackageGroup\" action=\"merge\" where=\"[PackageGroup].[source_id] = '"
                                         + currPackageId
                                         + "' and [PackageGroup].[name] = '"
                                         + typeGroup.Key
                                         + "'\"><name>"
                                         + typeGroup.Key
                                         + "</name></Item>", true).SingleOrDefault().Attribute("id");

                foreach (var elem in typeGroup)
                {
                    _conn.GetItems("ApplyItem",
                                   "<Item type=\"PackageElement\" action=\"merge\" where=\"[PackageElement].[source_id] = '"
                                   + groupId
                                   + "' and [PackageElement].[element_id] = '"
                                   + (elem.InstalledId ?? elem.Reference.Unique)
                                   + "'\">"
                                   + "<element_type>" + typeGroup.Key + "</element_type>"
                                   + "<element_id>" + (elem.InstalledId ?? elem.Reference.Unique) + "</element_id>"
                                   + "<source_id>" + groupId + "</source_id>"
                                   + "<name>" + elem.Reference.KeyedName + "</name></Item>", true);
                }

                idx++;
            }

            return(true);
        }