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

Fixing up the git support #20

Open
wants to merge 4 commits 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
3 changes: 2 additions & 1 deletion warmup/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using warmup.commands;
using warmup.infrastructure;
using warmup.infrastructure.settings;

namespace warmup
{
internal class Program
{
private static void Main(string[] args)
{
if (args.Length == 0)
if (args.Length == 0) // || !WarmupConfiguration.settings.SourceControlWarmupLocationIsValid)
{
CommonHelp.ShowHelp();
Environment.Exit(-1);
Expand Down
7 changes: 7 additions & 0 deletions warmup/infrastructure/CommonHelp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public static void ShowHelp()
WarmupConfiguration.settings.SourceControlType,
WarmupConfiguration.settings.SourceControlWarmupLocation
);
if (!WarmupConfiguration.settings.SourceControlWarmupLocationIsValid)
{

Console.WriteLine("----------");
Console.WriteLine("The Source Control Warmup Location is not Valid");
Console.WriteLine("Please ensure that '{0}' is accessible", WarmupConfiguration.settings.SourceControlWarmupLocation);
}
Console.WriteLine("----------");
Console.WriteLine("usage");
Console.WriteLine("----------");
Expand Down
52 changes: 52 additions & 0 deletions warmup/infrastructure/console/Verifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace warmup.infrastructure
{
public static class Verifier
{


public static string TestPath(string path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean up the spacing above this

{
return new Uri(path).IsFile
? Directory.Exists(path)
? path
: AdjustedPath(path)
: isValid(path)
? path
: AdjustedPath(path);
}

public static string AdjustedPath(string path)
{
return path.EndsWith(".git")
? path
: path + ".git";
}

public static bool isValid(string url)
{
try
{
var urlReq = (HttpWebRequest)WebRequest.Create(url);
var urlRes = (HttpWebResponse)urlReq.GetResponse();
var sStream = urlRes.GetResponseStream();

string read = new StreamReader(sStream).ReadToEnd();
return true;

}
catch (Exception)
{
//Url not valid
return false;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space is no

}
}
}
106 changes: 72 additions & 34 deletions warmup/infrastructure/exporters/Git.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,94 @@
using System;
using System.Diagnostics;
using warmup.infrastructure.extractors;
using warmup.infrastructure.settings;
using System.IO;
using System.Net;

namespace warmup.infrastructure.exporters
{
public class Git : BaseExporter
{
public static void Clone(Uri sourceLocation, TargetDir target)
public override void Export(string sourceControlWarmupLocation, string templateName, TargetDir targetDir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So there is an export then?

{
var separationCharacters = new[] {".git"};
string[] piecesOfPath = sourceLocation.ToString().Split(separationCharacters, StringSplitOptions.RemoveEmptyEntries);
if (piecesOfPath != null && piecesOfPath.Length > 0)
var gitsrc = (new Uri(sourceControlWarmupLocation)).IsFile
? Path.Combine(sourceControlWarmupLocation, templateName)
: NewUri(sourceControlWarmupLocation, templateName).AbsoluteUri;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a fan of this. For instance, the terminator is not on its own line. but more in that I don't see this to be "that" long that it shouldn't all be on the same line. This goes for the others that you set up this way as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to write in a pointfree style where I can, and will avoid assignment where I can, as mutable values are a source of bugs.
I'd normally use a discriminated union, but for the value i'm getting this is the most concise, readable and provably correct manner I could ensure that the path was correctly joined despite differing slash forms.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, not sure if we already have a common pattern in here for this or not. If we do I prefer it followed, no matter what the personal preference.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the GitHub URI would differ and it doesn't appear you are accounting for that. For instance, I am going to put in the URL https://github.com/ferventcoder/warmup-templates.git. That is the repo location.

warmup base newname

How is that going to translate to https:// github.com/ferventcoder/warmup-templates/**tree/master/**base (space added for bolding)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, nevermind. You are still cloning the entire repository. I thought we were getting something awesome like svnexport where it just downloads only the pieces that you want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although rereading your comments I'm not sure you understood what I meant...
This:

var gitsrc = (new Uri(sourceControlWarmupLocation)).IsFile
      ? Path.Combine(sourceControlWarmupLocation, templateName)
      : NewUri(sourceControlWarmupLocation, templateName).AbsoluteUri;

Should be like this:

var gitsrc = (new Uri(sourceControlWarmupLocation)).IsFile ? Path.Combine(sourceControlWarmupLocation, templateName) : NewUri(sourceControlWarmupLocation, templateName).AbsoluteUri;

Yes, I'm literally being terse on line breaks ;)


var destination = targetDir == null
? new TargetDir(Environment.CurrentDirectory)
: targetDir;

var gitSrcPath = Verifier.TestPath(gitsrc);

var psi = new ProcessStartInfo("cmd", string.Format(" /c git clone {0} {1}", gitSrcPath, destination.FullPath));

psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;

//todo: better error handling
Console.WriteLine("Running: {0} {1}", psi.FileName, psi.Arguments);
string output, error = "";
using (Process p = Process.Start(psi))
{
string sourceLocationToGit = piecesOfPath[0] + ".git";
output = p.StandardOutput.ReadToEnd();
error = p.StandardError.ReadToEnd();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are cleaning this up, let's not put the execution stuff in here. It should be in its own separate static class and method called from here. See https://github.com/chucknorris/roundhouse/blob/master/product/roundhouse/consoles/CommandExecutor.cs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be used as an example. I have a couple of additions in the way that I would have it run but I don't have them in a non-private repository at the moment.

The point is, if you are going to redirect output, you should capture the messages in real time and output them to the console instead of logging it all at once after the fact.


RemoveGitConfig(destination);

Console.WriteLine(output);
Console.WriteLine(error);

var psi = new ProcessStartInfo("cmd",string.Format(" /c git clone {0} {1}", sourceLocationToGit, target.FullPath));
}

psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
private static void RemoveGitConfig(TargetDir destination)
{
Directory.Delete(path: Path.Combine(destination.FullPath, ".git"), recursive: true);
}

//todo: better error handling
Console.WriteLine("Running: {0} {1}", psi.FileName, psi.Arguments);
string output, error = "";
using (Process p = Process.Start(psi))
private static Uri NewUri(string baseUri, string relativeUri)
{
var r = CreateUri(baseUri, relativeUri);
if (r.Item1)
{
return r.Item2;
}
else
{
r = CreateUri(baseUri, "");
if (r.Item1)
{
output = p.StandardOutput.ReadToEnd();
error = p.StandardError.ReadToEnd();
return r.Item2;
}
else
{
throw new ArgumentException("The base is not valid");
}

Console.WriteLine(output);
Console.WriteLine(error);

var templateName = piecesOfPath[1];
GitTemplateExtractor extractor = new GitTemplateExtractor(target, templateName);
extractor.Extract();
//string git_directory = Path.Combine(target.FullPath, ".git");
//if (Directory.Exists(git_directory))
//{
// Console.WriteLine("Deleting {0} directory", git_directory);
// Directory.Delete(git_directory, true);
//}
}
}

public override void Export(string sourceControlWarmupLocation, string templateName, TargetDir targetDir)
private static Tuple<bool, Uri> CreateUri(string baseUri, string relativeUri)
{
var baseUri = new Uri(WarmupConfiguration.settings.SourceControlWarmupLocation + templateName);
Console.WriteLine("git exporting to: {0}", targetDir.FullPath);
Clone(baseUri, targetDir);
return CreateUri(
baseUri.EndsWith("/")
? new Uri(baseUri)
: new Uri(baseUri + "/"),
relativeUri);
}

private static Tuple<bool,Uri> CreateUri(Uri baseUri, string relativeUri) {
Uri ret;
return Tuple.Create(Uri.TryCreate(baseUri, relativeUri, out ret), ret);
}

//public static void Clone(Uri sourceLocation, TargetDir target)
//{
//}

//public static void Clone(string sourceLocation, TargetDir target)
//{
//}
}
}
75 changes: 0 additions & 75 deletions warmup/infrastructure/extractors/GitTemplateExtractor.cs

This file was deleted.

6 changes: 6 additions & 0 deletions warmup/infrastructure/settings/WarmupConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public string SourceControlWarmupLocation
get { return (string) this["sourceControlWarmupLocation"]; }
}

public bool SourceControlWarmupLocationIsValid
{
get { return Verifier.isValid((string)this["sourceControlWarmupLocation"]); }
}


/// <summary>
/// The token to replace in the warmup templates. Not required, default value is "__NAME__"
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion warmup/warmup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
<Compile Include="commands\AddTextReplacement.cs" />
<Compile Include="commands\GenerateWarmup.cs" />
<Compile Include="commands\ICommand.cs" />
<Compile Include="infrastructure\console\Verifier.cs" />
<Compile Include="infrastructure\exporters\BaseExporter.cs" />
<Compile Include="infrastructure\exporters\Folder.cs" />
<Compile Include="infrastructure\exporters\Git.cs" />
<Compile Include="infrastructure\exporters\GitHub.cs" />
<Compile Include="infrastructure\extractors\GitTemplateExtractor.cs" />
<Compile Include="infrastructure\exporters\IExporter.cs" />
<Compile Include="infrastructure\CommandAttribute.cs" />
<Compile Include="infrastructure\CommonHelp.cs" />
Expand Down