Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cmdline import command #2264

Merged
merged 3 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions Cmdline/Action/Import.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.IO;
using System.Collections.Generic;
using log4net;

namespace CKAN.CmdLine
{

/// <summary>
/// Handler for "ckan import" command.
/// Imports manually downloaded ZIP files into the cache.
/// </summary>
public class Import : ICommand
{

/// <summary>
/// Initialize the command
/// </summary>
/// <param name="user">IUser object for user interaction</param>
public Import(IUser user)
{
this.user = user;
}

/// <summary>
/// Execute an import command
/// </summary>
/// <param name="ksp">Game instance into which to import</param>
/// <param name="options">Command line parameters from the user</param>
/// <returns>
/// Process exit code
/// </returns>
public int RunCommand(CKAN.KSP ksp, object options)
{
try
{
ImportOptions opts = options as ImportOptions;
HashSet<FileInfo> toImport = GetFiles(opts);
if (toImport.Count < 1)
{
user.RaiseMessage("Usage: ckan import path [path2, ...]");
return Exit.ERROR;
}
else
{
log.InfoFormat("Importing {0} files", toImport.Count);
List<string> toInstall = new List<string>();
ModuleInstaller inst = ModuleInstaller.GetInstance(ksp, user);
inst.ImportFiles(toImport, user, id => toInstall.Add(id), !opts.Headless);
if (toInstall.Count > 0)
{
inst.InstallList(
toInstall,
new RelationshipResolverOptions()
);
}
return Exit.OK;
}
}
catch (Exception ex)
{
user.RaiseError("Import error: {0}", ex.Message);
return Exit.ERROR;
}
}

private HashSet<FileInfo> GetFiles(ImportOptions options)
{
HashSet<FileInfo> files = new HashSet<FileInfo>();
foreach (string filename in options.paths)
{
if (Directory.Exists(filename))
{
// Import everything in this folder
log.InfoFormat("{0} is a directory", filename);
foreach (string dirfile in Directory.EnumerateFiles(filename))
{
AddFile(files, dirfile);
}
}
else
{
AddFile(files, filename);
}
}
return files;
}

private void AddFile(HashSet<FileInfo> files, string filename)
{
if (File.Exists(filename))
{
log.InfoFormat("Attempting import of {0}", filename);
files.Add(new FileInfo(filename));
}
else
{
user.RaiseMessage("File not found: {0}", filename);
}
}

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

}
1 change: 1 addition & 0 deletions Cmdline/CKAN-cmdline.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Action\Compare.cs" />
<Compile Include="Action\CompatSubCommand.cs" />
<Compile Include="Action\ICommand.cs" />
<Compile Include="Action\Import.cs" />
<Compile Include="Action\Install.cs" />
<Compile Include="Action\ISubCommand.cs" />
<Compile Include="Action\KSP.cs" />
Expand Down
3 changes: 3 additions & 0 deletions Cmdline/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ private static int RunSimpleAction(Options cmdline, CommonOptions options, strin
Scan(GetGameInstance(manager), user, cmdline.action);
return (new Upgrade(user)).RunCommand(GetGameInstance(manager), cmdline.options);

case "import":
return (new Import(user)).RunCommand(GetGameInstance(manager), options);

case "clean":
return Clean(GetGameInstance(manager));

Expand Down
12 changes: 12 additions & 0 deletions Cmdline/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ internal class Actions : VerbCommandOptions
[VerbOption("remove", HelpText = "Remove an installed mod")]
public RemoveOptions Remove { get; set; }

[VerbOption("import", HelpText = "Import manually downloaded mods")]
public ImportOptions Import { get; set; }

[VerbOption("scan", HelpText = "Scan for manually installed KSP mods")]
public ScanOptions Scan { get; set; }

Expand Down Expand Up @@ -132,6 +135,9 @@ public string GetUsage(string verb)
case "compare":
ht.AddPreOptionsLine($"Usage: ckan {verb} [options] version1 version2");
break;
case "import":
ht.AddPreOptionsLine($"Usage: ckan {verb} [options] paths");
break;

// Now the commands with only --flag type options
case "gui":
Expand Down Expand Up @@ -433,6 +439,12 @@ internal class RemoveOptions : InstanceSpecificOptions
public bool rmall { get; set; }
}

internal class ImportOptions : InstanceSpecificOptions
{
[ValueList(typeof(List<string>))]
public List<string> paths { get; set; }
}

internal class ShowOptions : InstanceSpecificOptions
{
[ValueOption(0)] public string Modname { get; set; }
Expand Down
7 changes: 4 additions & 3 deletions Core/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,8 @@ private void DownloadModules(IEnumerable<CkanModule> mods, IDownloader downloade
/// <param name="files">Set of files to import</param>
/// <param name="user">Object for user interaction</param>
/// <param name="installMod">Function to call to mark a mod for installation</param>
public void ImportFiles(HashSet<FileInfo> files, IUser user, Action<string> installMod)
/// <param name="allowDelete">True to ask user whether to delete imported files, false to leave the files as is</param>
public void ImportFiles(HashSet<FileInfo> files, IUser user, Action<string> installMod, bool allowDelete = true)
{
Registry registry = registry_manager.registry;
HashSet<string> installable = new HashSet<string>();
Expand Down Expand Up @@ -1134,15 +1135,15 @@ public void ImportFiles(HashSet<FileInfo> files, IUser user, Action<string> inst
}
++i;
}
if (installable.Count > 0 && user.RaiseYesNoDialog($"Install {installable.Count} compatible imported mods?"))
if (installable.Count > 0 && user.RaiseYesNoDialog($"Install {installable.Count} compatible imported mods in game instance {ksp.Name} ({ksp.GameDir()})?"))
{
// Install the imported mods
foreach (string identifier in installable)
{
installMod(identifier);
}
}
if (user.RaiseYesNoDialog($"Import complete. Delete {deletable.Count} old files?"))
if (allowDelete && deletable.Count > 0 && user.RaiseYesNoDialog($"Import complete. Delete {deletable.Count} old files?"))
{
// Delete old files
foreach (FileInfo f in deletable)
Expand Down
2 changes: 1 addition & 1 deletion Core/Net/NetModuleCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public string Store(CkanModule module, string path, string description = null, b
}

// If no exceptions, then everything is fine
return cache.Store(module.download, path, description, move);
return cache.Store(module.download, path, description ?? module.StandardName(), move);
}

private NetFileCache cache;
Expand Down