Пример #1
0
        private string GetCommandFilePath(
            LockFile lockFile,
            LockFileTargetLibrary toolLibrary,
            LockFileItem runtimeAssembly)
        {
            var packageDirectory = lockFile.GetPackageDirectory(toolLibrary);

            if (packageDirectory == null)
            {
                throw new GracefulException(string.Format(
                                                LocalizableStrings.CommandAssembliesNotFound,
                                                toolLibrary.Name));
            }

            var filePath = Path.Combine(
                packageDirectory,
                PathUtility.GetPathWithDirectorySeparator(runtimeAssembly.Path));

            return(filePath);
        }
Пример #2
0
        internal void GenerateDepsJsonFile(
            LockFile toolLockFile,
            NuGetFramework framework,
            string depsPath,
            SingleProjectInfo toolLibrary,
            string toolDepsJsonGeneratorProject)
        {
            if (string.IsNullOrEmpty(toolDepsJsonGeneratorProject) ||
                !File.Exists(toolDepsJsonGeneratorProject))
            {
                throw new GracefulException(LocalizableStrings.DepsJsonGeneratorProjectNotSet);
            }

            Reporter.Verbose.WriteLine(string.Format(
                                           LocalizableStrings.GeneratingDepsJson,
                                           depsPath));

            var tempDepsFile = Path.GetTempFileName();

            var args = new List <string>();

            args.Add(toolDepsJsonGeneratorProject);
            args.Add($"-property:ProjectAssetsFile=\"{toolLockFile.Path}\"");
            args.Add($"-property:ToolName={toolLibrary.Name}");
            args.Add($"-property:ProjectDepsFilePath={tempDepsFile}");

            var toolTargetFramework = toolLockFile.Targets.First().TargetFramework.GetShortFolderName();

            args.Add($"-property:TargetFramework={toolTargetFramework}");


            //  Look for the .props file in the Microsoft.NETCore.App package, until NuGet
            //  generates .props and .targets files for tool restores (https://github.com/NuGet/Home/issues/5037)
            var platformLibrary = toolLockFile.Targets
                                  .FirstOrDefault(t => framework == t.TargetFramework)
                                  ?.GetPlatformLibrary();

            if (platformLibrary != null)
            {
                string buildRelativePath = platformLibrary.Build.FirstOrDefault()?.Path;

                var platformLibraryPath = toolLockFile.GetPackageDirectory(platformLibrary);

                if (platformLibraryPath != null && buildRelativePath != null)
                {
                    //  Get rid of "_._" filename
                    buildRelativePath = Path.GetDirectoryName(buildRelativePath);

                    string platformLibraryBuildFolderPath = Path.Combine(platformLibraryPath, buildRelativePath);
                    var    platformLibraryPropsFile       = Directory.GetFiles(platformLibraryBuildFolderPath, "*.props").FirstOrDefault();

                    if (platformLibraryPropsFile != null)
                    {
                        args.Add($"-property:AdditionalImport={platformLibraryPropsFile}");
                    }
                }
            }

            //  Delete temporary file created by Path.GetTempFileName(), otherwise the GenerateBuildDependencyFile target
            //  will think the deps file is up-to-date and skip executing
            File.Delete(tempDepsFile);

            var msBuildExePath = _environment.GetEnvironmentVariable(Constants.MSBUILD_EXE_PATH);

            msBuildExePath = string.IsNullOrEmpty(msBuildExePath) ?
                             Path.Combine(AppContext.BaseDirectory, "MSBuild.dll") :
                             msBuildExePath;

            var result = new MSBuildForwardingAppWithoutLogging(args, msBuildExePath)
                         .GetProcessStartInfo()
                         .ExecuteAndCaptureOutput(out string stdOut, out string stdErr);

            if (result != 0)
            {
                Reporter.Verbose.WriteLine(string.Format(
                                               LocalizableStrings.UnableToGenerateDepsJson,
                                               stdOut + Environment.NewLine + stdErr));

                throw new GracefulException(string.Format(LocalizableStrings.UnableToGenerateDepsJson, toolDepsJsonGeneratorProject));
            }

            try
            {
                File.Move(tempDepsFile, depsPath);
            }
            catch (Exception e)
            {
                Reporter.Verbose.WriteLine(string.Format(
                                               LocalizableStrings.UnableToGenerateDepsJson,
                                               e.Message));

                try
                {
                    File.Delete(tempDepsFile);
                }
                catch (Exception e2)
                {
                    Reporter.Verbose.WriteLine(string.Format(
                                                   LocalizableStrings.UnableToDeleteTemporaryDepsJson,
                                                   e2.Message));
                }
            }
        }