Skip to content

Commit

Permalink
Make cache non-instance-specific
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Oct 8, 2018
1 parent fc0fa20 commit 3ef9558
Show file tree
Hide file tree
Showing 44 changed files with 1,053 additions and 366 deletions.
193 changes: 193 additions & 0 deletions Cmdline/Action/Cache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
using System;
using System.Linq;
using CommandLine;
using CommandLine.Text;
using log4net;
using CKAN.Versioning;

namespace CKAN.CmdLine
{
public class Cache : ISubCommand
{
public Cache() { }

private class CacheSubOptions : VerbCommandOptions
{
[VerbOption("list", HelpText = "List the download cache path")]
public CommonOptions ListOptions { get; set; }

[VerbOption("set", HelpText = "Set the download cache path")]
public SetOptions SetOptions { get; set; }

[VerbOption("clear", HelpText = "Clear the download cache directory")]
public CommonOptions ClearOptions { get; set; }

[VerbOption("reset", HelpText = "Set the download cache path to the default")]
public CommonOptions ResetOptions { get; set; }

[HelpVerbOption]
public string GetUsage(string verb)
{
HelpText ht = HelpText.AutoBuild(this, verb);
// Add a usage prefix line
ht.AddPreOptionsLine(" ");
if (string.IsNullOrEmpty(verb))
{
ht.AddPreOptionsLine("ckan cache - Manage the download cache path of CKAN");
ht.AddPreOptionsLine($"Usage: ckan cache <command> [options]");
}
else
{
ht.AddPreOptionsLine("cache " + verb + " - " + GetDescription(verb));
switch (verb)
{
// First the commands with one string argument
case "set":
ht.AddPreOptionsLine($"Usage: ckan cache {verb} [options] path");
break;

// Now the commands with only --flag type options
case "list":
case "clear":
case "reset":
default:
ht.AddPreOptionsLine($"Usage: ckan cache {verb} [options]");
break;
}
}
return ht;
}
}

private class SetOptions : CommonOptions
{
[ValueOption(0)]
public string Path { get; set; }
}

/// <summary>
/// Execute a cache subcommand
/// </summary>
/// <param name="mgr">KSPManager object containing our instances and cache</param>
/// <param name="opts">Command line options object</param>
/// <param name="unparsed">Raw command line options</param>
/// <returns>
/// Exit code for shell environment
/// </returns>
public int RunSubCommand(KSPManager mgr, CommonOptions opts, SubCommandOptions unparsed)
{
string[] args = unparsed.options.ToArray();

int exitCode = Exit.OK;
// Parse and process our sub-verbs
Parser.Default.ParseArgumentsStrict(args, new CacheSubOptions(), (string option, object suboptions) =>
{
// ParseArgumentsStrict calls us unconditionally, even with bad arguments
if (!string.IsNullOrEmpty(option) && suboptions != null)
{
CommonOptions options = (CommonOptions)suboptions;
options.Merge(opts);
user = new ConsoleUser(options.Headless);
manager = mgr ?? new KSPManager(user);
exitCode = options.Handle(manager, user);
if (exitCode != Exit.OK)
return;
switch (option)
{
case "list":
exitCode = ListCacheDirectory((CommonOptions)suboptions);
break;
case "set":
exitCode = SetCacheDirectory((SetOptions)suboptions);
break;
case "clear":
exitCode = ClearCacheDirectory((CommonOptions)suboptions);
break;
case "reset":
exitCode = ResetCacheDirectory((CommonOptions)suboptions);
break;
default:
user.RaiseMessage("Unknown command: cache {0}", option);
exitCode = Exit.BADOPT;
break;
}
}
}, () => { exitCode = MainClass.AfterHelp(); });
return exitCode;
}

private int ListCacheDirectory(CommonOptions options)
{
IWin32Registry winReg = new Win32Registry();
user.RaiseMessage(winReg.DownloadCacheDir);
printCacheInfo();
return Exit.OK;
}

private int SetCacheDirectory(SetOptions options)
{
if (string.IsNullOrEmpty(options.Path))
{
user.RaiseError("set <path> - argument missing, perhaps you forgot it?");
return Exit.BADOPT;
}

string failReason;
if (manager.TrySetupCache(options.Path, out failReason))
{
IWin32Registry winReg = new Win32Registry();
user.RaiseMessage($"Download cache set to {winReg.DownloadCacheDir}");
printCacheInfo();
return Exit.OK;
}
else
{
user.RaiseError($"Invalid path: {failReason}");
return Exit.BADOPT;
}
}

private int ClearCacheDirectory(CommonOptions options)
{
manager.Cache.RemoveAll();
user.RaiseMessage("Download cache cleared.");
printCacheInfo();
return Exit.OK;
}

private int ResetCacheDirectory(CommonOptions options)
{
string failReason;
if (manager.TrySetupCache("", out failReason))
{
IWin32Registry winReg = new Win32Registry();
user.RaiseMessage($"Download cache reset to {winReg.DownloadCacheDir}");
printCacheInfo();
}
else
{
user.RaiseError($"Can't reset cache path: {failReason}");
}
return Exit.OK;
}

private void printCacheInfo()
{
int fileCount;
long bytes;
manager.Cache.GetSizeInfo(out fileCount, out bytes);
user.RaiseMessage($"{fileCount} files, {CkanModule.FmtSize(bytes)}");
}

private KSPManager manager;
private IUser user;

private static readonly ILog log = LogManager.GetLogger(typeof(Cache));
}

}
10 changes: 6 additions & 4 deletions Cmdline/Action/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public class Import : ICommand
/// Initialize the command
/// </summary>
/// <param name="user">IUser object for user interaction</param>
public Import(IUser user)
public Import(KSPManager mgr, IUser user)
{
manager = mgr;
this.user = user;
}

Expand All @@ -45,7 +46,7 @@ public int RunCommand(CKAN.KSP ksp, object options)
{
log.InfoFormat("Importing {0} files", toImport.Count);
List<string> toInstall = new List<string>();
ModuleInstaller inst = ModuleInstaller.GetInstance(ksp, user);
ModuleInstaller inst = ModuleInstaller.GetInstance(ksp, manager.Cache, user);
inst.ImportFiles(toImport, user, mod => toInstall.Add(mod.identifier), !opts.Headless);
if (toInstall.Count > 0)
{
Expand Down Expand Up @@ -99,8 +100,9 @@ private void AddFile(HashSet<FileInfo> files, string filename)
}
}

private readonly IUser user;
private static readonly ILog log = LogManager.GetLogger(typeof(Import));
private readonly KSPManager manager;
private readonly IUser user;
private static readonly ILog log = LogManager.GetLogger(typeof(Import));
}

}
21 changes: 18 additions & 3 deletions Cmdline/Action/Install.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@ public class Install : ICommand
private static readonly ILog log = LogManager.GetLogger(typeof(Install));

public IUser user { get; set; }
private KSPManager manager;

public Install(IUser user)
/// <summary>
/// Initialize the install command object
/// </summary>
/// <param name="mgr">KSPManager containing our instances</param>
/// <param name="user">IUser object for interaction</param>
public Install(KSPManager mgr, IUser user)
{
manager = mgr;
this.user = user;
}

/// <summary>
/// Installs a module, if available
/// </summary>
/// <param name="ksp">Game instance into which to install</param>
/// <param name="raw_options">Command line options object</param>
/// <returns>
/// Exit code for shell environment
/// </returns>
public int RunCommand(CKAN.KSP ksp, object raw_options)
{
InstallOptions options = (InstallOptions) raw_options;
Expand Down Expand Up @@ -113,7 +128,7 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)
// Install everything requested. :)
try
{
var installer = ModuleInstaller.GetInstance(ksp, user);
var installer = ModuleInstaller.GetInstance(ksp, manager.Cache, user);
installer.InstallList(options.modules, install_ops);
}
catch (DependencyNotSatisfiedKraken ex)
Expand Down Expand Up @@ -187,7 +202,7 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)
// Add the module to the list.
options.modules.Add(ex.modules[result].identifier);

return (new Install(user).RunCommand(ksp, options));
return (new Install(manager, user).RunCommand(ksp, options));
}
catch (FileExistsKraken ex)
{
Expand Down
20 changes: 17 additions & 3 deletions Cmdline/Action/Remove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@ public class Remove : ICommand
private static readonly ILog log = LogManager.GetLogger(typeof(Remove));

public IUser user { get; set; }
private KSPManager manager;

public Remove(IUser user)
/// <summary>
/// Initialize the remove command object
/// </summary>
/// <param name="mgr">KSPManager containing our instances</param>
/// <param name="user">IUser object for interaction</param>
public Remove(KSPManager mgr, IUser user)
{
manager = mgr;
this.user = user;
}

// Uninstalls a module, if it exists.
/// <summary>
/// Uninstalls a module, if it exists.
/// </summary>
/// <param name="ksp">Game instance from which to remove</param>
/// <param name="raw_options">Command line options object</param>
/// <returns>
/// Exit code for shell environment
/// </returns>
public int RunCommand(CKAN.KSP ksp, object raw_options)
{

Expand Down Expand Up @@ -62,7 +76,7 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)
{
try
{
var installer = ModuleInstaller.GetInstance(ksp, user);
var installer = ModuleInstaller.GetInstance(ksp, manager.Cache, user);
Search.AdjustModulesCase(ksp, options.modules);
installer.UninstallList(options.modules);
}
Expand Down
19 changes: 17 additions & 2 deletions Cmdline/Action/Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@ namespace CKAN.CmdLine
public class Update : ICommand
{
public IUser user { get; set; }
private KSPManager manager;

public Update(IUser user)
/// <summary>
/// Initialize the update command object
/// </summary>
/// <param name="mgr">KSPManager containing our instances</param>
/// <param name="user">IUser object for interaction</param>
public Update(KSPManager mgr, IUser user)
{
manager = mgr;
this.user = user;
}

/// <summary>
/// Update the registry
/// </summary>
/// <param name="ksp">Game instance to update</param>
/// <param name="raw_options">Command line options object</param>
/// <returns>
/// Exit code for shell environment
/// </returns>
public int RunCommand(CKAN.KSP ksp, object raw_options)
{
UpdateOptions options = (UpdateOptions) raw_options;
Expand Down Expand Up @@ -135,7 +150,7 @@ private void UpdateRepository(CKAN.KSP ksp, string repository = null)
RegistryManager registry_manager = RegistryManager.Instance(ksp);

var updated = repository == null
? CKAN.Repo.UpdateAllRepositories(registry_manager, ksp, user)
? CKAN.Repo.UpdateAllRepositories(registry_manager, ksp, manager.Cache, user)
: CKAN.Repo.Update(registry_manager, ksp, user, repository);

user.RaiseMessage("Updated information on {0} available modules", updated);
Expand Down
24 changes: 19 additions & 5 deletions Cmdline/Action/Upgrade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@ public class Upgrade : ICommand
private static readonly ILog log = LogManager.GetLogger(typeof(Upgrade));

public IUser User { get; set; }

public Upgrade(IUser user)
private KSPManager manager;

/// <summary>
/// Initialize the upgrade command object
/// </summary>
/// <param name="mgr">KSPManager containing our instances</param>
/// <param name="user">IUser object for interaction</param>
public Upgrade(KSPManager mgr, IUser user)
{
manager = mgr;
User = user;
}


/// <summary>
/// Upgrade an installed module
/// </summary>
/// <param name="ksp">Game instance from which to remove</param>
/// <param name="raw_options">Command line options object</param>
/// <returns>
/// Exit code for shell environment
/// </returns>
public int RunCommand(CKAN.KSP ksp, object raw_options)
{
UpgradeOptions options = (UpgradeOptions) raw_options;
Expand Down Expand Up @@ -115,13 +129,13 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)

}

ModuleInstaller.GetInstance(ksp, User).Upgrade(to_upgrade, new NetAsyncModulesDownloader(User));
ModuleInstaller.GetInstance(ksp, manager.Cache, User).Upgrade(to_upgrade, new NetAsyncModulesDownloader(User));
}
else
{
// TODO: These instances all need to go.
Search.AdjustModulesCase(ksp, options.modules);
ModuleInstaller.GetInstance(ksp, User).Upgrade(options.modules, new NetAsyncModulesDownloader(User));
ModuleInstaller.GetInstance(ksp, manager.Cache, User).Upgrade(options.modules, new NetAsyncModulesDownloader(User));
}
}
catch (ModuleNotFoundKraken kraken)
Expand Down
Loading

0 comments on commit 3ef9558

Please sign in to comment.