示例#1
0
        public static IBuildAction GetTopmostAction([CanBeNull] this MatchedBuildActions matchedBuildActions, [NotNull] object stage)
        {
            if (stage == null)
            {
                throw new ArgumentNullException(nameof(stage));
            }

            var actions = matchedBuildActions?.GetValueSafe(stage);

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

            actions.Sort((l, r) => r.Weight.CompareTo(l.Weight)); // sort descending

            if (actions.Count > 1)
            {
                if (actions[0].Weight == actions[1].Weight)
                {
                    Log.WriteLine(LogLevel.Info, "Two or more building actions have the same weight.");
                    throw new ArmatureException("Two or more building actions have the same weight. Enable logging to find the reason");
                }
            }

            return(actions[0].Entity);
        }
示例#2
0
        private List <BuildResult> BuildAllUnits(MatchedBuildActions matchedBuildActions)
        {
            if (matchedBuildActions == null)
            {
                return(null);
            }

            if (matchedBuildActions.Keys.Count > 1)
            {
                throw new ArmatureException("Actions only for one stage should be provided for BuildAll");
            }

            var result = new List <BuildResult>();

            foreach (var buildAction in matchedBuildActions.Values.Single().Select(_ => _.Entity))
            {
                var unitBuilder = new Interface(_buildSequence, this);

                using (Log.Block(LogLevel.Info, () => string.Format(LogConst.OneParameterFormat, "Execute action", buildAction)))
                {
                    buildAction.Process(unitBuilder);
                    buildAction.PostProcess(unitBuilder);
                }

                if (unitBuilder.BuildResult.HasValue)
                {
                    Log.WriteLine(LogLevel.Info, "");
                    Log.WriteLine(LogLevel.Info, () => string.Format("Build Result{{{0}:{1}}}", unitBuilder.BuildResult, unitBuilder.BuildResult.Value?.GetType().ToLogString()));
                    result.Add(unitBuilder.BuildResult);
                }
            }

            return(result);
        }
示例#3
0
        private BuildResult BuildUnit(MatchedBuildActions matchedBuildActions)
        {
            if (matchedBuildActions == null)
            {
                return(BuildViaParentBuilder(_buildSequence.Last()));
            }

            // builder to pass into IBuildActon.Execute
            var unitBuilder      = new Interface(_buildSequence, this);
            var performedActions = new Stack <IBuildAction>();

            foreach (var stage in _buildStages)
            {
                var buildAction = matchedBuildActions.GetTopmostAction(stage);
                if (buildAction == null)
                {
                    continue;
                }

                performedActions.Push(buildAction);

                using (Log.Block(LogLevel.Info, () => string.Format(LogConst.OneParameterFormat, "Execute action", buildAction)))
                {
                    buildAction.Process(unitBuilder);
                }

                if (unitBuilder.BuildResult.HasValue)
                {
                    Log.WriteLine(LogLevel.Info, "");
                    Log.WriteLine(LogLevel.Info, () => string.Format("Build Result{{{0}:{1}}}", unitBuilder.BuildResult, unitBuilder.BuildResult.Value?.GetType().ToLogString()));
                    break; // object is built, unwind called actions in reverse orders
                }
            }

            foreach (var buildAction in performedActions)
            {
                Log.WriteLine(LogLevel.Info, "");
                using (Log.Block(LogLevel.Info, () => string.Format(LogConst.OneParameterFormat, "Rewind action", buildAction)))
                {
                    buildAction.PostProcess(unitBuilder);
                }
            }

            return(unitBuilder.BuildResult);
        }
示例#4
0
        public static MatchedBuildActions Merge(this MatchedBuildActions left, MatchedBuildActions right)
        {
            if (left == null)
            {
                return(right);
            }
            if (right == null)
            {
                return(left);
            }

            var result = new MatchedBuildActions();

            foreach (var pair in left)
            {
                List <Weighted <IBuildAction> > resultValue;
                if (!right.TryGetValue(pair.Key, out var rightValue)) // if key is presented only in 'left' dictionary - get value from it
                {
                    resultValue = pair.Value;
                }
                else // if key is presented in both dictionaries create a new list and merge items from both
                {
                    resultValue = new List <Weighted <IBuildAction> >(pair.Value);
                    resultValue.AddRange(rightValue);
                }

                result.Add(pair.Key, resultValue);
            }

            foreach (var pair in right)
            {
                if (!left.ContainsKey(pair.Key))
                {
                    result.Add(pair.Key, pair.Value); // for all keys presented in 'right' and not presented in 'left' dictionary get value from 'right'
                }
            }
            return(result);
        }