コード例 #1
0
        // Methods
        protected override void ProcessRecord()
        {
            if (FunctionCache == null)
            {
                UpdateCache();
            }

            if (string.IsNullOrEmpty(Name))
            {
                WriteError(new ErrorRecord(new AmbiguousMatchException(
                                               "Function name was not provided."),
                                           "sitecore_name_missing", ErrorCategory.NotSpecified, null));
                return;
            }

            var filteredFunctions = FunctionCache.Where(f => string.Equals(f.Name, Name, StringComparison.InvariantCultureIgnoreCase)).ToList();

            if (!string.IsNullOrEmpty(Module))
            {
                filteredFunctions = filteredFunctions.Where(f => string.Equals(f.Module, Module, StringComparison.InvariantCultureIgnoreCase)).ToList();
            }

            if (!string.IsNullOrEmpty(Library))
            {
                filteredFunctions = filteredFunctions.Where(f => f.Library.StartsWith(Library, StringComparison.InvariantCultureIgnoreCase)).ToList();
            }

            //var functionItems = new List<Item>();
            var roots = ModuleManager.GetFeatureRoots(IntegrationPoints.FunctionsFeature);

            if (filteredFunctions.Count > 1)
            {
                WriteError(new ErrorRecord(new AmbiguousMatchException(
                                               $"Ambiguous function name '{Name}' detected, please narrow your search by specifying sub-library and/or module name."),
                                           "sitecore_ambiguous_name", ErrorCategory.InvalidData, null));
                return;
            }

            if (filteredFunctions.Count == 0)
            {
                WriteError(new ErrorRecord(new AmbiguousMatchException(
                                               $"Function item with name '{Name}' could not be found in the specified module or library or it does not exist."),
                                           "sitecore_function_not_found", ErrorCategory.ObjectNotFound, null));
                return;
            }

            var functionItem = Factory.GetDatabase(ApplicationSettings.ScriptLibraryDb).GetItem(filteredFunctions[0].ScriptID);

            if (!IsPowerShellScriptItem(functionItem))
            {
                // this should never happen as cache only stores Scripts
                return;
            }

            var script = functionItem[Templates.Script.Fields.ScriptBody];

            if (ShouldProcess(functionItem.GetProviderPath(), "Import functions"))
            {
                var sendToPipeline = InvokeCommand.InvokeScript(script, false,
                                                                PipelineResultTypes.Output | PipelineResultTypes.Error, null);

                if (sendToPipeline != null && sendToPipeline.Any())
                {
                    WriteObject(sendToPipeline);
                }
            }
        }