コード例 #1
0
        private static Command BuildValidateCommand()
        {
            var modelFileOption = CommonOptions.ModelFile;

            modelFileOption.IsRequired = true; // Option is required for this command

            Command validateModel = new Command("validate")
            {
                modelFileOption,
                CommonOptions.Repo,
                CommonOptions.Strict
            };

            validateModel.Description = "Validates a model using the DTDL model parser & resolver. " +
                                        "Uses the target repository for model resolution. ";
            validateModel.Handler = CommandHandler.Create <FileInfo, string, IHost, bool>(async(modelFile, repository, host, strict) =>
            {
                ILogger logger = GetLogger(host);
                OutputHeaders(logger);

                Parsing parsing = new Parsing(repository, logger);
                bool isValid;
                try
                {
                    isValid = await parsing.IsValidDtdlFileAsync(modelFile, strict);
                }
                catch (ResolutionException resolutionEx)
                {
                    logger.LogError(resolutionEx.Message);
                    return(ReturnCodes.ResolutionError);
                }
                catch (ParsingException parsingEx)
                {
                    IList <ParsingError> errors = parsingEx.Errors;
                    string normalizedErrors     = string.Empty;
                    foreach (ParsingError error in errors)
                    {
                        normalizedErrors += $"{error.Message}{Environment.NewLine}";
                    }

                    logger.LogError(normalizedErrors);

                    return(ReturnCodes.ParserError);
                }
                catch (ResolverException resolverEx)
                {
                    logger.LogError(resolverEx.Message);
                    return(ReturnCodes.ResolutionError);
                }
                catch (ValidationException validationEx)
                {
                    logger.LogError(validationEx.Message);
                    return(ReturnCodes.ValidationError);
                }

                return(isValid ? ReturnCodes.Success : ReturnCodes.ValidationError);
            });

            return(validateModel);
        }
コード例 #2
0
        private static Command BuildImportModelCommand()
        {
            var modelFileOption = CommonOptions.ModelFile;

            modelFileOption.IsRequired = true; // Option is required for this command

            Command addModel = new Command("import")
            {
                modelFileOption,
                CommonOptions.LocalRepo
            };

            addModel.Description = "Adds a model to a local repository. " +
                                   "Validates Id's, dependencies and places model content in the proper location.";
            addModel.Handler = CommandHandler.Create <FileInfo, string, IHost>(async(modelFile, localRepository, host) =>
            {
                var returnCode = ReturnCodes.Success;
                ILogger logger = GetLogger(host);

                OutputHeaders(logger);

                if (localRepository == null)
                {
                    localRepository = Path.GetFullPath(".");
                }
                else if (Validations.IsRelativePath(localRepository))
                {
                    localRepository = Path.GetFullPath(localRepository);
                }

                DirectoryInfo repoDirInfo = new DirectoryInfo(localRepository);
                Parsing parsing           = new Parsing(repoDirInfo.FullName, logger);
                try
                {
                    var newModels = await ModelImporter.ImportModels(modelFile, repoDirInfo, logger);
                    foreach (var model in newModels)
                    {
                        var validationResult = await parsing.IsValidDtdlFileAsync(model, false);

                        if (!validationResult)
                        {
                            returnCode = ReturnCodes.ValidationError;
                        }
                    }
                }
                catch (ValidationException validationEx)
                {
                    logger.LogError(validationEx.Message);
                    return(ReturnCodes.ValidationError);
                }
                catch (IOException ioEx)
                {
                    logger.LogError(ioEx.Message);
                    return(ReturnCodes.ImportError);
                }

                return(returnCode);
            });

            return(addModel);
        }