public void GetModelInvalidDtmiDependencyThrowsException() { const string dtmi = "dtmi:com:example:invalidmodel;1"; const string invalidDep = "dtmi:azure:fakeDeviceManagement:FakeDeviceInformation;2"; string invalidDepPath = DtmiConventions.GetModelUri(invalidDep, new Uri(ModelsRepositoryTestBase.TestLocalModelsRepository)).LocalPath; string expectedExMsg = $"{string.Format(StandardStrings.GenericGetModelsError, invalidDep)} {string.Format(StandardStrings.ErrorFetchingModelContent, invalidDepPath)}"; ModelsRepositoryClient client = GetClient(ModelsRepositoryTestBase.ClientType.Local); Func <Task> act = async() => await client.GetModelAsync(dtmi); act.Should().Throw <RequestFailedException>().WithMessage(expectedExMsg); }
private static Queue <string> PrepareWork(string dtmi, Uri repositoryUri, bool tryExpanded) { Queue <string> work = new Queue <string>(); if (tryExpanded) { work.Enqueue(DtmiConventions.GetModelUri(dtmi, repositoryUri, true).AbsoluteUri); } work.Enqueue(DtmiConventions.GetModelUri(dtmi, repositoryUri, false).AbsoluteUri); return(work); }
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); }
public FetchModelResult FetchModel(string dtmi, Uri repositoryUri, bool tryFromExpanded, CancellationToken cancellationToken = default) { using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(FileModelFetcher)}.{nameof(FetchModel)}"); scope.Start(); try { var work = new Queue <string>(); if (tryFromExpanded) { work.Enqueue(DtmiConventions.GetModelUri(dtmi, repositoryUri, true).LocalPath); } work.Enqueue(DtmiConventions.GetModelUri(dtmi, repositoryUri, false).LocalPath); string fnfError = string.Empty; while (work.Count != 0) { cancellationToken.ThrowIfCancellationRequested(); string tryContentPath = work.Dequeue(); ModelsRepositoryEventSource.Instance.FetchingModelContent(tryContentPath); if (File.Exists(tryContentPath)) { return(new FetchModelResult { Definition = File.ReadAllText(tryContentPath, Encoding.UTF8), Path = tryContentPath }); } ModelsRepositoryEventSource.Instance.ErrorFetchingModelContent(tryContentPath); fnfError = string.Format(CultureInfo.InvariantCulture, StandardStrings.ErrorFetchingModelContent, tryContentPath); } throw new RequestFailedException( $"{string.Format(CultureInfo.InvariantCulture, StandardStrings.GenericGetModelsError, dtmi)} {fnfError}", new FileNotFoundException(fnfError)); } catch (Exception ex) { scope.Failed(ex); throw; } }
public static void Import(string modelContent, DirectoryInfo repository) { string rootId = ParsingUtils.GetRootId(modelContent); string createPath = DtmiConventions.GetModelUri(rootId, new Uri(repository.FullName)).AbsolutePath; Outputs.WriteOut($"- Importing model \"{rootId}\"..."); if (File.Exists(createPath)) { Outputs.WriteOut( $"- Skipping \"{rootId}\". Model file already exists in repository.", ConsoleColor.DarkCyan); return; } (new FileInfo(createPath)).Directory.Create(); Outputs.WriteToFile(createPath, modelContent); }
public static string EnsureValidModelFilePath(string modelFilePath, string modelContent, string repository) { if (RepoProvider.IsRelativePath(repository)) { repository = Path.GetFullPath(repository); } string rootId = ParsingUtils.GetRootId(modelContent); Uri targetModelPathUri = DtmiConventions.GetModelUri(rootId, new Uri(repository)); Uri modelFilePathUri = new Uri(modelFilePath); if (targetModelPathUri.AbsolutePath != modelFilePathUri.AbsolutePath) { return(Path.GetFullPath(targetModelPathUri.AbsolutePath)); } return(null); }
public void GetModelUri(string dtmi, string repository, string expectedUri) { var repositoryUri = new Uri(repository); if (string.IsNullOrEmpty(expectedUri)) { Action act = () => DtmiConventions.GetModelUri(dtmi, repositoryUri); act.Should().Throw <ArgumentException>().WithMessage(string.Format(StandardStrings.InvalidDtmiFormat, dtmi)); return; } Uri modelUri = DtmiConventions.GetModelUri(dtmi, repositoryUri); modelUri.AbsoluteUri.Should().Be(expectedUri); string expectedExpandedUri = expectedUri.Replace( ModelsRepositoryConstants.JsonFileExtension, ModelsRepositoryConstants.ExpandedJsonFileExtension); Uri expandedModelUri = DtmiConventions.GetModelUri(dtmi, repositoryUri, true); expandedModelUri.Should().Be(expectedExpandedUri); }
public static void GetModelUri() { #region Snippet:ModelsRepositorySamplesDtmiConventionsGetModelUri // This snippet shows obtaining a fully qualified path to a model file. // Local repository example Uri localRepositoryUri = new Uri("file:///path/to/repository/"); string fullyQualifiedModelPath = DtmiConventions.GetModelUri("dtmi:com:example:Thermostat;1", localRepositoryUri).AbsolutePath; // Prints '/path/to/repository/dtmi/com/example/thermostat-1.json' Console.WriteLine(fullyQualifiedModelPath); // Remote repository example Uri remoteRepositoryUri = new Uri("https://contoso.com/models/"); fullyQualifiedModelPath = DtmiConventions.GetModelUri("dtmi:com:example:Thermostat;1", remoteRepositoryUri).AbsoluteUri; // Prints 'https://contoso.com/models/dtmi/com/example/thermostat-1.json' Console.WriteLine(fullyQualifiedModelPath); #endregion Snippet:ModelsRepositorySamplesDtmiConventionsGetModelUri }