コード例 #1
0
        private void EnsureToolJsonDepsFileExists(
            LibraryRange toolLibrary,
            LockFile toolLockFile,
            string depsPath)
        {
            if (!File.Exists(depsPath))
            {
                var projectContext = new ProjectContextBuilder()
                                     .WithLockFile(toolLockFile)
                                     .WithTargetFramework(s_toolPackageFramework.ToString())
                                     .Build();

                var exporter = projectContext.CreateExporter(Constants.DefaultConfiguration);

                var dependencyContext = new DependencyContextBuilder()
                                        .Build(null,
                                               null,
                                               exporter.GetAllExports(),
                                               true,
                                               s_toolPackageFramework,
                                               string.Empty);

                using (var fileStream = File.Create(depsPath))
                {
                    var dependencyContextWriter = new DependencyContextWriter();

                    dependencyContextWriter.Write(dependencyContext, fileStream);
                }
            }
        }
コード例 #2
0
        // Need to unit test this, so public
        public void GenerateDepsJsonFile(
            LockFile toolLockFile,
            string depsPath)
        {
            Reporter.Verbose.WriteLine($"Generating deps.json at: {depsPath}");

            var projectContext = new ProjectContextBuilder()
                                 .WithLockFile(toolLockFile)
                                 .WithTargetFramework(s_toolPackageFramework.ToString())
                                 .Build();

            var exporter = projectContext.CreateExporter(Constants.DefaultConfiguration);

            var dependencyContext = new DependencyContextBuilder()
                                    .Build(null,
                                           null,
                                           exporter.GetAllExports(),
                                           true,
                                           s_toolPackageFramework,
                                           string.Empty);

            var tempDepsFile = Path.GetTempFileName();

            using (var fileStream = File.Open(tempDepsFile, FileMode.Open, FileAccess.Write))
            {
                var dependencyContextWriter = new DependencyContextWriter();

                dependencyContextWriter.Write(dependencyContext, fileStream);
            }

            try
            {
                File.Copy(tempDepsFile, depsPath);
            }
            catch (Exception e)
            {
                Reporter.Verbose.WriteLine($"unable to generate deps.json, it may have been already generated: {e.Message}");
            }
            finally
            {
                try
                {
                    File.Delete(tempDepsFile);
                }
                catch (Exception e2)
                {
                    Reporter.Verbose.WriteLine($"unable to delete temporary deps.json file: {e2.Message}");
                }
            }
        }