Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds CheckDependencies command #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RESTier.CLI/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<configuration>
<appSettings>
<add key="IISExpressPath" value="C:\Program Files (x86)\IIS Express\"/>
<add key="MSBuildPath" value="C:\Program Files (x86)\MSBuild\14.0\Bin\"/>
<add key="msbuild" value="C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe"/>
<add key="ProviderInvariantName" value="System.Data.SqlClient"/>
</appSettings>
<startup>
Expand Down
9 changes: 7 additions & 2 deletions RESTier.CLI/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ public static CommandLineApplication Create(ref string[] args)
app.Option("-c|--connectionstring",
"A connection string to a SQL Server database. Used to reverse engineer a RESTier API.",
CommandOptionType.SingleValue);
var downloadOption = app.Option("-d|--download",
"Automatically downloads dependencies and places in child folder.",
CommandOptionType.NoValue);

app.Command("new", c => NewCommand.Configure(c));
var checkDependenciesCommand = app.Command("dependencies", c => CheckDependenciesCommand.Configure(c));
var newCommand = app.Command("new", c => NewCommand.Configure(c));
app.Command("generate", c => GenerateCommand.Configure(c));
app.Command("build", c => BuildCommand.Configure(c));
app.Command("run", c => RunCommand.Configure(c));
Expand All @@ -54,7 +58,8 @@ public static CommandLineApplication Create(ref string[] args)

ConsoleHelper.WriteLine(string.Format("Creating new RESTier API for {0}.",
connectionStringBuilder.InitialCatalog + connectionStringBuilder.AttachDBFilename));
app.Commands.First(c => c.Name == "new").Execute();
checkDependenciesCommand.Execute();
newCommand.Execute();
return 0;
});

Expand Down
2 changes: 1 addition & 1 deletion RESTier.CLI/Commands/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static void CmdMSBuild(string projectName, string buildSetting)

p.StartInfo.UseShellExecute = false;

p.StartInfo.Arguments = "/c " + "\"" + Path.Combine(ConfigurationManager.AppSettings["MSBuildPath"], "MSBuild.exe") + "\" " + projectName +
p.StartInfo.Arguments = "/c " + "\"" + ConfigurationManager.AppSettings["msbuild"] + "\" " + projectName +
(string.IsNullOrEmpty(buildSetting) ? "" : " " + buildSetting);

p.Start();
Expand Down
100 changes: 100 additions & 0 deletions RESTier.CLI/Commands/CheckDependenciesCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Net;
using Microsoft.Extensions.CommandLineUtils;

namespace Microsoft.RESTier.Cli.Commands
{
public class CheckDependenciesCommand
{
public static void Configure(CommandLineApplication command)
{
command.Description = "Determines whether dependencies are accessible to RESTier.exe";

command.OnExecute(() =>
{
ConsoleHelper.WriteVerbose("Checking dependencies.");
var downloadDependencies = command.Parent.Options.Exists(o => o.LongName == "download");
var dependencies = new[]
{
new Dependency
{
Name = "msbuild",
Path = ConfigurationManager.AppSettings["msbuild"],
DownloadInstructionsUri = "https://www.microsoft.com/en-us/download/details.aspx?id=40760",
DownloadInstallerUri =
"https://download.microsoft.com/download/9/B/B/9BB1309E-1A8F-4A47-A6C5-ECF76672A3B3/BuildTools_Full.exe",
FileName = "msbuild.exe",
InstallerExe = "BuildTools_Full.exe",
InstallerArgs = "/Q /Layout {dependencypath}"
}
};
foreach (var dependency in dependencies)
{
if (!DependencyExists(dependency))
{
if (downloadDependencies)
{
DownloadDependency(dependency);
}
else
{
return -1;
}
}
}
return 0;
});
}

private static void DownloadDependency(Dependency dependency)
{
string directoryPath = Path.Combine(Directory.GetCurrentDirectory(), "dependencies", dependency.Name);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
ConsoleHelper.WriteVerbose("Downloading {0}.", dependency.Name);
try
{
var dependencyPath = Path.Combine(directoryPath, dependency.InstallerExe);
var webClient = new WebClient();
webClient.DownloadFile(dependency.DownloadInstallerUri, dependencyPath);
dependency.InstallerArgs = dependency.InstallerArgs.Replace("{dependencypath}",
directoryPath);
Process.Start(dependencyPath, dependency.InstallerArgs);
}
catch (Exception ex)
{
ConsoleHelper.WriteError(ex.Message + ex.InnerException.Message);
}
}

private static bool DependencyExists(Dependency dependency)
{
ConsoleHelper.WriteVerbose("Checking for {0}.", dependency.Name);

ConsoleHelper.WriteVerbose("Checking for {0} at {1}.", dependency.Name, dependency.Path);
if (File.Exists(dependency.Path))
{
ConsoleHelper.WriteVerbose("Found {0} at {1}.", dependency.Name, dependency.Path);
return true;
}

// TODO #5: Place static strings into a centralized place.
var alternatePath = Path.Combine("dependencies", dependency.Name, Path.GetFileName(dependency.Path));
if (File.Exists(alternatePath))
{
ConsoleHelper.WriteVerbose("Found {0} at {1}.", dependency.Name, alternatePath);
return true;
}

ConsoleHelper.WriteError(
"Could not locate {0}. You can manually download and install this dependency by following the instructions here: {1}",
dependency.Name, dependency.DownloadInstructionsUri);
return false;
}
}
}
2 changes: 0 additions & 2 deletions RESTier.CLI/Commands/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ public static void Configure(CommandLineApplication command)
{
command.Description = "Reverse engineers a data access layer from a database.";

command.Option("-c|--connection-string", "The connection string to connect to the database.",
CommandOptionType.SingleValue);
command.Option("-p|--project", "The name for the RESTier project", CommandOptionType.SingleValue);
command.HelpOption("-h|--help");

Expand Down
8 changes: 4 additions & 4 deletions RESTier.CLI/ConsoleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ private static void WritePretty(ConsoleColor color, Action<string> action, strin
}
}

public static void Verbose(string message)
public static void WriteVerbose(string message, params object[] args)
{
if (IsVerbose)
{
WriteLine(message);
WriteLine(ConsoleColor.Yellow, message, args);
}
}

public void WriteTrace(string message)
=> Verbose(message);
=> WriteVerbose(message);

public void WriteDebug(string message)
=> Verbose(message);
=> WriteVerbose(message);

public void WriteInformation(string message)
=> WriteLine(message);
Expand Down
13 changes: 13 additions & 0 deletions RESTier.CLI/Dependency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Microsoft.RESTier.Cli
{
internal class Dependency
{
public string Name { get; set; }
public string Path { get; set; }
public string FileName { get; set; }
public string DownloadInstructionsUri { get; set; }
public string DownloadInstallerUri { get; set; }
public string InstallerExe { get; set; }
public string InstallerArgs { get; set; }
}
}
25 changes: 25 additions & 0 deletions RESTier.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

Expand All @@ -10,6 +11,8 @@ public static int Main(string[] args)
{
ConsoleHelper.IsVerbose = HandleVerboseOption(ref args);

HandleDebugSwitch(ref args);

try
{
return CommandExecutor.Create(ref args).Execute(args);
Expand Down Expand Up @@ -39,5 +42,27 @@ private static bool HandleVerboseOption(ref string[] args)
}
return false;
}


[Conditional("DEBUG")]
private static void HandleDebugSwitch(ref string[] args)

{
for (var i = 0; i < args.Length; i++)

{
if (args[i] == "--debug")

{
args = args.Take(i).Concat(args.Skip(i + 1)).ToArray();

Console.WriteLine("Waiting for debugger to attach. Press ENTER to continue");

Console.WriteLine($"Process ID: {Process.GetCurrentProcess().Id}");

Console.ReadLine();
}
}
}
}
}
2 changes: 2 additions & 0 deletions RESTier.CLI/RESTier.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
<ItemGroup>
<Compile Include="CodeGenerationEngine.cs" />
<Compile Include="Commands\BuildCommand.cs" />
<Compile Include="Commands\CheckDependenciesCommand.cs" />
<Compile Include="Commands\CodeGenerationEngine.cs" />
<Compile Include="Dependency.cs" />
<Compile Include="ConsoleHelper.cs" />
<Compile Include="CoreStrings.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down