internal static string GetDownloadPath(StrawberryPerl perl) { string path; try { if (!Directory.Exists(perl.ArchivePath)) { Directory.CreateDirectory(perl.ArchivePath); } return(perl.ArchivePath + @"\" + perl.ArchiveName); } catch (UnauthorizedAccessException) { Console.WriteLine("Error, do not have permissions to create directory: " + perl.ArchivePath); } Console.WriteLine("Creating temporary directory instead"); do { path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); } while (Directory.Exists(path)); Directory.CreateDirectory(path); return(path + @"\" + perl.ArchiveName); }
internal static void Available() { List <StrawberryPerl> perls = GatherPerls(); Console.WriteLine("\nThe following Strawberry Perls are available:\n"); StrawberryPerl current_perl = CheckWhichPerlInPath(); string column_spaces = " "; foreach (StrawberryPerl perl in perls) { // cheap printf string name_to_print = perl.Name + column_spaces.Substring(0, column_spaces.Length - perl.Name.Length); Console.Write("\t" + name_to_print); if (PerlInstalled(perl)) { Console.Write(" [installed]"); } if (perl.Name == current_perl.Name) { Console.Write("*"); } Console.Write("\n"); } Console.WriteLine("\n* Currently using"); }
internal static StrawberryPerl CheckWhichPerlInPath() { // get user PATH and remove trailing semicolon if exists string path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User); StrawberryPerl current_perl = new StrawberryPerl(); if (path != null) { string[] paths = path.Split(';'); foreach (StrawberryPerl perl in GatherPerls()) { for (int i = 0; i < paths.Length; i++) { if (paths[i] == perl.PerlPath || paths[i] == perl.CPath || paths[i] == perl.PerlSitePath) { current_perl = perl; break; } } } } return(current_perl); }
internal static void Extract(StrawberryPerl perl, string archive_path) { if (File.Exists(archive_path)) { Console.WriteLine("Extracting " + archive_path); ExtractZip(archive_path, perl.InstallPath); } }
internal static bool PerlInstalled(StrawberryPerl perl) { if (Directory.Exists(perl.InstallPath) && File.Exists(perl.PerlPath + @"\perl.exe")) { return(true); } return(false); }
internal static string Fetch(StrawberryPerl perl) { WebClient webClient = new WebClient(); string archive_path = GetDownloadPath(perl); // Download if archive doesn't already exist if (!File.Exists(archive_path)) { Console.WriteLine("Downloading " + perl.Url + " to " + archive_path); webClient.DownloadFile(perl.Url, archive_path); } Console.WriteLine("Confirming checksum ..."); using (var cryptoProvider = new SHA1CryptoServiceProvider()) { using (var stream = File.OpenRead(archive_path)) { string hash = BitConverter.ToString(cryptoProvider.ComputeHash(stream)).Replace("-", "").ToLower(); if (perl.Sha1Checksum != hash) { Console.WriteLine("Error checksum of downloaded archive \n" + archive_path + "\ndoes not match expected output\nexpected: " + perl.Sha1Checksum + "\n got: " + hash); stream.Dispose(); Console.Write("Whould you like berrybrew to delete the corrupted download file? y/n [n]"); if (Console.ReadLine() == "y") { string retval = RemoveFile(archive_path); if (retval == "True") { Console.WriteLine("Deleted! Try to install it again!"); } else { Console.WriteLine("Unable to delete " + archive_path); } } Environment.Exit(0); } } } return(archive_path); }
internal static void RemovePerl(string version_to_remove) { try { StrawberryPerl perl = ResolveVersion(version_to_remove); StrawberryPerl current_perl = CheckWhichPerlInPath(); if (perl.Name == current_perl.Name) { Console.WriteLine("Removing Perl " + version_to_remove + " from PATH"); RemovePerlFromPath(); } if (Directory.Exists(perl.InstallPath)) { try { Directory.Delete(perl.InstallPath, true); Console.WriteLine("Successfully removed Strawberry Perl " + version_to_remove); } catch (System.IO.IOException) { Console.WriteLine("Unable to completely remove Strawberry Perl " + version_to_remove + " some files may remain"); } } else { Console.WriteLine("Strawberry Perl " + version_to_remove + " not found (are you sure it's installed?"); Environment.Exit(0); } } catch (ArgumentException) { Console.WriteLine("Unknown version of Perl. Use the available command to see what versions of Strawberry Perl are available"); Environment.Exit(0); } catch (UnauthorizedAccessException) { Console.WriteLine("Unable to remove Strawberry Perl " + version_to_remove + " permission was denied by System"); } }
internal static void AddPerlToPath(StrawberryPerl perl) { // get user PATH and remove trailing semicolon if exists string path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User); string[] new_path; if (path == null) { new_path = new string[] { perl.CPath, perl.PerlPath, perl.PerlSitePath }; } else { if (path[path.Length - 1] == ';') path = path.Substring(0, path.Length - 1); new_path = new string[] { path, perl.CPath, perl.PerlPath, perl.PerlSitePath }; } Environment.SetEnvironmentVariable("PATH", String.Join(";", new_path), EnvironmentVariableTarget.User); }
internal static void AddPerlToPath(StrawberryPerl perl) { // get user PATH and remove trailing semicolon if exists string path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User); string[] new_path; if (path == null) { new_path = new string[] { perl.CPath, perl.PerlPath, perl.PerlSitePath }; } else { if (path[path.Length - 1] == ';') { path = path.Substring(0, path.Length - 1); } new_path = new string[] { path, perl.CPath, perl.PerlPath, perl.PerlSitePath }; } Environment.SetEnvironmentVariable("PATH", String.Join(";", new_path), EnvironmentVariableTarget.User); }
internal static void Switch(string version_to_switch) { try { StrawberryPerl perl = ResolveVersion(version_to_switch); // if Perl version not installed, can't switch if (!PerlInstalled(perl)) { Console.WriteLine("Perl version " + perl.Name + " is not installed. Run the command:\n\n\tberrybrew install " + perl.Name); Environment.Exit(0); } RemovePerlFromPath(); if (ScanUserPath(new Regex("perl.bin"))) { Console.WriteLine("Warning! Perl binary found in your user PATH: " + "\nYou should remove this as it can prevent berrybrew from working."); } if (ScanSystemPath(new Regex("perl.bin"))) { Console.WriteLine("Warning! Perl binary found in your system PATH: " + "\nYou should remove this as it can prevent berrybrew from working."); } AddPerlToPath(perl); Console.WriteLine("Switched to " + version_to_switch + ", start a new terminal to use it."); } catch (ArgumentException) { Console.WriteLine("Unknown version of Perl. Use the available command to see what versions of Strawberry Perl are available"); Environment.Exit(0); } }
internal static bool PerlInstalled(StrawberryPerl perl) { if (Directory.Exists(perl.InstallPath) && File.Exists(perl.PerlPath + @"\perl.exe")) { return true; } return false; }
internal static string GetDownloadPath(StrawberryPerl perl) { string path; try { if (! Directory.Exists(perl.ArchivePath)) Directory.CreateDirectory(perl.ArchivePath); return perl.ArchivePath + @"\" + perl.ArchiveName; } catch (UnauthorizedAccessException) { Console.WriteLine("Error, do not have permissions to create directory: " + perl.ArchivePath); } Console.WriteLine("Creating temporary directory instead"); do { path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); } while (Directory.Exists(path)); Directory.CreateDirectory(path); return path + @"\" + perl.ArchiveName; }
internal static string Fetch(StrawberryPerl perl) { WebClient webClient = new WebClient(); string archive_path = GetDownloadPath(perl); // Download if archive doesn't already exist if (! File.Exists(archive_path)) { Console.WriteLine("Downloading " + perl.Url + " to " + archive_path); webClient.DownloadFile(perl.Url, archive_path); } Console.WriteLine("Confirming checksum ..."); using(var cryptoProvider = new SHA1CryptoServiceProvider()) { using (var stream = File.OpenRead(archive_path)) { string hash = BitConverter.ToString(cryptoProvider.ComputeHash(stream)).Replace("-","").ToLower(); if (perl.Sha1Checksum != hash) { Console.WriteLine("Error checksum of downloaded archive does not match expected output\nexpected: " + perl.Sha1Checksum + "\n got: " + hash); Environment.Exit(0); } } } return archive_path; }
internal static StrawberryPerl CheckWhichPerlInPath() { // get user PATH and remove trailing semicolon if exists string path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User); StrawberryPerl current_perl = new StrawberryPerl(); if (path != null) { string[] paths = path.Split(';'); foreach (StrawberryPerl perl in GatherPerls()) { for (int i = 0; i < paths.Length; i++) { if (paths[i] == perl.PerlPath || paths[i] == perl.CPath || paths[i] == perl.PerlSitePath) { current_perl = perl; break; } } } } return current_perl; }
static void Main(string[] args) { if (args.Length == 0) { PrintHelp(); Environment.Exit(0); } switch (args[0]) { case "install": if (args.Length == 1) { Console.WriteLine("install command requires a version argument. Use the available command to see what versions of Strawberry Perl are available"); Environment.Exit(0); } try { StrawberryPerl perl = ResolveVersion(args[1]); string archive_path = Fetch(perl); Extract(perl, archive_path); Available(); } catch (ArgumentException) { Console.WriteLine("Unknown version of Perl. Use the available command to see what versions of Strawberry Perl are available"); Environment.Exit(0); } break; case "switch": if (args.Length == 1) { Console.WriteLine("switch command requires a version argument. Use the available command to see what versions of Strawberry Perl are available"); Environment.Exit(0); } Switch(args[1]); break; case "available": Available(); break; case "config": Config(); break; case "remove": if (args.Length == 1) { Console.WriteLine("remove command requires a version argument. Use the available command to see what versions of Strawberry Perl are available"); Environment.Exit(0); } RemovePerl(args[1]); break; case "exec": if (args.Length == 1) { Console.WriteLine("exec command requires a command to run."); Environment.Exit(0); } args[0] = ""; Exec(String.Join(" ", args).Trim()); break; case "license": if (args.Length == 1) { PrintLicense(); Environment.Exit(0); } break; default: PrintHelp(); break; } }