private int New(NewOptions opts) { string version = opts.Version ?? installationManager.GetLatestDarkRiftVersion(); ServerTier tier = opts.Pro ? ServerTier.Pro : ServerTier.Free; string targetDirectory = opts.TargetDirectory ?? Environment.CurrentDirectory; installationManager.Install(version, tier, opts.Platform, false); try { templater.Template(opts.Type, targetDirectory, version, tier, opts.Platform, opts.Force); } catch (DirectoryNotEmptyException) { Console.Error.WriteLine(Output.Red("Cannot create from template, the directory is not empty. Use -f to force creation.")); Console.Error.WriteLine("\t" + Environment.GetCommandLineArgs()[0] + " " + Parser.Default.FormatCommandLine(new NewOptions { Type = opts.Type, TargetDirectory = opts.TargetDirectory, Force = true })); return 1; } catch (ArgumentException e) { Console.Error.WriteLine(Output.Red(e.Message)); return 1; } return 0; }
public DarkRiftInstallation(string version, ServerTier tier, ServerPlatform platform, string installationPath) { Version = version; Tier = tier; Platform = platform; InstallationPath = installationPath; }
/// <summary> /// Expands a template. /// </summary> /// <param name="type">The template to expand</param> /// <param name="targetDirectory">The directory to exapand into.</param> /// <param name="version">The DarkRift version to template with.</param> /// <param name="tier">The DarkRift tier to template with.</param> /// <param name="platform">The DarkRift platform to template with.</param> /// <param name="force">Whether to force expansion on a non-empty directory.</param> public void Template(string type, string targetDirectory, string version, ServerTier tier, ServerPlatform platform, bool force) { string templatePath = Path.Combine(templatesPath, type + ".zip"); Directory.CreateDirectory(targetDirectory); if (Directory.GetFiles(targetDirectory).Length > 0 && !force) { throw new DirectoryNotEmptyException(); } if (!File.Exists(templatePath)) { throw new ArgumentException("Cannot create from template, no template with that name exists.", nameof(type)); } Console.WriteLine($"Creating new {type} '{Path.GetFileName(targetDirectory)}' from template..."); ZipFile.ExtractToDirectory(templatePath, targetDirectory, true); Console.WriteLine($"Cleaning up extracted artifacts..."); foreach (string path in Directory.GetFiles(targetDirectory, "*.*", SearchOption.AllDirectories)) { FileTemplater.TemplateFileAndPath(path, Path.GetFileName(targetDirectory), version, tier, platform); } Console.WriteLine(Output.Green($"Created '{Path.GetFileName(targetDirectory)}'")); }
/// <summary> /// Gets the path to a specified installation, or null if it is not installed. /// </summary> /// <param name="version">The version number required.</param> /// <param name="pro">Whether the pro version should be used.</param> /// <param name="platform">Whether the .NET Standard build should be used.</param> /// <returns>The path to the installation, or null, if it is not installed.</returns> public DarkRiftInstallation GetInstallation(string version, ServerTier tier, ServerPlatform platform) { string path = GetInstallationPath(version, tier, platform); if (fileUtility.DirectoryExists(path)) { return(new DarkRiftInstallation(version, tier, platform, path)); } return(null); }
/// <summary> /// Downloads and installs a DarkRift version. /// </summary> /// <param name="version">The version to be installed</param> /// <param name="tier">The tier</param> /// <param name="platform">The platform</param> /// <param name="downloadDirectory">The directory to download the release to</param> /// <returns>True if installed successfully otherwise false</returns> public bool DownloadVersionTo(string version, ServerTier tier, string platform, string downloadDirectory) { string stagingPath = fileUtility.GetTempFileName(); string uri = $"/DarkRift2/Releases/{version}/{tier}/{platform}/"; if (tier == ServerTier.Pro) { string invoiceNumber = invoiceManager.GetInvoiceNumber(); if (invoiceNumber == null) { Console.Error.WriteLine(Output.Red($"You must provide an invoice number in order to download Pro DarkRift releases.")); return(false); } uri += $"?invoice={invoiceNumber}"; } try { webClientUtility.DownloadFile(uri, stagingPath); } catch (WebException e) { Console.Error.WriteLine(Output.Red($"Could not download DarkRift {version} - {tier} (.NET {platform}):\n\t{e.Message}")); return(false); } Console.WriteLine($"Extracting package..."); try { fileUtility.ExtractZipTo(stagingPath, downloadDirectory); } catch (Exception) { // Make sure we don't leave a partial install if (fileUtility.DirectoryExists(downloadDirectory)) { fileUtility.Delete(downloadDirectory); } throw; } finally { fileUtility.Delete(stagingPath); } Console.WriteLine(Output.Green($"Successfully downloaded DarkRift {version} - {tier} (.NET {platform}).")); return(true); }
/// <summary> /// Installs a version of DarkRift, if not alredy installed. /// </summary> /// <param name="version">The version number required.</param> /// <param name="pro">Whether the pro version should be used.</param> /// <param name="platform">Whether the .NET Standard build should be used.</param> /// <returns>The path to the installation, or null, if it is not available</returns> public DarkRiftInstallation Install(string version, ServerTier tier, ServerPlatform platform, bool forceRedownload) { string path = GetInstallationPath(version, tier, platform); if (forceRedownload || !fileUtility.DirectoryExists(path)) { if (!remoteRepository.DownloadVersionTo(version, tier, platform, path)) { return(null); } } return(new DarkRiftInstallation(version, tier, platform, path)); }
/// <summary> /// Gets a list of versions installed with specific tier and platform /// </summary> /// <param name="tier">The tier</param> /// <param name="platform">The platform</param> /// <returns>A list of installed versions</returns> public List <DarkRiftInstallation> GetVersions(ServerTier tier, ServerPlatform platform) { string searchPath = Path.Combine(installationDirectory, tier.ToString().ToLower(), platform.ToString().ToLower()); if (!fileUtility.DirectoryExists(searchPath)) { return(new List <DarkRiftInstallation>()); } else { return(fileUtility.GetDirectories(searchPath) .Select(path => new DarkRiftInstallation(path, tier, platform, Path.Combine(searchPath, path))) .ToList()); } }
/// <summary> /// Gets a list of versions installed with specific tier and platform /// </summary> /// <param name="tier">The tier</param> /// <returns>A list of installed versions</returns> public List <DarkRiftInstallation> GetVersions(ServerTier tier) { string searchPath = Path.Combine(installationDirectory, tier.ToString().ToLower()); if (!fileUtility.DirectoryExists(searchPath)) { return(new List <DarkRiftInstallation>()); } else { return(fileUtility.GetDirectories(searchPath) .SelectMany(path => fileUtility.GetDirectories(Path.Combine(searchPath, path)) .Select(subPath => new DarkRiftInstallation( subPath, tier, path switch { "core" => "Core", "framework" => "Framework", _ => path },
/// <summary> /// Gets the path to a specified installation, downloading it if required. /// </summary> /// <param name="version">The version number required.</param> /// <param name="pro">Whether the pro version should be used.</param> /// <param name="netStandard">Whether the .NET Standard build should be used.</param> /// <returns>The path to the installation, or null, if it cannot be provided.</returns> public static string GetInstallationPath(string version, ServerTier tier, ServerPlatform platform) { string fullPath = Path.Combine(USER_DR_DIR, "installed", tier.ToString().ToLower(), platform.ToString().ToLower(), version); if (!Directory.Exists(fullPath)) { Console.WriteLine($"DarkRift {version} - {tier} (.NET {platform}) not installed! Downloading package..."); string stagingPath = Path.Combine(USER_DR_DIR, "Download.zip"); string uri = $"https://www.darkriftnetworking.com/DarkRift2/Releases/{version}/{tier}/{platform}/"; if (tier == ServerTier.Pro) { string invoiceNumber = GetInvoiceNumber(); if (invoiceNumber == null) { Console.WriteLine(Output.Red($"You must provide an invoice number in order to download Pro DarkRift releases.")); return(null); } uri += $"?invoice={invoiceNumber}"; } try { using (WebClient myWebClient = new WebClient()) { myWebClient.DownloadFile(uri, stagingPath); } } catch (WebException e) { Console.WriteLine(Output.Red($"Could not download DarkRift {version} - {tier} (.NET {platform}):\n\t{e.Message}")); return(null); } Console.WriteLine($"Extracting package..."); Directory.CreateDirectory(fullPath); ZipFile.ExtractToDirectory(stagingPath, fullPath, true); Console.WriteLine(Output.Green($"Successfully downloaded package.")); } return(fullPath); }
/// <summary> /// Gets a list of versions with specific tier and platform /// </summary> /// <param name="tier">The tier</param> /// <param name="platform">The platform</param> /// <returns>List of paths to the versions</returns> public static List <string> GetVersions(ServerTier tier, ServerPlatform platform) { var installationFolder = GetInstallationPath("", tier, platform); List <string> versions = new List <string>(); if (Directory.Exists(installationFolder)) { string[] paths = Directory.GetDirectories(installationFolder); // This removes the path and just leaves the version number for (int i = 0; i < paths.Length; i++) { versions.Add(Path.GetFileName(paths[i])); } } return(versions); }
/// <summary> /// Downloads and installs a DarkRift version /// </summary> /// <param name="version">The version to be installed</param> /// <param name="tier">The tier</param> /// <param name="platform">The platform</param> /// <returns>True if installed successfully otherwise false</returns> public static bool DownloadVersion(string version, ServerTier tier, ServerPlatform platform) { string fullPath = GetInstallationPath(version, tier, platform); string stagingPath = Path.Combine(Config.USER_DR_DIR, "Download.zip"); string uri = $"{Config.DR_DR2_RELEASE_URI}/{version}/{tier}/{platform}/"; if (tier == ServerTier.Pro) { string invoiceNumber = GetInvoiceNumber(); if (invoiceNumber == null) { Console.Error.WriteLine(Output.Red($"You must provide an invoice number in order to download Pro DarkRift releases.")); return(false); } uri += $"?invoice={invoiceNumber}"; } try { using (WebClient myWebClient = new WebClient()) { myWebClient.DownloadFile(uri, stagingPath); } } catch (WebException e) { Console.Error.WriteLine(Output.Red($"Could not download DarkRift {version} - {tier} (.NET {platform}):\n\t{e.Message}")); return(false); } Console.WriteLine($"Extracting package..."); Directory.CreateDirectory(fullPath); ZipFile.ExtractToDirectory(stagingPath, fullPath, true); Console.WriteLine(Output.Green($"Successfully downloaded DarkRift {version} - {tier} (.NET {platform})")); return(true); }
/// <summary> /// Prints formatted DarkRift version information on the console /// </summary> /// <param name="version">The version to be printed.</param> /// <param name="pro">Whether the pro version should be used.</param> /// <param name="platform">Whether the .NET Standard build should be used.</param> private static void PrintVersion(string version, ServerTier tier, ServerPlatform platform) { string output = ""; // There's no free or pro in documentation output += $"DarkRift {version} - {tier} (.NET {platform})"; if (Directory.Exists(Path.Combine(Config.USER_DR_DIR, "documentation", version))) { output += " and its documentation are"; } else { output += " is"; } output += " installed"; Console.WriteLine(output); }
private static int Pull(PullOptions opts) { // If --list was specified, list installed versions and tell if documentation for that version is available locally if (opts.List) { VersionManager.ListInstalledVersions(); return(0); } // if version provided is "latest", it is being replaced with currently most recent one if (opts.Version == "latest") { opts.Version = VersionManager.GetLatestDarkRiftVersion(); } if (opts.Version == null) { // if version info was omitted, overwrite any parameters with current project settings if (Project.Loaded) { opts.Version = Project.Runtime.Version; opts.Platform = Project.Runtime.Platform; opts.Pro = Project.Runtime.Tier == ServerTier.Pro; } else { Console.Error.WriteLine(Output.Red($"Couldn't find a version to install. To download latest version use \"latest\" as the version")); return(2); } } ServerTier actualTier = opts.Pro ? ServerTier.Pro : ServerTier.Free; // If --docs was specified, download documentation instead bool success = false; if (opts.Docs) { bool docsInstalled = VersionManager.IsDocumentationInstalled(opts.Version); if (docsInstalled && !opts.Force) { Console.WriteLine(Output.Green($"Documentation for DarkRift {opts.Version} - {actualTier} (.NET {opts.Platform}) already installed! To force a reinstall use darkrift pull docs {opts.Version} -f")); success = true; } else { success = VersionManager.DownloadDocumentation(opts.Version); } } else { bool versionInstalled = VersionManager.IsVersionInstalled(opts.Version, actualTier, opts.Platform); if (versionInstalled && !opts.Force) { Console.WriteLine(Output.Green($"DarkRift {opts.Version} - {actualTier} (.NET {opts.Platform}) already installed! To force a reinstall use darkrift pull {opts.Version} -f")); success = true; } else { success = VersionManager.DownloadVersion(opts.Version, actualTier, opts.Platform); } } if (!success) { Console.Error.WriteLine(Output.Red("Invalid command")); Console.Error.WriteLine("\t" + Environment.GetCommandLineArgs()[0] + " " + Parser.Default.FormatCommandLine(new PullOptions())); return(1); } return(0); }
/// <summary> /// Template the given path file's path and content. /// </summary> /// <param name="filePath">The path of the file.</param> /// <param name="resourceName">The name of the resource being created.</param> /// <param name="darkriftVersion">The version of DarkRift being used.</param> /// <param name="tier">The tier of DarkRift being used.</param> /// <param name="platform">The platform the DarkRift being used was built for.</param> public static void TemplateFileAndPath(string filePath, string resourceName, string darkriftVersion, ServerTier tier, ServerPlatform platform) { resourceName = Normalize(resourceName); string resolvedPath = TemplateString(filePath, resourceName, darkriftVersion, tier, platform); // Template the content of files containing __c__ if (resolvedPath.Contains("__c__")) { resolvedPath = resolvedPath.Replace("__c__", ""); File.WriteAllText(filePath, TemplateString(File.ReadAllText(filePath), resourceName, darkriftVersion, tier, platform)); } if (resolvedPath != filePath) { File.Move(filePath, resolvedPath); } // Delete files containing __d__ if (resolvedPath.Contains("__d__")) { File.Delete(resolvedPath); } }
/// <summary> /// Template the given string. /// </summary> /// <param name="text">The string to template.</param> /// <param name="resourceName">The name of the resource being created.</param> /// <param name="darkriftVersion">The version of DarkRift being used.</param> /// <param name="tier">The tier of DarkRift being used.</param> /// <param name="platform">The platform the DarkRift being used was built for.</param> private static string TemplateString(string text, string resourceName, string darkriftVersion, ServerTier tier, ServerPlatform platform) { // Keep files containing __k__ if (text.Contains("__k__")) { text = text.Replace("__k__", ""); } // Template __n__ to the resource name if (text.Contains("__n__")) { text = text.Replace("__n__", resourceName); } // Template __v__ to the darkrift version if (text.Contains("__v__")) { text = text.Replace("__v__", darkriftVersion); } // Template __t__ to 'Pro' or 'Free' if (text.Contains("__t__")) { text = text.Replace("__t__", tier.ToString()); } // Template __p__ to 'Standard' or 'Framework' if (text.Contains("__p__")) { text = text.Replace("__p__", platform.ToString()); } return(text); }
/// <summary> /// Creates a new Runtime configuration element. /// </summary> /// <param name="version">The version of DarkRift to use.</param> /// <param name="tier">The tier of DarkRift to use.</param> /// <param name="platform">If .NET standard or .NET framework should be used.</param> public Runtime(string version, ServerTier tier, ServerPlatform platform) { Version = version; Tier = tier; Platform = platform; }
/// <summary> /// Gets the path to a specified installation, or null if it is not installed. /// </summary> /// <param name="version">The version number required.</param> /// <param name="pro">Whether the pro version should be used.</param> /// <param name="platform">Whether the .NET Standard build should be used.</param> /// <returns>The path to the installation, or null, if it is not installed.</returns> private string GetInstallationPath(string version, ServerTier tier, ServerPlatform platform) { return(Path.Combine(installationDirectory, tier.ToString().ToLower(), platform.ToString().ToLower(), version)); }
/// <summary> /// Checks if a version of Dark Rift is installed /// </summary> /// <param name="version">Version to be checked</param> /// <param name="tier">The tier</param> /// <param name="platform">The platform</param> /// <returns>True if is installed otherwise false</returns> public static bool IsVersionInstalled(string version, ServerTier tier, ServerPlatform platform) { return(GetVersions(tier, platform).Contains(version)); }
/// <summary> /// Gets the path to a specified installation, downloading it if required. /// </summary> /// <param name="version">The version number required.</param> /// <param name="pro">Whether the pro version should be used.</param> /// <param name="platform">Whether the .NET Standard build should be used.</param> /// <returns>The path to the installation, or null, if it cannot be provided.</returns> public static string GetInstallationPath(string version, ServerTier tier, ServerPlatform platform) { return(Path.Combine(Config.USER_DR_DIR, "installed", tier.ToString().ToLower(), platform.ToString().ToLower(), version)); }