Skip to content

Commit

Permalink
Merge pull request KSP-CKAN#11 from Ormira/issue#12
Browse files Browse the repository at this point in the history
Add a --all option to upgrade.
  • Loading branch information
AlexanderDzhoganov committed Dec 31, 2014
2 parents 1bdec08 + 9dc765c commit 297368c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
54 changes: 51 additions & 3 deletions Action/Upgrade.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using log4net;

namespace CKAN.CmdLine
{
public class Upgrade : ICommand
{
private static readonly ILog log = LogManager.GetLogger(typeof(Upgrade));

public IUser User { get; set; }

public Upgrade(IUser user)
Expand All @@ -23,18 +26,63 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)
options.modules.Add(MainClass.LoadCkanFromFile(ksp, options.ckan_file).identifier);
}

if (options.modules.Count == 0)
if (options.modules.Count == 0 && ! options.upgrade_all)
{
// What? No files specified?
User.RaiseMessage("Usage: ckan upgrade Mod [Mod2, ...]");
User.RaiseMessage(" or ckan upgrade --all");
return Exit.BADOPT;
}

User.RaiseMessage("\nUpgrading modules...\n");
// TODO: These instances all need to go.

try
{
ModuleInstaller.GetInstance(ksp, User).Upgrade(options.modules, new NetAsyncDownloader(User));
if (options.upgrade_all)
{
var installed = new Dictionary<string, Version>(ksp.Registry.Installed());
var to_upgrade = new List<CkanModule>();

foreach (KeyValuePair<string, Version> mod in installed)
{
Version current_version = mod.Value;

if ((current_version is ProvidesVersion) || (current_version is DllVersion))
{
continue;
}
else
{
try
{
// Check if upgrades are available
CkanModule latest = ksp.Registry.LatestAvailable(mod.Key, ksp.Version());

if (latest.version.IsGreaterThan(mod.Value))
{
// Upgradable
log.InfoFormat("New version {0} found for {1}",
latest.version, latest.identifier);
to_upgrade.Add(latest);
}

}
catch (ModuleNotFoundKraken)
{
log.InfoFormat("{0} is installed, but no longer in the registry",
mod.Key);
}
}

}

ModuleInstaller.GetInstance(ksp, User).Upgrade(to_upgrade, new NetAsyncDownloader(User));
}
else
{
// TODO: These instances all need to go.
ModuleInstaller.GetInstance(ksp, User).Upgrade(options.modules, new NetAsyncDownloader(User));
}
}
catch (ModuleNotFoundKraken kraken)
{
Expand Down
3 changes: 3 additions & 0 deletions Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ internal class UpgradeOptions : CommonOptions
[Option("with-all-suggests", HelpText = "Install suggested modules all the way down")]
public bool with_all_suggests { get; set; }

[Option("all", HelpText = "Upgrade all available updated modules")]
public bool upgrade_all { get; set; }

// TODO: How do we provide helptext on this?
[ValueList(typeof (List<string>))]
public List<string> modules { get; set; }
Expand Down

0 comments on commit 297368c

Please sign in to comment.