public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        CopyComponent c = (CopyComponent)target;

        if (GUILayout.Button("Copy"))
        {
            c.Copy();
        }
    }
Ejemplo n.º 2
0
        public CopyFromIndexComponent(BuildAssembler assembler, XPathNavigator configuration)
            : base(assembler, configuration)
        {
            _copyComponents = new List <CopyComponent>();
            _copyCommands   = new List <CopyFromIndexCommand>();
            _context        = new CustomContext();

            // set up the context
            XPathNodeIterator contextNodes = configuration.Select("context");

            foreach (XPathNavigator contextNode in contextNodes)
            {
                string prefix = contextNode.GetAttribute("prefix", String.Empty);
                string name   = contextNode.GetAttribute("name", String.Empty);
                _context.AddNamespace(prefix, name);
            }

            // set up the indices
            _documentController = new IndexedDocumentController(this,
                                                                configuration, _context, Data);

            // get the copy commands
            XPathNodeIterator copyNodes = configuration.Select("copy");

            foreach (XPathNavigator copyNode in copyNodes)
            {
                string sourceName = copyNode.GetAttribute("name", String.Empty);
                if (String.IsNullOrEmpty(sourceName))
                {
                    throw new BuildComponentException(
                              "Each copy command must specify an index to copy from.");
                }

                string keyXPath = copyNode.GetAttribute("key", String.Empty);

                string sourceXPath = copyNode.GetAttribute("source", String.Empty);
                if (String.IsNullOrEmpty(sourceXPath))
                {
                    throw new BuildComponentException(
                              "When instantiating a CopyFromDirectory component, you must specify a source xpath format using the source attribute.");
                }

                string targetXPath = copyNode.GetAttribute("target", String.Empty);
                if (String.IsNullOrEmpty(targetXPath))
                {
                    throw new BuildComponentException(
                              "When instantiating a CopyFromDirectory component, you must specify a target xpath format using the target attribute.");
                }

                string attributeValue = copyNode.GetAttribute("attribute", String.Empty);

                string ignoreCaseValue = copyNode.GetAttribute("ignoreCase", String.Empty);

                string missingEntryValue  = copyNode.GetAttribute("missing-entry", String.Empty);
                string missingSourceValue = copyNode.GetAttribute("missing-source", String.Empty);
                string missingTargetValue = copyNode.GetAttribute("missing-target", String.Empty);

                // Note that sourceController != _documentController
                IndexedDocumentController sourceController =
                    (IndexedDocumentController)Data[sourceName];
                CopyFromIndexCommand copyCommand = new CopyFromIndexCommand(
                    sourceController[sourceName], keyXPath, sourceXPath,
                    targetXPath, attributeValue, ignoreCaseValue);
                if (!String.IsNullOrEmpty(missingEntryValue))
                {
                    try
                    {
                        copyCommand.MissingEntry = (MessageLevel)Enum.Parse(
                            typeof(MessageLevel), missingEntryValue, true);
                    }
                    catch (ArgumentException)
                    {
                        WriteMessage(MessageLevel.Error, String.Format("'{0}' is not a message level.", missingEntryValue));
                    }
                }
                if (!String.IsNullOrEmpty(missingSourceValue))
                {
                    try
                    {
                        copyCommand.MissingSource = (MessageLevel)Enum.Parse(
                            typeof(MessageLevel), missingSourceValue, true);
                    }
                    catch (ArgumentException)
                    {
                        WriteMessage(MessageLevel.Error, String.Format(
                                         "'{0}' is not a message level.", missingSourceValue));
                    }
                }
                if (!String.IsNullOrEmpty(missingTargetValue))
                {
                    try
                    {
                        copyCommand.MissingTarget = (MessageLevel)Enum.Parse(
                            typeof(MessageLevel), missingTargetValue, true);
                    }
                    catch (ArgumentException)
                    {
                        WriteMessage(MessageLevel.Error, String.Format(
                                         "'{0}' is not a message level.", missingTargetValue));
                    }
                }

                _copyCommands.Add(copyCommand);
            }

            XPathNodeIterator componentNodes = configuration.Select("components/component");

            foreach (XPathNavigator componentNode in componentNodes)
            {
                // get the data to load the component
                string assemblyPath = componentNode.GetAttribute(
                    "assembly", String.Empty);
                if (String.IsNullOrEmpty(assemblyPath))
                {
                    WriteMessage(MessageLevel.Error, "Each component element must have an assembly attribute.");
                }
                string typeName = componentNode.GetAttribute("type",
                                                             String.Empty);
                if (String.IsNullOrEmpty(typeName))
                {
                    WriteMessage(MessageLevel.Error, "Each component element must have a type attribute.");
                }

                // expand environment variables in the path
                assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath);

                try
                {
                    Assembly      assembly  = Assembly.LoadFrom(assemblyPath);
                    CopyComponent component = (CopyComponent)assembly.CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, null, new Object[2] {
                        componentNode.Clone(), Data
                    }, null, null);

                    if (component == null)
                    {
                        WriteMessage(MessageLevel.Error, String.Format(
                                         "The type '{0}' does not exist in the assembly '{1}'.", typeName, assemblyPath));
                    }
                    else
                    {
                        _copyComponents.Add(component);
                    }
                }
                catch (IOException e)
                {
                    WriteMessage(MessageLevel.Error, String.Format(
                                     "A file access error occurred while attempting to load the build component '{0}'. The error message is: {1}", assemblyPath, e.Message));
                }
                catch (BadImageFormatException e)
                {
                    WriteMessage(MessageLevel.Error, String.Format(
                                     "A syntax generator assembly '{0}' is invalid. The error message is: {1}.", assemblyPath, e.Message));
                }
                catch (TypeLoadException e)
                {
                    WriteMessage(MessageLevel.Error, String.Format(
                                     "The type '{0}' does not exist in the assembly '{1}'. The error message is: {2}", typeName, assemblyPath, e.Message));
                }
                catch (MissingMethodException e)
                {
                    WriteMessage(MessageLevel.Error, String.Format(
                                     "The type '{0}' in the assembly '{1}' does not have an appropriate constructor. The error message is: {2}", typeName, assemblyPath, e.Message));
                }
                catch (TargetInvocationException e)
                {
                    WriteMessage(MessageLevel.Error, String.Format(
                                     "An error occurred while attempting to instantiate the type '{0}' in the assembly '{1}'. The error message is: {2}", typeName, assemblyPath, e.InnerException.Message));
                }
                catch (InvalidCastException)
                {
                    WriteMessage(MessageLevel.Error, String.Format(
                                     "The type '{0}' in the assembly '{1}' is not a SyntaxGenerator.", typeName, assemblyPath));
                }
            }

            WriteMessage(MessageLevel.Info, String.Format(
                             "Loaded {0} copy components.", _copyComponents.Count));
        }