Exemple #1
0
        public static async Task <int> RepoExpand(DirectoryInfo localRepo)
        {
            if (localRepo == null)
            {
                localRepo = new DirectoryInfo(Path.GetFullPath("."));
            }

            var repoProvider = new RepoProvider(localRepo.FullName);

            if (!localRepo.Exists)
            {
                Outputs.WriteError($"Invalid target repository directory: {localRepo.FullName}.");
                return(ReturnCodes.InvalidArguments);
            }

            foreach (string file in Directory.EnumerateFiles(localRepo.FullName, "*.json",
                                                             new EnumerationOptions {
                RecurseSubdirectories = true
            }))
            {
                if (file.ToLower().EndsWith(".expanded.json"))
                {
                    continue;
                }

                try
                {
                    var    modelFile = new FileInfo(file);
                    string dtmi      = ParsingUtils.GetRootId(modelFile);

                    if (string.IsNullOrEmpty(dtmi))
                    {
                        continue;
                    }
                    List <string> expandedModel = await repoProvider.ExpandModel(dtmi);

                    string formattedJson = Outputs.FormatExpandedListAsJson(expandedModel);

                    string createPath = DtmiConventions.GetModelUri(dtmi, new Uri(localRepo.FullName), true).AbsolutePath;
                    Outputs.WriteToFile(createPath, formattedJson);
                    Outputs.WriteOut($"Created: {createPath}");
                }
                catch (Exception e)
                {
                    Outputs.WriteError($"Failure expanding model file: {file}, {e.Message}");
                    return(ReturnCodes.ProcessingError);
                }
            }

            return(ReturnCodes.Success);
        }
Exemple #2
0
        public static async Task <int> Export(string dtmi, FileInfo modelFile, string repo, FileInfo outputFile)
        {
            // Check that we have either model file or dtmi
            if (string.IsNullOrWhiteSpace(dtmi) && modelFile == null)
            {
                string invalidArgMsg = "Please specify a value for --dtmi";
                Outputs.WriteError(invalidArgMsg);
                return(ReturnCodes.InvalidArguments);
            }

            var repoProvider = new RepoProvider(repo);

            try
            {
                if (string.IsNullOrWhiteSpace(dtmi))
                {
                    dtmi = ParsingUtils.GetRootId(modelFile);
                    if (string.IsNullOrWhiteSpace(dtmi))
                    {
                        Outputs.WriteError("Model is missing root @id");
                        return(ReturnCodes.ValidationError);
                    }
                }

                List <string> expandedModel = await repoProvider.ExpandModel(dtmi);

                string formattedJson = Outputs.FormatExpandedListAsJson(expandedModel);

                Outputs.WriteOut(formattedJson);
                Outputs.WriteToFile(outputFile, formattedJson);
            }
            catch (RequestFailedException requestEx)
            {
                Outputs.WriteError(requestEx.Message);
                return(ReturnCodes.ResolutionError);
            }
            catch (System.Text.Json.JsonException jsonEx)
            {
                Outputs.WriteError($"Parsing json-ld content. Details: {jsonEx.Message}");
                return(ReturnCodes.InvalidArguments);
            }

            return(ReturnCodes.Success);
        }