Ejemplo n.º 1
0
        public void Build(IBuildContext context)
        {
            string prefix = context.OnlyCheck ? "Проверка" : "Сборка";

              context.ClearBuildMessage();
              context.BuildMessageWriteLine(prefix + " начата.", BuildMessageTypes.Notification);

              // Общее действие для вызова методов с отменой выполнения задач.
              // Параметры:
              // a - метод для вызова;
              // c - токен для отмены и других операций;
              // m - сообщение о начале действия.
              Action<Action<IBuildContext, CancellationTokenSource>, IBuildContext, CancellationTokenSource, string> actionWithErrorHandling =
            (Action<IBuildContext, CancellationTokenSource> a, IBuildContext ctx, CancellationTokenSource c, string m) =>
            {
              // Не будем выводить названия действия при проверке, так как некоторые могут не выполнятся.
              if (!context.OnlyCheck)
            context.BuildMessageWriteLine(m, BuildMessageTypes.Notification);
              try
              {
            a(ctx, c);
              }
              catch (Exception e)
              {
            context.BuildMessageWriteLine(e.Message, BuildMessageTypes.Error);
            c.Cancel();
            c.Token.ThrowIfCancellationRequested();
              }
            };

              Stopwatch stopwatch = Stopwatch.StartNew();
              CancellationTokenSource cts = new CancellationTokenSource();
              Task.Factory.
            StartNew(delegate
            {
              actionWithErrorHandling(CheckProduct, context, cts, "");
            }, cts.Token).
            ContinueWith(delegate
            {
              // Загружаем шаблоны во временную папку.
              actionWithErrorHandling(LoadingTemplates, context, cts, "Загрузка шаблонов.");
            }, cts.Token).
            ContinueWith(delegate
            {
              actionWithErrorHandling(ProcessingTemplates, context, cts, "Обработка шаблонов.");
            }, cts.Token).
            ContinueWith(delegate
            {
              actionWithErrorHandling(CompilationAndBuild, context, cts, "Компиляция и сборка.");
            }, cts.Token).
            ContinueWith(delegate
            {
              stopwatch.Stop();
              // Сюда токен не передаем.
              // Здесь должен быть код, выполняющийся в любом случае, завершились задачи или нет.
              context.BuildMessageWriteLine(
            string.Format(prefix + " завершена {0} за {1}.", cts.IsCancellationRequested ? "с ошибками" : "успешно",
            stopwatch.Elapsed.ToString("mm\\:ss")),
            cts.IsCancellationRequested ? BuildMessageTypes.Error : BuildMessageTypes.Information);
              if (context.BuildIsFinished != null)
            context.BuildIsFinished();
            });
        }