Esempio n. 1
0
        public int Execute()
        {
            if (Directory.Exists(HttpCacheDirectory))
            {
                Reports.Information.WriteLine($"Clearing cache directory {HttpCacheDirectory}");

                try
                {
                    FileOperationUtils.DeleteFolder(HttpCacheDirectory);
                    Reports.Information.WriteLine("Cache cleared.");
                }
                catch (Exception e)
                {
                    Reports.Error.WriteLine($"Unable to clear cache directory: {e.Message}");
                    return(1);
                }
            }

            return(0);
        }
Esempio n. 2
0
        // Creates a temporary project with the specified package as dependency
        private string CreateTemporaryProject(string installFolder, string packageName, string packageVersion)
        {
            var tempFolderName     = Guid.NewGuid().ToString("N");
            var tempFolderFullPath = Path.Combine(installFolder, tempFolderName);

            // Delete if exists already
            FileOperationUtils.DeleteFolder(tempFolderFullPath);
            Directory.CreateDirectory(tempFolderFullPath);

            WriteVerbose("Temporary folder name: {0}", tempFolderFullPath);
            var projectFileFullPath = Path.Combine(tempFolderFullPath, "project.json");

            File.WriteAllText(
                projectFileFullPath,
                string.Format(
                    @"{{
    ""dependencies"":{{
        ""{0}"":""{1}""
    }}
}}", packageName, packageVersion));

            return(projectFileFullPath);
        }
Esempio n. 3
0
        public async Task <bool> Execute(string packageId, string packageVersion)
        {
            // 0. Resolve the actual package id and version
            var packageIdAndVersion = await ResolvePackageIdAndVersion(packageId, packageVersion);

            if (packageIdAndVersion == null)
            {
                WriteError($"Failed to install {packageId}");
                return(false);
            }

            packageId      = packageIdAndVersion.Item1;
            packageVersion = packageIdAndVersion.Item2;

            WriteVerbose("Resolved package id: {0}", packageId);
            WriteVerbose("Resolved package version: {0}", packageVersion);

            // 1. Get the package without dependencies. We cannot resolve them now because
            // we don't know the target frameworks that the package supports

            FeedOptions.TargetPackagesFolder = _commandsRepository.PackagesRoot.Root;

            var temporaryProjectFileFullPath = CreateTemporaryProject(FeedOptions.TargetPackagesFolder, packageId, packageVersion);

            var packageFullPath = Path.Combine(
                _commandsRepository.PackagesRoot.Root,
                _commandsRepository.PathResolver.GetPackageDirectory(packageId, new SemanticVersion(packageVersion)));

            if (OverwriteCommands)
            {
                if (Directory.Exists(packageFullPath))
                {
                    WriteInfo($"Deleting {packageFullPath}");
                    FileOperationUtils.DeleteFolder(packageFullPath);
                    Directory.Delete(packageFullPath);
                }
            }

            try
            {
                RestoreCommand.RestoreDirectories.Add(temporaryProjectFileFullPath);
                if (!await RestoreCommand.Execute())
                {
                    return(false);
                }
            }
            finally
            {
                var folderToDelete = Path.GetDirectoryName(temporaryProjectFileFullPath);
                FileOperationUtils.DeleteFolder(folderToDelete);
                Directory.Delete(folderToDelete);
            }

            if (!ValidateApplicationPackage(packageFullPath))
            {
                return(false);
            }

            var packageAppFolder = Path.Combine(
                packageFullPath,
                InstallBuilder.CommandsFolderName);

            // 2. Now, that we have a valid app package, we can resolve its dependecies
            RestoreCommand.RestoreDirectories.Clear();
            RestoreCommand.RestoreDirectories.Add(packageAppFolder);
            if (!await RestoreCommand.Execute())
            {
                return(false);
            }

            // 3. Dependencies are in place, now let's install the commands
            if (!InstallCommands(packageId, packageFullPath))
            {
                return(false);
            }

            return(true);
        }
Esempio n. 4
0
        private bool TryResolveReferences(out XDocument resolutionResults, out string errorMessage)
        {
            resolutionResults = new XDocument();
            errorMessage      = string.Empty;

            if (!File.Exists(MsBuildPath))
            {
                errorMessage = string.Format("Unable to locate {0}", MsBuildPath.Red().Bold());
                return(false);
            }

            // Put ReferenceResolver.xml and intermediate result files into a temporary dir
            var tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            Directory.CreateDirectory(tempDir);

            try
            {
                _referenceResolverPath = Path.Combine(tempDir, ReferenceResolverFileName);
                var assembly             = typeof(WrapCommand).GetTypeInfo().Assembly;
                var embeddedResourceName = "Microsoft.Dnx.Tooling.compiler.resources." + ReferenceResolverFileName;
                using (var stream = assembly.GetManifestResourceStream(embeddedResourceName))
                {
                    File.WriteAllText(_referenceResolverPath, stream.ReadToEnd());
                }

                resolutionResults.Add(new XElement("root"));
                var projectFiles = new List <string> {
                    Path.GetFullPath(InputFilePath)
                };
                var intermediateResultFile = Path.Combine(tempDir, Path.GetRandomFileName());

                for (var i = 0; i != projectFiles.Count; i++)
                {
                    var properties = new[]
                    {
                        string.Format("/p:CustomAfterMicrosoftCommonTargets=\"{0}\"", _referenceResolverPath),
                        string.Format("/p:ResultFilePath=\"{0}\"", intermediateResultFile),
                        string.Format("/p:Configuration={0}", Configuration),
                        "/p:DesignTimeBuild=true",
                        "/p:BuildProjectReferences=false",
                        "/p:_ResolveReferenceDependencies=true" // Dump entire assembly reference closure
                    };

                    var msBuildArgs = string.Format("\"{0}\" /t:ResolveAndDump {1}",
                                                    projectFiles[i], string.Join(" ", properties));

                    Reports.Verbose.WriteLine("Resolving references for {0}", projectFiles[i].Bold());
                    Reports.Verbose.WriteLine();
                    Reports.Verbose.WriteLine("Command:");
                    Reports.Verbose.WriteLine();
                    Reports.Verbose.WriteLine("\"{0}\" {1}", MsBuildPath, msBuildArgs);
                    Reports.Verbose.WriteLine();
                    Reports.Verbose.WriteLine("MSBuild output:");
                    Reports.Verbose.WriteLine();

                    var startInfo = new ProcessStartInfo()
                    {
                        UseShellExecute        = false,
                        FileName               = MsBuildPath,
                        Arguments              = msBuildArgs,
                        RedirectStandardOutput = true,
                        RedirectStandardError  = true
                    };

                    var process = Process.Start(startInfo);
                    var stdOut  = process.StandardOutput.ReadToEnd();
                    var stdErr  = process.StandardError.ReadToEnd();
                    process.WaitForExit();

                    Reports.Verbose.WriteLine(stdOut);

                    if (process.ExitCode != 0)
                    {
                        errorMessage = string.Format("Failed to resolve references for {0}",
                                                     projectFiles[i].Red().Bold());
                        return(false);
                    }

                    var projectXDoc = XDocument.Parse(File.ReadAllText(intermediateResultFile));

                    foreach (var item in GetItemsByType(projectXDoc.Root, type: "ProjectReference"))
                    {
                        var relativePath = item.Attribute("evaluated").Value;
                        var fullPath     = PathUtility.GetAbsolutePath(projectFiles[i], relativePath);
                        if (!projectFiles.Contains(fullPath))
                        {
                            projectFiles.Add(fullPath);
                        }
                    }

                    resolutionResults.Root.Add(projectXDoc.Root);
                }
            }
            finally
            {
                FileOperationUtils.DeleteFolder(tempDir);
            }

            return(true);
        }