Exemplo n.º 1
0
        /// <summary>
        /// Given a CyPhy Component, builds a .ZIP-format Component Package, which includes
        /// an ACM version, as well as all artifacts from the component's
        /// backend folder. Note that it will grab ALL artifacts from that folder, not just
        /// those called out as Resources by the Component. It will also exclude any
        /// *.acm file found in that folder, opting to create a new ACM file from the
        /// current state of the component.
        /// </summary>
        /// <param name="component"></param>
        /// <param name="outputFolder">The folder where the component package should be created. If null, a temporary folder will be used.</param>
        /// <returns>The absolute path of the component package.</returns>
        public static String ExportComponentPackage(CyPhy.Component component, String outputFolder = null)
        {
            if (String.IsNullOrWhiteSpace(outputFolder))
            {
                // Assign a temp folder
                outputFolder = Path.Combine(System.IO.Path.GetTempPath(), Path.GetRandomFileName());
                Directory.CreateDirectory(outputFolder);
            }

            String acmFilePath = String.Format("{0}\\{1}.component.acm", outputFolder, System.IO.Path.GetRandomFileName());

            if (acmFilePath == null)
            {
                return(null);
            }

            avm.Component avmComponent;

            var componentBuilder = new CyPhyML2AVM.AVMComponentBuilder();

            avmComponent = componentBuilder.CyPhyML2AVMNonStatic(component);

            var safe_component_name = InvalidFileNameRegex.Replace(component.Name, "_");

            // Create a ZIP filename
            String filename = String.Format("{0}.zip", safe_component_name);

            // If not unique, try a few different ones
            int counter = 1;

            while (File.Exists(Path.Combine(outputFolder, filename)))
            {
                filename = String.Format("{0}({1}).zip", safe_component_name, counter++);
            }

            String zipFileAbsPath = Path.Combine(outputFolder, filename);

            using (ZipFile zip = new ZipFile(zipFileAbsPath)
            {
                CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression
            })
            {
                String compDirAbsPath = component.GetDirectoryPath(META.ComponentLibraryManager.PathConvention.ABSOLUTE);
                IEnumerable <string> filePaths;
                if (Directory.Exists(compDirAbsPath))
                {
                    filePaths = Directory
                                .EnumerateFiles(compDirAbsPath, "*.*", SearchOption.AllDirectories)
                                .Where(f => Path.GetExtension(f).ToLower() != ".acm" &&
                                       Path.GetFileName(f).ToLower() != "componentdata.xml");
                }
                else
                {
                    filePaths = new string[] { };
                }
                foreach (var filePath in filePaths)
                {
                    String fileRelDir;
                    if (compDirAbsPath.EndsWith("/"))
                    {
                        fileRelDir = Path.GetDirectoryName(
                            ComponentLibraryManager.MakeRelativePath(compDirAbsPath, filePath));
                    }
                    else
                    {
                        fileRelDir = Path.GetDirectoryName(
                            ComponentLibraryManager.MakeRelativePath(compDirAbsPath + "/", filePath));
                    }

                    zip.AddFile(filePath, fileRelDir);
                    Match match = cadResourceRegex.Match(Path.Combine(fileRelDir, Path.GetFileName(filePath)));
                    if (match.Success)
                    {
                        Func <Resource, bool> sameFile = delegate(Resource x) {
                            if (canonicalizeRelativePath(x.Path).Equals(canonicalizeRelativePath(match.Groups[1].Value + match.Groups[2].Value), StringComparison.InvariantCultureIgnoreCase))
                            {
                                return(true);
                            }
                            Match m = cadResourceRegex.Match(x.Path);
                            return(m.Success && m.Groups[1].Value.Equals(match.Groups[1].Value, StringComparison.InvariantCultureIgnoreCase) &&
                                   m.Groups[2].Value.Equals(match.Groups[2].Value, StringComparison.InvariantCultureIgnoreCase));
                        };
                        foreach (var resource in avmComponent.ResourceDependency.Where(sameFile))
                        {
                            Match currentMatch = cadResourceRegex.Match(resource.Path);
                            if (currentMatch.Success == false || (Int32.Parse(match.Groups[3].Value) > Int32.Parse(currentMatch.Groups[3].Value)))
                            {
                                resource.Path = Path.Combine(fileRelDir, Path.GetFileName(filePath));
                            }
                        }
                    }
                }
                // Add the ACM file.
                SerializeAvmComponent(avmComponent, acmFilePath);

                zip.AddFile(acmFilePath, "").FileName = String.Format("{0}.acm", safe_component_name);

                zip.Save();
            }

            // Delete that ACM file
            File.Delete(acmFilePath);

            return(zipFileAbsPath);
        }
        /// <summary>
        /// Given a CyPhy Component, builds a .ZIP-format Component Package, which includes
        /// an ACM version, as well as all artifacts from the component's
        /// backend folder. Note that it will grab ALL artifacts from that folder, not just
        /// those called out as Resources by the Component. It will also exclude any
        /// *.acm file found in that folder, opting to create a new ACM file from the
        /// current state of the component.
        /// </summary>
        /// <param name="component"></param>
        /// <param name="outputFolder">The folder where the component package should be created. If null, a temporary folder will be used.</param>
        /// <returns>The absolute path of the component package.</returns>
        public static String ExportComponentPackage(CyPhy.Component component, String outputFolder = null)
        {
            if (String.IsNullOrWhiteSpace(outputFolder))
            {
                // Assign a temp folder
                outputFolder = Path.Combine(System.IO.Path.GetTempPath(), Path.GetRandomFileName());
                Directory.CreateDirectory(outputFolder);
            }

            String acmFilePath = String.Format("{0}\\{1}.component.acm", outputFolder, System.IO.Path.GetRandomFileName());
            if (acmFilePath == null)
            {
                return null;
            }

            avm.Component avmComponent;

            var componentBuilder = new CyPhyML2AVM.AVMComponentBuilder();
            avmComponent = componentBuilder.CyPhyML2AVMNonStatic(component);

            // Create a ZIP filename
            String filename = String.Format("{0}.zip", component.Name);
            
            // If not unique, try a few different ones
            int counter = 1;
            while (File.Exists(Path.Combine(outputFolder, filename)))
            {
                filename = String.Format("{0}({1}).zip", component.Name, counter++);
            }

            String zipFileAbsPath = Path.Combine(outputFolder, filename);
            using (ZipFile zip = new ZipFile(zipFileAbsPath)
            {
                CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression
            })
            {
                String compDirAbsPath = component.GetDirectoryPath(META.ComponentLibraryManager.PathConvention.ABSOLUTE);
                foreach (var filePath in Directory
                                    .EnumerateFiles(compDirAbsPath,"*.*",SearchOption.AllDirectories)
                                    .Where(f => Path.GetExtension(f).ToLower() != ".acm"
                                             && Path.GetFileName(f).ToLower() != "componentdata.xml"))
                {
                    String fileRelDir;
                    if (compDirAbsPath.EndsWith("/"))
                    {
                        fileRelDir = Path.GetDirectoryName(
                                                ComponentLibraryManager.MakeRelativePath(compDirAbsPath, filePath));
                    }
                    else
                    {
                        fileRelDir = Path.GetDirectoryName(
                                                ComponentLibraryManager.MakeRelativePath(compDirAbsPath + "/", filePath));
                    }


                    zip.AddFile(filePath, fileRelDir);
                    Match match = cadResourceRegex.Match(Path.Combine(fileRelDir, Path.GetFileName(filePath)));
                    if (match.Success)
                    {
                        Func<Resource, bool> sameFile = delegate (Resource x) {
                            if (x.Path == match.Groups[1].Value + match.Groups[2].Value)
                                return true;
                            Match m = cadResourceRegex.Match(x.Path);
                            return m.Success && m.Groups[1].Value == match.Groups[1].Value && m.Groups[2].Value == match.Groups[2].Value;
                        };
                        foreach (var resource in avmComponent.ResourceDependency.Where(sameFile))
                        {
                            Match currentMatch = cadResourceRegex.Match(resource.Path);
                            if (currentMatch.Success == false || (Int32.Parse(match.Groups[3].Value) > Int32.Parse(currentMatch.Groups[3].Value)))
                            {
                                resource.Path = Path.Combine(fileRelDir, Path.GetFileName(filePath));
                            }
                        }
                    }
                }
                // Add the ACM file.
                SerializeAvmComponent(avmComponent, acmFilePath);
                zip.AddFile(acmFilePath, "").FileName = String.Format("{0}.acm", component.Name);

                zip.Save();
            }

            // Delete that ACM file
            File.Delete(acmFilePath);

            return zipFileAbsPath;
        }