Ejemplo n.º 1
0
        // AOP: [TryCatchException]
        private bool ValidateStepResult(RouteStationResult result)
        {
            IValidator validator = StepResultValidatorFactory.CreateValidator(
                result.ValidationType, m_process.Item.Part.Attributes,
                result.Result);

            // Validate
            var isValid = false;

            var error = ExecutionHelpers.TryCatchException(() => isValid = validator.Validated());

            if (error != null)
            {
                Error = error.Message;
                return(false);
            }

            if (!isValid)
            {
                Error = validator.Error;
                return(false);
            }

            return(true);
        }
        private static bool TryGenerateFile(
            ISolution solution,
            IProjectFile sourceFile,
            string filePath,
            string generatedText,
            out IProjectFile generatedFile)
        {
            using (var progressIndicator = NullProgressIndicator.Create())
            {
                IProjectModelTransactionCookie transaction =
                    solution.CreateTransactionCookie(DefaultAction.Rollback, "T4 Generating file", progressIndicator);
                using (transaction)
                {
                    if (!TryCreateFile(filePath, solution, sourceFile, transaction, out generatedFile))
                    {
                        return(false);
                    }

                    IDocument generatedDocument = generatedFile.GetDocument();
                    generatedDocument.ReplaceText(generatedDocument.DocumentRange, generatedText);

                    ExecutionHelpers.OrginizeUsingsAndFormatFile(solution, generatedDocument);

                    transaction.Commit(progressIndicator);
                }
            }

            return(true);
        }
        private static void MakeDocumentChanges(
            ISolution solution,
            List <FileGeneratorOutput> fileGeneratorOutputs,
            List <InPlaceGeneratorOutput> inPlaceGeneratorOutputs)
        {
            foreach (FileGeneratorOutput output in fileGeneratorOutputs)
            {
                IDocument document = output.GeneratedProjectFile.GetDocument();
                document.ReplaceText(document.DocumentRange, output.GeneratedText);
                ExecutionHelpers.OrginizeUsingsAndFormatFile(solution, document);
            }

            foreach (InPlaceGeneratorOutput output in inPlaceGeneratorOutputs)
            {
                IDocument document = output.ProjectFile.GetDocument();
                if (!output.RangeToDelete.IsEmpty)
                {
                    document.DeleteText(output.RangeToDelete);
                }

                document.InsertText(output.PositionToInsert, output.GeneratedText);

                var treeTextRange = TreeTextRange.FromLength(new TreeOffset(output.PositionToInsert), output.GeneratedText.Length);
                ExecutionHelpers.FormatFileRangeAndAddUsingDirectives(solution, document, treeTextRange, output.MissingUsingDirectives);
            }
        }
        protected override bool TryExecute(IFileGenerator generator, GeneratorExecutionHost executionHost)
        {
            ISolution    solution   = generator.DataProvider.Solution;
            IProjectFile sourceFile = generator.DataProvider.SourceFile.ToProjectFile();

            if (!ExecutionHelpers.TryExecuteGenerator(generator, out string generatedText, out string filePath))
            {
                return(false);
            }

            bool fileCreated = TryGenerateFile(
                solution,
                sourceFile,
                filePath,
                generatedText,
                out IProjectFile generatedFile
                );

            if (!fileCreated)
            {
                return(false);
            }

            NavigateToFile(solution, generatedFile);

            return(true);
        }
        private static bool TryExecuteGenerators(
            IGenerator[] generators,
            out List <FileGeneratorOutput> fileGeneratorOutputs,
            out List <InPlaceGeneratorOutput> inPlaceGeneratorOutputs)
        {
            fileGeneratorOutputs    = new List <FileGeneratorOutput>();
            inPlaceGeneratorOutputs = new List <InPlaceGeneratorOutput>();

            List <FileGeneratorOutput>    localFileGeneratorOutputs    = fileGeneratorOutputs;
            List <InPlaceGeneratorOutput> localInPlaceGeneratorOutputs = inPlaceGeneratorOutputs;

            foreach (IGenerator generator in generators)
            {
                bool executed = ExecutionHelpers.TryExecuteGenerator(
                    generator,
                    () =>
                {
                    switch (generator)
                    {
                    case IFileGenerator fileGenerator:

                        var fileGeneratorOutput = new FileGeneratorOutput(
                            fileGenerator.GetFileName(),
                            fileGenerator.TransformText(),
                            fileGenerator.DataProvider.SourceFile.ToProjectFile()
                            );

                        localFileGeneratorOutputs.Add(fileGeneratorOutput);
                        break;

                    case IInPlaceGenerator inPlaceGenerator:

                        var inPlaceGeneratorOutput = new InPlaceGeneratorOutput(
                            inPlaceGenerator.DataProvider.SourceFile.ToProjectFile(),
                            inPlaceGenerator.GetTextRangeToDelete(),
                            inPlaceGenerator.GetPositionToInsert(),
                            inPlaceGenerator.TransformText(),
                            inPlaceGenerator.MissingUsingDirectives
                            );

                        localInPlaceGeneratorOutputs.Add(inPlaceGeneratorOutput);
                        break;

                    default:
                        throw new NotSupportedException(
                            $"The generator of type '{generator.GetType()}' is not supported as a part of MultipleOutputGenerator." +
                            "Only IFileGenerator and IInPlaceGenerator are suppored."
                            );
                    }
                }
                    );

                if (!executed)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 6
0
        public ShipmentScript(
            IMapper <ShipmentFacts, Shipment> shipmentMapper,
            IMapper <Shipment, Transaction> transactionMapper)
        {
            ExecutionHelpers.ThrowIfNull(() => shipmentMapper, "Shipment Mapper cannot be null");
            ExecutionHelpers.ThrowIfNull(() => transactionMapper, "Transaction Mapper cannot be null");

            ShipmentMapper    = shipmentMapper;
            TransactionMapper = transactionMapper;
        }
        public bool Undo(RepairComponent rc, Transaction transaction)
        {
            var part = m_repo.Get <Part>(p => p.Id == transaction.Part.Id);
            var sfl  = m_repo.Get <Shopfloorline>(s => s.Label == transaction.DepartLocation);

            var transType = new AdjustmentTransactionType("MATLUNDO", "Service part consumption reversed", "IN");

            ExecutionHelpers.ThrowIfNull(() => rc, "Cannot find repair record for this transaction.");

            rc.ConsumptionId = default(int);

            return(MaterialService.AdjustMaterialConsumableItem
                       (part, sfl, transaction.Qty, "Service part consumption reversed.", transType, null));
        }
        public MaterialConsumableItem DecreaseItemQty(Part part, Shopfloorline sfl, int qty)
        {
            MaterialConsumableItem item = null;
            Expression <Func <MaterialConsumableItem, bool> > criteria =
                mci => mci.Part.Id == part.Id && mci.Shopfloorline.Id == sfl.Id && mci.Qty > 0;

            ExecutionHelpers.ThrowIfNull(
                () => item = m_repository.Get(criteria),
                "Consumable item not found"
                );

            item.DecreaseQty(qty);

            return(item);
        }
Ejemplo n.º 9
0
        private void Dispose(bool disposing)
        {
            // will not dispose wrapped value - leave it for the client code
            // (the lifetime of value can be managed by DI container, for example)

            if (_disposed)
            {
                return;
            }

            _disposed = true;
            ExecutionHelpers.Try(() => _disposeAction?.Invoke(Value));
            ExecutionHelpers.Try(() => _disposable?.Dispose());

            if (disposing)
            {
                GC.SuppressFinalize(this);
            }
        }
        protected override bool TryExecute(IInPlaceGenerator generator, GeneratorExecutionHost executionHost)
        {
            IDocument document = generator.DataProvider.Document;

            bool executed = ExecutionHelpers.TryExecuteGenerator(
                generator,
                out string generatedText,
                out TextRange rangeToDelete,
                out int positionToInsert,
                out IReadOnlyCollection <IUsingDirective> missingUsingDirectives
                );

            if (!executed)
            {
                return(false);
            }

            ISolution solution = generator.DataProvider.Solution;

            using (var progressIndicator = NullProgressIndicator.Create())
            {
                IProjectModelTransactionCookie transaction =
                    solution.CreateTransactionCookie(DefaultAction.Rollback, "T4 Generating file", progressIndicator);
                using (transaction)
                {
                    if (!rangeToDelete.IsEmpty)
                    {
                        document.DeleteText(rangeToDelete);
                    }

                    document.InsertText(positionToInsert, generatedText);

                    var treeTextRange = TreeTextRange.FromLength(new TreeOffset(positionToInsert), generatedText.Length);
                    ExecutionHelpers.FormatFileRangeAndAddUsingDirectives(solution, document, treeTextRange, missingUsingDirectives);

                    transaction.Commit(progressIndicator);
                }
            }

            return(true);
        }
        private static bool TryEnsureFileExists(
            RelativePath relativeFilePath,
            IProjectFolder projectFolder,
            IProjectFile sourceFile,
            IProjectModelTransactionCookie transaction,
            out IProjectFile file)
        {
            file = projectFolder.GetSubFiles(relativeFilePath.Name).FirstOrDefault();
            if (file != null)
            {
                var message =
                    $"The file '{relativeFilePath.FullPath}' already exists.{Environment.NewLine}" +
                    "Do you want to overwrite it?";

                return(MessageBox.ShowYesNo(message));
            }

            file = ExecutionHelpers.CreateFile(relativeFilePath.Name, projectFolder, sourceFile, transaction);

            return(true);
        }
        private static bool TryCreateFiles(
            List <FileGeneratorOutput> fileGeneratorOutputs,
            IProjectModelTransactionCookie transaction)
        {
            var alreadyExistedFilePaths = new List <string>();

            foreach (FileGeneratorOutput output in fileGeneratorOutputs)
            {
                IProjectFile file = output.ProjectFolder.GetSubFiles(output.FileName).FirstOrDefault();
                if (file == null)
                {
                    file = ExecutionHelpers.CreateFile(
                        output.FileName,
                        output.ProjectFolder,
                        output.SourceProjectFile,
                        transaction
                        );
                }
                else
                {
                    alreadyExistedFilePaths.Add(output.FilePath);
                }

                output.GeneratedProjectFile = file;
            }

            if (alreadyExistedFilePaths.Count > 0)
            {
                string message =
                    $"The following files already exist:{Environment.NewLine}" +
                    string.Join(Environment.NewLine, alreadyExistedFilePaths) + Environment.NewLine + Environment.NewLine +
                    "Do you want to overwrite them?";

                return(MessageBox.ShowYesNo(message));
            }

            return(true);
        }
        protected override bool TryExecute(
            IMultipleOutputGenerator generator,
            GeneratorExecutionHost executionHost)
        {
            ISolution solution = generator.DataProvider.Solution;

            IGenerator[] generators    = null;
            bool         gotGenerators = ExecutionHelpers.TryExecuteGenerator(
                generator,
                () => generators = generator.GetGenerators().ToArray()
                );

            if (!gotGenerators)
            {
                return(false);
            }

            bool generatorsExecuted = TryExecuteGenerators(
                generators,
                out List <FileGeneratorOutput> fileGeneratorOutputs,
                out List <InPlaceGeneratorOutput> inPlaceGeneratorOutputs
                );

            if (!generatorsExecuted)
            {
                return(false);
            }

            if (!TryGenerateFiles(solution, fileGeneratorOutputs, inPlaceGeneratorOutputs))
            {
                return(false);
            }

            NavigateToFiles(solution, fileGeneratorOutputs, inPlaceGeneratorOutputs);

            return(true);
        }
Ejemplo n.º 14
0
 public bool Save()
 {
     return(ExecutionHelpers.TryCatch(() => m_uow.Commit()));
 }