Esempio n. 1
0
        private async Task EnsurePackagesInProjectAsync(FileInfo projectFile, CodeGenerator?codeGenerator)
        {
            var urlPackages = await LoadPackageVersionsFromURLAsync();

            var attributePackages = GetServicePackages(codeGenerator);

            foreach (var kvp in attributePackages)
            {
                var packageId = kvp.Key;
                var version   = urlPackages != null && urlPackages.ContainsKey(packageId) ? urlPackages[packageId] : kvp.Value;

                var args = new[] {
                    "add",
                    "package",
                    packageId,
                    "--version",
                    version,
                    "--no-restore"
                };

                var muxer = DotNetMuxer.MuxerPathOrDefault();
                if (string.IsNullOrEmpty(muxer))
                {
                    throw new ArgumentException($"dotnet was not found on the path.");
                }

                var startInfo = new ProcessStartInfo
                {
                    FileName               = muxer,
                    Arguments              = string.Join(" ", args),
                    WorkingDirectory       = projectFile.Directory.FullName,
                    RedirectStandardError  = true,
                    RedirectStandardOutput = true,
                };

                var process = Process.Start(startInfo);

                var timeout = 20;
                if (!process.WaitForExit(timeout * 1000))
                {
                    throw new ArgumentException($"Adding package `{packageId}` to `{projectFile.Directory}` took longer than {timeout} seconds.");
                }

                if (process.ExitCode != 0)
                {
                    Out.Write(process.StandardOutput.ReadToEnd());
                    Error.Write(process.StandardError.ReadToEnd());
                    throw new ArgumentException($"Could not add package `{packageId}` to `{projectFile.Directory}`");
                }
            }
        }
Esempio n. 2
0
        private async Task EnsurePackagesInProjectAsync(FileInfo projectFile, CodeGenerator?codeGenerator)
        {
            var urlPackages = await LoadPackageVersionsFromURLAsync();

            var attributePackages = GetServicePackages(codeGenerator);

            foreach (var kvp in attributePackages)
            {
                var packageId = kvp.Key;
                var version   = urlPackages != null && urlPackages.ContainsKey(packageId) ? urlPackages[packageId] : kvp.Value;

                await TryAddPackage(packageId, version, projectFile);
            }
        }
Esempio n. 3
0
    internal async Task AddOpenAPIReference(
        string tagName,
        FileInfo projectFile,
        string sourceFile,
        CodeGenerator?codeGenerator,
        string sourceUrl = null)
    {
        // EnsurePackagesInProjectAsync MUST happen before LoadProject, because otherwise the global state set by ProjectCollection doesn't pick up the nuget edits, and we end up losing them.
        await EnsurePackagesInProjectAsync(projectFile, codeGenerator);

        var project   = LoadProject(projectFile);
        var items     = project.GetItems(tagName);
        var fileItems = items.Where(i => string.Equals(GetFullPath(i.EvaluatedInclude), GetFullPath(sourceFile), StringComparison.Ordinal));

        if (fileItems.Any())
        {
            Warning.Write($"One or more references to {sourceFile} already exist in '{project.FullPath}'. Duplicate references could lead to unexpected behavior.");
            return;
        }

        if (sourceUrl != null)
        {
            if (items.Any(
                    i => string.Equals(i.GetMetadataValue(SourceUrlAttrName), sourceUrl, StringComparison.Ordinal)))
            {
                Warning.Write($"A reference to '{sourceUrl}' already exists in '{project.FullPath}'.");
                return;
            }
        }

        var metadata = new Dictionary <string, string>();

        if (!string.IsNullOrEmpty(sourceUrl))
        {
            metadata[SourceUrlAttrName] = sourceUrl;
        }

        if (codeGenerator != null)
        {
            metadata[CodeGeneratorAttrName] = codeGenerator.ToString();
        }

        project.AddElementWithAttributes(tagName, sourceFile, metadata);
        project.Save();
    }
Esempio n. 4
0
        private static IDictionary <string, string> GetServicePackages(CodeGenerator?type)
        {
            CodeGenerator generator  = type ?? CodeGenerator.NSwagCSharp;
            var           name       = Enum.GetName(typeof(CodeGenerator), generator);
            var           attributes = typeof(Program).Assembly.GetCustomAttributes <OpenApiDependencyAttribute>();

            var packages = attributes.Where(a => a.CodeGenerators.Contains(generator));
            var result   = new Dictionary <string, string>();

            if (packages != null)
            {
                foreach (var package in packages)
                {
                    result[package.Name] = package.Version;
                }
            }

            return(result);
        }