示例#1
0
        public PyRevitExtension(string extensionPath)
        {
            InstallPath = extensionPath;
            var extDefFile = GetExtensionDefFilePath(InstallPath);

            if (CommonUtils.VerifyFile(extDefFile))
            {
                Definition = new PyRevitExtensionDefinition(extDefFile);
            }
            else
            {
                // if def file is not found try to grab the definitions from registered extensions
                try {
                    Definition = PyRevitExtensions.FindRegisteredExtension(Name);
                }
                catch {
                    // let Definition be null if extension is not registered
                }
            }
        }
示例#2
0
        // other private helprs  =====================================================================================
        // find extension with search patten in extension lookup resource (file or url to a remote file)
        // @handled @logs
        private static List <PyRevitExtensionDefinition> LookupExtensionInDefinitionFile(
            string fileOrUri,
            string searchPattern = null)
        {
            var    pyrevtExts = new List <PyRevitExtensionDefinition>();
            string filePath   = null;

            // determine if path is file or uri
            logger.Debug("Determining file or remote source \"{0}\"", fileOrUri);
            Uri uriResult;
            var validPath = Uri.TryCreate(fileOrUri, UriKind.Absolute, out uriResult);

            if (validPath)
            {
                if (uriResult.IsFile)
                {
                    filePath = fileOrUri;
                    logger.Debug("Source is a file \"{0}\"", filePath);
                }
                else if (uriResult.HostNameType == UriHostNameType.Dns ||
                         uriResult.HostNameType == UriHostNameType.IPv4 ||
                         uriResult.HostNameType == UriHostNameType.IPv6)
                {
                    logger.Debug("Source is a remote resource \"{0}\"", fileOrUri);
                    logger.Debug("Downloading remote resource \"{0}\"...", fileOrUri);
                    // download the resource into TEMP
                    try {
                        filePath =
                            CommonUtils.DownloadFile(fileOrUri,
                                                     Path.Combine(Environment.GetEnvironmentVariable("TEMP"),
                                                                  PyRevitConsts.EnvConfigsExtensionDBFileName)
                                                     );
                    }
                    catch (Exception ex) {
                        throw new PyRevitException(
                                  string.Format("Error downloading extension metadata file. | {0}", ex.Message)
                                  );
                    }
                }
            }
            else
            {
                throw new PyRevitException(
                          string.Format("Source is not a valid file or remote resource \"{0}\"", fileOrUri)
                          );
            }

            // process file now
            if (filePath != null)
            {
                if (Path.GetExtension(filePath).ToLower() == ".json")
                {
                    logger.Debug("Parsing extension metadata file...");

                    dynamic extensionsObj;
                    if (filePath != null)
                    {
                        try {
                            extensionsObj = JObject.Parse(File.ReadAllText(filePath));
                        }
                        catch (Exception ex) {
                            throw new PyRevitException(string.Format("Error parsing extension metadata. | {0}", ex.Message));
                        }

                        // make extension list
                        foreach (JObject extObj in extensionsObj.extensions)
                        {
                            var extDef = new PyRevitExtensionDefinition(extObj);

                            logger.Debug("Registered extension \"{0}\"", extDef.Name);
                            if (searchPattern != null)
                            {
                                if (CompareExtensionNames(extDef.Name, searchPattern))
                                {
                                    logger.Debug(string.Format("\"{0}\" Matched registered extension \"{1}\"",
                                                               searchPattern, extDef.Name));
                                    pyrevtExts.Add(extDef);
                                }
                            }
                            else
                            {
                                pyrevtExts.Add(extDef);
                            }
                        }
                    }
                }
                else
                {
                    throw new PyRevitException(
                              string.Format("Definition file is not a valid json file \"{0}\"", filePath)
                              );
                }
            }

            return(pyrevtExts);
        }
示例#3
0
 // installs extension
 // @handled @logs
 public static void InstallExtension(PyRevitExtensionDefinition extDef,
                                     string destPath = null, string branchName = null)
 {
     logger.Debug("Installing extension \"{0}\"", extDef.Name);
     InstallExtension(extDef.Name, extDef.Type, extDef.Url, destPath, branchName);
 }