diff --git a/warmup/Program.cs b/warmup/Program.cs index d3bf167..dcc7bf2 100644 --- a/warmup/Program.cs +++ b/warmup/Program.cs @@ -1,6 +1,7 @@ using System; using warmup.commands; using warmup.infrastructure; +using warmup.infrastructure.settings; namespace warmup { @@ -8,7 +9,7 @@ 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); diff --git a/warmup/infrastructure/CommonHelp.cs b/warmup/infrastructure/CommonHelp.cs index fc94571..09588c5 100644 --- a/warmup/infrastructure/CommonHelp.cs +++ b/warmup/infrastructure/CommonHelp.cs @@ -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("----------"); diff --git a/warmup/infrastructure/console/Verifier.cs b/warmup/infrastructure/console/Verifier.cs new file mode 100644 index 0000000..651f54a --- /dev/null +++ b/warmup/infrastructure/console/Verifier.cs @@ -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) + { + 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; + } + + } + } +} diff --git a/warmup/infrastructure/exporters/Git.cs b/warmup/infrastructure/exporters/Git.cs index 183730d..b295801 100644 --- a/warmup/infrastructure/exporters/Git.cs +++ b/warmup/infrastructure/exporters/Git.cs @@ -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) { - 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; + + 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(); + } + + 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 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 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) + //{ + //} } } \ No newline at end of file diff --git a/warmup/infrastructure/extractors/GitTemplateExtractor.cs b/warmup/infrastructure/extractors/GitTemplateExtractor.cs deleted file mode 100644 index 17ebd85..0000000 --- a/warmup/infrastructure/extractors/GitTemplateExtractor.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; - -namespace warmup.infrastructure.extractors -{ - public class GitTemplateExtractor - { - private const StringComparison Comparison = StringComparison.InvariantCultureIgnoreCase; - private readonly TargetDir _target; - private readonly string _templateName; - - public GitTemplateExtractor(TargetDir target, string templateName) - { - _target = target; - _templateName = templateName; - } - - public void Extract() - { - var topParent = new DirectoryInfo(_target.FullPath); - var directories = topParent.GetDirectories(); - var files = topParent.GetFiles(); - - if (TemplateNotFound(directories, files)) return; - - CleanTopParent(directories, files); - - var templateDir = directories.FirstOrDefault(d => d.Name.Equals(_templateName, Comparison)); - if (templateDir != null) MoveTemplateContent(templateDir, topParent); - } - - private void CleanTopParent(IEnumerable directories, IEnumerable files) - { - foreach (var directory in directories.Where(directory => - directory.Name != _templateName)) - DeleteDirectory(directory); - foreach (var file in files.Where(file => - !file.Name.Equals(_templateName + file.Extension, Comparison))) - SafeDeleteFile(file); - } - - private bool TemplateNotFound(IEnumerable directories, IEnumerable files) - { - return !directories.Any(di => di.Name.Equals(_templateName, Comparison)) && - !files.Any(f => f.Name.Equals(_templateName + f.Extension, Comparison)); - } - - private static void SafeDeleteFile(FileInfo file) - { - file.Attributes = FileAttributes.Normal; - file.Delete(); - } - - private static void DeleteDirectory(DirectoryInfo directory) - { - foreach (var dir in directory.GetDirectories()) - DeleteDirectory(dir); - foreach (var file in directory.GetFiles()) - SafeDeleteFile(file); - directory.Attributes = FileAttributes.Normal; - directory.Delete(); - } - - private static void MoveTemplateContent(DirectoryInfo templateDir, DirectoryInfo destinationDir) - { - foreach (var dir in templateDir.GetDirectories()) - dir.MoveTo(Path.Combine(destinationDir.FullName, dir.Name)); - foreach (var file in templateDir.GetFiles()) - file.MoveTo(Path.Combine(destinationDir.FullName, file.Name)); - templateDir.Delete(); - } - } -} \ No newline at end of file diff --git a/warmup/infrastructure/settings/WarmupConfiguration.cs b/warmup/infrastructure/settings/WarmupConfiguration.cs index 51c74a5..0a8cfb1 100644 --- a/warmup/infrastructure/settings/WarmupConfiguration.cs +++ b/warmup/infrastructure/settings/WarmupConfiguration.cs @@ -27,6 +27,12 @@ public string SourceControlWarmupLocation get { return (string) this["sourceControlWarmupLocation"]; } } + public bool SourceControlWarmupLocationIsValid + { + get { return Verifier.isValid((string)this["sourceControlWarmupLocation"]); } + } + + /// /// The token to replace in the warmup templates. Not required, default value is "__NAME__" /// diff --git a/warmup/warmup.csproj b/warmup/warmup.csproj index 37ca9bb..761cbc7 100644 --- a/warmup/warmup.csproj +++ b/warmup/warmup.csproj @@ -50,11 +50,11 @@ + -