private bool InstallCommands(string packageId, string appPath) { string commandsFolder = Path.Combine(appPath, InstallBuilder.CommandsFolderName); IEnumerable <string> allAppCommandsFiles; if (RuntimeEnvironmentHelper.IsWindows) { allAppCommandsFiles = Directory.EnumerateFiles(commandsFolder, "*.cmd"); } else { // We have cmd files and project.*.json files in the same folder allAppCommandsFiles = Directory.EnumerateFiles(commandsFolder, "*.*") .Where(cmd => !cmd.EndsWith(".cmd") && !cmd.EndsWith(".json")) .ToList(); } var allAppCommands = allAppCommandsFiles .Select(cmd => Path.GetFileNameWithoutExtension(cmd)) .ToList(); IEnumerable <string> conflictingCommands; if (OverwriteCommands) { conflictingCommands = new string[0]; } else { // Conflicting commands are only the commands not owned by this application conflictingCommands = allAppCommands.Where(appCmd => { var commandOwner = _commandsRepository.FindCommandOwner(appCmd); return(commandOwner != null && !packageId.Equals(commandOwner.Id, StringComparison.OrdinalIgnoreCase)); }); } if (conflictingCommands.Any()) { WriteError(string.Format( "The application commands cannot be installed because the following commands already exist: {0}. No changes were made. Rerun the command with the --overwrite switch to replace the existing commands.", string.Join(", ", conflictingCommands))); return(false); } var installPath = Path.GetFullPath(_commandsRepository.Root.Root); foreach (string commandFileFullPath in allAppCommandsFiles) { string commandFileName = RuntimeEnvironmentHelper.IsWindows ? Path.GetFileName(commandFileFullPath) : Path.GetFileNameWithoutExtension(commandFileFullPath); string commandScript; if (RuntimeEnvironmentHelper.IsWindows) { commandScript = string.Format( "@\"%~dp0{0}\" %*", commandFileFullPath.Substring(installPath.Length)); } else { commandScript = string.Format( "\"$(dirname $0){0}\" $@", commandFileFullPath.Substring(installPath.Length)); } string scriptFilePath = Path.Combine(installPath, commandFileName); File.WriteAllText(scriptFilePath, commandScript); if (!RuntimeEnvironmentHelper.IsWindows) { FileOperationUtils.MarkExecutable(commandFileFullPath); FileOperationUtils.MarkExecutable(scriptFilePath); } } var installedCommands = allAppCommandsFiles.Select(cmd => Path.GetFileNameWithoutExtension(cmd)); WriteInfo("The following commands were installed: " + string.Join(", ", installedCommands)); return(true); }
public void DnuRestore_ExecutesScripts(string flavor, string os, string architecture) { bool isWindows = TestUtils.CurrentRuntimeEnvironment.OperatingSystem == "Windows"; var environment = new Dictionary <string, string> { { "DNX_TRACE", "0" }, }; var expectedPreContent = @"""one"" ""two"" "">three"" ""four"" "; var expectedPostContent = @"""five"" ""six"" ""argument seven"" ""argument eight"" "; string projectJsonContent; string scriptContent; string scriptName; if (isWindows) { projectJsonContent = @"{ ""frameworks"": { ""dnx451"": { } }, ""scripts"": { ""prerestore"": [ ""script.cmd one two > pre"", ""script.cmd ^>three >> pre && script.cmd ^ four >> pre"" ], ""postrestore"": [ ""\""%project:Directory%/script.cmd\"" five six > post"", ""\""%project:Directory%/script.cmd\"" \""argument seven\"" \""argument eight\"" >> post"" ] } }"; scriptContent = @"@echo off :argumentStart if ""%~1""=="""" goto argumentEnd echo ""%~1"" shift goto argumentStart :argumentEnd"; scriptName = "script.cmd"; } else { projectJsonContent = @"{ ""frameworks"": { ""dnx451"": { } }, ""scripts"": { ""prerestore"": [ ""script one two > pre"", ""script.sh \\>three >> pre; ./script.sh four >> pre"" ], ""postrestore"": [ ""\""%project:Directory%/script\"" five six > post"", ""\""%project:Directory%/script.sh\"" \""argument seven\"" \""argument eight\"" >> post"" ] } }"; scriptContent = @"#!/usr/bin/env bash set -o errexit for arg in ""$@""; do printf ""\""%s\""\n"" ""$arg"" done"; scriptName = "script.sh"; } var projectStructure = $@"{{ '.': ['project.json', '{ scriptName }'] }}"; var runtimeHomePath = _fixture.GetRuntimeHomeDir(flavor, os, architecture); using (var testEnv = new DnuTestEnvironment(runtimeHomePath, projectName: "Project Name")) { var projectPath = testEnv.ProjectPath; DirTree.CreateFromJson(projectStructure) .WithFileContents("project.json", projectJsonContent) .WithFileContents(scriptName, scriptContent) .WriteTo(projectPath); FileOperationUtils.MarkExecutable(Path.Combine(projectPath, scriptName)); string output; string error; var exitCode = DnuTestUtils.ExecDnu( runtimeHomePath, subcommand: "restore", arguments: null, stdOut: out output, stdErr: out error, environment: environment, workingDir: projectPath); Assert.Equal(0, exitCode); Assert.Empty(error); Assert.Contains("Executing script 'prerestore' in project.json", output); Assert.Contains("Executing script 'postrestore' in project.json", output); var preContent = File.ReadAllText(Path.Combine(projectPath, "pre")); Assert.Equal(expectedPreContent, preContent); var postContent = File.ReadAllText(Path.Combine(projectPath, "post")); Assert.Equal(expectedPostContent, postContent); } }